
var position = 0;
var max = 0;

$(document).ready(function(){

    Shadowbox.init({
        modal:true
    });

    $(".footer ul.social img").width("25px").height("25px");
    //Mouse hover effect
    $(".partners img").imghover({
        suffix: "-on",
        preload: true
    });
    $(".partners-2 img").imghover({
        suffix: "-on",
        preload: true
    });
    $(".social img").imghover({
        suffix: "-on",
        preload: true
    });

    $("#contact a").click(function(){
        $(".light-box-white").fadeIn();
        return false;
    });

    $(".form-contact").submit(function(){
        var nom = $(".form-contact input[name=nom]").val();
        var mail = $(".form-contact input[name=mail]").val();
        var message = $(".form-contact textarea").val();
        $.post("content/externals/contact.php", {
            nom: nom,
            mail: mail,
            message: message
        }, function(data){
            $(".contact-contain .result").remove();
            if(data == "success"){
                $(".contact-contain .info").after($('<p class="result">Votre message a bien été envoyé !</p>'));
                setTimeout($(".light-box-white").fadeOut, 1500);
                $(".form-contact input").val("");
                $(".form-contact textarea").val("");
            } else {
                $(".contact-contain .info").after($('<p class="result error">Une erreur est survenue</p>'));
            }
        });

        return false;
    });

    $(".light-box-white .btn-close").click(function(){
        $(".light-box-white").fadeOut();
        return false;
    });

    $(".slideshow.XL").each(function(){

        //SLIDESHOW
        currentIndex = 0;
        widthPicture = 1000;
        heightDescription = 383;
		
        $(".reticule").addClass("hidden");

        var instance = $(this);

        instance.find(".item").mouseover(function(){
            position = $(this).position();
            posLeft = position.left - 28;
            $(this)
            .find(".reticule")
            .removeClass("hidden")
            .css("left",posLeft);
            return false;
        }).mouseout(function(){
            $(".reticule").addClass("hidden");
            return false;
        });

        if(!instance.hasClass("disabled")){
            instance.find(".inner-pict").wrap("<div class='wrapper-pict'></div>");
            instance.find(".inner-desc").wrap("<div class='wrapper-desc'></div>");

            instance.find(".nav .item")
            .eq(currentIndex)
            .addClass("selected")
            .find(".inner-pict > div").show();

            instance.find(".last").click(function(){
                instance.find(".inner-pict > div").show();
                if(currentIndex < instance.find(".nav .item").length - 1){
                    currentIndex += 1;
                } else{
                    currentIndex = 0;
                }
                movePictureAndDescription(currentIndex);
                return false;
            });

            instance.find(".first").click(function(){
                instance.find(".inner-pict > div").show();

                if(currentIndex > 0){
                    currentIndex -= 1;
                } else {
                    currentIndex = instance.find(".nav .item").length - 1;
                }

                movePictureAndDescription(currentIndex);
                return false;
            });

            instance.find(".nav .item").click(function(){
                itemSelected = $(this).index() - 1;
                currentIndex = itemSelected;

                movePictureAndDescription(itemSelected);

                return false;
            });
        }

        function movePictureAndDescription(index){
            instance.find(".nav .item")
            .removeClass("selected")
            .eq(index)
            .addClass("selected");

            picturePosition = -index * widthPicture;
            descriptionPosition = -index * heightDescription;

            instance.find(".inner-pict").stop().animate({
                marginLeft: picturePosition
            }, "slow");
            instance.find(".inner-desc").stop().animate({
                marginTop: descriptionPosition
            }, "slow");
        }
    });

    $(".XL .inner-pict").css("width", $(".XL .inner-pict .picture").size() * 1000 + "px");
    $(".slideshow ul.nav").css("margin-left", 360 - ($(".slideshow ul.nav li").size()-3) * 13 + "px");

    $("li.video .btn-more, .description .btnProd").click(function() {
        var lien = $(this).attr("name");

        $("#modal").fadeIn();
        $("#lightbox").fadeIn();
		
        showVideoSlider(lien);
        return false;
    });

    $("#modal").click(function() {
        $("#lightbox").fadeOut();
        $("#modal").fadeOut();
        $("#lightbox #viewer .video").html("");
    });

    $("img.leftArrow").hide();
    max = $(".videos .gallery li").size();
    $("img.rightArrow").click(function() {
        $(".videos .gallery").animate({
            "left": "-=798px"
        }, "slow");
        position = parseInt(position) + 3;
        if(position > (max - 3)) $("img.rightArrow").hide();
        else $("img.rightArrow").show();
        $("img.leftArrow").show();
    });
    $("img.leftArrow").click(function() {
        $(".videos .gallery").animate({
            "left": "+=798px"
        }, "slow");
        position = parseInt(position) - 3;
        if(position < 3) $("img.leftArrow").hide();
        else $("img.leftArrow").show();
        $("img.rightArrow").show();
    });

    $(".videos .gallery").wrap('<div class="wrapVideo" />');
    var newWidth = $(".videos .gallery li").size() * 266 + "px";
    $(".wrapVideo").width("798px").addClass("left");
    $(".videos .gallery").width(newWidth).css("position", "relative");

    $(".biographie .viewport").width("1050px");

});

function showVideoSlider(lien){
    $('#viewer .video').empty().html(lien);
    $('#viewer .video iframe').width("600px").height("400px");
    return false;
}

function showVideo(id){
    $.get("content/externals/getVideo.php", {
        id: id
    }, function(data){
        $('#viewer .video').empty().html(data);
        $('#viewer .video iframe').width("374px").height("299px");
    });
}
