var map=null;
var select_day=parseInt(get_hash('day',0));//URL
var select_metric=get_cookie('metric','c');//Cookie
var select_type=get_hash('type','physical');//URL
var select_place=parseInt(get_hash("place",0));//URL
var map_lat=get_hash("lat",0);
var map_lng=get_hash("lng",0);
var map_zoom=parseInt(get_hash("zoom",0));
var map_icon=Array();

if (!GBrowserIsCompatible()){

}else{
	// Make Icon Weather
	for (i=0;i<=47;i++){
		map_icon[i] = new GIcon();
		map_icon[i].image = server_url+"imagens/tempo/32x32/"+i+".png";
		map_icon[i].iconSize = new GSize(32, 32);
		map_icon[i].iconAnchor = new GPoint(16, 16);
		map_icon[i].infoWindowAnchor = new GPoint(22,7);
	}
	map_icon[48] = new GIcon();
	map_icon[48].image = server_url+"imagens/tempo/32x32/-.png";
	map_icon[48].iconSize = new GSize(32, 32);
	map_icon[48].iconAnchor = new GPoint(16, 16);
	map_icon[48].infoWindowAnchor = new GPoint(22,7);

	//Make Icon Place
	map_local = new GIcon();
	map_local.image = server_url+"imagens/place.png";
	map_local.iconSize = new GSize(10, 10);
	map_local.iconAnchor = new GPoint(5, 5);
	map_local.infoWindowAnchor = new GPoint(7,7);



	function make_map(lat, lng, zoom) {
		if (map_lat==0 && map_lng==0) {
			map_lat=lat;
			map_lng=lng;
		}
		if (map_zoom==0) {
			map_zoom=zoom;
		}
		if (GBrowserIsCompatible()){
			map = new GMap2(document.getElementById("map_map"));
			map.addControl(new GSmallMapControl ());
			//map.addControl(new GOverviewMapControl());
			map.enableDoubleClickZoom();
			map.enableScrollWheelZoom();
			map.setCenter(new GLatLng(map_lat , map_lng), map_zoom);

			change_type(select_type);
			change_day(select_day);
			change_metric(select_metric);
			map_move();

			GEvent.addListener(map, "moveend", map_move);
			if (select_place!=0) {
				change_place(select_place);
			}
		}
	}
}

function map_move(){
	var center = map.getCenter();
	map_zoom=map.getZoom();
	map_lat=center.lat();
	map_lng=center.lng();
	map_hash();

	var bounds = map.getBounds();
	var min = bounds.getSouthWest();
	var max = bounds.getNorthEast();

	url=server_url+"map.php?min_lat="+ min.lat() +"&max_lat="+ max.lat() +"&min_lng="+ min.lng() +"&max_lng="+ max.lng() +"&zoom="+ map_zoom +"&day="+ select_day;
	//document.getElementById("search_input").value=url;
	request_JSON(url,"map");
}



function change_day(dia){//altera o dia dos icons no mapa
	for(i=0;i<5;i++){
		document.getElementById("map_day_"+i).className='';
	}
	document.getElementById("map_day_"+dia).className='on';
	select_day=dia;
	map_move();

	return false;
}


function change_type(type){//Tipo de Mapa TYPES:normal satellite hybrid physical
	document.getElementById("map_type_normal").className='';
	document.getElementById("map_type_satellite").className='';
	document.getElementById("map_type_hybrid").className='';
	document.getElementById("map_type_physical").className='';
	switch(type){
	case "satellite":
		document.getElementById("map_type_satellite").className='on';
		map.setMapType(G_SATELLITE_MAP);
		break;
	case "hybrid":
		document.getElementById("map_type_hybrid").className='on';
		map.setMapType(G_HYBRID_MAP);
		break;
	case "physical":
		document.getElementById("map_type_physical").className='on';
		map.setMapType(G_PHYSICAL_MAP);
		break;
	default:
		type="normal";
		document.getElementById("map_type_normal").className='on';
		map.setMapType(G_NORMAL_MAP);
	}
	select_type=type;
	map_hash();
	return false;
}

function change_place(id,name){
	select_place=id;
	map_hash();
	//Buscar JSON
	url=server_url+"map.php?place="+select_place+def_user();
	document.getElementById("det_today").innerHTML ='<h1>'+(name!=undefined?name:'...')+'</h1>'+'<div class="loading">&nbsp;</div>';
	request_JSON(url,"place");
	//apresentar icon "loading"


	//actualizar Caixas
}

function act_place(data){
	if (data.id==select_place) {//det_today     det_next_days
		html='<h1>'+data.local+'</h1><h2>'+data.estado+'</h2>';
		html+='<img src="'+server_url+'imagens/tempo/100x100/'+data.icon+'.png" alt="'+data.desc+'" width="100" height="100" align="left" />';
		html+='<h3>'+data.temp+'</h3><h4>'+data.desc+'</h4><br><a href="'+data.url+'" class="right">'+data.url_desc+'</a>';
		html+='<br clear="all" />'+data.extra+'';
		document.getElementById("det_today").innerHTML = html;
		var html_days='';
		for (i=0; i<data.days.length; i++) {
			var dia=data.days[i];
			html_days+='<div class="det_day"><img src="'+server_url+'imagens/tempo/32x32/'+dia.icon+'.png" alt="'+dia.desc+'" width="32" height="32" align="left" />';
			html_days+='<span class="max">'+dia.max+'</span><span class="min">'+dia.min+'</span><span class="desc">'+dia.dia+'</span></div>';
		}
		document.getElementById("det_next_days").innerHTML = '<h1>'+data.next_days+':</h1>'+html_days;
	}
}


function map_hash(){// formato da hash #LAT;LNG;ZOOM;PLACE;DAY;TYPE
	set_hash('lat='+map_lat+'&lng='+map_lng+'&zoom='+map_zoom+(select_place!=0?'&place='+select_place:'')+'&day='+select_day+'&type='+select_type);
}



function update_icon(data){
	var i;
	map.clearOverlays();
	if (data.station){
		//alert(data.station.length);
		for (i=0;i<data.station.length; i++) {
			station=data.station[i];
			//alert((station.icon)+' - '+i);
			var point = new GLatLng(parseFloat(station.lat),parseFloat(station.lng));
			createMarker(point,map_icon[station.icon],station);
		}
	}

	if (data.place){
		//alert(data.place.length);
		for (i=0;i <data.place.length; i++) {
			place=data.place[i];
			var point = new GLatLng(parseFloat(place.lat),parseFloat(place.lng));
			createMarker(point,map_local,place);
		}
	}
}

function createMarker(point,icon,data) {
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, "click", function() {//readmore
		change_place(data.id,data.name);
	});
	i++;
	map.addOverlay(marker);
}

function rat(){

	var bounds = map.getBounds();
	var min = bounds.getSouthWest();
	var max = bounds.getNorthEast();

	url=(max.lat()-min.lat())+" ; "+(max.lng()-min.lng());
	alert(url);
}