// JavaScript Document
var filter=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; 
var input = '';
var texto = '';
var htmlForm = '';
var key = 'ABQIAAAAJYfkOzLTVGIer5nRtAhH4xTNlU4QgUFRf3yTGs3Sewy7-tfsYhSpgyHaur1SZn9x2Dn_UqGJos0edA';

var SICILIA = {
	init : function(){
		SICILIA.hacerBlank();
		if ($chk($('formBoletinPie')))
			SICILIA.formBoletin();
		if ($chk($('formContacto')))
			SICILIA.formContacto();
		if ($chk($('contentThumbs')))
			SICILIA.cargarSlide();
		if ($chk($('lnkmarcartodos')))
			SICILIA.marcarTodos();
		if ($chk($('contentMapa'))){
    		var latlng = new google.maps.LatLng(40.457715, -3.649821);
   	 		var myOptions = {
      			zoom: 15,
      			center: latlng,
      			mapTypeId: google.maps.MapTypeId.ROADMAP
    		};
   		 	var map = new google.maps.Map(document.getElementById("contentMapa"), myOptions);

   		 	var marker = new google.maps.Marker({
				position: latlng, 
				map: map,
				title:"Sicilia Catering"
			});
  			google.maps.event.addListener(marker, 'click', function() {
    			map.setZoom(10);
  			});

   		}
	},
	marcarTodos : function(){
		$('lnkmarcartodos').addEvent('click',function(){
			$each($$(('#formularioContactos input[type=checkbox]')), function(element){
				$(element).checked = true;
			});
			return false;
		})
	},
	cargarSlide : function(){
		$each($$('#contentThumbs a'), function(element){
			$(element).addEvent('click', function(element){
				if ($('contentImg').hasClass('dnone')){
					$each($$('.contenido .dblock'), function(element){
						$(element).removeClass('dblock');
						$(element).addClass('dnone');
					});
					$('contentImg').removeClass('dnone');
					$('contentImg').addClass('dblock');
				}
				$('imagenBig').src = $(this.id).getProperty('href');
				$('tith1').removeClass('dblock');
				$('tith1').addClass('dnone');
				$$('div.derecha')[0].addClass('fotos');
				return false;
			});
		});
		$('lnkCerrar').addEvent('click',function(){
			$('tith1').removeClass('dnone');
			$('tith1').addClass('dblock');
			$$('div.derecha')[0].removeClass('fotos');
			$each($$('.contenido .dnone'), function(element){
				$(element).removeClass('dnone');
				$(element).addClass('dblock');
			});
			$('contentImg').removeClass('dblock');
			$('contentImg').addClass('dnone');
			return false;
		});
	},
	formContacto : function(){
		$('formContacto').addEvent('submit',function(){
			todoOk = true;
			$each($$('#formContacto input.required'),function(element){
				if (($(element.id).value == '') || ($(element.id).value == 'Requerido')){
					$(element.id).setStyle('color','#f00');
					$(element.id).setStyle('background-color','#fff');
					$(element.id).value = 'Requerido';
					todoOk = false;
				}
			});
			if (todoOk){
				 if (!(filter.test($('inpmail').value))){
					$('inpmail').setStyle('color','#f00');
					$('inpmail').setStyle('background-color','#fff');
					$('inpmail').value = 'Email Incorrecto';
					todoOk = false;
				 }
			}
			if (!todoOk){
				$each($$('#formContacto input["type=text"]'),function(element){
					if ($(element.id).getStyle('background-color') == '#ffffff'){
						$(element).addEvent('focus',function(){
							input = this.id;
							texto = '';
							SICILIA.recuperarInput();
						});
					}
			  });
			}
			if (todoOk) {
				htmlForm = $('contentFormContacto').innerHTML;
				var myAjax = new Request({
					url : location.protocol+'//'+location.hostname+'/includes/processor.php',
					method : 'post',
					data : 	{
						'seccion' 	: 'contacto',
						'nombre' 	: $('inpnombre').value,
						'apellido' 	: $('inpapellido').value,
						'empresa' 	: $('inpempresa').value,
						'direccion' : $('inpdireccion').value,
						'numero' 	: $('inpnumero').value,
						'piso' 		: $('inppiso').value,
						'codpostal' : $('inpcodpostal').value,
						'localidad' : $('inplocalidad').value,
						'telefono' 	: $('inptelefono').value,
						'email' 	: $('inpmail').value,
						'tipoevento': $('inptipoevento').value,
						'dia'		: $('inpdia').value,
						'direvento' : $('inpdireccionevento').value,
						'numevento' : $('inpnumeroevento').value,
						'pisevento' : $('inppisoevento').value,
						'codevento' : $('inpcodpostalevento').value,
						'locdevento': $('inplocalidadevento').value,
						'hora' 		: $('inphora').value,
						'duracion' 	: $('inpduracion').value,
						'invitados' : $('inpinvitados').value,
						'motivo' 	: $('inpmotivoevento').value
					},
					onComplete : function(element){
						datos = JSON.decode(element);
						if (datos == 'ok') {
							$('contentFormContacto').innerHTML = '<p>Todo Ok</p>';							
							var time = SICILIA.pintarFormulario.delay(1500);
							
						}
						else if (datos == 'ko') {
							$('contentFormContacto').innerHTML = '<p>Error</p>';
							SICILIA.formContacto();
						}
					}
				});
				$('contentFormContacto').innerHTML = '<img src="../img/flechas-ok.gif" alt="cargando"><p>enviando</p>';
				myAjax.send();
				return false;
			}
			else
				return false;
		});
	},
	pintarFormulario : function(){
		$('contentFormContacto').innerHTML = htmlForm;
		$('formContacto').reset;
		SICILIA.formContacto();
	},
	formBoletin : function(){
		$('xemail').addEvent('blur',function(){
			if ($('xemail').value == '')
				$('xemail').value = 'Introduzca su email';
		});
		$('xemail').addEvent('focus',function(){
			if ($('xemail').value == 'Introduzca su email')
				$('xemail').value = '';
		});
		$('formBoletinPie').addEvent('submit',function(){
			if ($('xemail').value == ''){
				$('xemail').setStyle('color','#f00');
				$('xemail').setStyle('font-weight','bold');
				$('xemail').setStyle('background-color','#fff');
				$('xemail').value = 'Email incorrecto';
				input = 'xemail';
				texto = 'Introduzca su email';
				var time = SICILIA.recuperarInput.delay(1500);
				return false;
			}
			else if (!(filter.test($('xemail').value))) {
				$('xemail').setStyle('color','#f00');
				$('xemail').setStyle('font-weight','bold');
				$('xemail').setStyle('background-color','#fff');
				$('xemail').value = 'Email incorrecto';
				input = 'xemail';
				texto = 'Introduzca su email';
				var time = SICILIA.recuperarInput.delay(1500);
				return false;
			}
			else{
				var myAjax = new Request({
					url : location.protocol+'//'+location.hostname+'/includes/processor.php',
					method : 'post',
					data : 	{
						'seccion' : 'boletin',
						'email' : $('xemail').value
					},
					onComplete : function(element){
						datos = JSON.decode(element);
						if (datos == 'existe')
						{
							$('xemail').setStyle('color','#f00');
							$('xemail').setStyle('font-weight','bold');
							$('xemail').setStyle('background-color','#fff');
							$('xemail').value = 'Ya existe el email';
							input = 'xemail';
							texto = 'Introduzca su email';
							var time = SICILIA.recuperarInput.delay(1500);
						}
						else if (datos == 'ko')
						{
							$('xemail').setStyle('color','#f00');
							$('xemail').setStyle('font-weight','bold');
							$('xemail').setStyle('background-color','#fff');
							$('xemail').value = 'Error BBDD';
							input = 'xemail';
							texto = 'Introduzca su email';
							var time = SICILIA.recuperarInput.delay(1500);
						}
						else if (datos == 'ok')
						{
							$('xemail').setStyle('color','#0f0');
							$('xemail').setStyle('font-weight','bold');
							$('xemail').setStyle('background-color','#fff');
							$('xemail').value = 'Perfecto';
							input = 'xemail';
							texto = 'Introduzca su email';
							var time = SICILIA.recuperarInput.delay(1500);
						}
					}
				});
				myAjax.send();
				return false;
			}
	   });	
	},
	recuperarInput : function(){
		$(input).setStyle('color','#000');
		$('xemail').setStyle('font-weight','normal');
		$(input).setStyle('background-color','#4d4d4d');
		$(input).value = texto;
	},
	hacerBlank : function()
	{
		var className = 'hacerblank';
		var as = document.getElementsByTagName('a');
		for(i=0;i<as .length;i++)
		{
			var a = as[i];
			r=new RegExp("(^| )"+className+"($| )");
			if(r.test(a.className))
			{
				a.onclick = function()
				{
					window.open(this.href);
					return false;
				}
			}
		}
	}

}

/* LOAD */

function addLoadEvent(fn)
{
var old = window.onload;
	if(typeof window.onload != 'function')
		window.onload = fn;
	else
	window.onload = function()
	{
		old();
		fn();
	}
}

addLoadEvent( function() { SICILIA.init(); } );
