window.addEvent('domready', function(){
	var slides = new Array();
	var menupos = -1;
	var selected = $('itemsel');
	var current = (selected) ? selected.getParent() : false;
	$$('.item').each(function(div, k){
		var divtit = div.getElement('div.sele');
		var divdet = div.getElement('div.cont');
		var divsub = div.getElements('div.hide');
		
		
		var fx = new Fx.Slide(divdet);
		fx.hide();
		
		
		slides[k] = fx;
		if (divtit != current) {
			fx.hide();
		} else {
			fx.show();
			menupos = k;	
		}

		
		divtit.addEvent('click', function(){
			//this.setStyle('display', '');
					divdet.setStyle('display', '');
			fx.toggle();
			if (divsub.length == 0) {
				if (menupos != -1) { 
					slides[menupos].slideOut();
				}
				menupos = k;
			}
		});
	});
	
	//$('fade').addEvent('click', function(e) { hideBox() });
	//document.addEvent('keydown', function(e){ if (e.key == 'esc' && openbox) { hideBox(); }});
	//window.addEvent('resize', function(e) { setBox() });

	if ($('signupspa_btn')) {
		$('signupspa_btn').addEvent('click', function() {
					var url_query = new Hash({'action' : 'subscribe_for_specials', 'email_address' : $('signupspa_email_address').get('value'), 'store_id' : $('signupspa_store_id').get('value')});
					var myRequest = new Request.HTML(
					{
						method: 'get', url: 'actions.php?' + url_query.toQueryString()
					}
				).send();
		});

	}
	/*
	if (Browser.Engine.trident) {
		//document.write('<style type="text/css">@import url(css/ie.css);</style>');
	}
	*/
});

var pathurl = "";
var openbox = false;
function goSection(idstore) {

	if (pathurl.contains("?")) {
		window.location = pathurl + "&action=setstore&idstore=" + idstore;
	} else {
		window.location = pathurl + "?action=setstore&idstore=" + idstore;	
	}
}
function goSearch() {
	$("rpta").set("html", "<p class=\"result\">Searching...</p>");
	var path = "/inc.stores.php?query=" + $("cityup").value;
	var json = new Request.JSON({url: path, onComplete: function(obj) {
		var results = obj.data;
		if (obj.data.length > 0) {
			var tmpHTML = "<p class=\"result\">Spas near "+obj.city+", "+obj.state+":</p>";
			for (i=0; i<obj.data.length; i++) {
				var tmp = obj.data[i];
				if(tmp.is_coming_soon != 1) {
					tmpHTML+= "<p class=\"spa\"><a href=\"#\" onclick=\"goSection('"+tmp.idstore+"')\">"+tmp.name+"</a><br />"+tmp.address+"</p>";
				} else {
					tmpHTML+= "<p class=\"spa\">"+tmp.name+" (Coming Soon)<br />"+tmp.address+"</p>";
				}
			}
		} else {
			var tmpHTML = "<p class=\"result\"><em>No location found.</em></p><p class=\"result\">Try search in <a href=\"locations.php\" class=\"link\">Locations page</a></p>";
		}
		$("rpta").set("html", tmpHTML);
	}});
	json.get();
}
function searchStore() {
	$("msgsearch").set("html", "Searching...");
	var path = "/inc.stores.php?query=" + $("city").value;
	var json = new Request.JSON({url: path, onComplete: function(obj) {
		var results = obj.data;
		if (obj.data.length > 0) {
			var tmpHTML = "Spas near "+obj.city+", "+obj.state+":";
			$("msgsearch").set("html", tmpHTML);
			tmpHTML = "<table width=\"214\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
			for (i=0; i<obj.data.length; i++) {
				var tmp = obj.data[i];
				tmpHTML+= "<tr><td width=\"28\" height=\"36\">&nbsp;</td>";
				tmpHTML+= "<td width=\"150\" valign=\"top\">";
				if(tmp.is_coming_soon != 1) {
					tmpHTML+= "<a href=\""+tmp.url+"\" class=\"link\">"+tmp.name+"</a><br />"+tmp.address+"</td>";
				} else {
					tmpHTML+= tmp.name+" (Coming Soon)<br />"+tmp.address+"</td>";
				}
				tmpHTML+= "<td align=\"right\" valign=\"top\"><a href=\"javascript:showMap('"+tmp.lat+"','"+tmp.lon+"','"+tmp.name+"','"+tmp.address+"', '" + tmp.gmap_query + "');\">&gt; Map</a></td>";
				tmpHTML+= "</tr>";
			}
			tmpHTML+= "</table>";
		} else {
			var tmpHTML = "<p class=\"result\">Check out our <a href=\"locations.php\" class=\"link\">Locations page</a> to find a spa near you.</p>";
			$("msgsearch").set("html", "<em>No location found.</em>");
		}
		
		$("divsearch").set("html", tmpHTML);
	}});
	json.get();
}
function showFinder(url) {
	pathurl = url;
	showBox(392, 400);
}

function setTextPull(e) {
	$('txt'+e.name).set('text', e.options[e.selectedIndex].text);
}
function getValuePull(e) {
	return e.options[e.selectedIndex].text
}
/* Lightbox */
function showMap(lat, lon, name, address, gmap_query) {
	w = 520;
	h = 400;
	openbox = true;
	wsize = $(document.body).getSize();
	wleft = (wsize.x - w)/2;
	wtop = (wsize.y - h)/2;

	var window_size = window.getScrollSize();
		
	$('lightMap').setStyles({display: 'block', width: w, height: h, left: wleft, top: wtop });
	$('fadeMap').setStyles({display:'block', width:window_size.x, height:window_size.y});
	
	if (GBrowserIsCompatible()) {
		var map = new GMap2($("map_canvas"));

		var geocoder = new GClientGeocoder();

		  geocoder.getLatLng(
		    gmap_query,
		    function(point) {
		      if (!point) {
		        alert("We're Sorry, but we could not map this store's address.");
		      } else {
		        map.setCenter(point, 13);
		        var marker = new GMarker(point);
						map.openInfoWindowHtml(map.getCenter(), "<strong>"+name+"</strong><br>"+address);
				    map.addControl(new GSmallMapControl());
				    map.addControl(new GMapTypeControl());
		        //var marker = new GMarker(point);
		        //map.addOverlay(marker);
		        //marker.openInfoWindowHtml(address);
		      }
		    }
		  );

/*
		function showAddress(address) {
		  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(address);
		      }
		    }
		  );
		 }
*/

		//map.setCenter(new GLatLng(lat, lon), 13);
		/*
		map.openInfoWindowHtml(map.getCenter(), "<strong>"+name+"</strong><br>"+address);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    */

	}
}
function hideMap() {
	openbox = false;
	$('lightMap').setStyle('display', 'none');
	$('fadeMap').setStyle('display', 'none');
}
function showBox(w, h) {
	openbox = true;
	wsize = $(document.body).getSize();
	var window_size = window.getScrollSize();
	wleft = (wsize.x - w)/2;
	wtop = (wsize.y - h)/2;
	if (Browser.Engine.trident4) {
		$$('.xx_spacer').each(function(v) {
			v.setProperty('src', 'img/x.gif');
			v.setStyle('background-color', '#F3F1EC');
			
		});
	}
	$('light').setStyles({display: 'block', width: w, height: h, left: wleft, top: wtop });
	$('fade').setStyles({'display': 'block', width:window_size.x, height:window_size.y});	
}
function hideBox() {
	openbox = false;
	$('light').setStyle('display', 'none');
	$('fade').setStyle('display', 'none');
}
function setBox() {
	if (openbox) {
		wsize = $(document.body).getSize();
		bsize = $('light').getSize();
		wleft = (wsize.x - bsize.x)/2;
		wtop = (wsize.y - bsize.y)/2;
		$('light').setStyles({left: wleft, top: wtop });
	}
}
/* More Info */
function showMore() {
	w = 439;
	h = 400;
	openbox = true;
	wsize = $(document.body).getSize();
	wleft = (wsize.x - w)/2;
	wtop = (wsize.y - h)/2;
	$('light').setStyles({display: 'block', width: w, height: h, left: wleft, top: wtop });
	$('fade').setStyle('display', 'block');	
}