/* 	JS Menu Progressive Enhancement.
----------------------------------------------------------------------------------------------------*/
var block;
function generateSprites(parent, selectedPrefix, setActive, hoverSpeed, style) {
	// throw the parent object's class into a variable
	var parentClass = $(parent).attr("class");

	// start a loop that cycles through each of the li elements inside the parent element
	$(parent).children("li").each(function() {
		// create a few variables that we'll need during this function:
		// myClass = the class of the object we're currently inspecting
		// current = what the selected class should look like for the parent of the object we're currently inspecting
		var myClass = ($(this).attr("class"))
		var current = parent.substring(1) + " current-" + ($(this).attr("class"));

		// turn on nav events for element this loop identifies
		attachNavEvents(parent, myClass, setActive, hoverSpeed, style);
	
		// let's hide the CSS-defined background image, but only if this isn't the currently-selected item
		if (parentClass != current) {
			$(this).children("a").css({backgroundImage:"none"});
		}

	});
}


function attachNavEvents(parent, myClass, setActive, hoverSpeed, style) {
	$(parent + " ." + myClass).mouseover(function() {
		// create pseudo-link
		$(this).append('<div class="nav-' + myClass + '"></div>');
		// either slide or fade, depending on the style value
		if (style == "slide") {
			// slide down the pseudo-link
			$("div.nav-" + myClass).css({display:"none"}).slideDown(hoverSpeed);
		} else {
			// fade in the pseudo-link
			$("div.nav-" + myClass).css({display:"none"}).fadeIn(hoverSpeed);
		}
	}).mouseout(function() {
		// either slide or fade, depending on the style value
		if (style == "slide") {
			// slide up & destroy pseudo-link
			$("div.nav-" + myClass).slideUp(hoverSpeed, function() {
				$(this).remove();
			});
		} else {
			// fade out & destroy pseudo-link
			$("div.nav-" + myClass).fadeOut(hoverSpeed, function() {
				$(this).remove();
			});
		}
	});


	// we only want to check the mousedown/up events if the CSS exists for :active states
	// if so, let's apply our selective filtering to undo the events above
	if (setActive) {
		$(parent + " ." + myClass).mousedown(function() {
			$("div.nav-" + myClass).attr("class", "nav-" + myClass + "-click");
		}).mouseup(function() {
			$("div.nav-" + myClass + "-click").attr("class", "nav-" + myClass);
		});
	}
}

$(document).ready(function() {

	generateSprites(".nav", "current-", true, 150, "fade");

});


/*  Home Slider.
----------------------------------------------------------------------------------------------------*/
$(document).ready(function() {
						   
	if (!Boolean($(this).cycle)) {
		return;
	}
							   
	// Add rounded corners.
	$("#slider-wrapper").append('<div class="slider-corner top-left"></div>');
	$("#slider-wrapper").append('<div class="slider-corner top-right"></div>');
	$("#slider-wrapper").append('<div class="slider-corner bottom-left"></div>');
	$("#slider-wrapper").append('<div class="slider-corner bottom-right"></div>');
	
	// Add next and prev arrows.
	$("#slider-wrapper").prepend('<div class="nivo-directionNav" style="display: none"><a class="nivo-prevNav">Anterior</a></div>');
	$("#slider-wrapper").prepend('<div class="nivo-directionNav" style="display: none"><a class="nivo-nextNav">Anterior</a></div>');
	
	// Hide/Show arrows.
	$("#slider-wrapper").hover(
		function() {
			$(".nivo-directionNav").show();
		},
		function() {
			$(".nivo-directionNav").hide();
		}
	);
	
	// Cycle Slider.
	$('#slider').cycle({
		timeout: 7500,
		speed: 750,
		pause: true,
		prev: ".nivo-prevNav",
		next: ".nivo-nextNav"
	});
	
});


/*  Add container's and form's top and bottom.
----------------------------------------------------------------------------------------------------*/
$(document).ready(function() {
	
	$("#container").prepend('<div class="container-top"></div>').append('<div class="container-bottom"></div>');
	$("#cform").append('<div class="cform-top"></div>').append('<div class="cform-bottom"></div>');
						   
});

/*  Pretify blackbar.
----------------------------------------------------------------------------------------------------*/
$(document).ready(function() {
	
	$(".blackbar").wrapInner('<div class="blackbar-left">');
						   
});

/*  Equal height for product boxes.
----------------------------------------------------------------------------------------------------*/
$(window).load(function() {
	
	$(".product").equalHeights();
	
	$(".product").each( function() {
		var offset = $(this).height() - ($(this).find(".listing-bottom").position().top + $(this).find(".listing-bottom").outerHeight());
		$(this).find(".description").css({
			height: function(index, value) {
				return ($(this).height() + offset);
			}
		});
	});
	
	/* Resize container */
	var total = $("#listing").children(".product").length;
	var rows = Math.ceil(total / 3);
	
	var height = $("#listing").children(".product:last").outerHeight() + parseInt($("#listing").children(".product:last").css("marginBottom"));
	$("#listing").css({
		paddingBottom: "0",
		height: function(index, value) { return (height * rows); }
	}); 
	
});

/*  Pagination.
----------------------------------------------------------------------------------------------------*/
$(document).ready(function() {
	
	$("#pagination .active").addClass("rounded").wrapInner('<span class="left"></span>');
	
});

/*  Pretify cform for IE6.
----------------------------------------------------------------------------------------------------*/
$(document).ready(function() {
	
	$("#cform input[type=text]").addClass("edit");
						   
});

/* 	Dropdowns.
----------------------------------------------------------------------------------------------------*/
var showDropdown = function(target) {
	if (block) {
	return false;	
	}
	target.children(".dropdown-close").click(function (e) {
		e.preventDefault();
		hideDropdown($(this).parent("div"));
	});
	if (target.css("display") == "none") {
		target.find("#submit").hide();
		target.slideDown(200, function() {
			target.find("#submit").fadeIn(200);
		});
	}
}

var hideDropdown = function(target) {
	target.parents("li").find(".trigger a").removeClass("pressed");
	if (target.css("display") != "none") {
		target.find("#submit").hide();
		target.slideUp(200);
	}
}

$(document).ready(function() {
						   
	// Widen elemsents with more than one list.
	$(".dropdown").each(function() {
		if ($(this).find("ul").length > 1) {
			$(this).addClass("double-width");	
		}
	});
	
	// Remove separator from las item on the list.
	$(".dropdown li:last").addClass("borderless");
	
	// Click events.
	$(".dropdown-menu .trigger a").click(function (e) {
		e.preventDefault();
		$(this).addClass("pressed");
		// Hide other dropdowns.
		$(".dropdown:visible").each(function() {
			$(this).css({'z-index': 10});
			hideDropdown($(this));							 
		});
		$(this).parents("li").find(".dropdown").css({'z-index': 20});
		showDropdown($(this).parents("li").find(".dropdown"));
	});
	
	// Check / uncheck all.
	$(".dropdown form .all").change(function() {
		if ($(this).attr("checked")) {
			$(this).parents("form").find("input:checkbox").attr("checked", "checked");
		} else {
			$(this).parents("form").find("input:checkbox").attr("checked", "");
		}								
	});
	
	// On form change.
	$(".dropdown form").change(function() {
		// find if ANY box is unckeked.
		var boxes 	= $(this).find("input:checkbox:not(.all)").length;
		var checked = $(this).find("input:checkbox:not(.all):checked").length;
		$(this).find("input:checkbox.all").attr("checked", (boxes == checked) ? "checked" : "");						
	});
	
});
$(function() {
	$('a[rel=ImageUP]').lightBox();
});

function NewValues() {
	var send;
	var sexo;
i=1; 

while(i<=totalLineas) {
	if ($('#products-'+i).attr('checked')) {
		send += $('#products-'+i).val()+",";
	}
	i++;
}
i=1;
while(i<=3) {
		if ($('#sexo-'+i).attr('checked')) {
		sexo += $('#sexo-'+i).val()+",";
	}
	i++;
}
block = true;
$.get("categorias.php?lineas="+send+"&sexo="+sexo,false,function(responseText){
											 
				$("#otrosids").html(responseText);
				block = false;
			});	
	
}

function AddProduct(id,num) {
	if ($('#quote-'+num).attr('checked')) {
     $.get("add.php?id="+id+"&quantity="+$('#quantity-'+num).val());
} else {
	$.get("del.php?id="+id);
}
	
	
	
}
