// JavaScript Document
function cambiar_cabecera(id){
	$(".cabecera_flash").not("#cabecera_flash_"+id).hide();
	$("#cabecera_flash_"+id).show();
}
function mostrar_productos_home(id_categoria, id_coleccion){
		//alert(id_categoria+" "+id_coleccion);
		var id_coleccion;
		$(".thumbnail_home_wrap").hide();
		$(".title_home_col").show();
		$("#thumbnail_home_wrap_"+id_coleccion).load("ajax.thumbnails.php", {id: id_categoria}, function(){
			$("#title_home_col_"+id_coleccion).hide();
			$("#thumbnail_home_wrap_"+id_coleccion).slideDown();
		});
}
function mainmenu(){
	$(" #menu_principal ul ").css({display: "none"}); // Opera Fix
	$(" #menu_principal li").hover(
		function(){
			$(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(200);
		},
		function(){
			$(this).find('ul:first').css({visibility: "hidden"});
		}
	);
}

function validarDatos(nameformulario, texto_alerta_campos){
	var Validar = new clsValidator();
	Validar.setEncabezado(texto_alerta_campos);
	Validar.setErrorColor("#bbbbbb");
	Validar.setFormatoHora('24');
	Validar.setFormatoFecha('EURO');
	//Primero busco todos los campos a evaluar
	elementos = document.forms[nameformulario].elements;
	var array_validaciones=new Array();
	var inc=0;
	for (i=0; i<elementos.length; i++){
		className = elementos[i].className;
		validadonull = false;
		if(className.substring(className.length-4)=="NULL"){
			if(trim(elementos[i].value)=="") validadonull=true;
			className = className.substring(0, className.length-4);
		}
		if((className=="frmvalEmail")&&(validadonull==false)){
			label = elementos[i].name+"_lb";
			descripcion = document.forms[nameformulario].elements[label].value;
			Validar.Email(elementos[i].name, descripcion);
		}
		if((className=="frmvalNoVacio")&&(validadonull==false)){
			label = elementos[i].name+"_lb";
			descripcion = document.forms[nameformulario].elements[label].value;
			Validar.Vacio(elementos[i].name, descripcion);
		}
		if((className=="frmvalFecha")&&(validadonull==false)){ 
			label = elementos[i].name+"_lb";
			descripcion = document.forms[nameformulario].elements[label].value;
			Validar.Fecha(elementos[i].name, descripcion);
		}
		if((className=="frmvalEntero")&&(validadonull==false)){
			label = elementos[i].name+"_lb";
			descripcion = document.forms[nameformulario].elements[label].value;
			Validar.Entero(elementos[i].name, descripcion);
		}
		if((className=="frmvalDecimal")&&(validadonull==false)){
			label = elementos[i].name+"_lb";
			descripcion = document.forms[nameformulario].elements[label].value;
			Validar.Decimal(elementos[i].name, descripcion);
		}
		if((className=="frmvalCP")&&(validadonull==false)){
			label = elementos[i].name+"_lb";
			descripcion = document.forms[nameformulario].elements[label].value;
			Validar.CP(elementos[i].name, descripcion);
		}
	}
	
	if (Validar.Validar()){
		return true;
	}
	else{
		Validar.getErrors();
		return false;
	}
}

function enviar_form_contacto(nameformulario, texto_alerta_ley, texto_alerta_campos){
	if(document.forms[nameformulario].elements["acepto"].checked==false){
		alert(texto_alerta_ley); 
	}
	else{
		if(validarDatos(nameformulario, texto_alerta_campos)){
			document.forms[nameformulario].submit();
		}
	}
}



/* 

	List Expander 
	written by Alen Grakalic, provided by Css Globe (cssglobe.com)
	
*/

this.listexpander = function(){
	
	// edit 
	
	var expandTo = 1; // level up to which you want your lists to be initially expanded. 1 is minimum
	var listClass = "listexpander" // class name that you want to assign to list(s). If you wish to change it make sure to update the css file as well  
	
	// end edit (do not edit below this line)
	
	this.start = function(){
		var ul = document.getElementsByTagName("ul");
		for (var i=0;i<ul.length;i++){
			if(ul[i].className == listClass){
				create(ul[i]);
				buttons(ul[i])
			};
		};
	};

	this.create = function(list) {	
		var items = list.getElementsByTagName("li");
		for(var i=0;i<items.length;i++){
			listItem(items[i]);
		};
	};	

	this.listItem = function(li){
		if(li.getElementsByTagName("ul").length > 0){
			var ul = li.getElementsByTagName("ul")[0];
			ul.style.display = (depth(ul) <= expandTo) ? "block" : "none";
			li.className = (depth(ul) <= expandTo) ? "expanded" : "collapsed";
			li.over = true;	
			ul.onmouseover = function(){li.over = false;} 
			ul.onmouseout = function(){li.over = true;} 
			li.onclick = function(){
				if(this.over){
					ul.style.display = (ul.style.display == "none") ? "block" : "none";
					this.className = (ul.style.display == "none") ? "collapsed" : "expanded";				
				};
			};
		};		
	};	
	
	this.buttons = function(list){
		var parent = list.parentNode;
		var p = document.createElement("p");
		p.className = listClass;
		var a = document.createElement("a");
		a.innerHTML = expandText;
		a.onclick = function(){expand(list)};
		p.appendChild(a);
		var a = document.createElement("a");
		a.innerHTML = collapseText;
		a.onclick = function(){collapse(list)};
		p.appendChild(a);
		parent.insertBefore(p,list);
	};
	
	this.expand = function(list){
		li = list.getElementsByTagName("li");
		for(var i=0;i<li.length;i++){
			if(li[i].getElementsByTagName("ul").length > 0){
				var ul = li[i].getElementsByTagName("ul")[0];
				ul.style.display = "block";
				li[i].className = "expanded";
			};
		};
	};
	
	this.collapse = function(list){
		li = list.getElementsByTagName("li");
		for(var i=0;i<li.length;i++){
			if(li[i].getElementsByTagName("ul").length > 0){
				var ul = li[i].getElementsByTagName("ul")[0];
				ul.style.display = "none";
				li[i].className = "collapsed";
			};
		};
	};
	
	this.depth = function(obj){
		var level = 1;
		while(obj.parentNode.className != listClass){
			if (obj.tagName == "UL") level++;
			obj = obj.parentNode;
		};
		return level;
	};	
	
	start();
	
};

function ir_a_mapa(id){
	$(".mapa_distri").hide();
	$("#mapa_distri_"+id).show();
}
function openOverlay(id) { 
    var api = $("#overlay_"+id).overlay(); 
    api.load();             
}
function cambiar_foto_detalle(foto){
	$("#imagen_detalle").attr("src", foto);
}
function desplegar_iconos(capa){
	var miCapa = capa;
	$(".iconos_detalle_prod").not("#"+miCapa).slideUp("slow");
	$("#"+miCapa).slideToggle();
}

function ver_medidas(medidas, http_root, croquis){
	if(medidas=="usa"){
		$("#tabla_combinaciones_prescripcion tr.c").hide();
		$("#tabla_combinaciones_prescripcion tr.u").show();
		$("#croquis").attr("src", http_root+"uploads/lamparas/"+croquis);
		$("#medidas").attr("value", "usa");
		$("#bombillas_ce").hide();
		$("#acabados_ce").hide();
		$("#complementos_ce").hide();
		$("#filtro_ce").removeClass("selected");
		$("#bombillas_usa").show();
		$("#acabados_usa").show();
		$("#complementos_usa").show();
		$("#filtro_usa").addClass("selected");
	}
	else if(medidas=="ce"){
		$("#tabla_combinaciones_prescripcion tr.u").hide();
		$("#tabla_combinaciones_prescripcion tr.c").show();
		$("#croquis").attr("src", http_root+"uploads/lamparas/"+croquis);
		$("#medidas").attr("value", "ce");
		$("#bombillas_usa").hide();
		$("#acabados_usa").hide();
		$("#complementos_usa").hide();
		$("#filtro_usa").removeClass("selected");
		$("#bombillas_ce").show();
		$("#acabados_ce").show();
		$("#complementos_ce").show();
		$("#filtro_ce").addClass("selected");
	}
}
function toggle_popup_prescripcion(){
	$("#popup_prescripcion").slideToggle('slow');
}
function form_prescripcion_correcto(){
	email_telf = $("#form_prescripcion #arquitecto_tel_email").val();
	if(email_telf==""){
		$("#form_prescripcion #arquitecto_tel_email").css("background-color", "#ff0000");
		return false;
	}
	else{
		$("#form_prescripcion #arquitecto_tel_email").css("background-color", "#ffffff");
		return true;
	}
}
function enviar_prescripcion(mensaje_form_incorrecto){
	if(form_prescripcion_correcto()){
		$("#form_prescripcion").submit();
		toggle_popup_prescripcion();
	}
	else{
		alert(mensaje_form_incorrecto);
	}
}

$(document).ready(function(){
	mainmenu();
});
