
//var siteUrl="http://localhost/issue/200604/alhambra/";
var tourUri = "/issue/200604/alhambra/",
	siteUrl="http://www.saudiaramcoworld.com/issue/200604/alhambra/";
//-------Overview Items-----------
function SelectOverview(overview, subArea){
	SetCookie('ActiveOverview', overview);
	SetCookie('ActiveArea', subArea);
	window.location="tour.htm";	
}
function SetCookie(cookieName,cookieValue) {
	document.cookie = cookieName+"="+escape(cookieValue) + ";path="+tourUri+";";
}
function ReadCookie(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));
}

//------------------------------------------


function ChangeMovie(type, folder, changeTo){
	//console.log(type+" - "+folder+" / "+changeTo);

	if(!(folder in tourAreas) || !(changeTo in tourAreas[folder])){
		console.log(folder + " or "+ changeTo + " is not a valid location");
		return;	
	}
	
	var location = tourAreas[folder][changeTo];
	if("reference" in location){
		folder = location.reference[0];
		changeTo = location.reference[1];	
	}

	ActiveOverview = folder;
	ActiveArea = changeTo;
	SetCookie("ActiveOverview", ActiveOverview);
	SetCookie("ActiveArea", ActiveArea);
	
	
	document.getElementById("audioswf").SetVariable("activeOverview", folder);
	document.getElementById("audioswf").SetVariable("activeArea", changeTo);
	document.getElementById("audioswf").SetVariable("changeSound", 1);
	
	
	WriteMovie();
	
	if(ThisWindow=="normal" && type=="normal"){	
		document.getElementById('menuswf').SetVariable("activeOverview", folder);
		document.getElementById('menuswf').SetVariable("myMovie", changeTo);
		
		document.getElementById('iframeDesc').src=tourUri+"desc/"+folder+"/"+changeTo+".htm";	
		
		/*document.getElementById('qtmovie').SetURL(tourUri+"mov/"+folder+"/"+changeTo+".mov");
		document.getElementById('qtmovie').SetMovieName("qtmovie");*/
		
	}else if(ThisWindow=="fullscreen" && type=="fullscreen"){
		/*document.getElementById("fullscreenaudioswf").SetVariable("activeOverview", folder);
		document.getElementById("fullscreenaudioswf").SetVariable("activeArea", changeTo);
		document.getElementById("fullscreenaudioswf").SetVariable("changeSound", 1);
		document.getElementById('movDisplay').innerHTML='<embed name="qtfullscreenmovie" id="qtfullscreenmovie" moviename="qtfullscreenmovie" src="mov/'+folder+'/'+changeTo+'.mov" width="100%" height="100%" alt="" cache="TRUE" kioskmode="true" controller="false" scale="toFit" PLUGINSPAGE="http://www.apple.com/quicktime/download/"></embed>';
		
		WriteMovie();
		//document.getElementById('qtfullscreenmovie').SetResetPropertiesOnReload(true);
		//document.getElementById('qtfullscreenmovie').SetURL(siteUrl+"mov/"+folder+"/"+changeTo+".mov");*/
	}else if(type=="popup"){
		SetCookie("ActiveDetail",changeTo);
		window.open("detail.htm","DetailImage",'width=' + 600 + ',height=' + 600 + ',scrollbars=no,status=no,toolbar=no,directories=no,menubar=no,location=no' );
	}
}
function WriteDetailMovie(){
	document.getElementById('detailMovie').innerHTML='<embed name="qtdetailmovie" id="qtdetailmovie" moviename="qtdetailmovie" src="mov/'+ActiveOverview+'/'+ShowMovieName+'.mov" width=720 height=480 alt="" cache="TRUE" kioskmode="true" controller="true" scale="toFit" PLUGINSPAGE="http://www.apple.com/quicktime/download/"></embed>';
}
function WriteMovie(){
	try{
		var area = tourAreas[ActiveOverview],
			location = area[ActiveArea],
			pano = location.pano;
		//console.log("ActiveOverview: "+ActiveOverview+"\nActiveArea: "+ActiveArea);
	}catch(err){
		console.log(err);
		return;		
	}
	//console.log(pano);
	//alert(pano);
	
	var height = (ThisWindow === "fullscreen") ? "100%" : 400,
		width = (ThisWindow === "fullscreen") ? "100%" : 500;
	
	
	var swf = createswf("flash_panos/global/krpano.swf", "krpano", width, height);
	swf.addVariable("license", "flash_panos/global/krpano-license.xml");
	swf.addVariable("xml", pano);
	swf.embed("movDisplay");
	
	//document.getElementById('movDisplay').innerHTML='<embed name="qtmovie" id="qtmovie" moviename="qtmovie" src="mov/'+ActiveOverview+'/'+ActiveArea+'.mov" width=500 height=400 alt="" cache="TRUE" kioskmode="true" controller="true" scale="toFit" PLUGINSPAGE="http://www.apple.com/quicktime/download/"></embed>';
}
/*
function WriteController(){
	document.getElementById('movController').innerHTML='<embed name="qtcontrol" id="qtcontrol" src="controller.mov" width=500 height=45 alt="Movie Controller" cache="TRUE" kioskmode="true" controller="false" autoplay="true" scale="toFit" PLUGINSPAGE="http://www.apple.com/quicktime/download/"></embed>';
}
function WriteFullscreenController(){
	document.getElementById('movController').innerHTML='<embed name="qtcontrol" id="qtcontrol" src="fullscreen_controller.mov" width=438 height=23 alt="Movie Controller" cache="TRUE" kioskmode="true" controller="false" autoplay="true" scale="toFit" PLUGINSPAGE="http://www.apple.com/quicktime/download/"></embed>';
}
function WriteFullscreenMovie(){
	document.getElementById('movDisplay').innerHTML='<embed name="qtfullscreenmovie" id="qtfullscreenmovie" moviename="qtfullscreenmovie" src="mov/'+ActiveOverview+'/'+ActiveArea+'.mov" width="100%" height="100%" alt="" cache="TRUE" kioskmode="true" controller="false" scale="toFit" PLUGINSPAGE="http://www.apple.com/quicktime/download/"></embed>';
}*/

function WriteIFrame(){
	alert("why");
	document.getElementById('iframeDiv').innerHTML='<iframe id="iframeDesc" align="top" frameborder="0" height="146" width="500" marginheight="0" marginwidth="0" name="iframeDesc" scrolling="auto" src="desc/alcazaba/alcazaba01.htm"></iframe> '
}

function OpenDetailMovie(movieName){
		SetCookie("ShowMovieName", movieName);
		window.open(tourUri+"movie.htm","DetailMovieWindow",'width=' + 770 + ',height=' + 530 + ',scrollbars=no,status=no,toolbar=no,directories=no,menubar=no,location=no' );
}

function FullScreenVersion(){
   //alert("This will show the full screen version of '"+ActiveOverview +" \ "+ ActiveArea+"'");
		//SetCookie("ActiveArea",ActiveArea+"hi");
		document.getElementById("audioswf").SetVariable("pauseAudio", true);
		var fsWin = window.open(tourUri+"fullscreen.htm","FullScreenPanorama",'fullscreen=yes,scrollbars=no,status=no,toolbar=no,directories=no,menubar=no,location=no' );
		fsWin.resizeTo(screen.availWidth, screen.availHeight);
		fsWin.moveTo(0,0);
}

function SmallScreenResize(){
	if(screen.width<1024){
		window.resizeTo(800,600);
	}
}




var tourAreas = {
	
};


tourAreas.alcazaba = {
	"alcazaba01":{
		pano: "flash_panos/01/alcazaba1Med.xml"	
	},
	"alcazaba02":{
		pano:"flash_panos/02/alcazaba2Med.xml"
	},
	"alcazaba03":{
		pano:"flash_panos/03/alcazaba3.xml"
	},
	"alcazaba04":{
		pano:"flash_panos/04/alcazaba04.xml",
		next:["nasrid", "nasridentrance"]
	}
};


tourAreas.nasrid = {
	"nasridentrance":{
		pano:"flash_panos/05/nazridentrance.xml",
		prev:["alcazaba", "alcazaba04"]
	},
	"mexuarhall":{
		pano:"flash_panos/06/01mexuarHallMedCubical.xml"
	},
	"mexuaroratory":{
		pano:"flash_panos/07/mexuarOratory.xml"
	},
	"mexuargilded":{
		pano:"flash_panos/08/03MexuarGildedRoomMed.xml"
	},
	"mexuarpatio01":{
		pano:"flash_panos/09/04MexuarPatio1Med.xml"
	},
	"mexuarpatio02":{
		pano:"flash_panos/10/05MexuarPatio2Med.xml"
	},
	"comareshall":{
		pano:"flash_panos/11/07comaresHallMed.xml"
	},
	"arrayanes01":{
		pano:"flash_panos/12/arrayanes01.xml"
	},
	"arrayanes03":{
		pano:"flash_panos/13/arrayanes03.xml"
	},
	"mocarabes":{
		pano:"flash_panos/14/09MocarabesMed.xml"
	},
	"leones01":{
		pano:"flash_panos/15/10Leones2Med.xml"
	},
	"leones03":{
		pano:"flash_panos/16/Leones3.xml"
	},	
	"leones02":{
		pano:"flash_panos/18/Leones2.xml"
	},
	"abencerrajes":{
		pano:"flash_panos/17/Abencerrajes.xml"
	},
	"doshermanas":{
		pano:"flash_panos/19/14dosHermanas.xml"
	},	
	"lindaraja":{
		pano:"flash_panos/20/Lindaraja.xml"
	},
	"apartment":{
		pano:"flash_panos/21/apartment.xml"
	},
	"partal04":{
		pano:"flash_panos/22/partal4.xml",
		next:["generalife","generalife01"]
	},
	"charlesvcenter":{
		pano:"flash_panos/25/charlesVcenter.xml",
		prev:["generalife","generalife02"]
		//reference:["generalife","charlesvcenter"]
	}
}


tourAreas.generalife = {
	"generalife01":{
		pano: "flash_panos/23/generalife4Med.xml",
		prev:["nasrid","partal04"]	
	},
	"generalife02":{
		pano:"flash_panos/24/generalife6Med.xml"
	},
	"charlesvcenter":{
		//pano:"flash_panos/25/charlesVcenter.xml"
		reference:["nasrid","charlesvcenter"]
	}
};



var getPrev = function(pivot){
	if(pivot==null) pivot = ActiveArea;
	var area = ActiveOverview,
		prev = null;
	for(var key in this){
		if(pivot == key){
			break;
		}
		if(typeof(this[key]) !== "function"){
			prev = key;
		}
	}
	//console.log("prev: "+prev + " from "+ ActiveArea);
	return prev;
};
	
var getNext = function(pivot){
	if(pivot==null) pivot = ActiveArea;
	var flag = false,
		next = null;
	for(var key in this){

		if(flag && typeof(this[key]) !== "function"){
			next = key;
			break;	
		}else if(pivot == key){
			flag = true;
		}
	}
	//console.log("next: "+next + " from "+ ActiveArea);
	return next;
};

for(var area in tourAreas){
	tourAreas[area].getPrev = getPrev;
	tourAreas[area].getNext = getNext;
}


function setTourLocationFirst(){
	ChangeMovie(ThisWindow, "alcazaba", "alcazaba01");
}

function setTourLocationNext(){
	var current = tourAreas[ActiveOverview][ActiveArea],
		next = tourAreas[ActiveOverview].getNext();
	
	if("next" in current){
		//Use "next" property of current location
		ChangeMovie(ThisWindow, current.next[0], current.next[1]);
	}else if(next != null){
		//Use next location in the sequence
		ChangeMovie(ThisWindow, ActiveOverview, next);
	}
}

function setTourLocationPrev(){
	var current = tourAreas[ActiveOverview][ActiveArea],
		prev = tourAreas[ActiveOverview].getPrev();
	
	if("prev" in current){
		//Use "next" property of current location
		ChangeMovie(ThisWindow, current.prev[0], current.prev[1]);
	}else if(prev != null){
		//Use next location in the sequence
		ChangeMovie(ThisWindow, ActiveOverview, prev);
	}
}



function showFullScreen(){
	FullScreenVersion();
}

function exitFullScreen(){
	window.close();
	//opener.setTourLocationByIndex(getTourLocationCookie());
	opener.ChangeMovie("normal", ReadCookie("ActiveOverview"), ReadCookie("ActiveArea"));
}

