$(document).ready(function() {					   

    //show_pic
	$(".show_pic").livequery('click', function() {
		$("#main_img").attr('src', $(this).attr('alt'));
	});
	
	//gal prod
	var $width = $("#gallery_prod ul li").length * $("#gallery_prod ul li").width();
	var $curr = 1;
	var $length = $("#gallery_prod ul li").length;
	$("#gallery_prod ul").width($width);
	$("#next").click(function(){
			//$("#gallery_prod ul li").eq($length-1).clone().appendTo($("#gallery_prod ul"));
			$("#gallery_prod ul li").eq($length-1).clone().prependTo($("#gallery_prod ul"));
			$width = $("#gallery_prod ul li").length * $("#gallery_prod ul li").width();
			$("#gallery_prod ul").width($width);
		return false;
	});
	$("#prev").click(function(){
			$("#gallery_prod ul li").eq($length-1).clone().prependTo($("#gallery_prod ul"));
			//$("#gallery_prod ul").css("left", "-356px");
			$width = $("#gallery_prod ul li").length * $("#gallery_prod ul li").width();
			$("#gallery_prod ul").width($width);
		return false;		
	});
	
	//end gal prod

	//buy
	$(".buy").livequery('click', function() {
		if($("#color").val()=='') {
			alert('Seleccione el color');
			$("#color").focus();
			return false;
		}
		if($("#size").val()=='') {
			alert('Seleccione el tamaño');
			$("#size").focus();
			return false;
		}
		if(isNaN($("#quantity").val())) {
			alert('Ingrese un número en cantidad');
			$("#quantity").focus();
			return false;
		}
		window.location.href='/add_product/?id_product=' + $("#id_product").val() + '&color=' + $("#color").val() + '&size=' + $("#size").val() + '&quantity=' + $("#quantity").val();
	});

	//logout
	$("#logout").click(function() {
		if(window.confirm("Esta seguro que desea cerrar la sesión?")) {
			self.location.href = $(this).attr("href");
		}
		return false;
	});
				
	
	$(".validatefrm").submit(function() {
		var frm = $(this);
		var errors = 0;
		frm.find("input").each(function(i) {
			var el = $(this);
			if(el.attr("req")=="true") {
				if(el.attr("type")=="text" || el.attr("type")=="password") {
					if($.trim(el.val())=="") {
						alert(el.attr("title"));
						el.focus();
						errors = 1;
						return false;
					}
				}
			}
		});
		if(errors==1) {
			return false;
		}
		frm.find("select").each(function(i) {
			var el = $(this);
			if(el.attr("req")=="true") {
				if($.trim(el.val())=="0") {
					alert(el.attr("title"));
					el.focus();
					errors = 1;
					return false;
				}
			}
		});
		frm.find("textarea").each(function(i) {
			var el = $(this);
			if(el.attr("req")=="true") {
				if($.trim(el.val())=="") {
					alert(el.attr("title"));
					el.focus();
					errors = 1;
					return false;
				}
			}
		});		
		if(errors==1) {
			return false;
		}
	});
	


	var margen = (($("#page_wrapper").width() - $("#slider-wrapper").width()) / 2) * -1 - 1;
	$("#slider-wrapper").width($("#page_wrapper").width()).css("margin-left", margen);
	var items = $("#scroll-content li").length;
	$("#scroll-content").width(items*370);
	
	var scrollPane = $("#scroll-pane"),
		scrollContent = $("#scroll-content");
		
	$("#scroll-bar").slider({
		slide: function( event, ui ) {
				scrollContent.css( "margin-left", Math.round(
					ui.value / 100 * ( 0 - scrollContent.width() + $("#page_wrapper").width() -10 )
				) + "px" );
		}
	});


	$(function() {
		setInterval( "slideSwitch()", 4000 );
	});
		
});


function slideSwitch() {
    var $active = $('#gallery IMG.active');

    if ( $active.length == 0 ) $active = $('#gallery IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#gallery IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });

	
}

