(function($) {
    $.fn.customFadeIn = function(speed, callback) {
        $(this).fadeIn(speed, function() {
            if(jQuery.browser.msie)
                $(this).get(0).style.removeAttribute('filter');
            if(callback != undefined)
                callback();
        });
    };
    $.fn.customFadeOut = function(speed, callback) {
        $(this).fadeOut(speed, function() {
            if(jQuery.browser.msie)
                $(this).get(0).style.removeAttribute('filter');
            if(callback != undefined)
                callback();
        });
    };
})(jQuery);


jQuery(document).ready(function() {

    //    $(".w_close").click(function() {
    //       $(".wicket-mask-dark").hide();
    //       $(".wicket-mask-dark").remove();
    //       location.reload();
    //       alert("yoo");
    //    });

    // LIEN NOUVELLE FENETRE ---------------------------------------
    $('.lienExterne').click( function() {
        window.open(
            $(this).attr('href') );
        return false;
    });

    // LE COVERFLOW ------------------------------------------------
    if ($.browser.msie) {
        setInterval(animateCoverFlowIE, 5000);
    } else {
        setInterval(animateCoverFlow, 5000);
    }
    $('#coverflow .startup').click(function() {
        $(location).attr('href','catalogue');
    });

    // LES 4 BLOCS ACCUEIL -----------------------------------------
    //  $('.kwicks li .kwicksContent .text_a_afficher').fadeTo(0, 0);

    $("#blocsAccueil .kwicks li .kwicksContent, #blocsCcm .kwicks li .kwicksContent").hover(function () {
        if(jQuery.browser.msie)
            $(this).get(0).style.removeAttribute('filter');
            
        $(this).find('.text_a_afficher').animate({
            marginTop : 0
        }, {
            duration: 400,
            easing : "easeOutBack"
        });
        $(this).find('h2').animate({
            top: "-180"
        },  {
            duration: 400,
            easing : "easeOutBack"
        });
    }, function(){
        $(this).find('.text_a_afficher').animate({
            marginTop: "200"
        },  {
            duration: 400,
            easing : "easeOutBack"
        });
        $(this).find('h2').animate({
            top:0
        },  {
            duration: 400,
            easing : "easeOutBack"
        });
    });

    $("#blocsAccueil .kwicks li .kwicksContent").click(function () {
        lien = $(this).find('a').attr("href");
        $(location).attr('href',lien);
    });

    // LES 4 BLOCS QUI SOMMES NOUS ---------------------------------
    $("#blocsQsn .kwicks li.kwick .kwicksContent").hover(function () {
        $(this).find('.text_a_afficher').animate({
            marginTop : "30"
        },  {
            duration: 400,
            easing : "easeOutBack"
        });
    }, function(){
        $(this).find('.text_a_afficher').animate({
            marginTop: "200"
        },  {
            duration: 400,
            easing : "easeOutBack"
        });
    });

    // LES 4 BLOCS NOTRE OFFRE -----------------------------------------

    $("#blocsNotreOffre .kwicks li .kwicksContent").hover(function () {
        $(this).find('.text_a_afficher').animate({
            marginTop : "32"
        }, {
            duration: 400,
            easing : "easeOutBack"
        });
    }, function(){
        $(this).find('.text_a_afficher').animate({
            marginTop: "200"
        },  {
            duration: 400,
            easing : "easeOutBack"
        });
    });

    // LES 4 BLOCS SECTEURS -----------------------------------------
    $("#blocsSecteurs .kwicks tr td .kwicksContent").hover(function () {
        $(this).find('.text_a_afficher').animate({
            marginTop : "60"
        }, {
            duration: 400,
            easing : "easeOutBack"
        });
    }, function(){
        $(this).find('.text_a_afficher').animate({
            marginTop: "200"
        },  {
            duration: 400,
            easing : "easeOutBack"
        });
    });



    // NEWS SLIDER -------------------------------------------------
    $('.newsSuivante').click(function() {
        margintop = $('#dernieresNews .news:first').css("margin-top");
        if (margintop.substr(0, margintop.length - 2) > (- ($('.news').size() - 1) * 300)) {
            $('#dernieresNews .news:first').animate({
                marginTop: '-=300'
            } , {
                duration: 500,
                easing : "easeOutCubic"
            });
            if (margintop.substr(0, margintop.length - 2) == (- ($('.news').size() - 2) * 300)) {
                $('.newsSuivante').addClass('disabled');
            }
        }
        if ($('.newsPrecedente').hasClass('disabled')) {
            $('.newsPrecedente').removeClass('disabled')
        }
    });
    $('.newsPrecedente').click(function() {
        margintop = $('#dernieresNews .news:first').css("margin-top");
        if (margintop.substr(0, margintop.length - 2) < 0) {
            if (margintop.substr(0, margintop.length - 2) == -300) {
                $('.newsPrecedente').addClass('disabled');
            }
            $('#dernieresNews .news:first').animate({
                marginTop: '+=300'
            } , {
                duration: 500,
                easing : "easeOutCubic"
            });

        }
        if ($('.newsSuivante').hasClass('disabled')) {
            $('.newsSuivante').removeClass('disabled')
        }
    });

    // VIGNETTE CATALOGUE --------------------------------------------
    $('.vignette .texteStartup').customFadeOut(0);
    $(".vignette").hover(function () {
        $(this).find('.contentVignette').customFadeOut(200);
        $(this).find('.texteStartup').delay(150).customFadeIn(200);
    }, function(){
        $(this).find('.texteStartup').customFadeOut(200);
        $(this).find('.contentVignette').delay(150).customFadeIn(200);
    });

    // COMMENT CA MARCHE & PRESSBOOK : TABS : -----------------------------------
    var tabContainers = $('div#tabs > div');
    tabContainers.hide().filter(':first').show();
    $('div#tabs ul.tabNavigation a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div#tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();
    
    // FAQ ----------------------------------------------------------
    $('#faqPage .faqContenu').hide().filter(':first').show();
    $('#faqPage h3:first').addClass('selected');
    $('#faqPage h3').click(function(){
        var checkElement = $(this).next();
        if ((checkElement.is('div.faqContenu')) && (checkElement.is(':visible'))) {
            return false;
        }
        if ((checkElement.is('div.faqContenu')) && (!checkElement.is(':visible'))) {
            $('#faqPage div.faqContenu:visible').slideUp('fast');
            $('#faqPage h3').removeClass('selected');
            $(this).addClass('selected');
            checkElement.slideDown('fast');
            return false;
        }
    });

    /////////// Voir / Cacher les commentaires d'une discussion ////////////////
    jQuery('ul.commentaires').hide();
    jQuery('.discussion_commentaires .display_commentaires').click(function(){
        var checkElement = jQuery(this).parent().next();
        jQuery('.discussion_commentaires .display_commentaires').html("Voir les commentaires");
        if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
            jQuery('ul.commentaires:visible').customFadeIn('normal');
            checkElement.customFadeOut('normal');
            jQuery(this).html("Voir les commentaires");
            return false;
        }
        if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
            jQuery('ul.commentaires:visible').customFadeOut('normal');
            checkElement.customFadeIn('normal');
            jQuery(this).html("Cacher les commentaires");
            return false;
        }
    });


    //////////// Voir / Cacher "Ouvrir une discussion" (GPC intrastartup / admin) /////////////
    jQuery('#nouvelle_discussion').hide();
    jQuery('#titre_nouvelle_discussion').click(function(){
        var checkElement = jQuery('#nouvelle_discussion');
        if (checkElement.is(':visible')) {
            jQuery('#nouvelle_discussion:visible').slideDown('normal');
            jQuery('#titre_nouvelle_discussion').attr('style', 'background-position: 0px 0px;');
            checkElement.slideUp('normal');
            return false;
        }
        if (!checkElement.is(':visible')) {
            jQuery('#nouvelle_discussion:visible').slideUp('normal');
            jQuery('#titre_nouvelle_discussion').attr('style', 'background-position: 0px -20px;');
            checkElement.slideDown('normal');
            return false;
        }
    });
    //////////// Voir / Cacher "Ouvrir une discussion" (GPC intrastartup / admin) /////////////
    jQuery('#nouvelle_question').hide();
    jQuery('#titre_nouvelle_question').click(function(){
        var checkElement = jQuery('#nouvelle_question');
        if (checkElement.is(':visible')) {
            jQuery('#nouvelle_question:visible').slideDown('normal');
            jQuery('#titre_nouvelle_question').attr('style', 'background-position: 0px 0px;');
            checkElement.slideUp('normal');
            return false;
        }
        if (!checkElement.is(':visible')) {
            jQuery('#nouvelle_question:visible').slideUp('normal');
            jQuery('#titre_nouvelle_question').attr('style', 'background-position: 0px -20px;');
            checkElement.slideDown('normal');
            return false;
        }
    });

    // Voir / Cacher les détails d'un Dossier (-> Comite d'investissement) -----------------
    jQuery('.listeStartupsComite .wrapper').hide();
    jQuery('.listeStartupsComite .lienPlusDeDetails').click(function(){
        var checkElement = jQuery(this).parent().find(".wrapper");
        jQuery('.lienPlusDeDetails').html("Plus de détails...");
        if ((checkElement.is('div.wrapper')) && (checkElement.is(':visible'))) {
            jQuery('div.wrapper:visible').slideDown('normal');
            checkElement.slideUp('normal');
            jQuery(this).html("Plus de détails...");
            return false;
        }
        if ((checkElement.is('div.wrapper')) && (!checkElement.is(':visible'))) {
            jQuery('div.wrapper:visible').slideUp('normal');
            checkElement.slideDown('normal');
            jQuery(this).html("Cacher les détails");
            return false;
        }
    });


    // FORMULAIRE VOTE (BOUTONS RADIO) ----------------------------------------------------------
    jQuery(".formVote .boutonRadio").click(function() {
        //  alert('Ca passe');
        jQuery(".formVote div.boutonRadio").removeClass("highlight")
        jQuery(this).addClass("highlight");
        jQuery(this).find(":radio").attr("checked","checked");
    });

    // Appel de la fonction pour la longueur max des textarea.maxLenght
    jQuery(".maxLenght").bind('keyup keydown click',function () {
        MaxLength(this);
    });

// COMMENTAIRES CATALOGUE -----------------------------------------------------

/*  jQuery(".reponseFormulaire").hide();
    jQuery(".displayRepondre").click(function() {
        jQuery(".reponseFormulaire").hide();
        jQuery(".displayRepondre").show();
        jQuery(this).next(".reponseFormulaire").show();
        jQuery(this).hide();
    });

    jQuery(".envoiCommentairePrincipal").click(function() {
        jQuery(".reponseFormulaire").hide();
    });*/

});
// <------- Fin du 'document.ready'

// Appel de la fonction pour la longueur max des textarea.maxLenght
jQuery(".maxLenght").bind('keyup keydown click',function () {
    MaxLength(this);
});

// Fonction appelée en boucle par setInterval ----------------------
function animateCoverFlow () {
    $('#coverflow .startup:first').animate({
        marginTop: "-125"
    }, {
        duration: 400,
        specialEasing: {
            marginTop: 'easeOutBack'
        },
        complete : function() {
            $(this).attr('style', 'height:121px;');
            $(this).appendTo($('#coverflow'));
        }
    });
}
// ... pareil pour INTERNET EXPLORER -------------------------------
function animateCoverFlowIE () {
    $('#coverflow .startup:first').animate({
        marginTop: "-125"
    }, {
        duration: 400,
        complete : function() {
            $(this).attr('style', 'height:121px;');
            $(this).appendTo($('#coverflow'));
        }
    });
}

// Fonction pour afficher le nombre de caractères maximum d'un textarea
function MaxLength(textarea) {
    var limit = jQuery(textarea).next(".longueurMax").html();
    var cadreLongMax = jQuery(textarea).next(".longueurMax");
    var cadreInfo;

    if (cadreLongMax.next(".infoNbCaracteres").length == 1) {
        cadreInfo = cadreLongMax.next(".infoNbCaracteres");
    } else if (jQuery(textarea).prev(".infoNbCaracteres").length == 1) {
        cadreInfo = jQuery(textarea).prev(".infoNbCaracteres");
    }
    
    var length = textarea.value.length;
    if(length >= limit) {
        cadreInfo.html('Limité à '+limit+' caractères.');
        cadreInfo.addClass("red");
        textarea.value = textarea.value.substr(0,limit);
    } else {
        if ((limit - length) < 16) {
            cadreInfo.addClass("red");
        } else {
            cadreInfo.removeClass("red");
        }
        cadreInfo.html((limit - length) +' caractères restants / ' + limit);
    }
}

//Animation du menu des startups PreselectionPrecedentes
$(function(){
    //Get our elements for faster access and set overlay width
    var div = $('div.ongletCentre'),
    ul = $('ul.ongletCentre'),
    // unordered list's left margin
    ulPadding = 15;

    //Get menu width
    var divHeight = div.height();

    //Remove scrollbars
    div.css({
        overflow: 'hidden'
    });

    //Find last image container
    var lastLi = ul.find('li:last-child');

    //When user move mouse over menu
    div.mousemove(function(e){

        //As images are loaded ul width increases,
        //so we recalculate it each time
        var ulHeight= lastLi[0].offsetTop+ lastLi.outerWidth() + ulPadding;

        var top = (e.pageY - div.offset().top) * (ulHeight-divHeight) / divHeight;
        div.scrollTop(top);
    });
    
    var elementMenu = $('ul.ongletCentre li a');

//    elementMenu.mouseover(function () {
//        $(this).stop().animate({
//            fontSize: "16px",
//            paddingLeft: "18px"
//        }), 20
//    });
//
//    elementMenu.mouseout(function () {
//        $(this).stop().animate({
//            fontSize: "14px",
//            paddingLeft: "16px"
//        }), 20
//    });

    elementMenu.hover(
        function () {
            $(this).stop().animate({
                fontSize: "16px",
                paddingLeft: "18px"
            }, 50);
        },
        function () {
            $(this).stop().animate({
                fontSize: "14px",
                paddingLeft: "16px"
            }, 50);
        }
        );


});



