/** 
 * alte Spin.js
 */

var is = new Is();
/** 
 * Konfiguration
 */

var bCounter = true;
 

/**
 * Setting up some vars
 */
 var spin = new Array();
 var anzahl_spins;
 var curr_spin;
 var editor;
 var old_spin;
 var cachHS;
 var tourrunning = true;
 var externe_popups	= false;
 if (!tunefullink) {
	 var tunefullink = "";
 }
 if (!size_x) {
	var size_x = 0;
 }
 if (!size_y) {
	var size_y = 0;
 }
 if (!curr_lang) {
	var curr_lang = "de";
 }
		
 // objekte für data.js vorbereiten
 var group = new Array();
 for (var a=1; a<=50; a++) {
	group[a] = new Array(5);
 }
 var objekt = new Array(100);
 for (var a=1; a<=100; a++) {
	objekt[a] = new Array(15);
 }
 // misc vars
 var floorId = "floorPlan";
 var spinplayerID = 'spinid';

 var waitdata   = false;
 var objektnr,hs,flashfilm,popupwindow,target;
 
/**
 *	Importing scripts..
 */
 // Common part
 importScript('swfobjectJs','scripts/swfobject.js');
 importScript('dataJs',curr_lang+'/data.js'); 	
 importScript('jquery-latest','scripts/jquery-latest.js');
 importScript('thickbox','scripts/thickbox.js');

 
 // Config part
 
 importScript('MenuGroupJs','scripts/showMenu.js');

 if(bCounter == true) 	importScript('counterJs','scripts/counter.js');
  
  
importScript('loadXMLJs','scripts/loadXML.js');








/**
 * Hotspot Funktionen
 */

 
function openHS(objnr) {
	objektnr=objnr;
	if (bCounter) counter(objektnr);
	
	if(extern_popup=="true") {
		hslink = curr_lang+ "/hs" + objnr +".html"
	} else {
		hslink = "popup.html"	
	}

	self.parent.tb_show('Hotspot',tunefullink + hslink +'?keepThis=true&TB_iframe=true&width=' + (objekt[objektnr]["size-x"] ) + '&height=' + (objekt[objektnr]["size-y"] ) + '&modal=true','');

}

function openPDF(objnr) {
	objektnr=objnr;
	if (bCounter) counter(objektnr);
	popupwindow = window.open("pdfs/" + objekt[objektnr]["pdf"],'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,');
}

function openPage(objnr) {
	objektnr=objnr;
	if (bCounter) counter(objektnr);
	popupwindow = window.open(objekt[objektnr]["link"],'','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,');
}

function openTuneful(link) {
	popupwindow = window.open(link,'','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,');
}

function openForm(form) {
//	produkt  = false;
//	objektnr = false;
	popupwindow = window.open(form,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=400,height=571');
}

/**
 * Player-Controls
 */
function nextPano() {
	if(curr_spin<anzahl_spins) {
		getPano(curr_spin+1);	
	} else {
		getPano(1);
	}
}

function lastPano() {
	if(curr_spin>1) {
		getPano(curr_spin-1);	
	} else {
		getPano(anzahl_spins);
	}
}

function tourOff() {
	tourrunning = false;	
}

function tourOn() {
	tourrunning = true;	
}
 
function getPano(s) {
	if(intern_floorplan == "true") grundrissBig();
	loaded    = false;
	old_spin  = curr_spin;
	curr_spin = s;
	
	writeFlashCode();
		
	
	showHotspots(curr_spin, true);
	if(extern_floorplan == "true") setCurrent();
	if(bCounter) writeCounterScript('spin'+s);
	

}


function hotspot(hsnr) {
	target = spin[curr_spin-1]['hotspot'][hsnr-1]['target'];
	if(target!="LOADPDF" && target!="LOADLINK") {
		window.document.pano.tunefulExternalPanoExecute('hotspot'+ (hsnr-1) +'.move');
	} else {
		eval(spin[curr_spin-1]['hotspot'][hsnr-1]['action']);
		window.document.pano.tunefulExternalPanoExecute('hotspot'+ (hsnr-1) +'.move');
	}
}

function hotspotNoMove(hsnr) {

	var panangle = Math.round(spin[curr_spin-1]['hotspot'][hsnr-1]['pos_x'] );
	var tilt     = Math.round(spin[curr_spin-1]['hotspot'][hsnr-1]['pos_y'] );
	target = spin[curr_spin-1]['hotspot'][hsnr-1]['target'];
	setTimeout("eval(target);",900);	
	
}

function anleitung() {
	tb_show('','images/player/anleitung.html?keepThis=true&TB_iframe=true&width=308&height=328&modal=true','');
}
	
/**
 * Floorplan
 */

function writeFloorplan() {
	if(extern_floorplan=="true") {	
	}
	if(intern_floorplan=="true") {
	
	
		if (curr_lang == "de") {
	
			var grundrissImage ='<a href="javascript:void(0);" onClick="grundrissPlayer();"><img src="images/grundriss.jpg" border="0" width="'+ floorplan_width +'" height="'+ floorplan_height +'" align="right"></a>';
			
			writeInto("floorPlan", grundrissImage);
			
		} else {
		
			var grundrissImage ='<a href="javascript:void(0);" onClick="grundrissPlayer();"><img src="images/grundriss_en.jpg" border="0" width="'+ floorplan_width +'" height="'+ floorplan_height +'" align="right"></a>';
			
			writeInto("floorPlan", grundrissImage);
		
		}
		
		
		/*
		var grundrissImage ='<a href="javascript:void(0);" onClick="grundrissSmall();"><img src="images/grundriss.jpg" border="0" width="'+ floorplan_width +'" height="'+ floorplan_height +'" id="grundrissImage" align="right"></a>';
		
		writeInto("floorPlan", grundrissImage);
		
		var img = document.getElementById('grundrissImage');
		img.state = 'large';
		img.largeWidth = parseInt(img.getAttribute('width'));
		img.largeHeight = parseInt(img.getAttribute('height'));
		img.smallWidth = parseInt(img.getAttribute('width')/10);
		img.smallHeight = parseInt(img.getAttribute('height')/10);
		img.ratio = img.largeHeight / img.largeWidth;
		*/
	}
}
 

function grundrissBig() {
	/*
	var img = document.getElementById('grundrissImage');		
	img.style.visibility = 'visible';
	img.style.display = 'block';	
	var interval = window.setInterval(scaleStep, 10);
	
	return;
	
	function scaleStep()
	{
		var step = 10;
		var width = parseInt(img.getAttribute('width'));
		var height = parseInt(img.getAttribute('height'));
		
		width += step;
		height += Math.floor(step * img.ratio);
		
		img.setAttribute('width', width);
		img.setAttribute('height', height);
		
		if (width > img.largeWidth - step)
		{
			img.setAttribute('width', img.largeWidth);
			img.setAttribute('height', img.largeHeight);
			window.clearInterval(interval);
		}
	}		
	*/
}

function grundrissPlayer() {
	window.document.pano.tunefulExternalPanoExecute('grundriss.onClick');
}

function grundrissSmall() {
	/*
	var img = document.getElementById('grundrissImage');		
	window.document.pano.tunefulExternalPanoExecute('grundriss.makeBig');
	var interval = window.setInterval(scaleStep, 10);
	
	return;
	
	function scaleStep()
	{
		var step = 10;
		var width = parseInt(img.getAttribute('width'));
		var height = parseInt(img.getAttribute('height'));
		
		
			
		width -= step;
		height -= Math.floor(step * img.ratio);

		img.setAttribute('width', width);
		img.setAttribute('height', height);
		
		if (width < img.smallWidth + step)
		{
			img.setAttribute('width', img.smallWidth);
			img.setAttribute('height', img.smallHeight);
			window.clearInterval(interval);
			img.style.visibility = 'hidden';
			img.style.display = 'none';
		}
		
	}
	*/
}

 
	
 function showPanos() {
	var hscont = '<img src="images/grundriss.jpg" border="0" width="'+ floorplan_width +'" height="'+ floorplan_height +'" id="grundrissImage" align="right">';

	for (var h=1; h<=anzahl_spins; h++) {
		hscont += '<div id="shs'+h+'" style="position:absolute;left:'+ spin[h-1]['map_x'] +'px;top:'+ spin[h-1]['map_y'] +'px;z-index:99;">';
		
		
		//nächsten 2 zeilen auskommentieren wenn menü3 (= alle hs sichtbar) und trotzdem plan mit zielscheiben
		
		hscont += '<a href="javascript:getPano('+h+');" ';

		hscont += ' onmouseover="showHotspotsFloorplan('+h+');" onmouseout="showHotspots();"';
		
		hscont += ' onclick="cachHS=false;">';
		
		
		hscont += '<img src="images/player/standort.gif" width="44" border="0" id="shs'+h+'img" /></a>';
		hscont += '</div>';
	}
	writeInto(floorId,hscont);
 }
 
 function setCurrent() {
 	if(old_spin != curr_spin) {
 	 	var oldId  = "shs"+old_spin+"img";
 		var old    = getById(oldId);
	 	old.src    = "images/player/standort.gif";
 	}
 	var currId = "shs"+curr_spin+"img";
 	var obj    = getById(currId);
 	obj.src = "images/player/standort_a_" + curr_lang + ".gif";
 }

/**
 * Misc
 */
 
function Is() {
	agent = navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.mac = (agent.indexOf("mac")!=-1);
	this.macff = (agent.indexOf('mac') != -1 && agent.indexOf('firefox')!=-1);
	this.win = ((agent.indexOf("win")!=-1) || (agent.indexOf("32bit")!=-1));
	this.ie = ((agent.indexOf("msie") != -1)&& (agent.indexOf('opera')==-1));
	this.macie = (this.mac && this.ie);
	this.firefox = (agent.indexOf("firefox")!=-1);
}

function importScript(id, jsfile) {
	document.write('<sc'+'ript type="text/javascript" src="'+jsfile+'"></sc'+'ript>');
}
 
function init() {
	if(bMenuType != "0") showHotspots(curr_spin,true);
	if(bCounter) writeCounterScript("spin" + curr_spin);
}


 
 
function writeInto(obj, text, win) {
	if(!obj || typeof obj != "string") return;
	
	obj = getById(obj, win);
	
	if(typeof obj == "undefined" || obj == null) return;
	
	if(typeof obj.innerHTML != 'undefined') {
  		obj.innerHTML = text;
	} else if(typeof document.layers != 'undefined') {
		obj.document.open('text/html');
	    	obj.document.write(text);
	     	obj.document.close();
	}
}

function getById(id, win) {
	if(typeof id == "undefined" || id == null) return null;
	
	var doc = (win) ? win.document : document;
	var obj  = null;

	if(document.getElementById) obj = doc.getElementById(id);
	else if(typeof document.layers != 'undefined')  obj = _findObj_(id, doc);
	else if(document.all) obj = doc.all[id];
	
	return obj;
}

function neuesFenster(link) {
	 window.open(link,'','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,');

}

/**
 * Schreiben des Players
 */
function writeCodeAfterWait() {
		writeFloorplan();
		if(extern_floorplan == "true") showPanos();
		if(extern_floorplan == "true") setCurrent();
		writeFlashCode();
}

function writeFlashCode() {
		/*if(spin[curr_spin-1]['backimage']) {
			dataExportFoto(curr_spin-1);
		} else {
			dataExport(curr_spin-1);
		}*/
		dataExport(curr_spin-1);
		cachHS = "";
		
		var so = new SWFObject("controls/pano.swf", "pano", size_x, size_y, "9.0.28", loaderbackground);
		so.addVariable("panoName", "scenes/scene"+curr_spin);
		so.addVariable("xml_text", panodata);
		so.addParam("allowFullScreen","true");
		so.addParam("scale", "noscale");
		so.addParam("menu", "false");
		so.addParam("allowScriptAccess", "always");
		so.addVariable("menu", "false");
		so.addVariable("quality", "best");
		
		if(!so.write("spinid")) { 				//Wenn flash 9 nicht installiert ist
			writeFlashCodeOld();
		} 

}

function writeFlashCodeOld() {

		flash6_text = '<table width="300"><tr><td valign="top"><img src="images/player/achtung.gif"></td><td>';

		if(parent.curr_lang=="de") {
			flash6_text += 'Um die optimale Darstellung zu sehen, installieren Sie bitte den aktuellen Adobe Flash Player.<br><a href="javascript:neuesFenster(\'http://www.adobe.com/de/products/flashplayer/\');">Flash kostenlos downloaden.</a>';
		} else {
			flash6_text += 'Please install the newest Adobe Flash Player to obtain optimum performance.<br><a href="javascript:neuesFenster(\'http://www.adobe.com/products/flashplayer/\');">Download Flash Player for free.</a>';
		}
		
		flash6_text += '</td></tr></table>';
		
		writeInto("spinid", flash6_text);
}
