$(document).ready(function(){
    
    $("a.disabled").click(function(){
        return false;
    });

    $(".box.cotations.devises").each(function(){
        var box = $(this);
        box.find(".toggler a").click(function(){
            type = $(this).attr("href").substr(1);
            box.find("li .valeur").hide();
            box.find("li .valeur."+type).show();
            box.find(".toggler a").removeClass("active");
            $(this).addClass("active");
            return false;
        });
    });

    $(".tabBar a").click(function(){
        if (!$(this).hasClass("active")){
            $(".tabBar a").removeClass("active");
            $(this).addClass("active");
        }
        return false;
    });

    $(".anode").html("<a href=\"http://www.anodeetcathode.net\">Anode & Cathode</a>");

	resizeContent();

});


/**
 * GESTION DU MENU
 *
 */

subnavOpen = false;
subnavHover = false;
subnavTogglerHover = false;

$(document).ready(function(){

	/**
	 * Modifications des HREF LPOC et Guide Numismatique
	 */

	link = $(".lingot-piece-cote");
	href = link.attr("href");
	link.attr("href", href+"napoleon/");

	link = $(".guide-numismatique");
	href = link.attr("href");
	link.attr("href", href+"piece-or-consulat/");

    /********
     *effet survol souris sur sous menu
     *********/
    $(".subsubnav a").fadeTo(0,0.8);
    $(".subsubnav a").mouseover(function(){
        $(this).css("opacity", 1);
    });
    $(".subsubnav a").mouseout(function(){
        $(this).css("opacity", 0.7);
    });

    /********
     *menu
     **********/

    $("#home, #mouseOutCatcher").mouseover(function(){
        $("#btn-societe").removeClass("selected");
        $("#btn-devises").removeClass("selected");
        $("#btn-or").removeClass("selected");
        if(subnavOpen) closeSubnav();
    }).mouseout(function(){
        $(this).removeClass("selected");
    });

    $(".subnav-toggler").mouseover(function(){
        id = $(this).attr("id");

        if(id != openSubnavId){
            closeSubnav();
        }

        if(!subnavOpen)
            openSubnav($(this).next("ul"));

        subnavTogglerHover = true;

    }).mouseout(function(){
        subnavTogglerHover = false;
    }).next("ul").hide();

});

var openSubnavId = "";
var openSubnavElement = null;

function openSubnav(subnav){
       
    openSubnavElement = subnav;
    openSubnavElement.prev("a").addClass("selected");
    openSubnavId =  openSubnavElement.prev("a").attr("id");
    subnav.unbind("mouseout", closeSubnav)
    subnav.find("li").mouseover(function(){
        subnavHover = true;
    }).mouseout(function(){
        subnavHover = false;
    });
    
    subnav.show();
    subnavOpen = true;
    subnav.mouseover(function(){
        $(this).bind("mouseout", delayClose);
    });

}

function delayClose(){
    setTimeout(closeSubnav, 150);
}

function closeSubnav(){
    if(!subnavHover && !subnavTogglerHover && subnavOpen){
        openSubnavElement.prev("a").removeClass("selected");
        openSubnavElement.hide();
        subnavOpen = false;
    }
}


/**
 *
 *  BLOCKS PAGINATION
 *
 */

$(document).ready(function(){
    $(".ul-wrapper.paginate").each(function(){

        var ul = $(this).find("ul");
        var displayedRows = parseInt($(this).find("input[name=displayedRows]").val());
        /**
		 * Pas la peine d'executer tout ça si y a pas besoin de paginer
		 */
        if(ul.find("li").length > displayedRows){
            var currentIndex = 0;
            var wrapperHeight = $(this).find("li:eq(0)").outerHeight(true) * displayedRows;
            $(this).height(wrapperHeight);
            var buttons = $(this).parents(".box").find("ul.paginate");
            var content = $(this).parents(".box").find("div.content");

            buttons.find("li a:not(.arrow)").click(function(){
                currentIndex = parseInt($(this).text());
                moveList();
                return false;
            });

            buttons.find("li a.arrow.left").click(function(){
                if (currentIndex > 0){
                    currentIndex--;
                    moveList();
                }
                return false;
            });

            buttons.find("li a.arrow.right").click(function(){
                if (currentIndex < Math.ceil(ul.find("li").length / displayedRows) - 1){
                    currentIndex++;
                    moveList();
                }
                return false;
            });
        }

        function moveList(){
            offset = wrapperHeight * -currentIndex;
            ul.stop().animate({
                marginTop: offset
            }, 700, "easeOutQuad");
            buttons.find("li a[title]").removeClass("active").addClass("idle");
            buttons.find("li a[title]").eq(currentIndex).addClass("active").removeClass("idle");
        }
    });
});



function resizeContent(){
    var tailleCol = $(".col-1").height()+290;
    var tailleContain = $("#content").height()+290;
    if (tailleCol > tailleContain) {
        $("#content.taille").height(tailleCol);
    }
    else {
        $("#content.taille").height(tailleContain);
    }

    $("#footer").slideDown(200);
}
