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

var SNOB = {
	init : function(){
		SNOB.hacerBlank();
		SNOB.formBoletin();
		SNOB.animarMenu();
		if ($chk($('contentMapa'))){
    		var latlng = new google.maps.LatLng(40.426355, -3.684568);
   	 		var myOptions = {
      			zoom: 16,
      			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:"S.NOB"
			});
  			google.maps.event.addListener(marker, 'click', function() {
    			map.setZoom(17);
  			});

   		}
		if ($chk($('contentHome'))){
			var flashvars = {
 				background: "transparent"
			};
			var params = {
				background: "transparent"
			};
			var attributes = {
				id: "myDynamicContent",
				name: "myDynamicContent",
				wmode: "transparent"
			};
 
			swfobject.embedSWF("../gallery.swf", "contentHome", "944", "347", "9.0.0", flashvars, params, attributes);
		}
	},
	animarMenu : function(){
		$each($$('#menu a'), function(element){
			var fx = new Fx.Morph(element, {duration:300, wait:true});
			if ((element.className != 'seleccionado') && (element.className != "socnet hacerblank")){
				$(element.id).addEvent('mouseenter', function(){
					fx.start({'border-bottom':'4px solid #585858'});
				});
				$(element.id).addEvent('mouseleave', function(){
					$(element.id).style.border = 'none';
				});
			}
		});
	},
	formBoletin : function(){
		$('email').addEvent('blur',function(){
			if ($('email').value == '')
				$('email').value = 'Introduzca su email';
		});
		$('email').addEvent('focus',function(){
			if ($('email').value == 'Introduzca su email')
				$('email').value = '';
		});
		$('formSubscription').addEvent('submit',function(){
			if ($('email').value == ''){
				$('email').setStyle('color','#f00');
				$('email').setStyle('font-weight','bold');
				$('email').setStyle('background-color','#fff');
				$('email').value = 'Campo vac’o';
				input = 'email';
				texto = 'Introduzca su email';
				var time = SNOB.recuperarInput.delay(1500);
				return false;
			}
			else if ($('email').value == 'Introduzca su email') {
				$('email').setStyle('color','#f00');
				$('email').setStyle('font-weight','bold');
				$('email').setStyle('background-color','#fff');
				$('email').value = 'Introduzca un email';
				input = 'email';
				texto = 'Introduzca su email';
				var time = SNOB.recuperarInput.delay(1500);
				return false;
			}
			else if (!(filter.test($('email').value))) {
				$('email').setStyle('color','#f00');
				$('email').setStyle('font-weight','bold');
				$('email').setStyle('background-color','#fff');
				$('email').value = 'Email incorrecto';
				input = 'email';
				texto = 'Introduzca su email';
				var time = SNOB.recuperarInput.delay(1500);
				return false;
			}
			else{
				var myAjax = new Request({
					url : location.protocol+'//'+location.hostname+'/includes/processor.php',
					method : 'post',
					data : 	{
						'seccion' : 'boletin',
						'email' : $('email').value
					},
					onComplete : function(element){
						datos = JSON.decode(element);
						if (datos == 'existe')
						{
							$('email').setStyle('color','#f00');
							$('email').setStyle('font-weight','bold');
							$('email').setStyle('background-color','#fff');
							$('email').value = 'Ya existe el email';
							input = 'email';
							texto = 'Introduzca su email';
							var time = SNOB.recuperarInput.delay(1500);
						}
						else if (datos == 'ko')
						{
							$('email').setStyle('color','#f00');
							$('email').setStyle('font-weight','bold');
							$('email').setStyle('background-color','#fff');
							$('email').value = 'Error BBDD';
							input = 'email';
							texto = 'Introduzca su email';
							var time = SNOB.recuperarInput.delay(1500);
						}
						else if (datos == 'ok')
						{
							$('email').setStyle('color','#0f0');
							$('email').setStyle('font-weight','bold');
							$('email').setStyle('background-color','#fff');
							$('email').value = 'Perfecto';
							input = 'email';
							texto = 'Introduzca su email';
							var time = SNOB.recuperarInput.delay(1500);
						}
					}
				});
				myAjax.send();
				return false;
			}
	   });	
	},
	recuperarInput : function(){
		$(input).setStyle('color','#a3a3a3');
		$(input).setStyle('font-weight','normal');
		$(input).setStyle('background-color','#424242');
		$(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() { SNOB.init(); } );
