document.getElementsByTagName('html')[0].className = "js";
$(document).ready(function() {
	cuentaSlide = -1;
	if($("body").attr("id") == "coleccion"){
		createSlideHome();
		intervaloSlide = setInterval(changeImgTimer,3000);	
	}
	setEvents();
	evalPos();	
	setSlides();

	if($("body").attr("id") == "filosofia") setClass();
});

$(document).ajaxStop(function() {
    cuentaSlide = -1;
	if($("body").attr("id") == "coleccion"){
		createSlideHome();
		intervaloSlide = setInterval(changeImgTimer,3000);	
	}
	setEvents();
	evalPos();	
	setSlides();

	if($("body").attr("id") == "filosofia") setClass();
});

$(window).load(function() {
	setSlides();
	var theWindow = $(window),
    $bg = $("#news-bg"),
    aspectRatio = $bg.width() / $bg.height();
	function resizeBg() {
		var wWidth = $(window).width();
		var wHeight = $(window).height();
    	if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
                    $bg.removeClass().addClass('bgheight');
					
                } else {
                    $bg.removeClass().addClass('bgwidth');
                }
        }
        theWindow.resize(function() {
                resizeBg();
        }).trigger("resize");
});

$(window).ajaxStop(function() {
    setSlides();
	var theWindow = $(window),
    $bg = $("#news-bg"),
    aspectRatio = $bg.width() / $bg.height();
	function resizeBg() {
		var wWidth = $(window).width();
		var wHeight = $(window).height();
    	if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
                    $bg.removeClass().addClass('bgheight');
					
                } else {
                    $bg.removeClass().addClass('bgwidth');
                }
        }
        theWindow.resize(function() {
                resizeBg();
        }).trigger("resize");
});

function evalPos(){
	/*$('#product-list a').bind('click', function(event) {
		window.location = $(this).attr("href");		
		window.location.reload(true);		
	});*/
	var urlP = document.URL;
	
	var catActiva = urlP.substring(urlP.indexOf("#")+1 ,urlP.length);
	var itemActivo = $("#" + catActiva);
	if(itemActivo.index() > 0){
	 cuentaSlide = itemActivo.index() -1;
	}
//	cuentaSlide  = $("#" + catActiva).index() -2;
	$("#product-list li").removeClass("active");
	if(urlP.indexOf("#") < 0){
		$("#prev-shoe").addClass("inactive");
		$("#product-list li:eq(0)").addClass("active");
		return;
	}
	itemActivo.addClass("active");
	
	var position = itemActivo.position();
	var posLeft = Math.round(position.left);
	var posLimit = $('#product-list').width() - $('#wrap-list').width();
	if(posLeft > posLimit){
		posLeft = posLimit;
		$("#next-shoe").addClass("inactive");
	}
	$("#product-list").css("marginLeft", -( posLeft));
}

function setEvents(){
	$('#area-link a').bind('click', function(event) {
		$('#access').toggle();
		cancelEvent(event);
	});
	$('#access').hover(function() {
		
	}, function() {
		$(this).hide();
		$('#registro').hide();
	});
	$("#reg-bot").bind('click', function(event) {
		$("#pass-form").hide();
		$("#login-form").hide();	
		$('#registro').toggle();
		
		cancelEvent(event);
	});
	$("#login-bot").bind('click', function(event) {
		$("#registro").hide();
		$("#pass-form").hide();
		
		$('#login-form').toggle();
		cancelEvent(event);
	});
	$('#remind-pass').bind('click', function(event) {
		$('#login-form').toggle();
		$('#pass-form').toggle();
		cancelEvent(event);
		
	});
	$('#prev-preview a').bind('click', function(event) {
		slidePreview(-1);
		cancelEvent(event);
	});
	$('#next-preview a').bind('click', function(event) {
		slidePreview(1);		
		cancelEvent(event);
	});
	$('#prev-color a').bind('click', function(event) {
		moveColor(-1);
		cancelEvent(event);
	});
	$('#next-color a').bind('click', function(event) {
		moveColor(+1);
		cancelEvent(event);
	});
	$('#close-modal a').bind('click', function(event) {
		closeModal();
		cancelEvent(event);
	});
	$("#pedido-bot a").bind('click', function(event) {
		$('#tabla-pedido').show();
		cancelEvent(event);
	});
	$('#cerrar-pedido a').bind('click', function(event) {
		cancelEvent(event);
		$('#tabla-pedido').hide();
	});
	$("#shoes-list a").bind('click', function(event) {
		var n = $(this).parent().index();
		slideShoe(n);
		cancelEvent(event);
	});
	$('#product-preview a').bind('click', function(event) {
		var n = $(this).index();
		launchModal(n,true);
		cancelEvent(event);
	});
	$("#prev-modal a").bind('click', function(event) {
		var n = $("#modal-img>div:visible").index() -1;
		launchModal(n);
		cancelEvent(event);
	});
	
	$("#next-modal a").bind('click', function(event) {
		var n = $("#modal-img>div:visible").index() +1;
		launchModal(n);
		cancelEvent(event);
	});
	
	$('#prev-news a').bind('click', function(event) {
		moveNews(-1);
		cancelEvent(event);
	});
	$("#modal-detalles li li a").bind('click', function(event) {
		var n = $(this).parent().index();
		$(this).parent().siblings().removeClass("active");
		$(this).parent().addClass("active");		
		$("#modal-img>div:visible img").fadeOut(300,function(){
		$("#modal-img>div:visible img:eq(" + n + ")").delay(400).fadeIn(300);		
		});

		cancelEvent(event);		
	});	
	$('#next-news a').bind('click', function(event) {
		moveNews(+1);
		cancelEvent(event);
	});
	$("#locate-tiendas .prev a").bind('click', function(event) {
		moveProv(-1,"#wrap-tiendas ul", "#locate-tiendas");
		cancelEvent(event);
	});
	$("#locate-tiendas .next a").bind('click', function(event) {
		moveProv(+1,"#wrap-tiendas ul", "#locate-tiendas");
		cancelEvent(event);
	});
	$("#locate-rep .prev a").bind('click', function(event) {
		moveProv(-1,"#wrap-rep ul", "#locate-rep");
		cancelEvent(event);
	});
	$("#locate-rep .next a").bind('click', function(event) {
		moveProv(+1,"#wrap-rep ul", "#locate-rep");
		cancelEvent(event);
	});
	$('#wrap-rep li a').bind('click', function(event) {
		var n = $(this).parent().index();
		var wrap = "#wrap-list-rep";
		viewProvider(n,wrap,"#list-rep");
		cancelEvent(event);
	});
	
	$('#wrap-tiendas li a').bind('click', function(event) {
		var n = $(this).parent().index();		
		var wrap = "#wrap-list-tiendas";
		viewProvider(n,wrap,"#list-tiendas");
		cancelEvent(event);
	});
	$("#next-shoe a").bind("click", function(event) {
		if($("#next-shoe").hasClass("inactive")){
			cancelEvent(event);			
			return;
		}
		moveSlide(+1);
		cancelEvent(event);
	});
	$("#prev-shoe a").bind('click', function(event) {
		if($("#prev-shoe").hasClass("inactive")){
			cancelEvent(event);			
			return;
		}
		moveSlide(-1);		
		cancelEvent(event);
	});
	viewProvider(0,"#wrap-list-tiendas","#list-tiendas")
	viewProduct(0);
	$("#modal-ficha").hide();
}

function setClass(){
	var nClass= Math.round(Math.random()*2) +1;
	$("body").addClass("filo" + nClass);
}

function createSlideHome(){
	$("#reel img").hide();
	$("#reel img:first-child").show();	
}

function changeImgTimer(){
	var nextId = $("#reel img:visible").index() +1;
	if(nextId == $("#reel img").length  || nextId == "") nextId = 0;
	changeImg(nextId);
}

function changeImg(id){
	$("#reel img:visible").fadeOut(400);
	$("#reel img:eq(" + id + ")").fadeIn(400);	
}

function slidePreview(dir){
	var pos = dir * $("#wrap-preview>div:first-child").innerWidth();
	if(	$("#wrap-preview").is(":animated")) return;
	$("#wrap-preview").animate({"marginLeft": '-='+pos}, 500,function(){
		var ml = $(this).css("marginLeft");
		var ml = Number(ml.substr(0,ml.length -2));
		if(ml < 0){
			 $("#prev-preview").show();
		}else{
			 $("#prev-preview").hide();	
		}
		if(ml < -($("#wrap-preview").width() - $("#wrap-preview>div:first-child").width()*2) ){
			 $("#next-preview").hide();		
		}else{
			 $("#next-preview").show();		
		}
	});
}
function launchModal(pos,start){
	
    if(start && $("#modal-ficha").html()==''){
        $.ajax({
            type: "POST",
            async: false,
            url: _base_url_lang+"coleccion/ajax_load_modal",
            data: "id_categoria="+$("#id_categoria").val(),
            success: function(msg){
                $("#modal-ficha").html(msg);
            }
        });   
    }
    
    var cant = 	$("#modal-img>div").length;
	if(pos >= cant) pos = 0;
	if(pos < 0) pos = cant -1;
	$("#modal-img>div").hide();
	$("#modal-img>div:eq("+pos+")").fadeIn();
	$("#titulo-modal>li, #modal-detalles>li").hide();			
	$("#titulo-modal>li:eq("+pos+")").fadeIn();		
	$("#modal-detalles>li:eq("+pos+")").fadeIn();		
	$("#modal-detalles>li:eq("+pos+") li:eq(1)").removeClass("active");	
	$("#modal-detalles>li:eq("+pos+") li:eq(0)").addClass("active");	
	$("#modal-img>div:eq("+pos+") img").hide();		
	$("#modal-img>div:eq("+pos+") img:eq(0)").fadeIn();	
	if(start){
		$("#modal-info").hide();
		$("#modal-ficha").fadeIn(300);
		$("#modal-info").delay(500).fadeIn();	
		$(document).keyup(function(e) {
			if (e.keyCode == 13 || e.keyCode == 27) {
				closeModal();
			}
			if(e.keyCode == 37){
				if($("#modal-img img").is(":animated")){
					return;
				}
				var n = $("#modal-img>div:visible").index() -1;
				launchModal(n);
				
			
			}
			if(e.keyCode == 39){
				if($("#modal-img img").is(":animated")){
					return;
				}
				var n = $("#modal-img>div:visible").index() +1;
				launchModal(n);
				
			}
		});
		$("#modal-bg").bind('click', function(event) {
			closeModal();
		  cancelEvent();
		});
	}

	$("#modal-info nav li").removeClass("active");	
	$("#modal-info nav li:eq(" + pos + ")").addClass("active");
	if($("#imgloaded" + pos).length){
		$("#imgloaded" + pos).stop().delay(300).fadeIn(300);
		return;
	}
}

function slideShoe(n){

	if($("#product-preview a").is(":animated")) return;
	var prevActivo = $("#shoes-list ul li.active").index();	
	var itemActivo = $("#product-preview a:eq("+n + ")");
	var w = $(window).width() - $("#wrap").width() + $("#wrap").width() ; 
	var posFinal = 500;
	if(n < prevActivo){
		itemActivo.show();
		itemActivo.css("left",-w);	
		itemActivo.animate({left: posFinal}, 700);	

		$("#product-preview a:eq("+ prevActivo + ")").animate({left: w}, 700,function(){$(this).hide()});
	}
	if(n > prevActivo){
		itemActivo.show();
		itemActivo.css("left",w);
		itemActivo.animate({left: posFinal}, 700);					
		$("#product-preview a:eq("+ prevActivo + ")").css("left",500);		
		$("#product-preview a:eq("+ prevActivo + ")").animate({left: -w}, 700,function(){$(this).hide()});

	}
	if (n == prevActivo ) return;
	$("#shoes-list ul li").removeClass("active");
	$("#shoes-list ul li:eq("+ n + ")").addClass("active");
	var imgVisible = $("#product-preview a:visible");
	
}

function viewProvider(n,wrap,listado){
	
	$("#wrap-tiendas li, #wrap-rep li").removeClass("active");
	$("li:eq("+n+")", wrap).addClass("active");
	$(listado).show();
	$(">li" , listado).hide();	
	$(">li:eq("+n+")" , listado).show();
	if(wrap == "#wrap-list-tiendas") {
		$("#wrap-list-rep").hide();
		$("#wrap-list-tiendas").show();				
		$("#wrap-list-tiendas").jScrollPane();		
	}
	if(wrap == "#wrap-list-rep") {
		$("#wrap-list-rep").show();		
		$("#wrap-list-rep").jScrollPane();				
		$("#wrap-list-tiendas").hide();		
	}

}

function moveNews(n){
	var h = $("#news-wrap").height();
	var newsWrap = $("#news-wrap");
	if($(newsWrap).is(":animated")) return;
	var speed = 500;
	var marginTop = $(newsWrap).css("marginTop").length;
	var mTop = Number($(newsWrap).css("marginTop").substr(0,marginTop -2));
	var idN = 200 * n;
	var pos = mTop - idN;
	$("#prev-news, #next-news").removeClass("inactive");		
	if(pos >= 0){
		 pos = 0;
		$("#prev-news").addClass("inactive");
	}
	var posLimit = $(newsWrap).height() - $("#list-news").height();
	if(pos < - posLimit){
		pos = - posLimit;
		$("#next-news").addClass("inactive");
	}
 	$(newsWrap).animate({marginTop: pos}, speed, function() {});	
}

function moveProv(n,item,wrap){
	if($(item).is(":animated")) return;
	var speed = 300;
	var marginTop = $(item).css("marginTop").length;
	var mTop = Number($(item).css("marginTop").substr(0,marginTop -2));
	var idN = $("li:first-child",item).height() * n;
	var pos = mTop - idN;
	$(".prev", wrap).removeClass("inactive");		
	$(".next", wrap).removeClass("inactive");			
	if(pos >= 0){
		pos = 0;
		$(".prev", wrap).addClass("inactive");
	}
	var posLimit = $(item).height() - $(item).parent().height();

	if(pos <= - posLimit){
		pos = - posLimit;
		$(".next",wrap).addClass("inactive");
	}	

 	$(item).animate({marginTop: pos}, speed, function() {
	});
}

function moveColor(n){
	var liVisible = $("#shoes-list>li:visible")
	if($(liVisible).is(":animated")) return;
	var speed = 500;
	var marginLength = $(liVisible).css("marginLeft").length;
	var mLeft = Number($(liVisible).css("marginLeft").substr(0,marginLength -2));
	var idN = 100 * n;
	var pos = mLeft - idN;
	$("#prev-color, #next-color").removeClass("inactive");		
	if(pos > 0){
		 pos = 0;
		$("#prev-color").addClass("inactive");
	}
	var posLimit = $(liVisible).width() - $("#shoes-list").width();
	if(pos < - posLimit){
		pos = - posLimit;
		$("#next-color").addClass("inactive");
	}
 	$(liVisible).animate({marginLeft: pos}, speed, function() {}); 
}

function closeModal(){
	$("#modal-ficha").fadeOut(300);	
	$("#modal-info").delay(300).fadeOut(300,function(){
		$("#modal-info").hide();
		$("#modal-img img").hide();
	});	
	$(document).unbind("keyup");
}

function viewProduct(n){
	var cantZapatos = 	$("#shoes-list>li:eq(" + n + ") li").length;
	if(cantZapatos <= 6){
		$('#color-bots').hide();
	}else{
		$('#color-bots').show();
	}
}

function setSlides(){
	//slides de zapatos
	var cant = $('#shoes-list li li').length;
	var w = new Number();
	for(var i=0; i<cant; i++){
		w += $('#shoes-list li li:eq(' + i + ')').innerWidth();
	}
	$('#shoes-list li ul').width(w);
	//slides producto en zona profesional
	var cantPreview = $("#wrap-preview>div").length;
	if(cantPreview < 2){
		$('#prev-preview').hide();
		$('#next-preview').hide();		
	}	
	var wPrev = new Number();
	for(var s=0; s<cant; s++){
		cantPreview += $('#wrap-preview>div:eq(' + s + ')').innerWidth();
	}
	$('#wrap-preview').width(cantPreview);
	var wCont = 0;
	var ulCont = 0;	
	var listProductos = $("#product-list");
	listProductos.idActivo = 3;
	listProductos.cant = $("#product-list li").length;	
	var tempMargin = 6;
	for(var i=0; i<4; i++){
		var tempWidth = $("#product-list li:eq("+i+")").width();
		if(tempWidth) var tempMargin = Number($("#product-list li:eq("+i+")").css("marginLeft").substr(0,1));
		wCont += tempWidth + tempMargin;
	}
	$("#wrap-list").width(wCont);	
	for(var s=0; s<listProductos.cant; s++){
		var liWidth = $("#product-list li:eq("+s+")").width();
		if(liWidth) var liMargin = Number($("#product-list li:eq("+s+")").css("marginLeft").substr(0,1));
		ulCont += liWidth + liMargin ;
	}
	$("#product-list").width(ulCont +10);		
	var cuentaSlide = -1;

}


function moveSlide(n){
		if($("#product-list").is(":animated")) return;	
		if(n == 1) cuentaSlide += n;
		var pos = $("#product-list li:eq(" + cuentaSlide + ")").width();
		pos *= n;
	 	$("#product-list").animate({"marginLeft": '-=' +pos}, 500, function() {
			var ml = $(this).css("marginLeft");
			var ml = Number(ml.substr(0,ml.length -2));
			if(ml >= 0){
				$("#prev-shoe").addClass("inactive");
			}else{
				$("#prev-shoe").removeClass("inactive");				
			}
			var posLeft = Math.abs(ml);
			var posLimit = $('#product-list').width() - $('#wrap-list').width();
			if(posLeft > posLimit){
				$("#next-shoe").addClass("inactive");					
			}else{
				$("#next-shoe").removeClass("inactive");									
			}
		}); 
		if(n == -1) cuentaSlide += n;				
	}
/*
var posLeft = Math.abs(ml);
var posLimit = $('#product-list').width() - $('#wrap-list').width();
if(posLeft > posLimit){
	posLeft = posLimit;
	$("#next-shoe").addClass("inactive");
}
*/
function cancelEvent(evento){
		if (evento && evento.preventDefault)evento.preventDefault();
		return false; 		
}

