﻿var TELEVA = {
	'opened': false,
	'mapa': null,
	'gdir': {},
	'locale': 'pt-br',
	'show': function(){
		if($('partida').value == "" || enderecoFesta == ""){
			alert("Digite o endereco.");
			return;
		}
		this.opened = true;
		bgSite.show();
		$('comoChegar').style.display = "block";
		
		if (GBrowserIsCompatible()) {
			this.mapa = new GMap2($("mapa"));
			
			this.gdir = new GDirections(this.mapa, $("descRota"));
    	    GEvent.addListener(this.gdir, "load", this.gLoad);
	        GEvent.addListener(this.gdir, "error", this.gError);
			
			this.goTo($('partida').value, enderecoFesta);
		}
		
		this.alinha();
	},
	'gLoad': function(){},
	'gError': function(){
		TELEVA.hide();		
		if (TELEVA.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
			alert('Endereço de partida não encontrado.');
			return;
		}
		alert('Erro no servidor, tente novamente mais tarde');
	},	
	'goTo': function(fromEndereco, toEndereco){
		if(fromEndereco == "" || toEndereco == ""){
			alert("Digite o endereco");
			return;
		}
		$("descRota").innerHTML = "";
		$('partida2').value = fromEndereco;
		this.gdir.load("from: " + fromEndereco + " to: " + toEndereco, { "locale": this.locale });
	},
	'alinha' : function(){
		if(!this.opened) return;
		var SC = getScroll();
		var VS = getDocVisibleSize();
		var SZ = getSize('comoChegar');
		newX = (VS.x-SZ.w)/2;
		newY = (VS.y-(SZ.h))/2;
		$('comoChegar').style.top = (newY+SC.y)+"px";
		$('comoChegar').style.left = (newX+SC.x)+"px";
		bgSite.alinha();
	},
	'hide': function(){
		this.opened = false;
		$('comoChegar').style.top = "-1000px";
		$('comoChegar').style.lef = "-1000px";
		$('comoChegar').style.display = "none";
		bgSite.hide();
	}
}
window.onload = function(e){
	mostraFlash("menu", "/media/swf/menu.swf", 986, 39);
	mostraFlash("logo", "/media/swf/logo.swf", 118, 108);
}

window.onresize = window.onstopscroll = function(e){
	TELEVA.alinha();
}