var debugMode=5;
var tourUrl="";

var flashTourMenu;
var flashAudio = {element:null, pause:false};
var flashAudioPause=false;
var locationIFrame;
var qtObject;
var qtObjectLoadedPercent=0;
var pageMode="normal"; //could be fullscreen

var currentTourLocation=tourMenus[0].locations[0];
var currentTourMenu=0;



function setTourLocation(tourLocation){
	
	if(pageMode=="normal"){
		//resizeWindow({height:768,width:1024});
	}else if(pageMode=="fullscreen"){
		resizeWindow({height:screen.availHeight,width:screen.availWidth});	
	}
	
	if(tourLocation.reference!=null){
		tourLocation = eval(tourLocation.reference);	
	}	
	
	flashTourMenu = document.getElementById('mapswf');
	flashAudio = document.getElementById("audioswf");
	locationIFrame = document.getElementById('iframeDesc');
	
	var indexes = indexOfLocation("pano",tourLocation.pano);
	setCookie("alharam_tour", indexes.menu+","+indexes.location);
	
	//if the flash menu is there...
	if(flashTourMenu){
		highlightFlashMenu(indexes);
	}
	
	//track locations menu
	if(indexes.menu!=currentTourMenu){
		buildLocationsMenu(indexes.menu);	
	}
	highlightLocationsMenu(indexes);
	
	
	//set the audio paths
	setFlashAudioPaths(tourLocation);
	
	
	//if the iframe is there...
	if(locationIFrame){
		locationIFrame.src=tourLocation.detailHtml;
	}
	
	if(tourLocation.panoScale==null){
		tourLocation.panoScale="TOFIT";
	}
	
	//set the quicktime movie path
	if(pageMode=="fullscreen"){
		writeQTObject(tourLocation.panoFull ,{height:"100%",width:"100%", scale:tourLocation.panoScale});
		//qtObject.SetURL(tourLocation.panoFull);
	}else{
		writeQTObject(tourLocation.pano ,{height:405,width:500, scale:tourLocation.panoScale});
		//qtObject.SetURL(tourLocation.pano);
	}	
	
	qtPluginReady(function(){		
		setFlashAudioPause(flashAudioPause);
	});	
	
	//set current location var	
	currentTourLocation = tourLocation;
	
	
}


function setFlashAudioPaths(locationItem){
	if(locationItem==null){
		locationItem = currentTourLocation;
	}
	
	try{
		flashAudio.setAudioPaths(locationItem.narration, locationItem.ambience);
		flashAudio.setPauseAudio(true);
	
	}catch(err){
		//keep trying until the flash accepts it
		setTimeout("setFlashAudioPaths()", 1000);
	}
}

function resizeWindow(dim){
	window.resizeTo(dim.width,dim.height);
	window.moveTo((screen.availWidth-dim.width)/2, (screen.availHeight-dim.height)/2);	
}

function setTourLocationByIndex(indexes){
	if(typeof indexes == "string"){
		indexes = {menu:indexes.split(",")[0], location:indexes.split(",")[1]};
	}
	localTrace("setTourLocationByIndex({"+indexes.menu +","+ indexes.location +"})", 3);
	try{
		setTourLocation(tourMenus[indexes.menu].locations[indexes.location]);
	}catch(err){
		localTrace(err, 1);	
	}
}


function setTourLocationNext(){
	localTrace("setTourLocationNext()", 2);
	var indexes = indexOfLocation("pano",currentTourLocation.pano);
	try{
		if(currentTourLocation.nextLocation!=null){
			setTourLocation(eval(currentTourLocation.nextLocation));
		}else{
			if(indexes.location < tourMenus[indexes.menu].locations.length-1){
				indexes.location+=1;
			}else{
				indexes.menu+=1;
				indexes.location=0;
			}
			setTourLocationByIndex(indexes);
		}
	}catch(err){
		console.log(err);	
	}
}

function setTourLocationPrev(){
	localTrace("setTourLocationPrev()", 2);
	var indexes = indexOfLocation("pano",currentTourLocation.pano);
	try{
		if(currentTourLocation.prevLocation!=null){
			setTourLocation(eval(currentTourLocation.prevLocation));
		}else{
			if(indexes.location > 0){
				indexes.location-=1;
			}else{
				indexes.menu-=1;
				indexes.location=tourMenus[indexes.menu].locations.length-1;
			}
			setTourLocationByIndex(indexes);
		}
	}catch(err){
		localTrace(err, 1);
	}
}

function getTourLocationCookie(){
	var cookieVal = getCookie("alharam_tour").split(",");
	return {menu:cookieVal[0], location:cookieVal[1]};
}

function indexOfLocation(propName,propValue){
	localTrace("indexOfLocation()", 2);
	var menuIdx,locationIdx;
	for(var tm=0;tm<tourMenus.length;tm++){
		for(var tl=0;tl<tourMenus[tm].locations.length;tl++){
			//console.log(tourMenus[tm].locations[tl][propName]+"\n\t"+propValue);
			if(tourMenus[tm].locations[tl][propName] == propValue){
				menuIdx=tm;	
				locationIdx=tl;
				break;	
			}			
		}
		if(locationIdx!=null){break;}
	}
	
	return {menu:menuIdx,location:locationIdx};
}


function buildLocationsMenu(menuIdx){
	currentTourMenu = menuIdx;
	var locationsList = tourMenus[menuIdx].locations;
	var maxPerCol = tourMenus[menuIdx].maxPerCol;	
	var locationsListSrc = "<ul>";
	var labelNumOffset = tourMenus[menuIdx].labelNumOffset;
	
	for(l=0;l<locationsList.length;l++){
		locationsListSrc += "<li class='item-location'><a href='javascript:setTourLocation(tourMenus["+menuIdx+"].locations["+l+"])'><span class='location-icon'>"+(locationsList[l].sequenceLabel)+"</span><span class='location-label'>"+ locationsList[l].label +"</span></a></li>";	
		if(l % maxPerCol==0 && l>0){
			locationsListSrc+="</ul><ul>";	
		}
		
	}
	locationsListSrc+="</ul>";
	
	var locationsListContainer = document.getElementById('locationsList');
	if(locationsListContainer){
		locationsListContainer.innerHTML=locationsListSrc;
	}
}


function highlightLocationsMenu(indexes){
	if(indexes==null){
		indexes = indexOfLocation("pano",currentTourLocation.pano);
	}
	//set active link in list
	var locationsListLinks = document.getElementById('locationsList');	
	if(locationsListLinks){
		locationsListLinks = locationsListLinks.getElementsByTagName('a');
		for(var ll=0;ll<locationsListLinks.length;ll++){
			if(locationsListLinks[ll].href.indexOf("tourMenus["+indexes.menu+"].locations["+indexes.location+"]")>-1){
			locationsListLinks[ll].className="active";																			  
		  }else{
			 locationsListLinks[ll].className=""; 
		  }
		}
	}	
}


function highlightFlashMenu(indexes){
	if(indexes==null){
		indexes = indexOfLocation("pano",currentTourLocation.pano);
	}
	
	try{
		flashTourMenu.highlightMap(indexes.menu+","+indexes.location);		
	}catch(err){
		//keep trying until the flash accepts it
		setTimeout("highlightFlashMenu()", 1000);
	}
		
}


function setFlashAudioPause(isPaused){
	localTrace("setFlashAudioPause("+isPaused+")", 2);
	try{
		flashAudioPause = (isPaused==true||isPaused=="true")?true:false;
		//flashAudio.SetVariable("pauseAudio", flashAudioPause);
		flashAudio.setPauseAudio(flashAudioPause);
		
		setCookie("alharam_audio_pause", flashAudioPause);
		
	}catch(err){
		//keep trying until the flash accepts it
		setTimeout("setFlashAudioPause("+isPaused+")", 1000);
	}
	
}


function showFullScreen(){
   //alert("This will show the full screen version of '"+ActiveOverview +" \ "+ ActiveArea+"'");
	flashAudio.setPauseAudio(true);
	window.open("fullscreen.htm","FullScreenPanorama",'fullscreen=yes,scrollbars=no,status=no,toolbar=no,directories=no,menubar=no,location=no' );
}

function exitFullScreen(){
	window.close();
	opener.setTourLocationByIndex(getTourLocationCookie());
}

function writeQTObject(movUrl, params){
	
	var qtObjSrc = "";
	
	if (document.addEventListener){
		qtObjSrc+='<embed height="'+ params.height+'" width="'+ params.width +'" src="'+ movUrl +'" ';
		qtObjSrc+='scale="'+ params.scale +'" moviename="qtmovie" id="qtmovie" controller="true" target="myself" enablejavascript="true" postdomevents="true" cache="true" showlogo="false" ';	
		qtObjSrc+=' type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/">'
		qtObjSrc+='</embed>';
	}else{
		// IE
		qtObjSrc+='<object id="qt_event_source" style="display:none;" classid="clsid:CB927D12-4FF7-4a9e-A169-56E4B8A75598" codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=7,2,1,0" ></object>';
		
		qtObjSrc+='<object id="qtmovie" type="video/quicktime" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" ';
		qtObjSrc+='height="'+ params.height +'" width="'+ params.width +'" ';
		qtObjSrc+='style="behavior:url(#qt_event_source);">';
		qtObjSrc+='<param name="src" value="'+ movUrl +'"/>';
		qtObjSrc+='<param name="moviename" value="qtmovie"/>';
		qtObjSrc+='<param name="autoplay" value="true"/>';
		qtObjSrc+='<param name="controller" value="true"/>';
		qtObjSrc+='<param name="scale" value="'+ params.scale +'"/>';
		qtObjSrc+='<param name="enablejavascript" value="true"/>';
		qtObjSrc+='<param name="postdomevents" value="true"/>';
		qtObjSrc+='<param name="cache" value="true"/>';
		qtObjSrc+='<param name="showlogo" value="false"/>';
		qtObjSrc+='</object>';
	}
	document.getElementById("movDisplay").innerHTML=(qtObjSrc);
	
	
	qtObject = document.getElementById("qtmovie");
	
}



function qtPluginReady(callback){
	
	var qtStatusInterval = setInterval(statusWatch, 10);
	
	function statusWatch(){
		//alert();
		localTrace("QT Status: "+document.getElementById("qtmovie").GetPluginStatus(), 3);
		if(document.getElementById("qtmovie").GetPluginStatus()=="Complete"){
			clearInterval(qtStatusInterval);
			qtObject.SetResetPropertiesOnReload(false);
			
			if(typeof callback == "function"){
				callback();	
			}
		}
	}
	
}

function setCookie(cookieName,cookieValue) {
	document.cookie = cookieName+"="+escape(cookieValue) + ";path=/;";
}
function getCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}




if(!console){
	var console = {log:function(msg){
		//alert(msg.toString());
	}};
}

function localTrace(msg, debugLevel){
	if(debugLevel==null || debugMode>=debugLevel){
		console.log(msg);	
	}
}