Event.observe(window, 'load', function() {
	initializeMap();

	Event.observe('message', 'focus', onFocusBody);
	Event.observe('message', 'blur', onLostFocusBody);
	Event.observe('email', 'focus', onFocusEmail);
	Event.observe('email', 'blur', onLostFocusEmail);	

	$('contact_form_help_email').hide();
	$('contact_form_help_body').hide();	
});

Event.observe(window, 'unload', function() {
	GUnload();
});


function onFocusBody() {
	$('contact_form_help_body').show();
}

function onLostFocusBody() {
	$('contact_form_help_body').hide();
}

function onFocusEmail() {
	$('contact_form_help_email').show();
}

function onLostFocusEmail() {
	$('contact_form_help_email').hide();
}

function validateForm() {
	if (!isValidEmail($F('email'))) {
		alert('Introduce un e-mail correcto');
		return false;
	}
	if ($F('message').blank()) {
		alert('No has escrito nada en el mensaje');
		return false;
	}
	return true;
}

function submitForm() {
	if (validateForm()) {
		document.contact_form_general.submit()
	}
}


/* Contact Map */

var map=null;
var latlongWez=null;
var secondsBetweenAnims=1.7;
var zoomValue=9;
var periodicalExec=null;

function setVisibleStaticPhotoMap(show) {
	var wezstudioMap=$("wezstudio_map");
	if (wezstudioMap==null) {
		return;
	}
	if (show) {
		wezstudioMap.show();	
	} else {
		wezstudioMap.hide();	
	}
}

function initializeMap() {
	var mapCanvas=$("map_canvas");
	if (mapCanvas==null) return;
	if (!GBrowserIsCompatible()) {
		//Si el browser no es compatible con Google Maps, revertirmos a una imagen estática
		mapCanvas.hide();
		setVisibleStaticPhotoMap(true);
		return;
	}
	setVisibleStaticPhotoMap(false);
	latlongWez=new GLatLng(41.2274743817066, 1.727750301361084);

    map=new GMap2(mapCanvas);
	//map.disableDragging();
	zoomMap();
	var marker=new GMarker(latlongWez,createWezIcon());
	map.addOverlay(marker);
}

function centerOnWez() {
	map.setCenter(latlongWez, zoomValue);	
}

function zoomMap() {
	if (periodicalExec!=null){
		periodicalExec.stop();
	}
	centerOnWez();
	zoomValue=zoomValue+2;
	if (zoomValue<=13) {
		secondsBetweenAnims=secondsBetweenAnims*1.5;
		periodicalExec=new PeriodicalExecuter(zoomMap, secondsBetweenAnims);
	}
}

function createWezIcon() {
	var wezIcon=new GIcon();
	wezIcon.image="../images/contact/bubble_wezstudio.png";
	wezIcon.iconSize=new GSize(58,52);
	
	//anchors 
	wezIcon.iconAnchor=new GPoint(58,52);
	wezIcon.infoWindowAnchor=new GPoint(0,0);
	
	//versión para imprimir 
	wezIcon.printImage=wezIcon.image;
	wezIcon.mozPrintImage=wezIcon.image;
	wezIcon.transparent="../images/contact/bubble_wezstudio_transparent.png";

	wezIcon.imageMap=[0,0, 58,0, 58,52, 0,52]; 
	wezIcon.maxHeight=50;
	
	//sombra
	wezIcon.shadow="../images/contact/bubble_wezstudio_shadow.png";
	wezIcon.shadowSize=new GSize(81,52); 
	wezIcon.printShadow="../images/contact/bubble_wezstudio_shadow.gif";

	//No es importante, en este caso no se deja hacer drag&drop
	wezIcon.dragCrossImage=wezIcon.image;
	wezIcon.dragCrossSize=wezIcon.iconSize;
	wezIcon.dragCrossAnchor=wezIcon.iconAnchor;
	return wezIcon;
}
