var CP = Array();

function checkCidade(){
	if(getE('keywords').value=='Onde?') {
		getE('keywords').value = '';
		getE('keywords').style.color = '#FF8000';
	}
}

function getE(obj){
	return document.getElementById(obj);
}

function changeSize(){
	var divCon = getE('DivContentCidades');
	var pos = findPositionWithScrolling(DivCidades);
	var nav = navDetect();
	var bodyheight = document.body.clientHeight;
	var tableheight = 0;
	if(bodyheight>550) tableheight = 550;
	else tableheight = bodyheight;
	
	getE('TbBody').style.height = tableheight+'px';
	
	if(/Firefox/.test(nav[0])) {
		var dif = 0;
	}else{
		var dif = bodyheight-tableheight;
	}
	
	divCon.style.bottom = (dif+60) + 'px';
	divCon.style.left = pos[0] + 'px';
}

function ini(){
	if(document.addEventListener) {
		window.addEventListener('resize', function(oEvent) {
			changeSize();
		}, false);
	}else if(document.attachEvent) {
		window.attachEvent("onresize", function(){
			changeSize();
		});	
	}
	
	changeSize();
	
	CP[0] = new Spry.Widget.CollapsiblePanel("CollapsiblePanel1", { contentIsOpen: false });
	CP[1] = new Spry.Widget.CollapsiblePanel("CollapsiblePanel2", { contentIsOpen: false });
	CP[2] = new Spry.Widget.CollapsiblePanel("CollapsiblePanel3", { contentIsOpen: false });
	CP[3] = new Spry.Widget.CollapsiblePanel("CollapsiblePanel4", { contentIsOpen: false });
	CP[4] = new Spry.Widget.CollapsiblePanel("CollapsiblePanel5", { contentIsOpen: false });
	CP[5] = new Spry.Widget.CollapsiblePanel("CollapsiblePanel6");
	CP[6] = new Spry.Widget.CollapsiblePanel("DivCidades", { contentIsOpen: false });
	
	var options_xml = {
		script: function (input) { return "/xajax/autocidades.php?input="+document.getElementById('keywords').value; },
		varname:"input"
	};
	var as_xml = new bsn.AutoSuggest('keywords', options_xml);
	
	document.getElementById('keywords').focus();
	
	var layoutCookie = readCookie('borderContainer_left');
	if(!layoutCookie) createCookie('borderContainer_left',450,10);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function findPositionWithScrolling( oElement ) {
  function getNextAncestor( oElement ) {
    var actualStyle;
    if( window.getComputedStyle ) {
      actualStyle = getComputedStyle(oElement,null).position;
    } else if( oElement.currentStyle ) {
      actualStyle = oElement.currentStyle.position;
    } else {
      //fallback for browsers with low support - only reliable for inline styles
      actualStyle = oElement.style.position;
    }
    if( actualStyle == 'absolute' || actualStyle == 'fixed' ) {
      //the offsetParent of a fixed position element is null so it will stop
      return oElement.offsetParent;
    }
    return oElement.parentNode;
  }
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    var originalElement = oElement;
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    if( !originalElement.parentNode || !originalElement.style || typeof( originalElement.scrollTop ) == 'undefined' ) {
      //older browsers cannot check element scrolling
      return [ posX, posY ];
    }
    oElement = getNextAncestor(originalElement);
    while( oElement && oElement != document.body && oElement != document.documentElement ) {
      posX -= oElement.scrollLeft;
      posY -= oElement.scrollTop;
      oElement = getNextAncestor(oElement);
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

function navDetect(){
	var browser=navigator.userAgent;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	
	return Array(browser, version);
}

function overMenu(menu){
	getE(menu).style.color = '#FF8000';
}

function outMenu(menu){
	getE(menu).style.color = '#CCC';
}

function formBuscar(){
	if(getE('mode_l').checked) getE('fbusca').action = '/lista.php';
	if(getE('keywords').value=='' || getE('keywords').value=='Onde?'){
		alert('Por favor digite o nome da cidade desejada!');
		getE('keywords').focus();
		return;
	}else{
		getE('DivBuscaBusca').innerHTML = '<img src="images/carregando.gif" width="50" height="52" alt="Carregando...">';
		document.fbusca.submit();
	}
}

function checkAction(){
	if(getE('mode_m').checked && getE('fbusca').action != '/busca.php')
		getE('fbusca').action='/busca.php';
	else if(getE('mode_l').checked)
		getE('fbusca').action='/lista.php';
	
}