var clickpages = Array();
		



function pageload(hash)
{
	if(hash == '')
		hash = 'index';


	loadpage2(hash);
}

function loadpage(e) {
			
			var targ;
			if (!e)
			{
				var e = window.event;
			}
		
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;
			if (targ.nodeType == 3) // defeat Safari bug
				targ = targ.parentNode;

			//loadpage2(targ.id);
}

function loadpage2(name)
{
			// Show loader
			$(".nav_btn").removeClass('loading');
			$(clickpages['#' + name]['loadimgid']).addClass('loading');

			// Add the div #NewBg to the body
			$(document.body).append("<div class='Bg below' id='NewBg'><\/div>");

			// Add the new background below the old one
			$("#NewBg").append('<img class="BgImage" id="NewBgImage" src="' + clickpages['#' + name]['img'] + '" />');		
			
			// Load complete
			$("#NewBgImage").load(function(evt){
				
				// Change id's and z-index for the OLD BG
				$('#Bg').attr('id', 'Wait');
				$('#BgImage').attr('id', 'WaitImage');
				$('#Wait').attr('class', 'Bg above');
				
				// Change id's and z-index for the NEW BG
				$('#NewBg').attr('id', 'Bg');
				$('#NewBgImage').attr('id', 'BgImage');
				$('#Bg').attr('class', 'Bg below');
									
				// Resize the NEW BG
				fillBg("#BgImage", "#Bg");				
				
				$('#Wait').fadeOut(500, function() {
					// When animation is completed, remove the OLD BG and reset the NEW BG's z-index
					$('#Wait').remove();
					$('#Bg').attr('class', 'Bg above');
					

					$.get(clickpages['#' + name]['page'], function(data) { 
						$('#content').html(data);
					});    					
				});	    				
			});

		}


	function fillBg(image, layer){
	
		function fillIt() {
								
			// Variables
			imgWidth = $(image).width();
			imgHeight = $(image).height();
			imgRatio = imgWidth/imgHeight;
			viewWidth = $(layer).width();
			viewHeight = $(layer).height();
			
			
			
			
			if(getInternetExplorerVersion() == -1 || getInternetExplorerVersion() >7)
			{
				// Resize image
				$(image).css({
					position: "absolute",
					width : viewWidth + "px",
					height : Math.round(viewWidth / imgRatio) + "px"						
				});
			}			
			
			// Reset imgW/H variables
			imgWidth = $(image).width();
			imgHeight = $(image).height();

			// Position image
			$(image).css({ 
				left : 0 + "px",
				top : Math.round(viewHeight - imgHeight) / 2 + "px"	
			});	
		
			// If not yet filling
			if( $(image).offset().top > 0 ) {
				// Resize image
				$(image).css({		
					height : viewHeight + "px",
					width : Math.round(viewHeight * imgRatio) + "px"
				});
		
				// Reset imgW/H variables
				imgWidth = $(image).width();			
				viewWidth = $(layer).width();
		
				// Position image
				$(image).css({					
					top : 0 + "px",
					left : Math.round(viewWidth - imgWidth) / 2 + "px"					
				});						
			}					
			
		}
		
		// Trigger on Load

		fillIt();
		
			
		// Trigger on Resize
		$(window).smartresize(function(){
			fillIt();
		});	
		
	}



$(document).ready(function() {

	//setup history
	$.history.init(pageload );
	
	// Disable caching of AJAX responses */
	$.ajaxSetup ({		
		cache: false
	});


	// no javascript message
	$("#no_js_message").css("-moz-opacity","1"); //this css is only used by Mozilla browsers like Firefox
		$(window).load(function(){
		$("#no_js_message").css("-moz-opacity","0");	
	});

	
	

	if (jQuery.browser.safari && document.readyState != "complete"){
		setTimeout( arguments.callee, 100 );
		return;
	} 
	

   					  								
	// LOAD NEW IMAGE FUNCTION
	
	for(var xyz in clickpages) {
		$(clickpages[xyz]['id']).click(loadpage);	
	}
				
});
		

function add_click_page_event(id, img, page, loadimgid)
{
	if(!loadimgid)
		loadimgid = id;

	clickpages[id] = Array();
	clickpages[id]['id'] = id;
	clickpages[id]['img'] = img;
	clickpages[id]['page'] = page;
	clickpages[id]['loadimgid'] = loadimgid;
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

