﻿
function createCountdown(className,endDate) {
    document.write('<span class="' + className + '" >');  
    document.write(getCountdownText(endDate));
    document.write('</span>');
    setTimeout('showCountdown(\'' + className + '\',\'' + endDate + '\');',1000);
}
function createPSNCountdown(className,endDate) {
    document.write('<div class="' + className + '" >');  
    document.write(getPSNCountdownText(endDate));
    document.write('</div>');
    setTimeout('showPSNCountdown(\'' + className + '\',\'' + endDate + '\');',1000);
}
function createPSNCountdown2(className,endDate) {
    document.write('<div class="' + className + '" >');  
    document.write(getPSNCountdownText2(endDate));
    document.write('</div>');
    setTimeout('showPSNCountdown(\'' + className + '\',\'' + endDate + '\');',1000);
}
function createIDOLCountdown(className,endDate) {
    document.write('<div class="' + className + '" >');  
    document.write(getIDOLCountdownText(endDate));
    document.write('</div>');
    setTimeout('showIDOLCountdown(\'' + className + '\',\'' + endDate + '\');',1000);
}

function showCountdown(className,endDate) { 
    var strContest = getCountdownText(endDate);     
        
    $("span." + className).html(strContest);
    setTimeout('showCountdown(\'' + className + '\',\'' + endDate + '\');',1000);

}  
function showPSNCountdown(className,endDate) { 
    var strContest = getPSNCountdownText(endDate);     
        
    $("div." + className).html(strContest);
    setTimeout('showPSNCountdown(\'' + className + '\',\'' + endDate + '\');',1000);

} 
function showPSNCountdown2(className,endDate) { 
    var strContest = getPSNCountdownText2(endDate);     
        
    $("div." + className).html(strContest);
    setTimeout('showPSNCountdown(\'' + className + '\',\'' + endDate + '\');',1000);

} 
function showIDOLCountdown(className,endDate) { 
    var strContest = getIDOLCountdownText(endDate);     
        
    $("div." + className).html(strContest);
    setTimeout('showIDOLCountdown(\'' + className + '\',\'' + endDate + '\');',1000);

} 
function getCountdownText(endDate) {
    var now = new Date();
    var contestEnds = Date.parse(endDate);
    var totalSeconds = (contestEnds - now)/(1000); 
    var daysLeft, hrsLeft, minsLeft, secsLeft;
    var strCD = '';
    
    if (totalSeconds <= 0) {
       strCD = 'CASTING ENDED';
    }
    else {
        daysLeft = Math.floor(totalSeconds/(24*60*60));
        totalSeconds = totalSeconds - (daysLeft*(24*60*60));
        if(daysLeft > 0)
            strCD += '<span class="days">' + daysLeft + '</span> days ';
            
        hrsLeft = Math.floor(totalSeconds/(60*60));
        totalSeconds = totalSeconds - (hrsLeft*(60*60));
        if(hrsLeft > 0)
            strCD += '<span class="hours">' + hrsLeft + '</span> hrs ';
        
        minsLeft = Math.floor(totalSeconds/(60));
        totalSeconds = totalSeconds - (minsLeft*(60));
        if(minsLeft > 0)
            strCD += '<span class="minutes">' + minsLeft + '</span> mins ';
        
        secsLeft = totalSeconds;
        if(secsLeft > 0)
            strCD += '<span class="seconds">' + Math.round(secsLeft) + '</span> secs '; 
    }
    
    return strCD;
}
function getPSNCountdownText(endDate) {
    var now = new Date();
    var contestEnds = Date.parse(endDate);
    var totalSeconds = (contestEnds - (now)) / (1000);
    var daysLeft, hrsLeft, minsLeft, secsLeft;
    var strCD = '';

    if (totalSeconds <= 0) {
        strCD = 'CASTING ENDED';
    }
    else {
        daysLeft = Math.floor(totalSeconds / (24 * 60 * 60));
        totalSeconds = totalSeconds - (daysLeft * (24 * 60 * 60));
        if (daysLeft > 0)
            strCD += '<div class="countdownUnit"><div class="countdownValue">' + daysLeft + '</div><div class="countdownLabel">days</div></div>';

        hrsLeft = Math.floor(totalSeconds / (60 * 60));
        totalSeconds = totalSeconds - (hrsLeft * (60 * 60));
	hrsLeft -=1;
	if (hrsLeft >= 6) hrsLeft -=6;
        if (hrsLeft > 0)
            strCD += '<div class="countdownUnit"><div class="countdownValue">' + hrsLeft + '</div><div class="countdownLabel">hrs</div></div>';

        minsLeft = Math.floor(totalSeconds / (60));
        totalSeconds = totalSeconds - (minsLeft * (60));
        if (minsLeft > 0)
            strCD += '<div class="countdownUnit"><div class="countdownValue">' + minsLeft + '</div><div class="countdownLabel">mins</div></div>';

        secsLeft = totalSeconds;
        if (secsLeft > 0)
            strCD += '<div class="countdownUnit"><div class="countdownValue">' + Math.round(secsLeft) + '</div><div class="countdownLabel">secs</div></div>';
    }

    return strCD;
}
function getPSNCountdownText2(endDate) {
    var nowUTC = Date.parse((new Date()).toUTCString());
    var contestEnds = Date.parse(endDate);
    var totalSeconds = (contestEnds - (nowUTC)) / (1000);
    var daysLeft, hrsLeft, minsLeft, secsLeft;
    var strCD = '';

    if (totalSeconds <= 0) {
        strCD = 'CASTING ENDED';
    }
    else {
        daysLeft = Math.floor(totalSeconds / (24 * 60 * 60));
        totalSeconds = totalSeconds - (daysLeft * (24 * 60 * 60));
        if (daysLeft > 0)
            strCD += '<div class="countdownUnit"><div class="countdownValue">' + daysLeft + '</div><div class="countdownLabel">days</div></div>';

        hrsLeft = Math.floor(totalSeconds / (60 * 60));
        totalSeconds = totalSeconds - (hrsLeft * (60 * 60));
        if (hrsLeft > 0)
            strCD += '<div class="countdownUnit"><div class="countdownValue">' + hrsLeft + '</div><div class="countdownLabel">hrs</div></div>';

        minsLeft = Math.floor(totalSeconds / (60));
        totalSeconds = totalSeconds - (minsLeft * (60));
        if (minsLeft > 0)
            strCD += '<div class="countdownUnit"><div class="countdownValue">' + minsLeft + '</div><div class="countdownLabel">mins</div></div>';

        secsLeft = totalSeconds;
        if (secsLeft > 0)
            strCD += '<div class="countdownUnit"><div class="countdownValue">' + Math.round(secsLeft) + '</div><div class="countdownLabel">secs</div></div>';
    }

    return strCD;
}

function getIDOLCountdownText(endDate) {
    var nowLocal = new Date();
    var contestEnds = Date.parse(endDate);
    var offset = nowLocal.getTimezoneOffset()/60;

    var totalSeconds = (contestEnds - nowLocal) / (1000);
    totalSeconds = totalSeconds - ((offset - 4) * 3600);
    var daysLeft, hrsLeft, minsLeft, secsLeft;
    var strCD = '';

    if (totalSeconds <= 0) {
        strCD = 'AUDITION ENDED';
    }
    else {
        daysLeft = Math.floor(totalSeconds / (24 * 60 * 60));
        totalSeconds = totalSeconds - (daysLeft * (24 * 60 * 60));

            strCD += '<span class="days">' + pad(daysLeft, 2) + '</span>';

        hrsLeft = Math.floor(totalSeconds / (60 * 60));
        totalSeconds = totalSeconds - (hrsLeft * (60 * 60));
            strCD += '<span class="hours">' + pad(hrsLeft, 2) + '</span>';

        minsLeft = Math.floor(totalSeconds / (60));
        totalSeconds = totalSeconds - (minsLeft * (60));
            strCD += '<span class="minutes">' + pad(minsLeft, 2) + '</span>';

        secsLeft = totalSeconds;
            strCD += '<span class="seconds">' + pad(Math.round(secsLeft), 2) + '</span>';
    }

    return strCD;
}

function pad(n, len) {
	s = n.toString();
	if (s.length < len) {
		s = ('00' + s).slice(-len);
	}
	return s;
}

function getRemainingDays(endDate) 
{
    var now = new Date();
    var contestEnds = Date.parse(endDate);
    var totalSeconds = (contestEnds - now) / (1000);
 
    if (totalSeconds <= 0)  
        return "0"; 
    else 
        return Math.floor(totalSeconds / (24 * 60 * 60));


}

function getRemainingHours(endDate) {
    var now = new Date();
    var contestEnds = Date.parse(endDate);
    var totalSeconds = (contestEnds - now) / (1000);
    var daysLeft, hrsLeft;
    var strCD;

    if (totalSeconds <= 0) {
        return "0";
    }
    else {
        daysLeft = Math.floor(totalSeconds / (24 * 60 * 60));
        totalSeconds = totalSeconds - (daysLeft * (24 * 60 * 60));
      
        hrsLeft = Math.floor(totalSeconds / (60 * 60));
        totalSeconds = totalSeconds - (hrsLeft * (60 * 60));

        if (hrsLeft < 0)
            hrsLeft = 0;
            
        return hrsLeft; 
    }

}

function getRemainingMinutes(endDate) {
    var now = new Date();
    var contestEnds = Date.parse(endDate);
    var totalSeconds = (contestEnds - now) / (1000);
    var daysLeft, hrsLeft, minsLeft; 
    
    if (totalSeconds <= 0) {
        return "0";
    }
    else {
        daysLeft = Math.floor(totalSeconds / (24 * 60 * 60));
        totalSeconds = totalSeconds - (daysLeft * (24 * 60 * 60));
        
        hrsLeft = Math.floor(totalSeconds / (60 * 60));
        totalSeconds = totalSeconds - (hrsLeft * (60 * 60));
        
        minsLeft = Math.floor(totalSeconds / (60));
        totalSeconds = totalSeconds - (minsLeft * (60));
        if (minsLeft < 0)
            minsLeft = 0;
            
        return minsLeft; 
    }
}

function getRemainingSeconds(endDate) {
    var now = new Date();
    var contestEnds = Date.parse(endDate);
    var totalSeconds = Math.floor((contestEnds - now) / (1000));
    var daysLeft, hrsLeft, minsLeft, secsLeft; 

    if (totalSeconds <= 0) {
        return "0";
    }
    else {
        daysLeft = Math.floor(totalSeconds / (24 * 60 * 60));
        totalSeconds = totalSeconds - (daysLeft * (24 * 60 * 60));
         
        hrsLeft = Math.floor(totalSeconds / (60 * 60));
        totalSeconds = totalSeconds - (hrsLeft * (60 * 60));
         
        minsLeft = Math.floor(totalSeconds / (60));
        totalSeconds = totalSeconds - (minsLeft * (60));
      
        secsLeft = totalSeconds;
        if (secsLeft < 1)
            secsLeft = 0; 
        
        return Math.floor(secsLeft); 
    }

    return "0";
}
