var krpano;
function setKrpano(){
	/*
		Called from krpano, Stores the krpano object, allows for communication with krpano
	*/
	krpano = document.getElementById("krpanoSWFObject");
	
	$(document).ready(function(){
		
						
		//Check if de tour is offline, then hide the social sharing icons		
		/* if(window.location.protocol != "http:" && window.location.protocol != "https:")
		{
			$('#twitter, #facebook, #linkedin, #hyves').css("display", "none");
		}
		else{ */
			$("#facebook_frame").attr("src","http://www.facebook.com/plugins/like.php?href="+ getUrl()+"&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;font=trebuchet+ms&amp;colorscheme=light&amp;height=40");
		
			setupSocial();		
		//}
		
	});
}

$(document).ready(function(){
		
   setupStage(); 
	
	$(window).resize(function(){
		resizeFrame();
	});	
	
	$('#sitemap_btn').click(function() {	
		toggleSitemap();
	});	
	
});

function resizeFrame() //resize without tween
{
	$("#container").css('height', $(window).height()); //Container with all elements resized to fit the screen without scrollbars	
	var correction = 0;
	if(toggled){
		//makes sure that if the sitemap is openend and a resize event is triggered, the sitemap doesn't collapse
		var correction = ($("#contact").height() + 30) > $('#sitemap').height() ? $("#contact").height() + 30 : $('#sitemap').height();
	}
	var newContentHeight = $(window).height() - parseInt($("#header").css('height')) - parseInt($("#footer").css('height'));	
	$("#krpano").css('height', newContentHeight - correction);	
	$("#CenterContent").css('height', newContentHeight - correction); //Content resized to fit the rest of the screen without scrollbars		
	
			
}
function setupStage(){ //with tween, tweens on page load.
	var newContentHeight = $(window).height() - parseInt($("#header").css('height')) - parseInt($("#footer").css('height'));	
	$('#CenterContent, #krpano').animate(
	{
		height: newContentHeight
	}, 
	{
		duration: 300,
		complete: function() {
			resizeFrame();
		}
	});	
}
function getrandomvalue()
{
	/* Old. The new template uses the Project.Version */
	/* creates random number to be put as parameter in files to force not to cache the files */
	//krpanoObj = document.getElementById('krpanoSWFObject');
	var today = new Date();
	var fivemin = 1000 * 60 * 5;
	var randy = Math.ceil(today.getTime()/(fivemin));
	krpano.set('timestamp', randy);
}

/*
	HTML TABS Related functions
*/

function getContent(id)
{
	return escape($("#"+id+"_content").html());
}

var toggled = false;
function toggleSitemap()
{
	if(!toggled){			
		toggled = true;
		var h = ($("#contact").height() + 30) > $('#sitemap').height() ? $("#contact").height() + 30 : $('#sitemap').height();
		var newh = $("#CenterContent").height() - h;		
		$("#CenterContent").animate({height: newh});
		$("#sitemap_btn").text("Sitemap sluiten");
	}else{
		$("#sitemap_btn").text("Sitemap openen");
		toggled = false;
		setupStage(); //tween sitemap
	}
	return false;
}
function toggleContact()
{
	if(!toggled){			
		toggled= true;
		var h = ($("#contact").height() + 30) > $('#sitemap').height() ? $("#contact").height() + 30 : $('#sitemap').height();
		
		var newh = $("#CenterContent").height() - h;		
		$("#CenterContent").animate({height: newh});
	}else{
		toggled = false;
		setupStage(); //tween sitemap
	}
	location.href = "#contact";
	return false;
}
function getUrl() {
	/*
		returns a clean url of the current location in the tour
	*/
	var url = getPathFromUrl(window.location.href); 
	var photourl = "?pano=bolfoto/photo_"+ krpano.get("location.id") + "_" + krpano.get("location.track") + ".xml";
	var siteurl = encodeUrl(url + photourl);
	
	return siteurl;
}
function getTitle(){
	/* 
		Gets the name of the location from krpano
	*/
	return encodeUrl(krpano.get("location.title"));
}
function encodeUrl(url){
	/*
		strips unsafe characters
	*/
     var encodedHtml = encodeURIComponent(url);
     encodedHtml = encodedHtml.replace(/\//g,"%2F");
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     return encodedHtml;
} 
function getPathFromUrl(url) {
	/*
		strips ?parameter=value&etc
	*/
  return url.split("?")[0];
}
