function getCC()
{
	var i=document.location.href.length;
	var reversedURL = "";
	for (i; i > 0; i--)
	{
		reversedURL += (document.location.href.charAt(i));
	}
	CCs = reversedURL.split(".");
	CC = CCs[1].charAt(1)+CCs[1].charAt(0);
	return CC;
}

function getEventURL()
{
    baseURL = document.location.href.split('/');
    return baseURL[2];

}

function getEvent()
{
    baseURL = document.location.href.split('/');
    return baseURL[3];
}

/*
 * ========================
 * --- Classe countdown ---
 * ========================
 * 
 * Thierry : 23.02.2010
 * 
 * 0.0.1 ( 26.02.2010 ) : Ajout du prefixMessage
 * 
 * */
var timeoutID;

function countdown() {
    
    _this = this;
    
	this.countdown_displayFormat  = '<p class="seconds"><span class="cd_up">%%S%%</span><span class="cd_down">sec</span></p>';
	this.countdown_displayFormat += '<p class="minutes"><span class="cd_up">%%M%%</span><span class="cd_down">min</span></p>';
	this.countdown_displayFormat += '<p class="hours"><span class="cd_up">%%H%%</span><span class="cd_down">hours</span></p>';
	this.countdown_displayFormat += '<p class="hours"><span class="cd_up">%%D%%</span><span class="cd_down">days</span></p>';


	
    this.countdown_finishMessage = "";
    this.countdown_targetDiv = "";
    this.countdown_prefixMessage = "";
    this.countdown_finishMessage = "";
    
    this.countdown_dsp = function(seconds) 
    {
        
        if ( seconds < 0 )
        {
            document.getElementById(this.countdown_targetDiv).innerHTML = this.countdown_finishMessage;
            return;
        }

        _html = '<span class="countdownTime"><span class="countdownPrefix">' + this.countdown_prefixMessage + '</span>' + this.countdown_displayFormat + '</span>';
        
        _html = _html.replace(/%%D%%/g, this.countdown_transformSeconds(seconds,86400,100000));
        _html = _html.replace(/%%H%%/g, this.countdown_transformSeconds(seconds,3600,24));
        _html = _html.replace(/%%M%%/g, this.countdown_transformSeconds(seconds,60,60));
        _html = _html.replace(/%%S%%/g, this.countdown_transformSeconds(seconds,1,60));

        document.getElementById(this.countdown_targetDiv).innerHTML = _html;

        timeoutID = setTimeout("_this.countdown_dsp(" + (seconds-1) + ")", 1000);
    }
    
    this.countdown_transformSeconds = function(seconds, num1, num2)
    {
        var result = ((Math.floor(seconds/num1))%num2).toString();
        if (result.length < 2) result = "0" + result;
        return result;
    }
    
    this.start = function(seconds,countdown_targetDiv,countdown_prefixMessage,countdown_finishMessage)
    {
        this.countdown_targetDiv = countdown_targetDiv;
        this.countdown_prefixMessage = countdown_prefixMessage;
        this.countdown_finishMessage = countdown_finishMessage;
        this.countdown_dsp(seconds);
    }
    
}

 
// ====================================== //
// --- Recuperation de l'event Status --- //
// ====================================== //
    
function refreshEventStatus()
{
    clearTimeout(timeoutID);
    quiksilverLive_eventStatus = '';

    $.ajax({
        async: true, 
        global: false, 
        type: "POST",
        url: "http://"+getEventURL()+"/soap/quiksilverlive-eventstatus.php",
        data: "event="+getEvent()+"&CC="+getCC(),
        success: function(json_string)
        {
            quiksilverLive_eventStatus = eval('(' + json_string + ')');
            //console.log(quiksilverLive_eventStatus);
            
            $('#quiksilverlive-eventStatus').html(quiksilverLive_eventStatus.status);
            
            // Decompte :
            
            if ( quiksilverLive_eventStatus.countdown == 1 )
            {
                var decompte = new countdown();
                var seconds = quiksilverLive_eventStatus.countdown_seconds;
                var prefixMessage = quiksilverLive_eventStatus.countdown_prefixMessage;
                var finishMessage = quiksilverLive_eventStatus.countdown_finishMessage;
                decompte.start(seconds,'quiksilverlive-eventStatus-countdown',prefixMessage,finishMessage); // Durée en secondes, div cible, message final
            }
            
            // --- Timeline
                
            var nbDays = quiksilverLive_eventStatus.timeline.nbDays;
            var currentDay = quiksilverLive_eventStatus.timeline.currentDay;					
            var timelineString = '';
            
            for (var i = 1; i <= nbDays; i++){
                var current = '';
                var day = '';
                if(i==currentDay)
                {
                    current = 'currentDay';
                    day = 'Day'	
                }

                    
                timelineString += '<p class="day '+current+'"><span class="up">'+day+'</span> <span class="down">'+i+'</span></p>';
            };
        
            $('#timeline #days').html(timelineString);
        
            // --- Refresh toutes les 5mn ( 300000 )
            
            setTimeout("refreshEventStatus()", 600000);
            
        }
    });
}
