var map=null; //variavel do Mapa
var pointers = new Array();
//var mouseisdown=false;
var mark=true;
var ballon=true;
//var marker=null;//???????

function pointlocais(id, x, y, zoom, nome, descricao, fotos, tabs){
	this.id			= id
	this.x			= x
	this.y			= y
	this.zoom		= zoom
	this.nome		= nome
	this.descricao	= descricao
	this.fotos		= fotos
	this.tabs		= tabs

	this.marker		= null
}

function loadselect(valor){//*****
	window.frames["framelocal"].location = "locais.php?tipo="+ valor
}

function resize(){//*****
	var winmax; var winmay;
	if(typeof( window.innerWidth ) == 'number' ) {
		//Não-IE
		winmax = window.innerWidth-3;
		winmay = window.innerHeight-3;
		//alert("1");
	}else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+
		winmax = document.documentElement.clientWidth;
		winmay = document.documentElement.clientHeight;
		//alert("2");
	}else if(document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4
		winmax = document.body.clientWidth;
		winmay = document.body.clientHeight;
		//alert("3");
	}else{
		winmax=800;
		winmay=600;
	}
	document.getElementById("map").style.width = (winmax-220)+"px";
	document.getElementById("map").style.height = (winmay-90)+"px";
	document.getElementById("logo").style.top = (winmay-80)+"px";
	document.getElementById("logo").style.left = 15+"px";
	document.getElementById("pub").style.top = (winmay-90)+"px";
	document.getElementById("pub").style.left = 150+"px";
	document.getElementById("barra").style.top = "0px";
	document.getElementById("barra").style.left = (winmax-215)+"px";
	//document.getElementById("barra").style.width = "200px";
	document.getElementById("barra").style.height = (winmay-90) + "px";
}

function load() {//*****
	if (GBrowserIsCompatible()) {

		resize();
		map = new GMap2(document.getElementById("map"));
		document.getElementById("framelocal").src="locais.php?tipo="+list;
		GEvent.addListener(map, "move", function() {
			var center = map.getCenter();
			var lat=center.lat();
			var lon=center.lng();
			var zoom=map.getZoom()-12;
			lat=Math.round(lat*100000)/100000;
			lon=Math.round(lon*100000)/100000;
			document.getElementById("message").innerHTML = 'Latitude: ' + lat + '<br>Longitude: ' + lon + '<br>Nivel de Zoom: '+zoom;
			checklimits();
		});

		GEvent.addListener(map, "load", function() {
			completload();
		});

		GEvent.addListener(map, "moveend", function() {
			getpoint();
			actlink();
		});

		var lz = map.getMapTypes();
		for (var i=0; i<lz.length; i++) {
			lz[i].getMinimumResolution = function() {return 13;}
			lz[i].getMaximumResolution = function() {return 18;}
		}

		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableContinuousZoom();
		map.enableDoubleClickZoom();
		map.setCenter(new GLatLng(lat,lon), zoom, G_HYBRID_MAP);//G_NORMAL_MAP //G_HYBRID_MAPGMapTypeControl
		if (msg!='') {
			removeactualpointer();
			opentab(msg,lat,lon);
		}
	}
}

var icon = new GIcon();
icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);

var allowedBounds = new GLatLngBounds(new GLatLng(40.77,-8.55), new GLatLng(40.93,-8.35));

function checklimits() {//*****
	if (allowedBounds.contains(map.getCenter())) {
		return;
	}else{
		var C = map.getCenter();
		var X = C.lng();
		var Y = C.lat();

		var AmaxX = allowedBounds.getNorthEast().lng();
		var AmaxY = allowedBounds.getNorthEast().lat();
		var AminX = allowedBounds.getSouthWest().lng();
		var AminY = allowedBounds.getSouthWest().lat();

		if (X < AminX) {X = AminX;}
		if (X > AmaxX) {X = AmaxX;}
		if (Y < AminY) {Y = AminY;}
		if (Y > AmaxY) {Y = AmaxY;}
		map.setCenter(new GLatLng(Y,X));
	}
}

function getpoint(){
	var center = map.getCenter();
	var lat=center.lat();
	var lon=center.lng();
	var zoom=map.getZoom()-12;
	window.frames["framemaps"].location = "point.php?lat="+lat+"&lon="+lon+"&zoom="+zoom;

}

function movetolocal(i){ 
	map.setCenter(new GLatLng(pointers[i].x,pointers[i].y));//,parseInt(pointers[i].zoom)
	addpointer(i);
}

function setmapcenter(i){//quando se carrega na combo list
	MoveToCenter(i);
	showballon(i);
}

function removeactualpointer(){
	cont = pointers.length;
	for (i=0;i<cont;i++) {
		if (pointers[i].marker!=null) {
			map.removeOverlay(pointers[i].marker);
			pointers[i].marker = null;
		}
	}
	map.clearOverlays();
}

function RemoveAll(){
	map.clearOverlays();
}

function addpointer(i){
	var point = new GLatLng(pointers[i].x,pointers[i].y)
	tabs=arraytotabs(pointers[i].id, '<b>'+pointers[i].nome+'</b><br>' + pointers[i].descricao, pointers[i].fotos, pointers[i].tabs);
	map.openInfoWindowTabsHtml(point, tabs);
	/*if (pointers[i].foto!=''){
	}else{
		var arr = new Array();
		arr=[new GInfoWindowTab("Info",pointers[i].descricao), new GInfoWindowTab("Foto",'Sem Fotos<br>Envie as suas fotos para fotos@oazonline.com')]
		map.openInfoWindowTabsHtml(point, arr);
	}*/
}

function MoveToCenter(id){//move o ponto para o centro do mapa
	map.setCenter(new GLatLng(pointers[id].x,pointers[id].y));
}

function showballon(id){//mostra o Balão
	if (ballon==true){
		point = new GLatLng(pointers[id].x,pointers[id].y)
		tabs=arraytotabs(pointers[id].id, '<b>'+pointers[id].nome+'</b><br>' + pointers[id].descricao, pointers[id].fotos, pointers[id].tabs);
		if (mark==false) {
			map.openInfoWindowTabsHtml(point, tabs);
		}else{
			pointers[id].marker.openInfoWindowTabsHtml(tabs);
		}
	}else{
		MoveToCenter(id);
	}
}

function AddEventBallon(marker,id){
	GEvent.addListener(marker, "click", function() {
		showballon(id);
	});
}

function addmark() {
	cont = pointers.length;
	for (i=0;i<cont;i++) {
		var point = new GLatLng(pointers[i].x,pointers[i].y);
		pointers[i].marker = new GMarker(point, icon);
		AddEventBallon(pointers[i].marker,i);
		map.addOverlay(pointers[i].marker);
	}
}

function loadlist(temp,m,b){
	mark=m;
	ballon=b;
	pointers = temp;
	if (mark==true) {
		addmark();
	}
}

function unlistpoints(){
	map.closeInfoWindow();
	removeactualpointer();
}

function actualizapoint(){
	removeactualpointer();
}

function foto(id){
	window.open('viewimage.php?imagem='+id ,"popup","WIDTH=10,HEIGHT=10,TOP=50,LEFT=50,NORESIZABLE,NOSCROLLBARS");
}



function arraytotabs (id, info , foto, others){//***** Cria array com os tabs dos balões
	temp_tabs = new Array();
	cont_tabs=1;
	temp_tabs[cont_tabs-1]= new GInfoWindowTab("Geral",'<div id="divtabs">'+info+'</div>');
	if (foto>0) {
		cont_tabs++;
		fotos='';
		for (i=1;i<=foto;i++) {
			fotos=fotos+ '<a href="javascript:foto('+ id +', '+i+');"><img src="getfoto.php?id=' + id + '&foto='+i+'" border=0></a>';
		}
		temp_tabs[cont_tabs-1]= new GInfoWindowTab("Fotos",'<div id="divtabs">'+fotos+'</div>');
	}
	if (others!=null && others.length>0) {
		for (i=0;i<others.length;i++) {
			cont_tabs++;
			temp_tabs[cont_tabs-1]= new GInfoWindowTab(others[i][0],'<div id="divtabs">'+others[i][1]+'</div>');
		}
	}
	return temp_tabs;
}

function completload(){//*****
	document.getElementById("loaddiv").style.visibility = "hidden";
	document.getElementById("barra").style.visibility = "visible";
}

//link's
function actlink(){
	var center = map.getCenter();
	var lat=center.lat();
	var lon=center.lng();
	var zoom=map.getZoom();
	var msg=document.getElementById("txt").value;
	msg=msg.replace(/\n/g,"<br>");
	msg=msg.replace(/&/g,"%26");
	msg=msg.replace(/"/g,"%22");
	msg=msg.replace(/'/g,"%27");
	msg=msg.replace(/\?/g,"%3F");
	var link="?lat="+ lat +"&lon="+ lon +"&zoom="+ zoom +"&msg="+ msg +""
	document.getElementById("link").value="http://"+window.location.host+window.location.pathname+link;
	document.getElementById("a_link").href="mailto.php"+link;
}

function openlink(url){
	if (window.open(url ,"popup","WIDTH=300,HEIGHT=300,TOP=200,LEFT=200,NORESIZABLE,NOSCROLLBARS")) {
		return false;
	}
}

function opentab(txt,clat,clon){
	if (clat==undefined && clon==undefined) {
		var center = map.getCenter();
		clat=center.lat();
		clon=center.lng();
	}
	point = new GLatLng(clat,clon);
	txt=txt.replace(/\n/g,"<br>");
	map.openInfoWindowHtml(point,'<div id="divtabs">'+txt+'</div>');
}
//link's



function openpopup(link){
	if (link==undefined) {
		link="";
	}
	window.open(link ,"popup","WIDTH=800,HEIGHT=520,TOP=0,LEFT=0,RESIZABLE,SCROLLBARS");

	return false;
}
