// Venca
// KV - 26.11.2008
//
// Libreria basica de funciones para la dinamica de interfaz.
// Hace uso del framework "jquery-1.2.6.min.js" y de las siguentes librerias:
// - "jquery.scrollable-0.13.min.js"


// Evitar conflictos con Prototype y otros frameworks
jQuery.noConflict();

// Temporizadores y funciones para gestion de capas
var t1, t2, t3, t4 = 0;
function closeMainNav() {
	jQuery("#main_nav > li").removeClass("active");
	jQuery("#main_nav > li > ul").hide();
	clearTimeout(t1);
}

jQuery(document).ready(function(){
	reInitJQuery();
});

function reInitJQuery() {
    // Ocultacion de elementos clase "js_hide"
    jQuery(".js_hide").hide();

    // Gestion de la navegacion principal
    jQuery("#main_nav > li").bind("mouseenter",
		function(event) {
		    closeMainNav();	
		    jQuery(this).addClass("active");
		    //var w = 155 * jQuery(this).children().filter("ul").children().children().filter("ul").length;
		    var w = 150 * jQuery(this).children("ul").children().children("ul").length + 12 * jQuery(this).children("ul").children("li.publi").children("ul").length + 12;

		    jQuery(this).children().filter("ul").css("width", w);
		    var position = jQuery(this).position();
		    var l = position.left;
		    if (jQuery("#general").hasClass("home")) {  // Home
		        if (jQuery("#main_nav").hasClass("B")) {  // Home B
		            if (l + w > 870) {
		                l = 860 - w;
		            }
		        } else {  // Home A
		            if (l + w > 865) { //650
		                l = 866 - w; //651
		            }
		        }
		    } else {  // Interior
		        if (l + w > 1000) {
		            l = 935 - w;
		        }
		    }
		    jQuery(this).children().filter("ul").css("left", l);
		    jQuery(this).children().filter("ul").show();
		}
	).bind("mouseleave",
		function(event) {
		    //t1 = setTimeout("closeMainNav()", 500);
		}
	);

    // Gestion de la navegacion principal
    jQuery("#main_nav").bind("mouseleave",
		function(event) {
		    t1 = setTimeout("closeMainNav()", 500);
		}
	);


    // Gestion de la navegacion del pie de pagina
    jQuery("#footer_nav > li").hover(
		function(event) {
		    jQuery(this).addClass("active");
		    jQuery(this).children().filter("ul").show();
		},
		function(event) {
		    jQuery(this).removeClass("active");
		    jQuery(this).children().filter("ul").hide();
		}
	);

    // Pestanas con submenu para Especiales
    jQuery(".tabs_espe > li").hover(
		function(event) {
		    if (jQuery(this).children('a').hasClass('consub')) {
		        jQuery(this).children('a').addClass('consub_hover');
		    }
		    jQuery(this).children().filter("ul").show();
		},
		function(event) {
		    if (jQuery(this).children('a').hasClass('consub')) {
		        jQuery(this).children('a').removeClass('consub_hover');
		    }
		    jQuery(this).children().filter("ul").hide();
		}
	);

    // Pestanas
    jQuery(".tabs ~ .med").hide();
    jQuery(".tabs + .med").show();
    jQuery(".tabs li a").click(function(e) {
        e.preventDefault();
        jQuery(this).parent().siblings().removeClass("active");
        jQuery(this).parent().addClass("active");
        var active = jQuery(this).attr("href");
        jQuery(this).parent().parent().siblings().filter(".med").hide();
        jQuery(active).show();
    });

    // Pestanas en Ficha de Producto
    jQuery(".tabs02 ~ .med .tabs02content").hide();
    jQuery(".tabs02 ~ .med .tabs02content:first").show();
    jQuery(".tabs02 li a").click(function(e) {
        e.preventDefault();
        jQuery(this).parent().siblings().removeClass("active");
        jQuery(this).parent().addClass("active");
        var active = jQuery(this).attr("href");
        jQuery(this).parent().parent().siblings().filter(".med").children().filter(".tabs02content").hide();
        jQuery(active).show();
    });

    // Pestanas en PopUp de tabla de tallas
    jQuery(".tabsTallas ~ .tabsTallas_content").hide();
    jQuery(".tabsTallas ~ .tabsTallas_content:first").show();
    jQuery(".tabsTallas li a").click(function(e) {
        e.preventDefault();
        jQuery(this).parent().siblings().removeClass("active");
        jQuery(this).parent().addClass("active");
        var active = jQuery(this).attr("href");
        jQuery(this).parent().parent().siblings().filter(".tabsTallas_content").hide();
        jQuery(active).show();
    });

    // Capas en PopUp de tabla de tallas
    jQuery(".tabsTallas_content .tallas_int").hide();
    jQuery(".tabsTallas_content .tallas_int img").show();
    jQuery(".tabsTallas_content .tallas_int:nth-child(3)").show();
    jQuery(".tabsTallas_content .submenu ul li a").click(function(e) {
        e.preventDefault();
        jQuery(this).parent().siblings().removeClass("active");
        jQuery(this).parent().addClass("active");
        var active = jQuery(this).attr("href");
        jQuery(this).parent().parent().parent().siblings().filter(".tallas_int").hide();
        jQuery(active).show();
    });



    // Carrusel horizontal para Tipo de producto, etc
    jQuery(".carrousel01").css("height", "132px");
    jQuery(".carrousel01 .items").wrapAll(document.createElement("div")).parent().addClass('visor');
    var cssObj = {
        'height': '132px',
        'width': '687px',
        'overflow': 'hidden',
        'margin-left': '24px',
        'position': 'absolute'
    }
    jQuery(".carrousel01 .visor").css(cssObj);
    cssObj = {
        'height': '132px',
        'width': '100000px',
        'overflow': 'hidden',
        'margin': '0px',
        'position': 'relative'
    }
    jQuery(".carrousel01 .items").css(cssObj);
    if (jQuery(".carrousel01 .items div").length > 8) {
        jQuery(".carrousel01 .prev").css("display", "block").css("position", "absolute");
        jQuery(".carrousel01 .next").css("display", "block").css("position", "absolute");
        jQuery(".carrousel01 .visor .items div.clear").remove();
    }
    jQuery(".carrousel01 .next").click(function(e) {
        e.preventDefault();
        jQuery(".carrousel01 .visor .items").stop();
        jQuery(".carrousel01 .visor .items").animate({
            left: "-88px"
        }, 400, '', function() {
            jQuery(".carrousel01 .visor .items div:first").remove().appendTo(".carrousel01 .visor .items");
            jQuery(".carrousel01 .visor .items").css("left", "0px");
        }
		);
    });
    jQuery(".carrousel01 .prev").click(function(e) {
        e.preventDefault();
        jQuery(".carrousel01 .visor .items").stop();
        jQuery(".carrousel01 .visor .items div:last").remove().prependTo(".carrousel01 .visor .items");
        jQuery(".carrousel01 .visor .items").css("left", "-88px");
        jQuery(".carrousel01 .visor .items").animate({
            left: "0"
        }, 400);
    });

    // Carrusel horizontal para Modulos especiales para Tipos de producto (module02)
    jQuery(".carrousel03").css("height", "220px");
    jQuery(".carrousel03 .items").wrapAll(document.createElement("div")).parent().addClass('visor');
    var cssObj = {
        'height': '220px',
        'width': '710px',
        'overflow': 'hidden',
        'margin-left': '16px',
        'position': 'absolute'
    }
    jQuery(".carrousel03 .visor").css(cssObj);
    cssObj = {
        'height': '220px',
        'width': '100000px',
        'overflow': 'hidden',
        'margin': '0px',
        'position': 'relative'
    }
    jQuery(".carrousel03 .items").css(cssObj);
    if (jQuery(".carrousel03 .items div").length > 4) {
        jQuery(".carrousel03 .prev").css("display", "block").css("position", "absolute");
        jQuery(".carrousel03 .next").css("display", "block").css("position", "absolute");
        jQuery(".carrousel03 .visor .items div.clear").remove();
    }
    jQuery(".carrousel03 .next").click(function(e) {
        e.preventDefault();
        jQuery(".carrousel03 .visor .items").stop();
        jQuery(".carrousel03 .visor .items").animate({
            left: "-180px"
        }, 400, '', function() {
            jQuery(".carrousel03 .visor .items div:first").remove().appendTo(".carrousel03 .visor .items");
            jQuery(".carrousel03 .visor .items").css("left", "0px");
        }
		);
    });
    jQuery(".carrousel03 .prev").click(function(e) {
        e.preventDefault();
        jQuery(".carrousel03 .visor .items").stop();
        jQuery(".carrousel03 .visor .items div:last").remove().prependTo(".carrousel03 .visor .items");
        jQuery(".carrousel03 .visor .items").css("left", "-180px");
        jQuery(".carrousel03 .visor .items").animate({
            left: "0"
        }, 400);
    });

    // Carrusel vertical para Ultimos productos visitados, etc
    if (jQuery(".carrousel02 .items div").length > 7) {
        jQuery(".carrousel02 .control_holder").css("display", "block");
        jQuery(".carrousel02 .items").wrapAll(document.createElement("div")).parent().addClass('visor');
        var cssObj = {
            'height': '264px',
            'width': '160px',
            'overflow': 'hidden',
            'position': 'relative'
        }
        jQuery(".carrousel02 .visor").css(cssObj);
        cssObj = {
            'height': '100000px',
            'width': '160px',
            'overflow': 'hidden',
            'position': 'relative'
        }
        jQuery(".carrousel02 .items").css(cssObj);
        jQuery(".carrousel02 .visor .items div.clear").remove();
    }
    jQuery(".carrousel02 .next").click(function(e) {
        e.preventDefault();
        jQuery(".carrousel02 .visor .items").stop();
        jQuery(".carrousel02 .visor .items").animate({
            top: "-88px"
        }, 400, '', function() {
            jQuery(".carrousel02 .visor .items div:first").remove().appendTo(".carrousel02 .visor .items");
            jQuery(".carrousel02 .visor .items div:first").remove().appendTo(".carrousel02 .visor .items");
            jQuery(".carrousel02 .visor .items").css("top", "0px");
        }
		);
    });
    jQuery(".carrousel02 .prev").click(function(e) {
        e.preventDefault();
        jQuery(".carrousel02 .visor .items").stop();
        jQuery(".carrousel02 .visor .items div:last").remove().prependTo(".carrousel02 .visor .items");
        jQuery(".carrousel02 .visor .items div:last").remove().prependTo(".carrousel02 .visor .items");
        jQuery(".carrousel02 .visor .items").css("top", "-88px");
        jQuery(".carrousel02 .visor .items").animate({
            top: "0"
        }, 400);
    });

    // Carrusel vertical para Ultimos productos visitados, etc con una anchura superior
    if (jQuery(".carrousel04 .items div").length > 7) {
        jQuery(".carrousel04 .control_holder").css("display", "block");
        jQuery(".carrousel04 .items").wrapAll(document.createElement("div")).parent().addClass('visor');
        var cssObj = {
            'height': '264px',
            'width': '180px',
            'overflow': 'hidden',
            'position': 'relative',
            'margin-left': '10px'
        }
        jQuery(".carrousel04 .visor").css(cssObj);
        cssObj = {
            'height': '100000px',
            'width': '160px',
            'overflow': 'hidden',
            'position': 'relative',
            'margin-left': '0'
        }
        jQuery(".carrousel04 .items").css(cssObj);
        jQuery(".carrousel04 .visor .items div.clear").remove();
    }
    jQuery(".carrousel04 .next").click(function(e) {
        e.preventDefault();
        jQuery(".carrousel04 .visor .items").stop();
        jQuery(".carrousel04 .visor .items").animate({
            top: "-88px"
        }, 400, '', function() {
            jQuery(".carrousel04 .visor .items div:first").remove().appendTo(".carrousel04 .visor .items");
            jQuery(".carrousel04 .visor .items div:first").remove().appendTo(".carrousel04 .visor .items");
            jQuery(".carrousel04 .visor .items").css("top", "0px");
        }
		);
    });
    jQuery(".carrousel04 .prev").click(function(e) {
        e.preventDefault();
        jQuery(".carrousel04 .visor .items").stop();
        jQuery(".carrousel04 .visor .items div:last").remove().prependTo(".carrousel04 .visor .items");
        jQuery(".carrousel04 .visor .items div:last").remove().prependTo(".carrousel04 .visor .items");
        jQuery(".carrousel04 .visor .items").css("top", "-88px");
        jQuery(".carrousel04 .visor .items").animate({
            top: "0"
        }, 400);
    });




    // Capa "Buscar en..." (cabecera)
    jQuery("#searchbox .search_select ul").css("position", "absolute");
    jQuery("#searchbox .search_select ul").hide();
    jQuery("#searchbox .search_select .bar").click(
		function(event) {
		    jQuery(this).siblings().filter("ul").show();
		}
	);
    jQuery("#searchbox .search_select").hover(
		function(event) {
		},
		function(event) {
		    jQuery(this).children().filter("ul").hide();
		}
	);

    // Capas de filtros
    jQuery("#filters .filter ul").css("position", "absolute");
    jQuery("#filters .filter ul").hide();
    jQuery("#filters .filter .bar").click(
		function(event) {
		    jQuery(this).siblings().filter("ul").show();
		},
		function(event) {
		}
	);
    jQuery("#filters .filter").hover(
		function(event) {
		},
		function(event) {
		    jQuery(this).children().filter("ul").hide();
		}
	);

    // Capa "Ordenar por..."
    jQuery(".result_control .sortby_select ul").css("position", "absolute");
    jQuery(".result_control .sortby_select ul").hide();
    jQuery(".result_control .sortby_select .bar").click(
		function(event) {
		    jQuery(this).siblings().filter("ul").show();
		},
		function(event) {
		}
	);
    jQuery(".result_control .sortby_select").hover(
		function(event) {
		},
		function(event) {
		    jQuery(this).children().filter("ul").hide();
		}
	);

    // Mas info / Menos info
    jQuery(".menosinfo").hide();
    jQuery(".masinfo").click(function(e) {
        e.preventDefault();
        var capa = jQuery(this).attr("href");
        jQuery(capa).show();
        jQuery(this).hide();
        jQuery(".menosinfo").filter(function() { return jQuery(this).attr("href") == capa; }).show();
    });
    jQuery(".menosinfo").click(function(e) {
        e.preventDefault();
        var capa = jQuery(this).attr("href");
        jQuery(capa).hide();
        jQuery(this).hide();
        jQuery(".masinfo").filter(function() { return jQuery(this).attr("href") == capa; }).show();
    });

    inicializarEventosColores();

    // Promociones desplegables en ficha de producto
    // Superior:
    jQuery(".product_promo_sup img.over").hide();
    jQuery(".product_promo_sup").bind("mouseenter",
		function(event) {
		    jQuery(".product_promo_sup img.normal").hide();
		    jQuery(".product_promo_sup img.over").show();
		}).bind("mouseleave",
		function(event) {
		    jQuery(".product_promo_sup img.over").hide();
		    jQuery(".product_promo_sup img.normal").show();
		});
    // Inferior:
    jQuery(".product_promo_inf img.over").hide();
    jQuery(".product_promo_inf").bind("mouseenter",
		function(event) {
		    jQuery(".product_promo_inf img.normal").hide();
		    jQuery(".product_promo_inf img.over").show();
		}).bind("mouseleave",
		function(event) {
		    jQuery(".product_promo_inf img.over").hide();
		    jQuery(".product_promo_inf img.normal").show();
		});

    // Desplegables en "ul_categorias" (Ayuda, etc)
    jQuery("ul.ul_categorias li ul").hide();
    jQuery(".lnk_desplegable").click(function(e) {
        this.blur();
        e.preventDefault();
        jQuery("ul.ul_categorias li ul").hide();
        var capa = jQuery(this).attr("href");
        jQuery(capa).show();
    });
    jQuery(".lnk_desplegartodas").click(function(e) {
        this.blur();
        e.preventDefault();
        jQuery("ul.ul_categorias li ul").show();
    });
    jQuery(".lnk_plegartodas").click(function(e) {
        this.blur();
        e.preventDefault();
        jQuery("ul.ul_categorias li ul").hide();
    });

    // Zoom en ficha de producto
    var zoomAnc = 470 //414
    var zoomAlt = 531 //468
    if (jQuery("#fotozoom").length != 0) {
        jQuery("#fotozoom").draggable({
            drag: function(event, ui) {
                var w = parseInt(jQuery("#fotozoom").css("width"));
                var h = Math.round(1.13 * w);
                if (ui.position.left > 0) { ui.position.left = 0 };
                if (ui.position.top > 0) { ui.position.top = 0 };
                if (ui.position.left < (zoomAnc - w)) { ui.position.left = zoomAnc - w };
                if (ui.position.top < (zoomAlt - h)) { ui.position.top = zoomAlt - h };
            }
        });
    };
    jQuery(".zoom_mas").click(function(e) {
        this.blur();
        e.preventDefault();
        var w = parseInt(jQuery("#fotozoom").css("width"));
        var wn = Math.round(parseInt(w) * 1.2);
        if (wn >= 884) { wn = 884 };
        var h = Math.round(1.13 * w);
        var hn = Math.round(1.13 * wn);
        // Centrado de la imagen
        var dl = -parseInt((wn - w) / 2);
        var l = parseInt(jQuery("#fotozoom").css("left"));
        var t = parseInt(jQuery("#fotozoom").css("top"));
        var ln = l + dl;
        var tn = t + dl;
        // Comprobacion de limites
        if (ln > 0) { ln = 0 };
        if (tn > 0) { tn = 0 };
        if (ln + wn < zoomAnc) { ln = zoomAnc - wn };
        if (tn + hn < zoomAlt) { tn = zoomAlt - hn };
        // Ampliacion
        jQuery("#fotozoom").animate({
            width: wn + "px",
            left: ln + "px",
            top: tn + "px"
        }, 200);
    });
    jQuery(".zoom_menos").click(function(e) {
        this.blur();
        e.preventDefault();
        var w = parseInt(jQuery("#fotozoom").css("width"));
        var wn = Math.round(parseInt(w) / 1.2);
        if (wn <= zoomAnc) { wn = zoomAnc };
        var h = Math.round(1.13 * w);
        var hn = Math.round(1.13 * wn);
        // Centrado de la imagen
        var dl = -parseInt((wn - w) / 2);
        var l = parseInt(jQuery("#fotozoom").css("left"));
        var t = parseInt(jQuery("#fotozoom").css("top"));
        var ln = l + dl;
        var tn = t + dl;
        // Comprobacion de limites
        if (ln > 0) { ln = 0 };
        if (tn > 0) { tn = 0 };
        if (ln + wn < zoomAnc) { ln = zoomAnc - wn };
        if (tn + hn < zoomAlt) { tn = zoomAlt - hn };
        // Ampliacion
        jQuery("#fotozoom").animate({
            width: wn + "px",
            left: ln + "px",
            top: tn + "px"
        }, 200);
    });

    // Carga Ajax de contenido en promociones "Los regalos Venca", etc
    if (jQuery("#promo_page_container") != "") {
        $first = jQuery("#promo_menu ul li a").attr("href");
        jQuery("div#promo_page_container").load($first);
        jQuery("#promo_menu ul li a").click(function(e) {
            e.preventDefault();
            $href = jQuery(this).attr("href");
            jQuery("div#promo_page_container").load($href);
            jQuery(this).parent().addClass("active");
            jQuery(this).parent().siblings().removeClass("active");
        });
    }
 }

function inicializarEventosColores() {
	// Capa de previsualizacion de color (caracteristicas)
	jQuery("ul.color li").bind("mouseenter",
		function(event){
			var layer_color = jQuery(this).parent().siblings().filter(".layer_color");
			var fondo = jQuery(this).children().children().filter("img").attr("src");
			fondo = "url("+fondo+") 0 0 no-repeat";
			jQuery(layer_color).children().filter(".image").css("background",fondo);
			var texto = jQuery(this).children().filter(".texto").html();
			jQuery(layer_color).children().filter(".text").html(texto);
			var x = event.pageX+10;
			if (x>jQuery(window).width()-200) {x=x-180};
			var y = event.pageY;
			jQuery(layer_color).css("top", y);
			jQuery(layer_color).css("left", x);
			layer_color.show()
		}).bind("mouseleave",
		function(event){
			var layer_color = jQuery(this).parent().siblings().filter(".layer_color");
			layer_color.hide()
		});
}
