/**
 *  Pour le bandeau d'alerte
 */
var nbAlerts = 0;
var currentAlert = 0;
var oldAlert = 0;
var defaultWidth = 860;
var alertWidth;

var initMarquee = function() {
    if (jQuery("#alerts").is("div")) {
        nbAlerts = jQuery("#alerts ul li").size();
        marquee();
        if (nbAlerts > 1) {
            var controlAlertHtml = '<div class="alertControl"><a class="goToLeft" id="goToLeft" href="#">&nbsp;</a> <a class="goToRight" id="goToRight" href="#">&nbsp;</a></div>';
            jQuery(controlAlertHtml).insertAfter("#alerts");
            alertControlStatus();
            jQuery("#goToLeft").click(function () {
                if (currentAlert > 0) {
                    currentAlert--;
                    jQuery("#alerts ul li").eq(oldAlert).stop().fadeOut("slow", function() {
                        jQuery("#alerts ul li").eq(currentAlert).stop().css({left: 0}).fadeIn();
                        marquee();
                    });
                    oldAlert = currentAlert;
                    alertControlStatus();
                }
                return false;
            });
            jQuery("#goToRight").click(function () {
                if (currentAlert < nbAlerts - 1) {
                    currentAlert++;
                    jQuery("#alerts ul li").eq(oldAlert).stop().fadeOut("slow", function() {
                        jQuery("#alerts ul li").eq(currentAlert).stop().css({left: 0}).fadeIn();
                        marquee();
                    });
                    oldAlert = currentAlert;
                    alertControlStatus();
                }
                return false;
            });
        }
    }
};

var marquee = function() {
    alertWidth = jQuery("#alerts ul li").eq(currentAlert).width();
    if (alertWidth > defaultWidth) {
        jQuery("#alerts ul li").eq(currentAlert).animate({ left: -alertWidth + "px"}, 20000, function() {
            jQuery("#alerts ul li").eq(currentAlert).css("left", defaultWidth + 10 + "px");
            marquee();
        });
    }
};

var alertControlStatus = function() {
    if (currentAlert > 0) {
        jQuery("#goToLeft").css("visibility", "visible");
    }
    else {
        jQuery("#goToLeft").css("visibility", "hidden");
    }

    if (currentAlert < nbAlerts - 1) {
        jQuery("#goToRight").css("visibility", "visible");
    }
    else {
        jQuery("#goToRight").css("visibility", "hidden");
    }
};

jQuery(document).ready(function () {
    initMarquee();
    if (jQuery(".contactList").is("div")) {
        jQuery(".contactList ul li:first").css("border-top", 0);
        jQuery(".contactList ul li:last").css("border-bottom", 0);
    }
});

/**
 * Pour le rollover sur les boutons services
 */
var initRolloverServices = function() {
    jQuery("#pictosServices li").hover(function() {
        rolloverService(jQuery(this));
    }, function() {
        rolloutService(jQuery(this));
    });
};

var rolloverService = function(elt) {
    var imgOn = jQuery(elt).find(".on").text();
    jQuery(elt).find("img").attr("src", imgOn);
};

var rolloutService = function(elt) {
    var imgOff = jQuery(elt).find(".off").text();
    jQuery(elt).find("img").attr("src", imgOff);
};

jQuery(document).ready(function () {
    initRolloverServices();
});
