function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();


var aa = null;
var stateSlider = null;
var http_request;
var sliderPos = "up";
var scon = null;
var sHeight;
var errMsg;
var loadin;
var loaded = false;
var stateSlider2 = null;
var stateCon = null;

function stateOver(el) {
	el.className = "stateStarOver";
	el.style.left = (parseInt(el.style.left) - 10) + "px";
	el.style.top = (parseInt(el.style.top) - 11) + "px";
}
function stateOut(el) {
	el.className = "stateStar";
	el.style.left = (parseInt(el.style.left) + 10) + "px";
	el.style.top = (parseInt(el.style.top) + 11) + "px";
}

//ANIMATION FUNCTIONS
///////////////////////////////////////////////////////

// Locations Menu
function slideStateDown() {
	if (aa != null) {
		aa.stop();
		aa = null;
	}
	
	sHeight = stateSlider.offsetHeight;
//	errMsg = "";
	stateSlider.style.top = (-1 * sHeight) + "px";
	stateSlider.style.visibility = "visible";

	aa = new Accelimation(stateSlider.style, "top", 20, 600, .8, "px");
	aa.onend = onStateDownEnd;
	aa.onframe = slideState;
	aa.start();
}

function slideStateUp() {
	if (aa != null) {
		aa.stop();
		aa = null;
	}
	
	var dest = -1 * sHeight;

	aa = new Accelimation(stateSlider.style, "top", dest, 500, .8, "px");
	aa.onend = onStateUpEnd;
	aa.onframe = slideState;
	aa.start();
}

function onStateDownEnd() {
	aa.stop();
	aa = null;
	sliderPos = "down";
//	alert(errMsg);
}

function onStateUpEnd() {
	aa.stop();
	aa = null;
	sliderPos = "up";
	stateSlider.style.visibility = "hidden";
	if(loaded) {
		loaded = false;
		stateSlider.innerHTML = http_request.responseText;
		loadin.style.display = "none";
		slideStateDown();
	}
}

function slideState(y) {
//	errMsg += (sHeight + y) + "\n";
	stateSlider.style.top = y + "px";
	scon.style.height = (sHeight + y + 100) + "px";
}


function slideStateLeft() {
	if (aa != null) {
		aa.stop();
		aa = null;
	}
	
	aa = new Accelimation(stateCon.style, "width", 456, 600, .8, "px");
	aa.onend = onStateOutEnd;
	aa.onframe = slideState2;
	aa.start();
	
	document.getElementById("searchbutt").disabled = true;
}

function clearStates() {
	if (aa != null) {
		aa.stop();
		aa = null;
	}
	
	aa = new Accelimation(stateCon.style, "width", 0, 500, .8, "px");
	aa.onend = onStateInEnd;
	aa.onframe = slideState2;
	aa.start();
}

function onStateOutEnd() {
	aa.stop();
	aa = null;
	sliderPos = "out";
}

function onStateInEnd() {
	aa.stop();
	aa = null;
	sliderPos = "in";
	document.getElementById("searchbutt").disabled = false;
}

function slideState2(x) {
	stateCon.style.width = x + "px";
}

function initLocations(sZip) {
	stateSlider = document.getElementById("stateSlider");
	scon = document.getElementById("slideContainer");

	stateSlider2 = document.getElementById("stateSlider2");
	stateCon = document.getElementById("slideContainer2");

	loadin = document.getElementById("loadin");
	xmlRequest();

	if(sZip != null) {
		document.getElementById("zipbox").value = sZip;
		goSearch();
	}
}



//XMLHTTP Functions
/////////////////////////////////////////////////////
function xmlRequest() {
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	} else {
		return true;	
	}
}

function goState(st) {
//	slideStateLeft();
//	return;
//	alert("State: " + st);
	if(sliderPos == "out") {
		clearStates();	
	}
	if(xmlRequest()) {
		http_request.onreadystatechange = stateResponse;
		http_request.open('POST', "locationFetcher.php", true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		var pInfo = "action=state&state=" + st;
		
		http_request.send(pInfo);
		loadin.style.display = "inline";
	}
}

function goSearch() {
//	alert("State: " + st);
	if(sliderPos == "out") {
		clearStates();	
	}
	if(xmlRequest()) {
		http_request.onreadystatechange = stateResponse;
		http_request.open('POST', "locationFetcher.php", true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		var pInfo = "action=search&zip=" + document.getElementById("zipbox").value;
//		alert(document.getElementById("zip").text);
		
		http_request.send(pInfo);
		loadin.style.display = "inline";
	}
}

function stateResponse() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			if(aa != null) {
				loaded = true;
			} else {
				document.getElementById("thegoods").innerHTML = http_request.responseText;
				slideStateLeft();
				loadin.style.display = "none";
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}

function loadMap(address) {
	if(address != "nomap") {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map"));
			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(address);
				}
			});
		}
	}
}

function checkDisplay() {
	if(sliderPos == "out") {
		clearStates();	
	}
}

//shadow stuff
function drawSmallShadow(w,h,x,y,z) {
	if (browser.isIE) {
		return smallShadowIE(w,h,x,y,z);
	} else {
		return smallShadow(w,h,x,y,z);
	}	
}

function smallShadow(w,h,x,y,z) {
	$output = "<div style=\"position:absolute;width:" + w + "px;height:" + h + "px;left:" + x + "px;top:" + y + "px;z-index:" + z + "\">";
	$output += "<table cellpadding=\"0\" cellspacing=\"0\" width=\"" + w + "\" height=\"" + h + "\">";
	$output += "<tr><td style=\"background-image:url(/images/shadow/small_1.png);width:8px;height:8px;\"></td>";
	$output += "<td style=\"background-image:url(/images/shadow/small_2.png);width:" + (w - 16) + "px;height:8px;\"></td>";
	$output += "<td style=\"background-image:url(/images/shadow/small_3.png);width:8px;height:8px;\"></td></tr>";
	$output += "<tr><td style=\"background-image:url(/images/shadow/small_4.png);width:8px;height:" + (h - 16) + "px;\"></td>";
	$output += "<td style=\"background-image:url(/images/shadow/small_5.png);width:" + (w - 16) + "px;height:" + (h - 16) + "px\"></td>";
	$output += "<td style=\"background-image:url(/images/shadow/small_6.png);width:8px;height:" + (h - 16) + "px\"></td></tr>";
	$output += "<tr><td style=\"background-image:url(/images/shadow/small_7.png);width:8px;height:8px;\"></td>";
	$output += "<td style=\"background-image:url(/images/shadow/small_8.png);width:" + (w - 16) + ";height:8px;\"></td>";
	$output += "<td style=\"background-image:url(/images/shadow/small_9.png);width:8px;height:8px;\"></td></tr></table></div>";
	return $output;
}

function smallShadowIE(w,h,x,y,z) {
	$output = "<div style=\"position:absolute;width:" + w + "px;height:" + h + "px;left:" + x + "px;top:" + y + "px;z-index:" + z + "\">";
	$output += "<table cellpadding=\"0\" cellspacing=\"0\" width=\"" + w + "\" height=\"" + h + "\">";
	$output += "<tr><td style=\"background-image:url(/images/spacer.gif);filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/shadow/small_1.png',sizingMethod='scale');width:8px;height:8px;\"></td>";
	$output += "<td style=\"background-image:url(/images/spacer.gif);filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/shadow/small_2.png',sizingMethod='scale');width:" + (w - 16) + "px;height:8px;\"></td>";
	$output += "<td style=\"background-image:url(/images/spacer.gif);filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/shadow/small_3.png',sizingMethod='scale');width:8px;height:8px;\"></td></tr>";
	$output += "<tr><td style=\"background-image:url(/images/spacer.gif);filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/shadow/small_4.png',sizingMethod='scale');width:8px;height:" + (h - 16) + "px;\"></td>";
	$output += "<td style=\"background-image:url(/images/spacer.gif);filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/shadow/small_5.png',sizingMethod='scale');width:" + (w - 16) + "px;height:" + (h - 16) + "px\"></td>";
	$output += "<td style=\"background-image:url(/images/spacer.gif);filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/shadow/small_6.png',sizingMethod='scale');width:8px;height:" + (h - 16) + "px\"></td></tr>";
	$output += "<tr><td style=\"background-image:url(/images/spacer.gif);filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/shadow/small_7.png',sizingMethod='scale');width:8px;height:8px;\"></td>";
	$output += "<td style=\"background-image:url(/images/spacer.gif);filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/shadow/small_8.png',sizingMethod='scale');width:" + (w - 16) + ";height:8px;\"></td>";
	$output += "<td style=\"background-image:url(/images/spacer.gif);filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/shadow/small_9.png',sizingMethod='scale');width:8px;height:8px;\"></td></tr></table></div>";
	return $output;
}

