﻿/* --------------------------------------------------------------- */
/* Ce code ne doit pas être dans un $(document).ready() */
var myriadPro = { src: 'scripts/sifr/MyriadPro.swf' };
var myriadProCond = { src: 'scripts/sifr/MyriadProCond.swf ' };
sIFR.activate(myriadPro, myriadProCond);
/* --------------------------------------------------------------- */

var currentIndex = 1;
var currentTimeout = null;
var loadingTimeout = null;
var switchDelay = 5000; /* milliseconde */

$(document).ready(function() {
    var imagesList = $("<ul class=\"images\"></ul>");
    $("#animation ul li").each(function() {
        var a = $(this).find("a");
        var url = a.attr("href");
        var img = $(this).find("img");
        var index = $(this).attr("class").replace("l", "");
        var title = a.html();
        var back = $("<div class=\"back\"></div>");
        var title1 = $("<span class=\"l" + index + " title\">" + title + "</span>");
        var title2 = $("<span class=\"title2\">" + title + "</span>");
        $(this).append(back);
        a.html("&nbsp;").append(title1).append(title2);

        var li = $("<li class=\"l" + index + "\"></li>");
        li.append(img);
        var li_a = $("<a href=\"" + url + "\" class=\"plus\">En savoir plus</a>");
        li.append(li_a);
        imagesList.append(li);

    });
    $("#animationImage").append(imagesList);

    sIFR.replace(myriadProCond, {
        selector: 'body #animation span.title',
        css: '.sIFR-root { color: #FFFFFF; font-size:20px;}',
        wmode: 'transparent',
        forceSingleLine: true,
        onRelease: function(fi) {
            var index = fi.getAncestor().className.replace(" title sIFR-replaced", "").replace("l", "");
            clearTimeout(currentTimeout);
            SetIndex(index);
        }
    });
    sIFR.replace(myriadPro, {
        selector: 'body #animation span.title2',
        css: '.sIFR-root{color:#FFFFFF; font-size:22px;}',
        wmode: 'transparent',
        forceSingleLine: true
    });

    loadingTimeout = setTimeout(LoadingCheck, 1000);

    $("#animation ul.items li").click(function() {
        clearTimeout(currentTimeout);
        if (!$(this).hasClass("hover")) {
            var index = $(this).attr("class").replace("l", "");
            SetIndex(index);
        }
        return false;
    });
});

function SetIndex(Index) {
	$("ul.images li.hover").removeClass("hover");
	$("ul.images li.l" + Index).addClass("hover");
	$("ul.items li.hover").removeClass("hover");
	$("ul.items li.l" + Index).addClass("hover");
	currentIndex = Index;
}

function SwitchIndex() {
	var Index = ++currentIndex;
	if (Index > 8) Index = 1;
	SetIndex(Index);
	currentTimeout = setTimeout(SwitchIndex, switchDelay);
}

function LoadingCheck() {
	if (isIE6) {
		clearTimeout(loadingTimeout);
		$("#animation").removeClass("loading");
		SetIndex(currentIndex);
		currentTimeout = setTimeout(SwitchIndex, switchDelay);
	} else {
		if ($('.sIFR-replaced').length == 16) {
			clearTimeout(loadingTimeout);
			$("#animation").removeClass("loading");
			SetIndex(currentIndex);
			currentTimeout = setTimeout(SwitchIndex, switchDelay);
		} else {
			loadingTimeout = setTimeout(LoadingCheck, 500);
		}
	}
}
