$(document).ready(function() {

    // ---------------  GLOBAL --------------- //
    // menu
    $('li.mega').hoverIntent(
    function() {
        $(this).find('h2').addClass("active");
        $(this).find('div').slideDown('fast');
    },
    function() {
        $(this).find('h2').removeClass("active");
        $(this).find('div').slideUp('fast');
    });

    // accordion
    
    
    $("#left-nav .block h2").click(function() {
        $(this).next("div").slideToggle("fast", function() {
            $("#left-nav, #content, #aside").equalHeight();
        }).siblings("div:visible").slideUp("fast");
        $(this).toggleClass("active");
        $(this).siblings("h2").removeClass("active");
    });
    
    
    /*
    $("#left-nav .block h3").toggle(function() {
        $(this).next("ul").slideToggle("fast");
        $(this).removeClass("active");
    },
    function() {
        $(this).next("ul").slideToggle("fast");
        $(this).toggleClass("active");
        $(this).siblings("h3").removeClass("active");
    });
    */
    
    // clickable block
    $("#cart-summary").click(function() {
        window.location = $(this).find("a").attr("href");
        return false;
    });

    // Fly cart
    //$('#flycart').prepend('<div class="flycart-top">&nbsp;</div>');
    //$('#flycart').append('<div class="flycart-bot">&nbsp;</div>');

    var timeout;
	
    // flycart show-hide
    $("#cart-summary").mouseenter(function() {
    	if (timeout)
    		clearTimeout(timeout);
        timeout = setTimeout(function() {
            $('.flycart').slideDown('fast');
        },
        350);
    });
    
    $("#cart-summary").mouseleave(function() {
	clearTimeout(timeout);
    });    
    
    $(".flycart").mouseenter(function() {
	clearTimeout(timeout);
    });      
    
    $(".flycart").mouseleave(function() {
        timeout = setTimeout(function() {
            $('.flycart').slideUp('fast');
            clearTimeout(timeout);
        },
        1000);
    }).find('a.close').click(function() {
        $(this).parents('.flycart').hide();
    });
    // ---------------  GLOBAL --------------- //
    
    
    if ($(".home-page.accueil").length > 0) {

        // ---------------  ACCUEIL --------------- //
        // clickable blocks
        $("#aside .block dl").click(function() {
            window.location = $(this).find("a").attr("href");
            return false;
        });

		$(".category:last-child").addClass("last_cat");
		
        // First-Last
        // RLA DELETE $("#product-grid .category:last-child, #product-grid .category .product:last-child").addClass("last");
        // RLA DELETE $("#main-nav ol li:first-child").addClass("first");
        // Equal height
        $("#left-nav, #content").equalHeight();

        // ---------------  ACCUEIL --------------- //
    }

    if ($(".inner-page.titre").length > 0) {

        // ---------------  TITRE --------------- //
        $("#aside .product").click(function() {
            window.location = $(this).find("a").attr("href");
            return false;
        });
        // First-Last
        // RLA DELETE $("#main-nav ol li:first-child").addClass("first");
        // Breadcrumb
        $("#breadcrumb li:not(:first)").prepend("&gt; &nbsp;");
        // Radio selection
        $('.offer h5').click(function() {
            var $radio = $(this).parent().find(':radio');
            $radio.attr('checked', !$radio.attr('checked'));
        });
        // Equalize
        $("#left-nav, #content, #aside").equalHeight();

        // ---------------  END TITRE --------------- //
    }

    if ($(".order-page.commandes-formulaire").length > 0) {

        // ---------------  COMMANDES FORUMULAIRE --------------- //
        // First-Last
        $(".fst1 fieldset:last").addClass("last");
        // RLA DELETE $("#main-nav ol li:first-child").addClass("first");
        // Equal height
        if ($.browser.msie) {
            $("#left-nav, #content").equalHeight();
        }
        else {
            $("#left-nav, #main").equalHeight();
        }
        // hover classes
        $("input.btn-2").hover(function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        });

        // ---------------  COMMANDES FORUMULAIRE --------------- //
    }

    if ($(".order-page.commandes").length > 0) {

        // ---------------  COMMANDES --------------- //
        // First-Last
        $("table.tst1 tr:last-child").addClass("last");
        // RLA DELETE $("#main-nav ol li:first-child").addClass("first");
        // Equal height
        if ($.browser.msie) {
            $("#left-nav, #content").equalHeight();
        }
        else {
            $("#left-nav, #main").equalHeight();
        }
        // hover classes
        $("input.btn-2, input.btn-2b").hover(function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        });

        // ---------------  COMMANDES --------------- //
    }

    if ($(".order-page.compte").length > 0) {

        // ---------------  COMPTE --------------- //
        // clickable blocks
        // First-Last
        $("table.tst1 tr:last-child, .fst1 fieldset:last").addClass("last");
        // RLA DELETE $("#main-nav ol li:first-child, .form.fst2 fieldset:first").addClass("first");
        // equalheight
        $('.fst2 .fields').equalHeight();
        // hover classes
        $("input.btn-2, input.btn-2b").hover(function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        });

        // ---------------  COMPTE --------------- //
    }

    if ($(".order-page.livriason").length > 0) {

        // ---------------  LIVIRIASON --------------- //
        // First-Last
        $("table.tst2 tr:last-child, .panier .action input:last").addClass("last");
        // RLA DELETE  $("#main-nav ol li:first-child").addClass("first");

        // table manipulation
        $("table.tst2 td:first-child, table.tst2 th:first-child").addClass("first-col-cell");
        $("table.tst2 td:last-child, table.tst2 th:last-child").addClass("last-col-cell");

        // targetting columns
        $('table.tst3 tbody td:nth-child(1)').addClass('first-col');
        $('table.tst3 tbody td:nth-child(2)').addClass('second-col');
        $('table.tst3 tbody td').find('a.remove').parent().addClass('second-last-cell');

        //grand total
        $('table.tst2 tfoot th').addClass('grand-total');
        $("table.tst2 tfoot th.grand-total:last").addClass("last");

        // panel
        $(".panel-trigger").click(function() {
            $(this).next(".panel").slideToggle("fast");
            $(this).toggleClass("active");
            return false;
        });
        // hover classes
        $("input.btn-2, input.btn-2b").hover(function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        });

        // ---------------  LIVIRIASON --------------- //
    }

    if ($(".order-page.panier").length > 0) {

        // ---------------  PANIER --------------- //
        // clickable blocks
        // First-Last
        $("table.tst2 tr:last-child, .panier .action input:last").addClass("last");
        // RLA DELETE $("#main-nav ol li:first-child, #breadcrumb a:first ").addClass("first");
        // table manipulation
        $("table.tst2 td:first-child, table.tst2 th:first-child").addClass("first-col-cell");
        $("table.tst2 td:last-child, table.tst2 th:last-child").addClass("last-col-cell");
        // first and last column
        $("table.tst2 tfoot td").removeClass('first-col-cell last-col-cell');
        $("table.tst2 tfoot td:first").addClass("first-col-cell");
        $("table.tst2 tfoot td:last-child").addClass("last-col-cell");
        // last product row
        $("table.tst2 tbody tr:last").addClass("last-row");
        // subtotal
        $("table.tst2 tfoot tr:first td:not(:first)").addClass("subtotal");
        $("table.tst2 tfoot td.subtotal:last").addClass("last");
        //grand total
        //$('table.tst2 tfoot tr:eq(2)').find('th').addClass('grand-total');
        //$("table.tst2 tfoot th.grand-total:last").addClass("last");
        // tfoot first and last
        $('table.tst2 tfoot td:first').addClass('addl');
        $('table.tst2 tfoot td:last').addClass('agreement');

        // panel
        $(".panel-trigger").click(function() {
            $(this).next(".panel").slideToggle("fast");
            $(this).toggleClass("active");
            return false;
        });
        // hover classes
        $("input.btn-2, input.btn-2b").hover(function() {
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        });

        // ---------------  PANIER --------------- //
    }

});
