/*
 * This file contains the script for processing key events.
 */
(function ($) {
    $.registerKeyHandler = function( l, r, u, s )
    {
        $(document).keydown( function( e ) 
        {
        	var id = "";
        	
        	// Left and right arrow and space are used for navigation
        	// Left arrow follows the link with the id "left"
        	// Right arrow and space follow the link with the id "right"
        	switch( e.which )
        	{
        		case 37: id = l; break; // left arrow
        		case 39: id = r; break; // right arrow
        		case 38: id = u; break; // up arrow
        		case 32: id = s; break; // space
        	}
        	
        	if( id )
        	{
            	var goTo = $( "#" + id ).attr( "href" );
            	
            	if( goTo )
            	{
            	    document.location = goTo;
            	}
            }
        } );
    };
})(jQuery);




/********************** 
 * updateBrowserInfo
 *
 * "Browser Info" is information that is available in the browser via Javascript
 * and that is passed on to the server-side. At the moment this is used for
 * window width and height.
 *
 * The info is passed to store_browser_info.php, which stores it in the session context.
 *
 * Then the content is reloaded, so that the server-side can make use of the stored information.
 */
(function ($) {
    $.updateBrowserInfo = function( $reload )
    {
        var h = $(window).height();
        var w = $(window).width();
        
        var url = "store_browser_info.php?h=" + h + "&w=" + w;

        if( navigator.cookieEnabled )
        {
	        if( $reload )
	        {
		        $.get( url, function( data ) {
		            location.reload();
		        });
			}
			else
			{
		        $.get( url );
			}
		}
    };
})(jQuery);



/********************** 
 * fadeInOnLoad
 *
 * Shows a fade-in animation for a picture.
 */
jQuery.fn.fadeInOnLoad = function()
{
    return this.each(function(){
            $(this)
                .hide()
                .load( function() { $(this).fadeIn(); } )
                .each( function() { if(this.complete) { $(this).trigger("load"); } } );
            return true;
        });
};


/********************** 
 * preLoadImages
 *
 * jQuery.preloader - v0.95 - K Reeve aka BinaryKitten
 * 
 * Source: http://binarykitten.me.uk/dev/jq-plugins/107-jquery-image-preloader-plus-callbacks.html
 */
(function ($) {
    $.preLoadImages = function(imageList,callback) {
        var pic = [], i, total, loaded = 0;
        if (typeof imageList != 'undefined') {
            if ($.isArray(imageList)) {
                total = imageList.length; // used later
                    for (i=0; i < total; i++) {
                        pic[i] = new Image();
                        pic[i].onload = function() {
                            loaded++; // should never hit a race condition due to JS's non-threaded nature
                            if (loaded == total) {
                                if ($.isFunction(callback)) {
                                    callback();
                                }
                            }
                        };
                        pic[i].src = imageList[i];
                    }
            }
            else {
                pic[0] = new Image();
                pic[0].onload = function() {
                    if ($.isFunction(callback)) {
                        callback();
                    }
                }
                pic[0].src = imageList;
            }
        }
        pic = undefined;
    };
 
    $.preLoadCSSImages = function(callback) {
        var pic = [], i, imageList = [], loaded = 0, total, regex = new RegExp("url\((.*)\)",'i'),spl;
        var cssSheets = document.styleSheets, path,myRules,Rule,match,txt,img,sheetIdx,ruleIdx;
        for (sheetIdx=0;sheetIdx < cssSheets.length;sheetIdx++){
            var sheet = cssSheets[sheetIdx];
            if (typeof sheet.href == 'string' && sheet.href.length > 0) {
                spl = sheet.href.split('/');spl.pop();path = spl.join('/')+'/';
            }
            else {
                path = './';
            }
            myRules = sheet.cssRules ? sheet.cssRules : sheet.rules;
            for (ruleIdx=0;ruleIdx < myRules.length;ruleIdx++) {
                Rule = myRules[ruleIdx];
                txt = Rule.cssText ? Rule.cssText : Rule.style.cssText;
                match = regex.exec(txt);
                if (match != null) {
                    img = match[1].substring(1,match[1].indexOf(')',1));
                    if (img.substring(0,4) == 'http') {
                        imageList[imageList.length] = img;
                    }
                    else if ( match[1].substring(1,2) == '/') {
                        var p2 = path.split('/');p2.pop();p2.pop();p2x = p2.join("/");
                        imageList[imageList.length] = p2x+img;
                    }
                    else {
                        imageList[imageList.length] = path+img;
                    }
                }
            };
        };
 
        total = imageList.length; // used later
        for (i=0; i < total; i++) {
            pic[i] = new Image();
            pic[i].onload = function() {
                loaded++; // should never hit a race condition due to JS's non-threaded nature
                if (loaded == total) {
                    if ($.isFunction(callback)) {
                        callback();
                    }
                }
            };
            pic[i].src = imageList[i];
        }
 
    };
    $.preLoadAllImages = function(imageList,callback) {
        if (typeof imageList != 'undefined') {
            if ($.isFunction(imageList)) {
                callback = imageList;
            }
            else if (!$.isArray(imageList)) {
                imageList = [imageList];
            }
        }
        $.preLoadCSSImages(function(){
            if (imageList.length > 0) {
                $.preLoadImages(imageList,function(){
                    callback();
                });
            }
            else {
                callback();
            }
        });
    }
})(jQuery);
