var api_key=document.getElementsByTagName('script')[0].src.replace('http://maps.google.com/maps?file=api&v=2.x&key=','');//'ABQIAAAAzhz6yy0oSoDsRJpv-fM1OxRnMWqRmu0us-Wf9hNxO02ZDhJTDRTOb0nKNKoJEmftofPbbehrvLlkSA';
var map = null;
var geocoder = null;
var zoom=14;
var lat,lng,marker,pos1aux,pos2aux;
var color='red';
var cursor=true;

function $(id){
	return document.getElementById(id);
}

function mostrarOcultarCursor(){
	cursor =!cursor;
	if(cursor){
		afegirCursor(	map.getCenter()	);
	}else{
		map.clearOverlays();
	}
	agafarPunt(map.getCenter());
}

function initialize() {canviarTamany(true);$('amagat').style.display='';
	if (GBrowserIsCompatible()) {
	    map = new GMap2($('map_canvas'));
		map.addControl(new GSmallZoomControl ());
		map.disableDoubleClickZoom();
		GEvent.addListener(map, "moveend", function() { 
			obj = map.getCenter();
			pos1aux=obj.lat().toFixed(5);
			pos2aux=obj.lng().toFixed(5);
		});
		GEvent.addListener(map, 'dblclick', function(overlay,punt) { 
			map.clearOverlays();
			afegirCursor(punt);
		});
		geocoder = new GClientGeocoder();
	}
	$('amagat').style.display='none';
}

function afegirCursor(punt){
	marker = new GMarker(punt, {draggable: true});
    GEvent.addListener(map, "zoomend", function(){
	    zoom = map.getZoom();
    	map.setCenter(marker.getPoint(), zoom);
    });
    GEvent.addListener(marker, "dragend", function() {
    	punt = marker.getPoint()	;	
		agafarPunt(punt);
	});
	map.addOverlay(marker);
    map.setCenter(marker.getPoint(), zoom);
}

function addAddressToMap(response) {
	map.clearOverlays();
	if (!response || response.Status.code != 200) {
		$('amagat').style.display='none';
        alert('no disponible');    	
	}else{
		$('amagat').style.display='';
        place = response.Placemark[0];
        punt = new GLatLng(place.Point.coordinates[1],
        place.Point.coordinates[0]);                    
		agafarPunt(punt);
		map.setCenter(punt, zoom);
		if(cursor)
			afegirCursor(punt);
	}
}
    
function agafarPunt(obj){
	lat=obj.lat().toFixed(5);
	lng=obj.lng().toFixed(5);
}

function generar(){
	width = $('txtample').value;
	height = $('txtaltura').value;
	color = $('selcolor').value;
	if($('chkcentrar').checked && $('chkcursor').checked){
		pos1=lat;
		pos2=lng;
	}else{
	  pos1=pos1aux;
	  pos2=pos2aux;
	}

	dir = 'http://maps.google.com/staticmap?center='+pos1+','+pos2+'&zoom='+zoom+'&size='+width+'x'+height+'&maptype='+$('selmapa').value;
	if(cursor)dir += '&markers='+lat+','+lng+','+color;
	dir += '&key='+api_key;
	codi = '<div><img src="'+dir+'" width="'+width+'" height="'+height+'"/></div><div style="padding-top:15px;text-align:center;">';
	codi += '<input value="Descargar mapa" type="button" onclick="document.location.href=\'descargar.php?url='+encodeURIComponent(dir)+'\';"/>';
	codi += '</div>';
	$('embed').innerHTML=codi;
}

function canviarTamany(igual){
	if(igual){
		tamany = $('selsize').value.split('x');
		width = tamany[0];
		height = tamany[1];
		$('txtample').value = width;
		$('txtaltura').value = height;
	}else{
		width = $('txtample').value;
		height = $('txtaltura').value;
		if(width>512)width=$('txtample').value =512;
		if(height>512)height=$('txtaltura').value=512;
	}
	$('map_canvas').style.width=width+'px';
	$('map_canvas').style.height=height+'px';
	if(marker){
		map.checkResize();
		map.setCenter(marker.getPoint(), zoom);
	}
}

window.onload=function(){
	initialize();
	$('selsize').onchange=function(){canviarTamany(true)};
	document.formulari.action="javascript:geocoder.getLocations($('on').value, addAddressToMap);";
	$('txtample').onchange=function(){canviarTamany(false);};
	$('txtaltura').onchange=function(){canviarTamany(false);};
	$('chkcursor').onclick=function(){mostrarOcultarCursor();};
	$('btn_gen').onclick=generar;
	$('on').focus();
};

window.onunload=function(){
	GUnload();
};
