/**************************************************************************************************/
/***
/***	ROCKVILLE ROSE INN JAVASCRIPT DOCUMENT
/***	-----------------------------------------------------------------------
/***	Written by Matthew Praetzel. Copyright (c) 2007 Matthew Praetzel.
/***	-----------------------------------------------------------------------
/***	All Rights Reserved. Any use of these functions & scripts without written consent is prohibited.
/***
/**************************************************************************************************/

/*-----------------------
	INITIALIZE
-----------------------*/
ternVENTS.addEvent(window,init,"load",false);
var g,m,map,address = "125 E Main St, Rockville, UT 84763",timer;
/*-----------------------
	ROCK
-----------------------*/
function init() {
	ternVENTS.addEvent(["rooms","inn","zion"],gallery,"click",false);
	ternVENTS.addEvent("gvcl",closeGallery,"click",false);
	ternVENTS.addEvent(["im","il"],showImage,"click",false);
	if(document.getElementById("map")) {
		gmap();
		ternVENTS.addEvent("gdr",dir,"click",false);
		ternVENTS.addEvent("cd",closeDir,"click",false);
	}
	if(document.getElementById("nfrm")) {
		ternFORM("nfrm").validateForm("req",window);
	}
}
function gallery(e) {
	g = ternVENTS.eventSource(e).id.toUpperCase();
	ternStyle(document.body).elapsor("","#000000",88,function () {
		var dr = "/GALLERIES/?"+g;
		ternJAX("prc").load(dr,document.getElementById("v").getElementsByTagName("div")[0],function () {
			var w = ternStyle().getWindowSize(),d = ternStyle("gv").getData(),is = document.getElementById("v").getElementsByTagName("img");
			ternStyle("gv").setStyle("top",((w[1]-d[1])/2)).setStyle("left",((w[0]-d[0])/2)).fadeIn("medium");
			m = ternMenus("pv").show(is,"mouseover","mouseout",showThumb,false,"show","center center",false);
		});
	});
}
function closeGallery() {
	ternStyle("gv").fadeOut("medium",function () {
		ternMenus("pv").removeShow();
		ternStyle(document.getElementById("v").getElementsByTagName("div")[0]).removeChildren();
		ternStyle(document.body).hideElapsor();
		g = false;
	});
}
function showThumb(s) {
	var p = s.src,p = p.substr(p.lastIndexOf("/")+1),d = ternStyle(s).getData();
	var pv = document.getElementById("pv");
	var v = pv.getElementsByTagName("div")[0];
	ternStyle(v).removeChildren();
	var i = document.createElement("img");
	i.setAttribute("src","http://www.rockvillerose.com/GALLERIES/"+g+"/200x200/"+p);
	v.appendChild(i);
}
function showImage(e) {
	var s = ternVENTS.eventSource(e),sz = s.innerHTML,i = s.parentNode.parentNode.getElementsByTagName("img")[0].src;
	i = i.substr(i.lastIndexOf("/")+1);
	openWindow("http://www.rockvillerose.com/GALLERIES/PIC/?g="+g+"&s="+sz+"&i="+i,"THUMB",800,800);
}
function openWindow(u,n,w,h,x,y) {
	var f = 'width='+w+',height='+h+',scrollbars=yes,resizable=yes';
	if(wn = window.open(u,n,f)) {
		wn.moveTo((screen.availWidth-w)/2,0);
		return wn;
	}
	else {
		alert('There has been an error opening this window. Is your browser blocking pop-ups?');
		return false;
	}
}
function gmap() {
	if(GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address,function(point) {
			if (!point) {
				alert(address + " not found");
			}
			else {
				map.setCenter(point,13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				marker.openInfoWindowHtml("<b>Rockville Rose Inn</b><br />" + address);
			}
		});
	}
}
function dir() {
	var d = ternStyle("dcn").getDimensions();
	if(d[1] > 0) {
		ternStyle("dcn").grow("medium","y",0).removeClass("dcnv");
	}
	else {
		ternStyle("dcn").addClass("dcnv").grow("medium","y",180);
	}
}
function directions() {
	var add1 = document.getElementById("add1").value;
	var add2 = document.getElementById("add2").value;
	var c = document.getElementById("city").value;
	var s = document.getElementById("state").value;
	var z = document.getElementById("zip").value;
	var dp = document.getElementById("directions");
	var dir = new GDirections(map,dp);
	GEvent.addListener(dir,"error",function () { 
		clearInterval(timer);
		ternStyle(document.body).hideElapsor();
		alert("Your address does not seem to be valid.");
	});
	ternStyle("dcn").grow("medium","y",0).removeClass("dcnv");
	ternStyle(document.body).elapsor("http://www.rockvilleroseinn.com/","#000000",65,function () {
		timer = setInterval(setDir,300);
		dir.load(add1+" "+add2+", "+c+", "+s+", "+z+" to "+address);
	});
}
function setDir() {
	var dp = document.getElementById("directions");
	if(dp.childNodes && dp.childNodes.length > 0) {
		clearInterval(timer);
		var h = document.getElementById("directions").getElementsByTagName("table");
		h = h[h.length-2].getElementsByTagName("td")[1];
		h.innerHTML = h.innerHTML.replace("Hurricane","Rockville").replace("84737","84763");
		//
		ternStyle("gdcn").setStyle("visibility","visible");
		ternStyle("map").setStyle("z-index",60);
		ternStyle(document.body).hideElapsor(false);
	}
}
function closeDir() {
	ternStyle("gdcn").setStyle("visibility","hidden");
	ternStyle("directions").removeChildren();
	ternStyle(document.body).hideElapsor();
	ternStyle("map").setStyle("z-index",0);
}
