﻿/* - - - - - - - - - - - - - - - - - - - - - - -
 JavaScript
 dimanche 22 mars 2009 19:50:41
 @author : Thomas RAMBAUD
 - - - - - - - - - - - - - - - - - - - - - - - */
var duration = 7000;
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 = 300;
var animate_width = null;
var logos_width = 740;
var animated = true;

function initCaroussel(){
    var i = 0;
    $(".ElementsContent .CarousselItem").each(function() {
        imgs[i] = $(this).attr("id");
        i++;
    });
    
    logos_width = withoutPx($(".Logos").css("width"));
    animate_width = $(".LogosInside").children("ul").children("li").length * 76;
    if (animate_width > logos_width) {
        $(".ArrowLeft a").click(function() {
            animateLeft();
        });
        $(".ArrowRight a").click(function() {
            animateRight();
        });
    }
    var index = 0;
    $(".LogosInside ul li a img").each(function() {
        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() {
                if ($(this).attr("class").indexOf("ImgOn") < 0) {
                    $(this).attr("src", hover);
                    $(this).attr("rel", base);
                    $(this).addClass("ImgHover");
                }
            },
            function() {
                if ($(this).attr("class").indexOf("ImgOn") < 0) {
                    $(this).attr("src", base);
                    $(this).attr("rel", hover);
                    $(this).removeClass("ImgHover");
                }
            }
        );
        index++;
    });
        
    if(i > 1) initTimers();
}

function fadeThis(id) {
    if (is_ready && id != imgs[current_img].substring(8, imgs[current_img].length)) {
        is_ready = false;
        animated = false;
        for (i = 0; i < imgs.length; i++) {
            if (imgs[i].substring(8, imgs[i].length) == 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].substring(8, imgs[current_img].length));
    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.children("img").attr("rel", current_selector_img_src);
    current_selector.children("img").attr("src", current_selector_img_rel);
    // SLIBBING
    $("#" + imgs[next_img]).fadeIn("slow");
    var next_selector = $("#Selector" + imgs[next_img].substring(8, imgs[next_img].length));
    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) {
            var new_left = getScrollNavLeft() - 75;
            if (new_left - 740 <= -animate_width) new_left = -animate_width + logos_width;
            $(".LogosInside").animate({ left: new_left }, 'slow');
        }
        else {
            $(".LogosInside").animate({ left: 0 }, 'slow');
        }
    }
    
    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 - 740 <= -animate_width) new_left = -animate_width + logos_width;
    $(".LogosInside").animate({ left: new_left }, 'slow');
}

function animateLeft() {
    var new_left = getScrollNavLeft() + animate_speed;
    if (new_left > 0) { new_left = 0; }
    $(".LogosInside").animate({ left: new_left }, 'slow');
}

function getScrollNavLeft() {
    return withoutPx($(".LogosInside").css("left"));
}

function withoutPx(val) {
    return parseInt(val.substring(0, val.indexOf('px')));
}


