$(function () {
	control_carrito();
	control_idioma();
	
	$('#f_filtro select').change(function () {
		$('#f_filtro').submit();
	});
})


	function control_idioma() {
		$("#chgIdioma a").click(function () {
			$("input[name=idioma]",$("#formIdioma")).val($(this).attr("rel"));
			$("form[name=formIdioma]").submit();
			return false;
		})
	}

function control_ayuda() {
	$(".enlace_faq").click(function() {
		id = $(this).attr("rel");
		$(".pregunta_faq.actual").fadeOut('medium', function(a) {
			$(this).removeClass('actual');
			$('.pregunta_faq[rel='+id+']').addClass('actual').fadeIn('medium');
			
			nheight	= ((($('.pregunta_faq.actual').height() + 50)<=413) ? (413) : ($('.pregunta_faq.actual').height() + 50) );
			$('.contentsDer').animate({
				height: nheight
			});
			$(".contentsIzq").animate({
				height: nheight
			});
		});
		return false;
	});
	
	$("#faq_siguiente").click(function() {
		id = $(".pregunta_faq.actual").attr("rel");
		if (parseInt(id) == id) {
			nuevo_id = parseInt(id) + 1;
			$(".enlace_faq[rel='"+nuevo_id+"']").trigger('click');
		} else {
			//console.log("!");
		}
		return false;
	});
	
	$("#faq_anterior").click(function() {
		id = $(".pregunta_faq.actual").attr("rel");
		if (parseInt(id) == id) {
			nuevo_id = parseInt(id) - 1;
			$(".enlace_faq[rel='"+nuevo_id+"']").trigger('click');
		} else {
			//console.log("!");
		}
		return false;
	});
}

function control_carrito() {
	$("#left").css({
		//top: 180 
		top: 10
	});
	
	$.ajax({
		url: "bloque_carrito.asp",
		data: "kc=" + $("input[name=killcache]").val(),
		success: function(data){
			$("#cart").html(data);
			
			if ($("#flogin").length==0) {
				$("#cart").animate({
					top: 0
				},'normal');
			} else {
				
				$("input[name=email]").focus(function () {
					$(this).css("background-image","none");
				}).blur(function () {
					if ($(this).val()=="") {
						$(this).css("background-image","url(img/bg_mail.gif)");
					} else {
						$(this).css("background-image","none");
					}
				});
				
				$("input[name=clave]").focus(function () {
					$(this).css("background-image","none");
				}).blur(function () {
					if ($(this).val()=="") {
						$(this).css("background-image","url(img/bg_clave.gif)");
					} else {
						$(this).css("background-image","none");
					}
				});
				
				$("#btnLogin").click(function () {
					$("#flogin").submit();
					return false;
				});
				var validator = $("#flogin").validate({
					onfocusout: false,
					onkeyup: false,
					rules: {
						email:	{	required:	true,
										email:		true
								}
					},
					submitHandler: function(form) {
						$(form).ajaxSubmit({
							success: function (data, statusText) {
								try {
									arr_data	= data.split("|");
									res			= arr_data[0];
									msg			= arr_data[1];
									
									if (res=="OK") {
										location.reload();
									} else {
										ayco_alertar(msg);
									}
								} catch (ex) {
									location.href="index.asp";
								}
							}
						});
					},
					invalidHandler: function(form, validator) {
						var errors = validator.numberOfInvalids();
						callback_form ('KO_FORM');
						setTimeout(function () { $.unblockUI() },2000);
					}
				});
			}
			
			$("#triggerLogin").click(function () {
				$("#left").animate({
						top: 0
				},'normal');
				
				return false;
			})
			
		}
	});
}

function control_index () {
	var $bnnrMujeres	= $("#bnnrMujeres")		.css({ opacity: 0, top: 0, left: -$("#bnnrMujeres").width() });
	var $bnnrCalidad	= $("#bnnrCalidad")		.css({ opacity: 0, top: -$("#bnnrCalidad").height(), right: $("#bnnrCatalogos").width() + 16 });
	var $bnnrCatalogos	= $("#bnnrCatalogos")	.css({ opacity: 0, top: 0, right: -$("#bnnrCatalogos").width() });
	var $bnnrComprar	= $("#bnnrComprar")		.css({ opacity: 0, bottom: -$("#bnnrComprar").height(), right: 0, width: (($("#bnnrEmpresa").length==1) ? 361 : $("#bnnrComprar").width() ) });
	var $bnnrEmpresa		= $("#bnnrEmpresa")		.css({ opacity: 0, bottom: 0, right: $("#portada").width() });
	
	$("#portada a").hover(
		function () {
			$("span.hover",$(this)).stop(true,true).fadeIn();
		},
		function () {
			$("span.hover",$(this)).stop(true,true).fadeOut();
		}
	).click(function () {
		$bnnrMujeres	.animate({ opacity: 0, top: 0, left: -$bnnrMujeres.width() },'fast');
		$bnnrCalidad	.animate({ opacity: 0, top: -$bnnrCalidad.height(), right: ($bnnrCatalogos.width() + 16) },'fast');
		$bnnrCatalogos	.animate({ opacity: 0, top: 0, right: -$bnnrCatalogos.width() },'fast');
		$bnnrComprar	.animate({ opacity: 0, bottom: -$bnnrComprar.height(), right: 0 },'fast');
		if ($bnnrEmpresa.length==1) {
			$bnnrEmpresa		.animate({ opacity: 0, bottom: 0, right: $("#portada").width() },'fast');
		}
		if ($(this).attr("href")!="#") {
			location.href=$(this).attr("href");
		} else {
			alert('En construcción');
		}
		return false;
	})
	
	var rand	= false;
	var timeout	= 300;
	
	setTimeout(function () { $bnnrMujeres	.animate({ opacity: 1, left: 0 },'normal'); }, ((rand) ? Math.floor(Math.random()*1000) : timeout ));
	
	setTimeout(function () { $bnnrCalidad	.animate({ opacity: 1, top: 0 },'normal'); }, ((rand) ? Math.floor(Math.random()*1000) : timeout ));
	setTimeout(function () { $bnnrCatalogos	.animate({ opacity: 1, right: 0 },'normal'); }, ((rand) ? Math.floor(Math.random()*1000) : timeout ));
	setTimeout(function () { $bnnrComprar	.animate({ opacity: 1, bottom: 0 },'normal'); }, ((rand) ? Math.floor(Math.random()*1000) : timeout ));
	if ($bnnrEmpresa.length==1) {
		setTimeout(function () { $bnnrEmpresa	.animate({ opacity: 1, right: $bnnrComprar.width() + 19 },'normal'); }, ((rand) ? Math.floor(Math.random()*1000) : timeout ));
	}
	
}

function control_portada() {
	
	if ($(".mujeres").length==1) {
		// para principal mujeres
		var $col_left	= $(".col_left",$(".mujeres"));
		if ($col_left.length==1) {
			var $children_left	= $col_left.children();
			$children_left.each(function () {
				b_opts	= {
					top: -$(this).height(),
					position: 'absolute',
					width: $col_left.width()-12
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				
				$(this).css(b_opts);
			});
			var lcont	= 0;
			var ltop	= 0;
			$children_left.each(function () {
				lcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						top: ltop
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,lcont*250);
					ltop	= ltop + $el.height() + 12
				},lcont*50)
			});
		}
		
		var $col_right	= $(".col_right",$(".mujeres"));
		if ($col_right.length==1) {
			var $children_right	= $col_right.children();
			$children_right.each(function () {
				b_opts	= {
					left: $(this).width(),
					position: 'absolute'
					//width: $col_left.width()-12
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var rcont	= 0;
			var rleft	= 0;
			$children_right.each(function () {
				rcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						left: 0
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,rcont*250);
				},rcont*100+300)
			});
		}
		
		var $col_bottom	= $(".col_bottom",$(".mujeres"));
		
		if ($col_bottom.length==1) {
			var $children_bottom	= $col_bottom.children();
			$children_bottom.each(function () {
				b_opts	= {
					top: $col_bottom.height(),
					position: 'absolute'
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var bcont	= 0;
			var btop	= 0;
			$children_bottom.each(function () {
				bcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						top: btop
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,bcont*250);
					btop	= btop + $el.height()
				},bcont*100+500)
			});
		}
		// fin para principal mujeres
	}
	
	if ($(".empresa").length==1) {
		// para principal empresa
		var $col_left	= $(".col_left",$(".empresa"));
		if ($col_left.length==1) {
			var $children_left	= $col_left.children();
			$children_left.each(function () {
				b_opts	= {
					top: -$(this).height(),
					position: 'absolute',
					width: $col_left.width()-12
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				
				$(this).css(b_opts);
			});
			var lcont	= 0;
			var ltop	= 0;
			$children_left.each(function () {
				lcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						top: ltop
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,lcont*250);
					ltop	= ltop + $el.height() + 12
				},lcont*50)
			});
		}
		
		var $col_right	= $(".col_right",$(".empresa"));
		if ($col_right.length==1) {
			var $children_right	= $col_right.children();
			$children_right.each(function () {
				b_opts	= {
					left: $(this).width(),
					position: 'absolute'
					//width: $col_left.width()-12
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var rcont	= 0;
			var rleft	= 0;
			$children_right.each(function () {
				rcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						left: 0
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,rcont*250);
				},rcont*100+300)
			});
		}
		
		var $col_bottom	= $(".col_bottom",$(".empresa"));
		
		if ($col_bottom.length==1) {
			var $children_bottom	= $col_bottom.children();
			$children_bottom.each(function () {
				b_opts	= {
					top: $col_bottom.height(),
					position: 'absolute'
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var bcont	= 0;
			var btop	= 0;
			$children_bottom.each(function () {
				bcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						top: btop
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,bcont*250);
					btop	= btop + $el.height()
				},bcont*100+500)
			});
		}
		// fin para principal empresa
	}
	
	if ($(".calidad").length==1) {
		// para principal calidad
		var $col_top	= $(".col_top",$(".calidad"));
		
		if ($col_top.length==1) {
			var $children_top	= $col_top.children();
			$children_top.each(function () {
				b_opts	= {
					top: -$(this).height(),
					position: 'absolute'
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var bcont	= 0;
			var btop	= 0;
			$children_top.each(function () {
				bcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						top: btop
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,bcont*250);
					btop	= btop + ($el.height() + ((bcont==2) ? -40 : 0 )) 
				},bcont*50)
			});
		}
		
		var $col_left	= $(".col_left",$(".calidad"));
		if ($col_left.length==1) {
			var $children_left	= $col_left.children();
			$children_left.each(function () {
				b_opts	= {
					top: $col_left.height(),
					position: 'absolute',
					width: $col_left.width()-12
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var lcont	= 0;
			var ltop	= 0;
			$children_left.each(function () {
				lcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						top: ltop
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,lcont*250);
					ltop	= ltop + $el.height() + 12
				},lcont*100+100)
			});
		}
		
		var $col_right	= $(".col_right",$(".calidad"));
		if ($col_right.length==1) {
			var $children_right	= $col_right.children();
			$children_right.each(function () {
				b_opts	= {
					left: $(this).width() + 12
					//width: $col_left.width()-12
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var rcont	= 0;
			var rleft	= 0;
			$children_right.each(function () {
				rcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						left: 0
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,rcont*250);
				},rcont*100+100)
			});
		}
		
		// fin para principal calidad
	}
	
	
	
	if ($(".compra").length==1) {
		// para principal compra
		var $col_top	= $(".col_top",$(".compra"));
		
		/*alert($col_top.length)*/
		if ($col_top.length==1) {
			
			var $children_top	= $col_top.children();
			$children_top.each(function () {
				b_opts	= {
					top: -$(this).height()
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var bcont	= 0;
			var btop	= 0;
			$children_top.each(function () {
				bcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						top: btop
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,bcont*250);
					//btop	= btop + ($el.height() + ((bcont==2) ? -88 : 0 )) 
					btop	= 0
				},bcont*300)
			});
		}
		
		var $col_left	= $(".col_left",$(".compra"));
		if ($col_left.length==1) {
			var $children_left	= $col_left.children();
			$children_left.each(function () {
				b_opts	= {
					top: $col_left.height(),
					position: 'absolute',
					width: $col_left.width()-12
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var lcont	= 0;
			var ltop	= 0;
			$children_left.each(function () {
				lcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						top: ltop
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,lcont*250);
					ltop	= ltop + $el.height() + 12
				},lcont*100+100)
			});
		}
		
		var $col_right	= $(".col_right",$(".compra"));
		if ($col_right.length==1) {
			var $children_right	= $col_right.children();
			$children_right.each(function () {
				b_opts	= {
					left: $(this).width() + 12
					//width: $col_left.width()-12
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var rcont	= 0;
			var rleft	= 0;
			$children_right.each(function () {
				rcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						left: 0
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,rcont*250);
				},rcont*100+100)
			});
		}
		
		// fin para principal compra
	}
	
	if ($(".catalogos").length==1) {
		// para principal catalogos
		var $col_top	= $(".col_left",$(".catalogos"));
		
		if ($col_top.length==1) {
			
			var $children_top	= $col_top.children();
			$children_top.each(function () {
				b_opts	= {
					top: -$(this).height()
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var bcont	= 0;
			var btop	= 0;
			$children_top.each(function () {
				bcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						top: btop
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,bcont*250);
					btop	= btop + ($el.height() + ((bcont==2) ? -135 : 0 )) 
				},bcont*100)
			});
		}
		
		
		var $col_right	= $(".col_right",$(".catalogos"));
		if ($col_right.length==1) {
			var $children_right	= $col_right.children();
			$children_right.each(function () {
				b_opts	= {
					left: $(this).width(),
					position: 'absolute'
					//width: $col_left.width()-12
				};
				if (!$.browser.msie) {
					b_opts  = $.extend({}, b_opts, {opacity: 0});
				}
				$(this).css(b_opts);
			});
			var rcont	= 0;
			var rleft	= 0;
			$children_right.each(function () {
				rcont++;
				var $el	= $(this);
				setTimeout(function () {
					b_opts	= {
						left: 0
					};
					if (!$.browser.msie) {
						b_opts  = $.extend({}, b_opts, {opacity: 1});
					}
					$el.animate(b_opts,rcont*250);
				},rcont*100+300)
			});
		}
		// fin para principal catalogos
	}
	
	$("a.plus,a.aleft,a.aright,a.atop,a.abottom").each(function () {
		var $a	= $(this);
		$a.closest('.article,.article_img').css({
			cursor: 'pointer'
		}).click(function () {
			location.href	= $a.attr('href');
		});
	});
}


function control_slider (id, wait, step) {
	var $slider	= $("#" + id);
	$slider.css({
		'z-index'	: 1,
		width		: 690,
		position	: 'absolute'
	});
	$("img",$slider).css({
		position: 'absolute'
	});
	if ($("img",$slider).length>1) {
		$("img:gt(0)",$slider).css({ display: 'none' });
		setTimeout(function () {
			setInterval(function () {
				$("img:eq(0)",$slider).appendTo($slider).fadeOut('slow');
				$("img:eq(0)",$slider).fadeIn('slow');
			},step)
			
		},wait)
	}
}


function control_filtro() {
	if ($("#filtro").length==1) {
		$("#filtroSup a").click(function () {
			if (!$("#div_" + $(this).attr("id")).is(":visible")) {
				$("#filtroInf div").slideUp();
				$("#div_" + $(this).attr("id")).slideDown();
			} else {
				$("#div_" + $(this).attr("id")).slideUp();
			}
			return false;
		});
	}
}

function envia_pag(pax) {
	document.acciones.pagina.value = pax;
	document.acciones.submit();
}

function ver_todos (regs) {
	document.acciones.pagina.value = 1;
	document.acciones.registrosporpagina.value = regs;	
	document.acciones.submit();
}





function creaAjax(){

	var objetoAjax=false;
	try {

	/*Para navegadores distintos a internet explorer*/
	objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
	
	} catch (e) {

		try {
		/*Para explorer*/
		objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E) {
		objetoAjax = false;
		}
	}

	if (!objetoAjax && typeof XMLHttpRequest!='undefined') {
		objetoAjax = new XMLHttpRequest();
	}

	return objetoAjax;
	
}

function FAjax (url,capa,valores,metodo,cargando,processScripts)
{
	//alert(url);
var ajax=creaAjax();
var capaContenedora = document.getElementById(capa);
/*Creamos y ejecutamos la instancia si el metodo elegido es POST*/
if(metodo.toUpperCase()=='POST'){
         ajax.open ('POST', url, true);
         ajax.onreadystatechange = function() {
         if (ajax.readyState==1) {
			 if (cargando=='')
			 	{ capaContenedora.innerHTML="Cargando......."; }
				else
			 	{ capaContenedora.innerHTML=cargando; }         }
         else if (ajax.readyState==4){
                   if(ajax.status==200)
                   {
					   	if(processScripts)
							{ SetContainerHTML(capa, ajax.responseText, true); }
						else
							{ document.getElementById(capa).innerHTML=ajax.responseText; }
 					  
                  } else if(ajax.status==404) {
					   capaContenedora.innerHTML = "La direccion no existe";
                   } else {
                        capaContenedora.innerHTML = "Error: "+ajax.status;
                   }
         }
}
ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
ajax.send(valores);
return;
}
}

function SetContainerHTML(id,html,processScripts)
{
	mydiv = document.getElementById(id);
	mydiv.innerHTML = html;
	if(processScripts!=false)
	{
	var elementos = mydiv.getElementsByTagName('script');
	for(i=0;i<elementos.length;i++) {
	var elemento = elementos[i];
	nuevoScript = document.createElement('script');
	nuevoScript.text = elemento.innerHTML;
	nuevoScript.type = 'text/javascript';
	if(elemento.src!=null && elemento.src.length>0)
	nuevoScript.src = elemento.src;
	elemento.parentNode.replaceChild(nuevoScript,elemento);
	}
	}
}
