
var isIE=(navigator.userAgent.toLowerCase().indexOf("msie")>=0);

var whereopen=0,wherestate=0,wheretime=0,wheretimer=null,wheredoclose=false,whereforceclose=false,wherehasfocus=false,wheredur=300;

function whereover()
{
	if(whereforceclose) return;
	if(wheredoclose) { wheredoclose=false; return; }
	if(wherestate==1||whereopen==1) return;
	wheretime=new Date().getTime()-whereopen*wheredur;
	wherestate=1;
	if(!wheretimer) wheretimer=setInterval(whereslide,15);
	document.getElementById("wherearr").src="/sys/images/ls/uarr6.gif";
}

function whereout()
{
	if(whereopen==0||wherehasfocus) return;
	wheredoclose=true;
	setTimeout(whereclose,100);
}

function whereclose()
{
	if(!wheredoclose||wherestate==2||whereopen==0) return;
	wheredoclose=false;
	wheretime=new Date().getTime()-(1-whereopen)*wheredur;
	wherestate=2;
	if(!wheretimer) wheretimer=setInterval(whereslide,15);	
}

function wherearrc()
{
	if(document.selection) document.selection.empty();
	else window.getSelection().removeAllRanges();
	whereforceclose=true;
	wheredoclose=true;
	whereclose();
}

function whereslide()
{
	if(!wherestate) {
		clearInterval(wheretimer);
		wheretimer=null;
		return;
	}
	if(wherestate==1) {
		whereopen=(new Date().getTime()-wheretime)/wheredur;
		if(whereopen>=1) {
			whereopen=1;
			wherestate=0;
			document.getElementById("wherearr").src="/sys/images/ls/uarr6.gif";
		}
	} else {
		whereopen=1-(new Date().getTime()-wheretime)/wheredur;
		if(whereopen<=0) {
			whereopen=0;
			wherestate=0;
			whereforceclose=false;
			wherehasfocus=false;
			document.getElementById("wherearr").src="/sys/images/ls/darr6.gif";
		}
	}
	var y=(whereopen*whereopen*2)*50;
	if(whereopen>0.5) y=50-((1-whereopen)*(1-whereopen)*2)*50;
	var el=document.getElementById("wherefade");
	var h=y; if(h>45) h=45;
	el.style.height=h+"px";
    el.style.display = h>0?"block":"none";
    document.getElementById("wherefimg").style.top=(y-13)+"px";
	document.getElementById("wherebot").style.top=Math.floor(-70+y+0.5)+"px";
	document.getElementById("wherebox").style.height=Math.floor(92+y+0.5)+"px";
}

function wherefocus() {
	wherehasfocus=true;
	setTimeout("document.vform.where.select()",10);
}

function whereblur(e)
{
	if(!e) e=window.event;
	var el=document.getElementById("wherebox");
	if(!el) return;
	var p=getPos(el);
	if(e.clientX<p.x||e.clientX>p.x+el.offsetWidth||e.clientY<p.y||e.clientY>p.y+el.offsetHeight) {
		wherehasfocus=false;
		whereout();
	}
}

function getWindowHeight()
{
	return window.innerHeight?window.innerHeight:(document.documentElement&&document.documentElement.clientHeight?document.documentElement.clientHeight:document.body.clientHeight);
}

function getWindowWidth()
{
	return window.innerWidth?window.innerWidth:(document.documentElement&&document.documentElement.clientWidth?document.documentElement.clientWidth:document.body.clientWidth);
}

function resized()
{
}

function getPos(el)
{
	var px=0,py=0;
	while(el) {
		px+=el.offsetLeft;
		py+=el.offsetTop;
		el=el.offsetParent;
	}
	return {x:px,y:py};
}

function processWhere(where)
{
	stateNames={"alabama":"AL","alaska":"AK","arizona":"AZ","arkansas":"AR","california":"CA","colorado":"CO","connecticut":"CT","delaware":"DE","florida":"FL","georgia":"GA","hawaii":"HI","idaho":"ID","illinois":"IL","indiana":"IN","iowa":"IA","kansas":"KS","kentucky":"KY","louisiana":"LA","maine":"ME","maryland":"MD","massachusetts":"MA","michigan":"MI","minnesota":"MN","mississippi":"MS","missouri":"MO","montana":"MT","nebraska":"NE","nevada":"NV","ohio":"OH","oklahoma":"OK","oregon":"OR","pennsylvania":"PA","tennessee":"TN","texas":"TX","utah":"UT","vermont":"VT","virginia":"VA","washington":"WA","wisconsin":"WI","wyoming":"WY","guam":"GU","palau":"PW","al":"AL","ak":"AK","az":"AZ","ar":"AR","ca":"CA","co":"CO","ct":"CT","de":"DE","fl":"FL","ga":"GA","hi":"HI","id":"ID","il":"IL","in":"IN","ia":"IA","ks":"KS","ky":"KY","la":"LA","me":"ME","md":"MD","ma":"MA","mi":"MI","mn":"MN","ms":"MS","mo":"MO","mt":"MT","ne":"NE","nv":"NV","nh":"NH","nj":"NJ","nm":"NM","ny":"NY","nc":"NC","nd":"ND","oh":"OH","ok":"OK","or":"OR","pa":"PA","ri":"RI","sc":"SC","sd":"SD","tn":"TN","tx":"TX","ut":"UT","vt":"VT","va":"VA","wa":"WA","wv":"WV","wi":"WI","wy":"WY","as":"AS","dc":"DC","gu":"GU","mh":"MH","pw":"PW","pr":"PR"};
	stateNames2={"new-hampshire":"NH","new-jersey":"NJ","new-mexico":"NM","new-york":"NY","north-carolina":"NC","north-dakota":"ND","rhode-island":"RI","south-carolina":"SC","south-dakota":"SD","west-virginia":"WV","american-samoa":"AS","puerto-rico":"PR"};
	if(where.length<4) return false;
	var w=where.split(",");
	var wu=w[0].replace(/[\\\/\. ]/g,"-").replace(/[^a-zA-Z0-9-]/g,"").replace(/-+/g,"-").replace(/^-/g,"").replace(/-$/g,"");
	if(w.length>1) {
		var sn=w[1].replace(/[\\\/\. ]/g,"-").replace(/[^a-zA-Z0-9-]/g,"").replace(/-+/g,"-").replace(/^-/g,"").replace(/-$/g,"");
		if(stateNames[sn.toLowerCase()]) sn=stateNames[sn.toLowerCase()];
		else if(stateNames2[sn.toLowerCase()]) sn=stateNames2[sn.toLowerCase()];
		wu+=","+sn;
	} else {
		if((stateNames[wu.toLowerCase()] || stateNames2[wu.toLowerCase()]) && wu.toLowerCase()!="new-york") return false;
		var i=wu.lastIndexOf("-");
		if(i>0) {
			var sn=wu.substr(i+1).toLowerCase();
			if(stateNames[sn]) wu=wu.substr(0,i)+","+stateNames[sn];
			else {
				i=wu.substr(0,i).lastIndexOf("-");
				if(i>0) {
					var sn=wu.substr(i+1).toLowerCase();
					if(stateNames2[sn]) wu=wu.substr(0,i)+","+stateNames2[sn];
	}	}	}	}
	return wu;
}

function processWhat(what)
{
	return what.replace(/[\'\"]/g,"").replace(/[\\\/]/g,"-").replace(/[\.,]/g," ");
}

var vrtimer=null,pwhere=null;
function dosubmit()
{
	var pw=processWhat(document.vform.what.value);
	if(pw.length) pw="/"+encodeURIComponent(pw);
	if(pwhere) location.href="/"+pwhere+pw;
}
function whereinvalid(alt)
{
	if(pwhere) {
		if(alt && alt.length>1) {
			if(confirm("\""+document.vform.where.value+"\" was not recognized.  Did you mean "+alt+"?")) {
				document.vform.where.value=alt;
				pwhere=processWhere(alt);
				resetUserLoc(); dosubmit();
			}
			return;
		}
		alert("\""+document.vform.where.value+"\" was not recognized.  Please enter a city or zip code.");
	}
	else alert("Please enter a city or zip code.");
}

function JSFeed(r)
{
	if(vrtimer) { clearTimeout(vrtimer); vrtimer=null; }
	if(r&&r.notfound) { whereinvalid(r.dym||0); pwhere=null; }
	else { resetUserLoc(); dosubmit(); }
}

function dogo(nochange)
{
	if(vrtimer) return;
	pwhere=processWhere(document.vform.where.value);
	if(document.vform.where.value!=vwhere && !nochange) {
		if(!pwhere) { whereinvalid(); return; }
		vrtimer=setTimeout("vrtimer=null;resetUserLoc();dosubmit();",1000);
		JSONrequest("http://www.localscout.com/sys/pageserver.dll?s=-21&pa="+encodeURIComponent(pwhere)+"&p="+Math.random());
	}
	else dosubmit(nochange);
	return false;
}

function resetUserLoc()
{
	setCookie("useraddr","");
	setCookie("userlat","");
	setCookie("userlon","");	
	return true;
}

function initheader()
{
	vrtimer=null;pwhere=null;
	resized();
	document.vform.what.value=vwhat;
	document.vform.where.value=vwhere;
	new AutoSuggestControl(document.getElementById("what"), new SuggProvider(), "what");
	new AutoSuggestControl(document.getElementById("where"), new LocProvider(), "where");
}

function init()
{
  setTimeout(function() {
  	initheader();
	if(typeof initpage!="undefined") setTimeout(initpage,1);
  },1);
}

