﻿
function createCountdown(className,endDate) {
    document.write('<span class="' + className + '" >');  
    document.write(getCountdownText(endDate));
    document.write('</span>');
    setTimeout('showCountdown(\'' + className + '\',\'' + endDate + '\');',1000);
}

function showCountdown(className,endDate) { 
    var strContest = getCountdownText(endDate);     
        
    $("span." + className).html(strContest);
    setTimeout('showCountdown(\'' + 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 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";
}