$(document).ready(function() {
    jQuery.preloadCssImages();

    var webappCache = window.applicationCache;
    if (webappCache) {
    	// Reload page right after update came. 
        webappCache.addEventListener("updateready", function() { location.reload(); }, false);
        var downloadingShown = false;
        webappCache.addEventListener("downloading", function() {
        	if (!downloadingShown) { 
        		$('#cache-prompt') .css("display", "block");
	            downloadingShown = true;
            }
        }, false);
    }

var navigateToScreen = function($new, $old, trigger) 
{
	var dir = 'forward';
	var delay = 300;
	if ($(trigger).is('.back-button') || $(trigger).is('.transition-slide-right')) dir = 'backward';
	if ($(trigger).is('.transition-none')) delay = 0;
	
	if ($(trigger).is('.transition-fade'))
	{
		if ($new .length > 0)
		{
			$old .addClass('fade out');
			$new .show().css({left: '0px'}).addClass('fade in');
			var t = setTimeout(function() 
			{
				$old .hide();
				$old .removeClass('fade out');
				$new .removeClass('fade in');
			}, delay);
		}
	}
	else if ($(trigger).is('.transition-flip-right'))
	{
		if ($new .length > 0) 
		{
			$old .addClass('flip out');

			var t = setTimeout(function() 
			{
				$old .hide();
				$old .removeClass('flip out');
				$new .show().css({left: '0'}).addClass('flip in');
				setTimeout(function() { $new .removeClass('flip in').css({left: '0'}) }, delay);
			}, delay);
		}
	}
	else if ($(trigger).is('.transition-flip-left'))
	{
		if ($new .length > 0) 
		{
			$old .addClass('flip out reverse');
	
			var t = setTimeout(function() 
			{
				$old .hide();
				$old .removeClass('flip out reverse');
				$new .show().css({left: '0'}).addClass('flip in reverse');
				setTimeout(function() { $new .removeClass('flip in reverse').css({left: '0'}) }, delay);
			}, delay);
		}
	}
	else if ($(trigger).is('.transition-cube'))
	{
		delay = '1200';
		if ($new .length > 0) 
		{
			$old .addClass('cube out');
			$new .show().css({left: '0px'}).addClass('cube in');
			var t = setTimeout(function() 
			{
				$old .hide();
				$old .removeClass('cube out');
				$new .removeClass('cube in');
			}, delay);
		}
	}
	else
	{
		var INC =  {forward: '-=', backward: '+='};
		var STEP = {forward: '+',  backward: '-'};
	
		if ($new .length > 0)
		{
			$old .animate({left: INC[dir]+'320px'}, delay, function() { $old .hide(); });
			$new .css({left: STEP[dir]+'320px'}).show().animate({left: INC[dir]+'320px'}, delay);
		}
	}
};
	
	$('.hyperlink').click(function() {
		var href = $(this).attr('href');
		if (href.match(/^#/)) {
			navigateToScreen($(href), $(this).closest('.root'), this);
		} else {
			window.location.href = href;
		}
	});
	
	   function SetCookie(sName, sValue)
       {
               document.cookie = sName + "=" + escape(sValue);
               var date = new Date();
               var expdate = date.getTime();
               expdate += 3600*1000 //expires (milliseconds - 1000 is for a day)
               date.setTime(expdate);
               document.cookie += ("; expires=" + date.toUTCString());
       }
       function DeleteCookie(sName)
       {
               document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
       }
       function GetCookie(sName)
       {
               var aCookie = document.cookie.split("; ");
               for (var i=0; i < aCookie.length; i++)
               {
                       var aCrumb = aCookie[i].split("=");
                       if (sName == aCrumb[0])
                               return unescape(aCrumb[1]);
               }
               return null;
       }

       if(window.navigator.standalone != true)
       {
               if(GetCookie('phc'))
                       // Hide the dialog box - to avoid scrolling
                       $('#homescreen-prompt') .hide();
               else {
                       // Show prompt dialog
                       SetCookie('phc','true');
                       $('#homescreen-prompt') .css({top: '400px', opacity: '0.0'});
                       setTimeout(function() { $('#homescreen-prompt') .animate({top: '300px', opacity: '1.0'}, 400); }, 600);
                       setTimeout(function() { $('#homescreen-prompt') .animate({opacity: '0'}, 400); }, 5000);
               }
       } else {
 			$('#homescreen-prompt') .hide();
	   }
	
});

