/* POI slider */

var POIWIDTH = 720;
var STEPSIZE = 20;
var STEPSPEED = 10;
var AUTOMOVEINTERVAL = 8000;
var poiCount = 0;
var poiPos = 0;
var moving = false;
var inPoi = -1;
var outPoi = -1;
var step = 0;
var curPoi = 0;
var autoMove = setInterval ('_nextPOI()', AUTOMOVEINTERVAL);

function prevPOI()
{
	if (moving)
	{
		return;
	}

	moving = true;
	clearInterval(autoMove);
	
	inPoi = (curPoi <= 0) ? poiCount - 1 : curPoi - 1;
	outPoi = curPoi;
	
	startPos = -POIWIDTH;
	poiPos = startPos;
	endPos = 0;
	step = STEPSIZE;

	document.getElementById("poi_" + inPoi).style.left=startPos+"px";
	
	movePOI();
}

function nextPOI()
{
	if (moving)
	{
		return;
	}
	
	clearInterval(autoMove);
	_nextPOI();
}

function _nextPOI()
{
	moving = true;
	
	inPoi = (curPoi >= poiCount-1) ? 0 : curPoi + 1;
	outPoi = curPoi;
	
	startPos = POIWIDTH;
	poiPos = startPos;
	endPos = 0;
	step = -STEPSIZE;

	document.getElementById("poi_" + inPoi).style.left=startPos+"px";
	
	movePOI();
}

function movePOI()
{
	if (poiPos == endPos)
	{
		moving = false;
		curPoi = inPoi;
					
		return;
	}

	poiPos = poiPos + step;		
	
	var prog = (poiPos-startPos)/(endPos-startPos);
	prog = (-Math.cos(prog*Math.PI)/2) + 0.5;
	var realPos = parseInt(startPos + (endPos-startPos)*prog);
	
	document.getElementById("poi_" + inPoi).style.left=realPos+"px";
	document.getElementById("poi_" + outPoi).style.left=(realPos-startPos)+"px";
	
    setTimeout ('movePOI()', STEPSPEED);
}

/* search box */
var searchBox;
function init ()
{
	searchBox = document.getElementById("search_box").getElementsByTagName("input")[0];
	searchBox.onkeypress = sbKeyPress;
	searchBox.onfocus = sbFocus;
	searchBox.onblur = sbBlur;
	var searchLnk = document.getElementById("search_box").getElementsByTagName("a")[0];
	searchLnk.onclick = doFind;
	
	poiCount = 0;
	while (document.getElementById("poi_" + poiCount))
	{
		poiCount++;
	}
}

window.onload = init;

function sbFocus () {
	searchBox.style.color = "black";
	if (searchBox.value == "where")
	{
		searchBox.value = "";
	}
	else
	{
		searchBox.select();
	}
}

function sbBlur () {
	searchBox.style.color = "gray";
	var srch = searchBox.value.replace (/^\s+/, '').replace (/\s+$/, '');
	if (srch == "")
	{
		srch = "where";
	}
	searchBox.value = srch;
}


function sbKeyPress (ev) {
  if (!ev)
	  ev = window.event;
  if (ev.which) {
    code = ev.which;
  } else if (ev.keyCode) {
	  code = ev.keyCode;
  }
  if (code == 13)
  {
	  doFind();
  }
}

function doFind() {
	var srch = searchBox.value.replace (/^\s+/, '').replace (/\s+$/, '');
	location.href = "/landing#?q=&where=" + srch;
}
