var firstload = true;
var songloaded = false;
var clp1,clp2,clp3,clp4;
var rendered = false;
var splashed = false;

function krpano() 
{
	return document.getElementById("krpanoSWFObject");
}

function setPano(pano, track)
{
	krpano().call("loadpano(../panos/"+pano+"/pano.xml,null,KEEPBASE,blend(1), delayedcall(1,js(showPopup("+pano+", popup_info, false, true, true))));");

	if(firstload)krpano().call("delayedcall(3,js(showPopup(default)));");
	firstload = false;
	
	krpano().call("delayedcall(1,js(playSong("+track+"))));");
}

function playSong(strname) {
	var j = krpano().playSong(strname);

	if(!songloaded && j == -1)krpano().playSong("start");
	songloaded = true;	
}

function hidesplash() {
	if(splashed)return false;
	splashed = true;
	
	krpano().call("set(plugin[menu].visible,true); set(plugin[gnametxt].visible,true); set(plugin[logo].visible,true); set(plugin[tooltip].visible,true); set(plugin[popup].visible,true); set(plugin[popup_info].visible,true);");
	krpano().call("set(plugin[zoomin].visible,true); set(plugin[zoomout].visible,true); set(plugin[rotate].visible,true); set(plugin[openfs].visible,true); set(plugin[openfstxt].visible,true);");
	krpano().call("set(plugin[splash].visible,false);");

	if(document.location.href.indexOf("norotate") < 0 && document.location.href.indexOf("options") < 0)krpano().call("set(autorotate.enabled,true);");
	if(document.location.href.indexOf("hidegallery") < 0 && document.location.href.indexOf("nogallery") < 0)krpano().call("set(plugin[gallery].visible,true);");
}

function showPopup(popup, icon, noclose, keeped, closeifkeeped) {

	if(closeifkeeped == "true")closeifkeeped = true; else closeifkeeped = false;
	if(keeped == "true")keeped = true; else keeped = false;
	if(noclose == "true")noclose = true; else noclose = false;

	if(!noclose)krpano().closePopups(closeifkeeped);
	var check = krpano().checkPopup(popup);
	
	if(check)krpano().showPopup(popup, keeped);

	if(icon) {
		if(check)krpano().call("set(plugin["+icon+"].visible, true); set(plugin["+icon+"].onclick,js(showPopup("+popup+", false, true, true, true)));");
		else krpano().call("set(plugin["+icon+"].visible, false);");
	}
}

function fullscreen() {
	krpano().call("set(plugin[openfs].visible,false); set(plugin[closefs].visible,true);");
	if(document.location.href.indexOf("nogallery") < 0)krpano().call("set(plugin[gallery].visible, true);");
}

function exitfullscreen() {
	krpano().call("set(plugin[openfs].visible,true); set(plugin[closefs].visible,false);");
	if(document.location.href.indexOf("hidegallery") >= 0)krpano().call("set(plugin[gallery].visible, false);");
}


function kronclick()
{
	clipbutton(false);
	if(document.location.href.indexOf("options")>=0)getlookat(true);
}

function getlookat(rspot)
{
	
	if(!krpano())return false;

	var mousex = krpano().get("mouse.x");
	var mousey = krpano().get("mouse.y");

	if(mousex && mousey)
	{
		var hvs = krpano().get("screentosphere("+mousex +","+mousey +")");
		var hva = hvs.split(",");
		
		var ath = Number(hva[0]);
		var atv = Number(hva[1]);

		document.getElementById('coords').innerHTML = '<b>ATH:</b> '+ath.toFixed(2)+', <b>ATV:</b> '+atv.toFixed(2);
		
		if(rspot) {

			ath = ath.toFixed(2);
			atv = atv.toFixed(2);

			clp1 = '<hotspot url="%SWFPATH%/plugins/hotspot.swf" ath="'+ath+'" atv="'+atv+'" scale="0.2" zoom="true" direction="up" pano="PANO_NAME" />';
			clp2 = '<lensflare set="DEFAULT" visible="true" ath="'+ath+'" atv="'+atv+'" size="0.30" blind="0.60" blindcurve="4.00" />';
			clp3 = '<hotspot url="%SWFPATH%/plugins/info.png" ath="'+ath+'" atv="'+atv+'" scale=".5" zoom="true" onclick="js(showPopup(POPUP_NAME));" />';
			clp4 = '<hotspot url="%SWFPATH%/plugins/camera.png" ath="'+ath+'" atv="'+atv+'" scale=".5" zoom="true" onclick="js(showPopup(POPUP_NAME));" />';

			document.getElementById('cbuttons').style.visibility = 'visible';
			document.getElementById('xmlst').innerHTML = (clp1+'<br>'+clp2+'<br>'+clp3+'<br>'+clp4).replace(new RegExp("<","g"),"&lt;").replace(new RegExp(">","g"),"&gt;").replace(new RegExp("&lt;br&gt;","g"),"<br>");
		}
	}
}


function clipbutton(button, clp) {

	var clipdiv = document.getElementById('clipdiv');
	var x,y,w,h;

	if(button == false) {
		clipdiv.style.left = 0;
		clipdiv.style.top = 0;
		return false;
	}
	
	document.getElementById('clipobject').setText(clp);
	
	x = button.offsetLeft;
	y = button.offsetTop;
	w = button.offsetWidth;
	h = button.offsetHeight;

	if(y < 300) {
		x += button.offsetParent.offsetLeft+button.offsetParent.offsetParent.offsetLeft;
		y += button.offsetParent.offsetTop+button.offsetParent.offsetParent.offsetTop;
	}
	
	clipdiv.style.left = x;
	clipdiv.style.top = y;
	clipdiv.style.width = w;
	clipdiv.style.height = h;
}

function renderFirst() {
	if(rendered)return false;
	rendered = true;

	if(document.location.href.indexOf("nosplash") >= 0)hidesplash();

	if(document.location.href.indexOf("options")>=0)
	{
		hidesplash();

		var flashcontent = document.getElementById('flashcontent');
		var editcontent = document.getElementById('editcontent');

		flashcontent.style.height = '600px';
		editcontent.style.display = 'block';

		editcontent.innerHTML = '<span style="padding:10px; float: left; clear: both;" id="coords">&nbsp;</span><span style="padding:10px; float: left; clear: both; margin-top: 10px;" id="xmlst">&nbsp;<br>&nbsp;</span><div id="cbuttons" style="visibility: hidden; clear: both; float: left; padding: 10px;"><button id="cbutton1" onMouseover="clipbutton(this, clp1);">Copy hotspot</button>&nbsp;&nbsp;&nbsp;<button id="cbutton2" onMouseover="clipbutton(this, clp2);">Copy lens flare</button>&nbsp;&nbsp;&nbsp;<button id="cbutton3" onMouseover="clipbutton(this, clp3);">Copy info popup hotspot</button>&nbsp;&nbsp;&nbsp;<button id="cbutton4" onMouseover="clipbutton(this, clp4);">Copy camera popup hotspot</button></div><div id="clipdiv" style="width: 1px; height: 1px; left: 0; top: 10px; position:absolute; z-index:10000;"></div>';
		
		var so = new SWFObject("krpano/plugins/clipboard.swf", "clipobject", "100%", "100%", "9.0.28");
		so.addParam("scale", "exactfit");
		so.addParam("wmode", "transparent");
		so.write("clipdiv")

		setInterval( 'getlookat(false)', 66);
	}
}

function changeLanguage(lang) {
	var url = document.location.href;
	if(url.indexOf("?") < 0)url += "?";

	url = url.replace("&lang=es", "");
	url = url.replace("&lang=en", "");
	url = url.replace("&lang=de", "");
	url = url.replace("&lang=fr", "");

	url += "&lang="+lang
	document.location.href = url;
}


window.onload = function() {

	var lang = "es";

	if(document.location.href.indexOf("lang=en") >= 0)lang = "en";
	if(document.location.href.indexOf("lang=fr") >= 0)lang = "fr";
	if(document.location.href.indexOf("lang=de") >= 0)lang = "de";
	
	var xmlfile = lang+'/default.xml';

	if(document.location.href.indexOf("options")>=0)
	{
		xmlfile = 'krpano/options.xml';
	}

	if (typeof(deconcept) !== 'undefined' && deconcept.SWFObjectUtil.getPlayerVersion().major >= 6)
	{
		var so = new SWFObject("krpano/krpano.swf", "krpanoSWFObject", "100%", "100%", "9.0.28","#000000", 100);
		so.addParam("allowFullScreen","true");
		so.addParam("allowScriptAccess","always");
		so.addVariable("pano", xmlfile);
		so.addVariable("license", "krpano/license.xml");
		so.addParam("wmode", "transparent");
		so.useExpressInstall("krpano/expressinstall.swf");
		so.setAttribute('xiRedirectUrl', window.location);
		if ( so.write("flashcontent") ) { var mousewheelfixes = new SWFkrpanoMouseWheel(so); }
	}

};
