﻿
var promos = new Array();

function checkPromo() {
    //Récupère la schedule des promotions
    $.get("promo/schedule.xml", {}, function (xml) {

        //Boucle dans chacune des promotions
        $("promo", xml).each(function () {
            var from = new Date
            var to = new Date();
            var today = new Date();
            today.setMonth(today.getMonth() + 1);
            eval("from.setFullYear(" + $(this).attr("from") + ")");
            eval("to.setFullYear(" + $(this).attr("to") + ")");
            ttoday = today.getTime();
            f = from.getTime();
            t = to.getTime();
            //Si la promo est active aujourd'hui
            if (f <= ttoday & ttoday <= t) {
                promos[promos.length] = new Array($(this).find("linkto").text(), $(this).find("image").text(), $(this).find("delay").text());
            }
        });

        //Si il y a des promos
        if (promos.length != 0) {
            //Choisir une aléatoire
            randomPromo = Math.floor(Math.random() * promos.length);
            //Appliquer la bannière, le lien, le délai
            $("#promo").attr("link", promos[randomPromo][0]);
            $("#promo").css("background", "transparent url('" + promos[randomPromo][1] + "')");
            $("#promo").show().click(function () {
                window.location = $(this).attr("link");
            });

            $("#promo").tickfn = setInterval(function () { killPromo() }, (promos[randomPromo][2] * 1000));
        } else {
            //Si aucune promo, on affiche les commentaires
            populateComments();
        }
    });

}

function killPromo() {
    clearInterval($("#promo").tickfn);
    $("#promo").fadeTo("fast", 0.1, function () {
        //une fois la promo disparue, on affiche les commentaires
        populateComments();
        $(this).remove();
    });


}

function populateComments() {
    //Récupère les commentaires
    $.get("get-commentaires.aspx", {}, function (data) {
        //Si ce n'est pas le premier get, on fait que repopuler la liste
        if ($("#comments li").length != 0) {
            $("#comments").append(data);
            $("#comments li").not(":eq(0)").hide().end();
        }
        //Si le premier get -- on remplit la liste et démarre l'afficheur
        else {
            $("ul#comments").append(data).commentsTicker();
            $("ul#comments").find("li").click(function () {
                window.location = "http://www.netmaths.net/commentaires.aspx";
            });
        }
    });
}



(function ($) {

    $.fn.commentsTicker = $.fn.commentsticker = function (delay) {
        delay = delay || 5000;
        initTicker = function (el) {
            stopTicker(el);
            el.items = $("li", el);
            // cacher les éléments de la liste
            el.items.not(":eq(0)").hide().end();
            el.currentitem = 0;
            el.lastitem = el.items.length;
            startTicker(el);
        };
        startTicker = function (el) {
            el.tickfn = setInterval(function () { doTick(el) }, delay)
        };
        stopTicker = function (el) {
            clearInterval(el.tickfn);
        };
        pauseTicker = function (el) {
            el.pause = true;
        };
        resumeTicker = function (el) {
            el.pause = false;
        };
        doTick = function (el) {
            // gestion de la pause onmouseover
            if (el.pause) return;
            // pause tant que le fade est pas finit
            el.pause = true;
            // on tu le commentaires affiché
            $("li:first", el).fadeOut("slow",
			function () {
			    $(this).fadeOut("slow", function () { $(this).remove(); });

			    $("li:first", el).fadeIn("slow",
				function () {
				    el.pause = false;
				    if ($("li", el).length == 3) {
				        populateComments();
				    }
				}
				);
			}
		);
        };
        this.each(
		function () {
		    if (this.nodeName.toLowerCase() != "ul") return;
		    initTicker(this);
		}
	)
	.addClass("commentsticker")
	.hover(
		function () {
		    // pause si over
		    pauseTicker(this);
		},
		function () {
		    // resume si n'est plus over
		    resumeTicker(this);
		}
	);
        return this;
    };

})(jQuery);
