/*
	@namespace jynx
	
	title: Jinx Javascript Library
	author: just.jake@rescomp.berkeley.edu
	date: 16 June 2011
	
	Namespace container for our javascript functions
*/
// create container only if it doesn't already exist
if ( typeof jynx === 'undefined' ) { jynx = {}; }

/*
	changeHash: change page hash without scrolling viewport all over the place
	
	Most webapps (like twitter) use 
	
	USAGE:
		jinx.changeHash( newHashString )
*/
jynx.changeHash = function( hash ) {
	hash = hash.replace( /^#/, '' );
	var $el, $withID = $( '#' + hash );
	
	// if any items on the page have an ID equal to our new hash,
	// remove that ID while we change the window's hash so the browser
	// doesn't scroll viewport
	if ( $withID.length ) {
		// create invisible element to prevent window from scrolling
		// (it scrolls to its current loction)
		$el = $( '<div></div>' )
		.css({
			'position': 'absolute'
			//, visibility:'hidden'
			, 'top': $(window).scrollTop()
		})
		.attr( 'id', hash )
		.prependTo( document.body );
		
		// remove id from real element
		$withID.attr( 'id', '' );
	}
	// change hash
	window.location.hash = hash;
	
	if ( $withID.length ) {
		$el.remove();	// remove plug
		$withID.attr( 'id', hash );	// restore id
	}
}
jynx.isTouchClient = function() {
    // Adapted from modernizr
    return (
            (!!('ontouchstart' in window) ? 1 : 0) ||
            (function(){
                var has_touch;
                try {
                    document.createEvent("TouchEvent");
                    has_touch = 1;
                } catch (e) {
                    has_touch = 0;
                }
                return has_touch;
            })() || 
            (!!('createTouch' in document) ? 1 : 0) ||
            (!!(typeof TouchEvent != "undefined") ? 1 : 0) ||
            (!!(typeof Touch == "object") ? 1 : 0) ||
            (!!('ontouchend' in document) ? 1 : 0)
           );
};


