//global vars
loadMap = null;
//local vars
var P7_SSMshowImage_orig = P7_SSMshowImage;
var P7_SSMdispFin_orig = P7_SSMdispFin;
var P7_SSMopenPanel_orig = P7_SSMopenPanel;
var P7_SSMszg_orig = P7_SSMszg;
var P7_SSMinit_orig = P7_SSMinit;
var modules = ["360","virtual tour","slideshow","google map","information"];
var noThumbModIndex = [2,3,4];
var activeModule;
var fullScreenButton;
var google_map_script;
var google_map;

var flashHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width=%WIDTH% height=%HEIGHT%>'+
				   '<param name="movie" value="%FILE%">'+
				   '<param name="quality" value="high">'+
				   '<!--param name="wmode" value="transparent"-->'+
				   '<embed src="%FILE%" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width=%WIDTH% height=%HEIGHT%></embed>'+
			    '</object>';

overrideFunctions();

function overrideFunctions() {
	P7_SSMinit = function (a1,a2,a3,a4,a5,a6,a7,a8,a9) {
		var tab = document.getElementById("p7SSMtp_1");
		var x = 2;
		var found;
		
		P7_SSMinit_orig(a1,a2,a3,a4,a5,a6,a7,a8,a9);

		//alter tabs
		while (tab) {
			found = false;
			//remove onmouse over effect
			for (var y=0; y < noThumbModIndex.length; y++) {
				if (tab.innerHTML.toLowerCase().indexOf(modules[noThumbModIndex[y]]) > -1) {
					tab.onmouseover = function(){};
					found = true;
					break;
				}
			}
			if (!found) //open all openable tabs
				P7_SSMopenPanel(tab.id);
			//add img overlay
			var img = document.createElement("img");
			img.style.position = "absolute";
			img.style.marginTop = "-3px";
			img.style.marginLeft = "-10px";
			img.style.border = "";
			img.border = 0;
			img.src = "../images/tab_"+tab.innerHTML.toLowerCase().replace(/ /g,"_")+".gif";
			tab.insertBefore(img,tab.firstChild);
			//
			tab = document.getElementById("p7SSMtp_"+x);
			x++;
		}
	};
	P7_SSMshowImage = function(n) {
		if (!fullScreenButton) 
			p7_addFullscreenButton();
		
		if (p7sscur != (n-1)) {
			P7_SSMshowImage_orig(n);
			for (var x = 0; x < modules.length; x++) {
				if (p7_isModule(modules[x])) {
					p7_removeModules();
					if (!P7_chkModule(p7sstmb[p7sscur],modules[x])) {
						p7sscur--;
						P7_SSMplay();
					}
					break;
				}
			}
		}
	};
	P7_SSMdispFin = function() {
		P7_SSMdispFin_orig();
		
		var isMod = false;
		
		if (fullScreenButton)
			fullScreenButton.style.display = "none";
		
		//if its a module ref show it and pause...
		for (var x = 0; x < modules.length; x++) {
			if (P7_chkModule(p7sstmb[p7sscur],modules[x])) {
				p7_displayModule(modules[x]);
				p7_disableControls();
				P7_SSMpause();
				isMod = true;
				break;
			}
		}
		if (!isMod) {
			for (var x = 0; x < modules.length; x++) {
				p7_removeModule(modules[x]);
			}
			p7_enableControls();
		}
	};
	P7_SSMopenPanel = function(iD,c) {
		doit = true;
		for (var x=0; x < noThumbModIndex.length; x++) {
			if (document.getElementById(iD).innerHTML.toLowerCase().indexOf(modules[noThumbModIndex[x]]) > -1) {
				doit = false;
				break;
			}
		}
		if (doit)
			P7_SSMopenPanel_orig(iD,c);
		else
			P7_SSMtmb(document.getElementById(iD.replace("tp","wp")).getElementsByTagName("A")[0]);
	};
	P7_SSMszg = function(id,id2,ch,cw,th,tw){
		//numbers to change speed of pic transition
		var ih=30,iw=30;
		//end numbers
       	var i,d,dd,mh,mw,dy=20;
       	d=document.getElementById(id);
       	dd=document.getElementById(id2);
       	d.style.height=ch+'px';
       	d.style.width=cw+'px';
       	mh=(ch<=th)?0:1;
       	mw=(cw<=tw)?0:1;
       	ih=(mh==1)?ih*-1:ih;
       	iw=(mw==1)?iw*-1:iw;
       	if(ch==th && cw==tw){
            dd.style.display="block";
            d.style.overflow="visible";
            d.style.height=dd.offsetHeight+"px";
            dd.style.visibility="visible";
            setTimeout("P7_SSMfadeIn('p7ssm_im',.05)",250);
   	 	}
    	else {
           	ch+=ih;
           	cw+=iw;
           	if(mh==0)
                ch=(ch>=th)?th:ch;
           	else
                ch=(ch<=th)?th:ch;
           	if(mw==0)
                cw=(cw>=tw)?tw:cw;
           	else
                cw=(cw<=tw)?tw:cw;
           	if(d.p7hssz)
            	clearTimeout(d.p7hssz);
           	d.p7hssz=setTimeout("P7_SSMszg('"+id+"','"+id2+"',"+ch+","+cw+","+th+','+tw+")",dy);
    	}		
	}
}
function P7_chkModule(sNode,module) {
	var node = sNode.parentNode;
	//second try
	while (node) {
		if (node.parentNode.id.indexOf("p7SSMwp") > -1) { 
			var id = node.parentNode.id.split("_");
			return (document.getElementById("p7SSMtp_"+id[1]).innerHTML.toLowerCase().indexOf(module) > -1);
		}
		node = node.parentNode; 
	}
	return false;
}
function p7_displayModule(module) {
	var pI = document.getElementById("p7ssm_im");
	var div = document.getElementById("p7ssm_fsc");
	var heightA = parseInt((pI.height) ? pI.height : pI.style.height);
	var widthA = parseInt((pI.width) ? pI.width : pI.style.width);
	var modNode = document.createElement("div");
	var html = "&nbsp;";
	activeModule = module;
	switch (module) {
		case "360":
		case "virtual tour":
			var file = pI.src.substr(0,pI.src.lastIndexOf(".")).replace(/fullsize/g,"tour")+".swf";
			html = flashHTML.replace(/%FILE%/g,file).replace(/%WIDTH%/g,widthA).replace(/%HEIGHT%/g,heightA);
			modNode.id = "p7SSM_mod_vt";
			fullScreenButton.style.display = "block";
			break;
		case "slideshow":
			var file = pI.src.substr(0,pI.src.lastIndexOf(".")).replace(/fullsize/g,"slideshow")+".swf";
			html = flashHTML.replace(/%FILE%/g,file).replace(/%WIDTH%/g,widthA).replace(/%HEIGHT%/g,heightA);
			modNode.id = "p7SSM_mod_ss";
			break;
		case "google map":
			if (location.href.indexOf("esitetours.com") > -1)
				var key = "ABQIAAAA59t2hgd-_YAA8r6z26k0UxTqqVfygukw0yHKQwBdHQSWdNt3LxQbZVKhaNF2BgWgpR8Pz_LfPZR7WQ";
			else
				var key = "ABQIAAAA59t2hgd-_YAA8r6z26k0UxTI5fVEDkV0jzUNt-R-Q9pDs8vkoRTpIw7vehDmNlOiGriO7e6akTdFxw";
			//insert early for the sake of loading
			if (!google_map_script) {
				google_map_script = document.createElement('script');
				google_map_script.setAttribute("type","text/javascript");
				google_map_script.setAttribute("src","http://maps.google.com/maps?file=api&v=2.x&key="+key+"&async=2&callback=p7_loadMap"); 
				google_map_script.setAttribute("id","p7SSM_mod_gm_script");
				document.documentElement.firstChild.appendChild(google_map_script);
			}
			else
				setTimeout(p7_loadMap,500);
			html = '<div id="google_map" style="width:'+widthA+'px;height:'+heightA+'px"></div>';
			modNode.id = "p7SSM_mod_gm";
			break;
		case "information":
			html = '<div style="background-color:white;"><iframe src="info.html" frameborder="0" width="'+widthA+'px" height="'+heightA+'px" style="border:none;"></iframe></div>';
			modNode.id = "p7SSM_mod_if"
			break;
	}
	if (modNode.id.length) {
		//make sure there are no mods in its way
		p7_removeModules();
		//display mod
		with (modNode.style) {
			zIndex = "999999";
			position = "absolute";
			width = widthA;
			height = heightA;
			overflow = "hidden";
		}
		modNode.innerHTML = html;
		div.insertBefore(modNode,div.firstChild);
		setTimeout("P7_fixWierdCache('"+modNode.id+"','"+module+"');",100);		
	}
}
function P7_fixWierdCache(id,module) {
	if (!document.getElementById(id)) {
		p7_displayModule(module);
	}
}
function p7_loadMap () {
	if (GBrowserIsCompatible() && document.getElementById("google_map_coordinates") && document.getElementById("google_map_coordinates").value.split(",").length > 1) {
    	google_map = new GMap2(document.getElementById("google_map"));
    	var points = document.getElementById("google_map_coordinates").value.split(",");
        var point = new GLatLng(points[0],points[1])
        google_map.addControl(new GMapTypeControl());
        google_map.addControl(new GLargeMapControl());
        google_map.setCenter(point,13);
        //add poiont
		var baseIcon = new GIcon();
		baseIcon.image = "http://www.google.com/mapfiles/marker.png";
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
        google_map.addOverlay(new GMarker(point,{icon:baseIcon}));
    }
    else if (!document.getElementById("google_map_coordinates")) {
    	document.getElementById("google_map").innerHTML = '<table width="100%" height="100%" bgcolor="white"><tr valign="middle"><td align="center">No coordinates specified</td></tr></table>';
    }
    else {
    	document.getElementById("google_map").innerHTML = '<table width="100%" height="100%" bgcolor="white"><tr valign="middle"><td align="center">Unsupported Browser</td></tr></table>';
    }
}
function p7_isModule(module) {
	var mod = null;
	switch (module) {
		case "360":
		case "virtual tour":
			mod = document.getElementById("p7SSM_mod_vt");
			break;
		case "slideshow":
			mod = document.getElementById("p7SSM_mod_ss");
			break;
		case "google map":
			mod = document.getElementById("p7SSM_mod_gm");
			break;
		case "information":
			mod = document.getElementById("p7SSM_mod_if");
			break;
	}
	return mod;
}
function p7_removeModule(module) {
	var mod = p7_isModule(module);
	if (mod) mod.parentNode.removeChild(mod);
}
function p7_removeModules() {
	for (var x = 0; x < modules.length; x++)
		p7_removeModule(modules[x]);
}
function p7_addFullscreenButton() {
	//add fullscreen button
	fullScreenButton = document.createElement("input");
	fullScreenButton.type = "image";
	fullScreenButton.setAttribute("id","p7_fullscreen_button");
	fullScreenButton.src = "../images/fullscreen.gif";
	fullScreenButton.alt = "fullscreen";
	fullScreenButton.onclick = function () { p7_fullscreenModule(); return false; };
	with (fullScreenButton.style) {
		position = "absolute";
		width = "640";
		height = "32";
		top = "2px";
		right = "20px";
	}
	document.getElementById("p7ssm_tools").appendChild(fullScreenButton);
}
function p7_fullscreenModule() {
	var modNode = p7_isModule(activeModule);
	if (modNode) {
		var body = document.documentElement.getElementsByTagName("BODY")[0];
		var pI = document.getElementById("p7ssm_im");
		var heightA = parseInt((pI.height) ? pI.height : pI.style.height);
		var widthA = parseInt((pI.width) ? pI.width : pI.style.width);
		var div = document.createElement("div");
		var newHTML = modNode.innerHTML;
		modNode.innerHTML = "";
		//add close link
		with (div.style) {
			position = "absolute";
			top = "0px";
			width = "100%";
			height = "100%";
			backgroundColor = "white";
			zIndex = "99999999";
			overflow = "hidden";
		}
		div.id = "p7_fullscreen_tools";
		div.innerHTML = "<div style='width:100%;' align='center'><a onclick='p7_windowedModule()' href='#'><img src='http://www.esitetours.com/c-icon.jpg' border='0' /></a></div>";
		//no scrolling
		body.style.margin = "0px";
		document.getElementById("p7ssm").setAttribute("old_height",document.getElementById("p7ssm").style.height);
		document.getElementById("p7ssm").style.height = "10px";
		document.getElementById("p7ssm").style.overflow = "hidden";
		//show mod in div
		switch (activeModule) {
			case "virtual tour":
				var file = pI.src.substr(0,pI.src.lastIndexOf(".")).replace(/fullsize/g,"tour")+".swf";
				div.innerHTML += flashHTML.replace(/%FILE%/g,file).replace(/%WIDTH%/g,"100%").replace(/%HEIGHT%/g,"95%");			
				break;
		}
		//add mod to beginning of body
		body.appendChild(div);
	}
}
function p7_windowedModule() {
	var modNode = p7_isModule(activeModule);
	if (modNode) {
		var body = document.documentElement.getElementsByTagName("BODY")[0];
		var pI = document.getElementById("p7ssm_im");
		var heightA = parseInt((pI.height) ? pI.height : pI.style.height);
		var widthA = parseInt((pI.width) ? pI.width : pI.style.width);
		//add scrolling
		body.style.margin = "8px";
		body.style.marginTop = "0px";
		document.getElementById("p7ssm").style.height = document.getElementById("p7ssm").getAttribute("old_height");
		document.getElementById("p7ssm").style.overflow = "visible";
		//remove tools
		body.removeChild(document.getElementById("p7_fullscreen_tools"));
		//reclick thumbnail
		temp = p7sscur;
		p7sscur = -1;
		P7_SSMshowImage(temp+1);
	}
}
function p7_disableControls() {
	var ids = ["first","prev","pp","next","last","slidechannel","counter"];
	
	for (var x=0; x < ids.length; x++) {
		document.getElementById("p7ssm_"+ids[x]).style.visibility = "hidden";
	}
}
function p7_enableControls() {
	var ids = ["first","prev","pp","next","last","slidechannel","counter"];
	
	for (var x=0; x < ids.length; x++) {
		document.getElementById("p7ssm_"+ids[x]).style.visibility = "visible";
	}
}
