 // If there is a hash then use that if not use the passed variable to load the page

var start_page

var hash

var page_loaded = false;

function reload_page(i){
	
	/*
	
		When the page loads check if there is a hash
		If there is split it up and send it back to flash navigation.
		
		If there isn't create a hash with "i" as the level 1 nav
			i.e. hash = i + "|0|"
			
		Then send the first level nav position to the banner
	
	*/
	
	if(page_loaded){
	
		start_page = i
		
		var reload_position = -1
		
		var URL = unescape(window.document.location);
		
		// This checks if there is a hash value and then acts upon it.		
		if (URL.indexOf("#") > -1) {
		//whether the location contains a "#" or not
			//Has a hash
				
			reload_position = URL.split("#");
			
			my_hash = reload_position[1];
			hash = my_hash
			
			
			reload_position = reload_position[1].split("|")
			parent_page = reload_position[0]
					
			//Banner nav
			load_page(reload_position[0])
			
			
			if(window.chunky_nav){
						
				window.document["chunky_nav"].SetVariable("level1_nav_id", reload_position[0]);
				window.document["chunky_nav"].SetVariable("level2_nav_id", reload_position[1]);
				if(reload_position.length > 2 && reload_position[2] !=''){
					window.document["chunky_nav"].SetVariable("level3_nav_id", reload_position[2]);
				}
				else{
					window.document["chunky_nav"].SetVariable("level3_nav_id", '-1');
				}
			}
			if(document.chunky_nav){
				
				document.chunky_nav.SetVariable("level1_nav_id", reload_position[0]);
				document.chunky_nav.SetVariable("level2_nav_id", reload_position[1]);
				if(reload_position.length > 2 && reload_position[2] !=''){
					document.chunky_nav.SetVariable("level3_nav_id", reload_position[2]);
				}
				else{
					document.chunky_nav.SetVariable("level3_nav_id", '-1');
				}
			}
		}
		else{
			if(window.chunky_nav){
				window.document["chunky_nav"].SetVariable("level1_nav_id",i);
				window.document["chunky_nav"].SetVariable("level2_nav_id",0);
				window.document["chunky_nav"].SetVariable("level3_nav_id", -1);
			}
			if(document.chunky_nav){
				document.chunky_nav.SetVariable("level1_nav_id",i);
				document.chunky_nav.SetVariable("level2_nav_id", 0);
				document.chunky_nav.SetVariable("level3_nav_id", -1);
			}
			load_page(i)
		}
	}
	else{
		page_loaded = true
	}	
	
	
	//Check page hash every second and reload if needed
	setTimeout(checkLocation, 1000);
}


function checkLocation(){
	var URL = unescape(window.document.location);
		
	
	if (URL.indexOf("#") > -1) {
		//whether the location contains a "#" or not
		reload_position = URL.split("#");
		my_hash = reload_position[1];
		//reload_position = reload_position[1].split("|")
				
		if(my_hash != hash){
			reload_page(reload_position[1])
		}
		
	}
	//Check page hash every second and reload if needed
	setTimeout(checkLocation, 1000);
}

function load_page(parent){	

	if(window.chunky_nav){
		window.document["chunky_nav"].SetVariable("level1_nav_id",parent);
		//window.document["chunky_nav"].SetVariable("level2_nav_id",0);
	}
	if(document.chunky_nav){
		document.chunky_nav.SetVariable("level1_nav_id",parent);
		//document.chunky_nav.SetVariable("level2_nav_id", 0);
	}
	
	load_nav(parent)
}

function load_nav(i){
	if(window.banner_nav){
		window.document["banner_nav"].SetVariable("parent_nav", i);
	}
	if(document.banner_nav){
		document.banner_nav.SetVariable("parent_nav", i);
	}
	
}

var l_page
var page_queue = { scope: 'copy', position: 'end' };
function load_html_page(page){

	exists = unescape(window.document.location);
	exists = exists.indexOf(page)
	
	
	//Stop the page trying to load twice
	if(page != l_page){
			l_page = page
			
			//Get rid of any flash
			flash = $$('.video_gallery *')
			for(i=0; i<flash.length;i++){
				flash[i].style.display = 'none'
			}
			
			new Effect.MoveBy( $('content'), 0, -427, {duration: 0.75, transition: Effect.Transitions.EaseFromTo , afterFinish: load_page_via_ajax, queue: page_queue});

			/*	Test if the user using IE6
				If they are don't add the hashing	*/
			var URL = unescape(window.document.location);
			var list1 = URL.split("#");	
			var x = list1[0];
	}
}
function slide_page_back_in(){
	new Effect.MoveBy( $('content'), 0, 427 , {duration: 0.75, transition: Effect.Transitions.EaseFromTo, queue: page_queue});
}

function load_page_via_ajax(){
	page = l_page
	
	new Ajax.Request(page, {
	  method: 'get', evalScript: true,
	  onSuccess: function(transport) {
		 
		  //Reset the window to the top
		  window.scroll(0,0); // horizontal and vertical scroll targets
		  
		  var resp = transport.responseText
		  
		  //The two plus x are to remove the content div html from the string
		  start = resp.indexOf('<!--$$StartHere$$-->')
		  end = resp.indexOf('<!--$$EndHere$$-->') 
		  cont = resp.slice(start, end)
		  $('content').innerHTML = cont
		  
		  //Check if there is a video element
		  vids = resp.split('<!--$$id') 
		   
		  if(vids.length > 0){
		  	startv = 0
		  	start_id = 0
		  	for(i=1; i < vids.length; i++){
		  	
		  		startv = vids[i].indexOf('<!--$$StartVideo$$-->')
				startv = startv + 21
				endv = vids[i].indexOf('<!--$$EndVideo$$-->') 
			  	video_path = vids[i].slice(startv, endv)
			  		  	
			  	
			  	//start_id = vids[i].indexOf('<!--$$id_', start_id+10)
			  	 
			  	video_id = vids[i].slice(1, 2)

			  	load_video(video_path, video_id)
			  	
			  }
		  }
		setupZoom()
		  slide_page_back_in()
		
		
	  }
	});
}

function load_video(video_path, id){
	var so = new SWFObject(video_path, "sotester", "430", "310", "9", "#efefef");
	so.addParam("allowFullScreen", "true");
	so.addParam("wmode", "transparent");
	so.write("video_gallery_" + id);
}

function set_hash(hash){

	if(hash.indexOf(-1)){
		hash = hash.replace(/-1/, "")
	}
	

	if(hash.indexOf('undefined') < 0){
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
			ar = navigator.userAgent.split(';');
			version = ar[1].split(' ');
			version = Number(version[2])
			if(version >= 7){
				
				window.location.hash = hash
			}
		}
		else{
			window.location.hash = hash
		}
		
		if(window.chunky_nav){
			window.document["chunky_nav"].SetVariable("my_hash", hash);
		}
		if(document.chunky_nav){
			document.chunky_nav.SetVariable("my_hash", hash);
		}
	}
}

///// Ajax Responders ///////////////////
Ajax.Responders.register({
	onCreate: function() {
		if(Ajax.activeRequestCount > 0){
		//Add a notification Div
		$('content_wrapper').insert({top:'<div id="notification" style="display: none;" ><img src="images/spinner.gif"></div>'});
		Effect.Appear('notification',{duration: 0.1, queue: 'end'});
		}
	},
	onComplete: function() {
		if($('notification') && Ajax.activeRequestCount == 0){
			Effect.Fade('notification',{duration: 0.5, queue: 'end', onFinish: remove_notification});
		}
	}
});
function remove_notification(){
	$('notification').remove()
}

function banner_clicked(i){

	if(window.chunky_nav){
		window.document["chunky_nav"].SetVariable("level1_nav_id",i);
		window.document["chunky_nav"].SetVariable("level2_nav_id",0);
		window.document["chunky_nav"].SetVariable("level3_nav_id", -1);
	}
	if(document.chunky_nav){
		document.chunky_nav.SetVariable("level1_nav_id",i);
		document.chunky_nav.SetVariable("level2_nav_id", 0);
		document.chunky_nav.SetVariable("level3_nav_id", -1);
	}
	load_page(i)
}
