$(function () {
    /**
     * support.php
     * FAQ - Affichage/masquage des questions/réponses
     */
    $("#faq .titre").click(function (e) {
        $("#faq p:visible").slideUp('fast');

        var p = $(this).next("p");
        if (p.is(":hidden")) {
            p.slideDown('fast');
            pageTracker._trackPageview('/evenement/support/question/' + $(this).parent().attr('id'));
        } else {
            p.hide('normal');
        }
    });
    
    // affichage des logos
    $('#portfolio').innerfade({ 
        animationtype: 'fade',  
        speed: 'slow',
        speed: 2000, 
        timeout: 4500, 
        type: 'random_start', 
        containerheight: '100px'
    });
    
    // fil d'info
    $('#news').jcarousel({
        wrap: 'circular',
        vertical: true,
        auto: 6,
        scroll: 1,
        itemVisibleInCallback: {onBeforeAnimation: news_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: news_itemVisibleOutCallback}
    });

    // cas abonnes
    $('#cas').jcarousel({
        scroll: 1,
        initCallback: cas_initCallback
    });

    // Effets du formulaire de demande de devis
    $("#tb_precision_cat1").click(function (e) {
        $("#details_cat1").show();
        $("#details_cat2").hide();
        $("#details_cat3").hide();
        $("#details_cat4").hide();
        $("#details_cat5").hide();
    });
    $("#tb_precision_cat2").click(function (e) {
        $("#details_cat1").hide();
        $("#details_cat2").show();
        $("#details_cat3").hide();
        $("#details_cat4").hide();
        $("#details_cat5").hide();
    });
    $("#tb_precision_cat3").click(function (e) {
        $("#details_cat1").hide();
        $("#details_cat2").hide();
        $("#details_cat3").show();
        $("#details_cat4").hide();
        $("#details_cat5").hide();
    });
    $("#tb_precision_cat4").click(function (e) {
        $("#details_cat1").hide();
        $("#details_cat2").hide();
        $("#details_cat3").hide();
        $("#details_cat4").show();
        $("#details_cat5").hide();
    });
    $("#tb_precision_cat5").click(function (e) {
        $("#details_cat1").hide();
        $("#details_cat2").hide();
        $("#details_cat3").hide();
        $("#details_cat4").hide();
        $("#details_cat5").show();
    });
    
    $("#details_cat1_aide").hover(
        function(){$("#details_cat1_aide_text").show();},
        function(){$("#details_cat1_aide_text").hide();}
    );
    $("#details_cat2_aide").hover(
        function(){$("#details_cat2_aide_text").show();},
        function(){$("#details_cat2_aide_text").hide();}
    );
    $("#details_cat3_aide").hover(
        function(){$("#details_cat3_aide_text").show();},
        function(){$("#details_cat3_aide_text").hide();}
    );
    $("#details_cat4_aide").hover(
        function(){$("#details_cat4_aide_text").show();},
        function(){$("#details_cat4_aide_text").hide();}
    );
    $("#details_cat5_aide").hover(
        function(){$("#details_cat5_aide_text").show();},
        function(){$("#details_cat5_aide_text").hide();}
    );



    
    
    /**
     * Pop-up pour la page support
     */
    
    // Definitions
    function makeKey(str) {
        return /(le|la|les|l')?(.*)/.exec(str.toLowerCase())[2].replace(/^\s+|\s+$/g, '');
    }
    
    var defs  = {};
    $("#lexique dt").each(function (idx, el) {
        var key = makeKey($(this).text()),
            def = $(this).next("dd").text();
    
        defs[key] = defs[key] || def;
    });
    
    
    $(".trigger").each(function (idx, el) {
        var key = makeKey($(this).text());
        
        if (defs[key]) {
            $(this).data("def", defs[key]);
        } else {
            $(this).removeClass("trigger");
        }
    });
    
    // Popup
    $(".trigger").tooltip({
        fade:250,
        bodyHandler: function () {
            return $(this).data("def");
        }
    });
});

/**
 * 
 */
function cas_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });

    jQuery('#cas-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#cas-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

/**
 * Fonctions du fil d'info
 */
function news_itemVisibleInCallback(carousel, item, i, state, evt) {
    var idx = carousel.index(i, news_itemList.length);
    carousel.add(i, news_getItemHTML(news_itemList[idx - 1]));
};
function news_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
};
function news_getItemHTML(item) {
    if (item) {
        return '<span class="feed-date">' + item.date + '</span> - ' + item.titre;
    }
};
/**
 * Fonctions du fil d'info
 */
function slogans_itemVisibleInCallback(carousel, item, i, state, evt) {
    var idx = carousel.index(i, slogans_itemList.length);
    carousel.add(i, slogans_getItemHTML(slogans_itemList[idx - 1]));
};
function slogans_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
};
function slogans_getItemHTML(item) {
    if (item) {
        return '<span class="big">' + item.titre + '</span><span class="little">' + item.text + '</span>';
    }
};
