/*
	Conjunto de reglas
*/
recargaReglasGeneral = {
	
	idioma : function () {
		$("#flecha_idioma").click(ejecutarReglasGeneral.menuIdioma);
		$(".bandPais").click(ejecutarReglasGeneral.registraIdioma);
		//$("#Espana").click(ejecutarReglasGeneral.banderaEspana);
		//$("#Cataluna").click(ejecutarReglasGeneral.banderaCataluna);
		//$("#Galicia").click(ejecutarReglasGeneral.banderaGalicia);
		return false;
	},
	
	reglax: function () {
		//$("#lalalarito").click(ejecutarReglas.regla2);
		$("#botonBuscarGoogle").click(ejecutarReglasGeneral.buscadorGoogle);
		return false;
	},
	
	cesta : function (){
		$(".carroCompra").click(ejecutarReglasGeneral.cesta);
		$('#desconectarUsuario').click(ejecutarReglasGeneral.desconectarUsuario);
		$("#vaciaCesta").click(ejecutarReglasGeneral.vaciaCesta);
	}
			
}

/* 
	Reglas con funciones manejadoras asociadas a los eventos 
*/
ejecutarReglasGeneral = {
	
	menuIdioma : function () {
		if ($("#flecha_idioma").attr("value") == "oculto"){
			//$("#menu_idioma").show("slow");
			$("#menu_idioma").slideDown("fast");
			$("#flecha_idioma").val("visible");
		}else{
			//$("#menu_idioma").hide("slow");
			$("#menu_idioma").slideUp("fast");
			$("#flecha_idioma").val("oculto");
		}
		return false;
	},
	
	banderaEspana : function () {
		$("#banderaActiva :first-child").attr("src","images/espana.gif"); 
		$("#menu_idioma").slideUp("fast");
		$("#flecha_idioma").val("oculto");
		return false;
	},
	
	banderaCataluna : function () {
		$("#banderaActiva :first-child").attr("src","images/catalunya.gif");
		$("#menu_idioma").slideUp("fast");
		$("#flecha_idioma").val("oculto");
		return false;
	},
	
	banderaGalicia : function () {
		$("#banderaActiva :first-child").attr("src","images/galicia.gif");
		$("#menu_idioma").slideUp("fast");
		$("#flecha_idioma").val("oculto");
		return false;
	},
	
	buscadorGoogle : function () {
		
//		alert("haciendo algo");
		
		$("#buscador").remove();
		$(".contenido_pd").attr("id","contenidoCentral");
		$("#contenidoCentral").hide();
		$(".contenido").after('<div id="buscador" class="contenido" style="display:none"><div id="buscadorGoogle" style="margin: 20px 20px 20px 20px"></div></div>');
		
		$("#buscador").show();
		
		var search = $("#cadenaBusqueda").val();
		
			
		customSearchControl.draw("buscadorGoogle");
		customSearchControl.execute(search);
		
		
//		var action  = $(this).parent().parent().parent().parent().parent().attr("action");
//		alert("action: " + action);
//		//var parametros = $("#formularioGoogle").serialize();
//		alert(parametros);
//		
//		var url = action + "?" + parametros;
//		alert(url);
//		var htmlBuscador;
		
		return false;
		
	},
	
	cesta : function (e) {
		
		//alert("t: " + $(this).attr("t") + " | e:" + $(this).attr("e"));
		var sesion = $("#formSessid input").attr("value");
		var aP = "tipoarticulo=" + $(this).attr("t") + "&clavearticulo=" + $(this).attr("e") + "&PHPSESSID=" + sesion;
		$.ajax({url: "../llamadas_ajax/cestacompra.php", data:aP, type : "GET", success : function(){
		
			$.ajax({url: "../llamadas_ajax/AJAXModuloCesta.php", data:aP, type : "GET", success : function(html){
				if (html != ""){
					$("#cestaCompra").slideUp("slow",function(){
						$("#cestaCompra").html(html);	
					});
					$("#cestaCompra").slideDown(function (){
						$("#cestaCompra").each(recargaReglasGeneral.cesta);	
					});
				}
			}});
		}});
		
		$.ajax({url: "../llamadas_ajax/AJAXTiendaCestaBocadillo.php", data:aP, type : "GET", success : function(html){
			$("#modal").html(html);
			$("#modal").css("left",e.pageX-310);
			$("#modal").css("top",e.pageY-75);
			$("#overlayer").css("height",$("body").height());
			$("#overlayer").fadeIn(function(){
				$("#modal").show();
				$("#bocadilloContinuar").click(function(){
					$("#modal").html("");
					$("#overlayer").fadeOut();
					return false;
					
				});
			});
									
//			alert($(this).width());
//			alert($(this).height());
		}});
		return false;
		
	},
	
	refrescoCesta : function (){
		var sesion = $("#formSessid input").attr("value");
		var aP = "tipoarticulo=" + $(this).attr("t") + "&clavearticulo=" + $(this).attr("e") + "&PHPSESSID=" + sesion;
		$.ajax({url: "../llamadas_ajax/AJAXModuloCesta.php", data:aP, type : "GET", success : function(html){
			if (html != ""){
				$("#cestaCompra").slideUp("slow",function(){
					$("#cestaCompra").html(html);	
				});
				$("#cestaCompra").slideDown();
				$("#cestaCompra").each(recargaReglasGeneral.cesta);
			}
		}});
	},
	
	registraIdioma : function () {
		
		//alert("t: " + $(this).attr("t") + " | e:" + $(this).attr("e"));
		var sesion = $("#formSessid input").attr("value");
		var aP = "&PHPSESSID=" + sesion + "&idioma=" + $(this).attr("clave");
		$.ajax({url: "../llamadas_ajax/AJAXRegistraIdioma.php", data:aP,async: false, type : "GET", success : function(){
			return true;
		}});
	
		return true;
			
	},
	
	desconectarUsuario : function(){
		//	Se desconecta el usuario 
		//	Se muestra la pantalla desconexion de usuario   		
   		var aP = "param1=param1";
		$.ajax({url: "../llamadas_ajax/AJAXTiendaLoginDesconectar.php",data: aP,type: "POST",async: false,success: function(html){
			// Se elimina el mensaje lateral superior de bienvenida
			$("#login").slideUp("normal",function(){
				$("#clienteBienvenida").text("");
			});
			
			// Se recarga el bloque de la tienda
			$("#bloqueTienda").slideUp("normal",function(){
				$("#bloqueTienda").html(html);
				$("#bloqueTienda").each(recargaReglasTiendaVirtual.paso2);
			});
			$("#bloqueTienda").slideDown("normal");
			
		}});
		return false;
	},
	
	vaciaCesta : function () {
		
		var aP = "accion=vaciaCesta";
		
		$.ajax({url: "../llamadas_ajax/AJAXTiendaGestionCesta.php",data: aP,type: "POST",async: false,success: function(html){
			$.ajax({url: "../llamadas_ajax/AJAXModuloCesta.php", data:aP, type : "GET", success : function(html){
					$("#cestaCompra").slideUp("slow",function(){
						$("#cestaCompra").html(html);	
					});
					$("#cestaCompra").each(recargaReglasGeneral.cesta);
			}});						
		}});
				
		return false;
	}	
}

/*
	Carga inicial del listado de reglas
*/

$(document).ready(ejecutarReglasGeneral.refrescoCesta);
$(document).ready(recargaReglasGeneral.idioma);
$(document).ready(recargaReglasGeneral.reglax);
$(document).ready(recargaReglasGeneral.cesta);

$(document).ready(function(){
	$("form").find("input[type=hidden]").hide();
	$("ul").prev("br").remove();
	
	return false;
});

/*
 // Colocar el título de la página
$(document).ready(function(){
	var titulo = $("div[rel=submenu_superior] h1").text();
	$(document).attr("title", titulo);
	
	return false;
});*/


/*<script src="http://www.google.com/jsapi" type="text/javascript"></script>
$(document).ready(googleBuscador);*/

var customSearchControl;

/*function googleBuscador(){
	google.load('search', '1');
	google.setOnLoadCallback(function(){
	    var customSearchControl = new google.search.CustomSearchControl('012116895994619551238:kyvfs-ybzxs');
	    //customSearchControl.draw('contenido_pd');
	  }, true);
	
	
}*/


function emailvalido(valor){

	re=/^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9]+([._-][a-zA-Z0-9]+)*([.][a-zA-Z]{2,4})$/;
    if(!re.exec(valor))    {
        return false;
    }else{
        return true;
    }
}

