﻿/* - - - - - - - - - - - - - - - - - - - - - - -
 JavaScript
 dimanche 22 mars 2009 19:50:41
 @author : Thomas RAMBAUD
 - - - - - - - - - - - - - - - - - - - - - - - */
var duration = 8000;
var imgs = new Array();
var current_img = 0;
var next_img = 1;
var timer = null;
var timer_change = null;
var is_ready = true;
var animate_speed = 85;
var animate_width = null;
var logos_width = 580;
var animated = true;

function initDiapo(){
    var i = 0;
    $(".ElementsContent .DiapoItem").each(function() {
        imgs[i] = $(this).attr("id");
        i++;
    });

    logos_width = withoutPx($(".vignettes").css("width"));
    animate_width = $(".vignetteInside").children("ul").children("li").length * animate_speed;
    if (animate_width > logos_width) {
        $(".ArrowLeft a").click(function() {
            animateLeft();
        });
        $(".ArrowRight a").click(function() {
            animateRight();
        });
    }
    $(".vignetteInside ul li a img").each(function(index) {
        if (index == 0) {
            var base = $(this).attr("rel");
            var hover = $(this).attr("src");
        }
        else {
            var base = $(this).attr("src");
            var hover = $(this).attr("rel");
        }
        $(this).hover(
            function() {
                var m = $(this);
                if (m.attr("class").indexOf("ImgOn") < 0) {
                    m.attr("src", hover);
                    m.attr("rel", base);
                    m.addClass("ImgHover");
                }
            },
            function() {
                var m = $(this);
                if (m.attr("class").indexOf("ImgOn") < 0) {
                    m.attr("src", base);
                    m.attr("rel", hover);
                    m.removeClass("ImgHover");
                }
            }
        );
    });
        
    if(i > 1) initTimers();
}

function fadeThis(id) {
    if (is_ready && id != imgs[current_img]) {
        is_ready = false;
        //animated = false;
        for (i = 0; i < imgs.length; i++) {
            if (imgs[i] == id) {
                next_img = i;
                break;
            }
        }
        clearTimeout(timer);
        timer_change = self.setTimeout("makeItReady()",800);
        fadeInOut();           
    }
}

function makeItReady(){
    is_ready = true;
    clearTimeout(timer_change);
}

function initTimers(){
  timer = self.setTimeout("fadeInOut()",duration);
  timer_change = null;
}

function fadeInOut() {
    // PRECEDING
    $("#" + imgs[current_img]).fadeOut("slow");
    var current_selector = $("#Selector" + imgs[current_img]);
    var current_selector_img = current_selector.children("img");
    var current_selector_img_src = current_selector_img.attr("src");
    var current_selector_img_rel = current_selector_img.attr("rel");
    current_selector.removeClass("On");
    current_selector_img.removeClass("ImgOn");
    current_selector_img.attr("rel", current_selector_img_src);
    current_selector_img.attr("src", current_selector_img_rel);
    // SIBBLING    
    $("#" + imgs[next_img]).fadeIn("slow");
    var next_selector = $("#Selector" + imgs[next_img]);
    var next_selector_img = next_selector.children("img");
    var next_selector_img_src = next_selector_img.attr("src");
    var next_selector_img_rel = next_selector_img.attr("rel");
    next_selector.addClass("On");
    next_selector_img.addClass("ImgOn");
    if (next_selector_img.attr("class").indexOf("ImgHover") < 0) {
        next_selector_img.attr("rel", next_selector_img_src);
        next_selector_img.attr("src", next_selector_img_rel);
    }

    if (animated) {
        if (next_img != 0) {
            if (current_img >= 3 && current_img < imgs.length - 4) {
                animateRight();
            }
        }
        else {
            $(".vignetteInside").animate({ left: 0 }, 'slow');
            showHideArrows(0);
        }
    }
    
    if(next_img == (imgs.length - 1)){
       current_img = next_img;
       next_img = 0;
    }
    else{
       current_img = next_img;
       next_img++;
   }

   if (animated) {
       initTimers();
   }
}
function animateRight() {
    var new_left = getScrollNavLeft() - animate_speed;
    if ((-new_left + logos_width) >= animate_width) { new_left = -animate_width + logos_width + 15; }
    showHideArrows(new_left);
    $(".vignetteInside").animate({ left: new_left }, 'slow');
}

function animateLeft() {
    var new_left = getScrollNavLeft() + animate_speed;
    if (new_left > 0) { new_left = 0; }
    showHideArrows(new_left);
    $(".vignetteInside").animate({ left: new_left }, 'slow');
}

function getScrollNavLeft() {
    return withoutPx($(".vignetteInside").css("left"));
}

function withoutPx(val) {
    return parseInt(val.substring(0, val.indexOf('px')));
}

function showHideArrows(new_left) {
    if (new_left == 0) {
        $('.ArrowLeft').hide();
    }
    else {
        $('.ArrowLeft').show();
    }
    if ((-new_left + logos_width) >= animate_width - 20) {
        $('.ArrowRight').hide();
    }
    else {
        $('.ArrowRight').show();
    }
}



