﻿function ContestCountdownStart(intContestId) {
    $j.ajax({
        type: "GET",
        url: "/Api/Contest/End-In-Seconds.ashx?ContestId=" + intContestId,
        dataType: "xml",
        success: function(xml) {
            $j(xml).find('EndIn').each(function() {
                var dtStart = new Date();
                dtStart.setSeconds(dtStart.getSeconds() + parseInt($j(this).text()));
                ContestCountdown(dtStart);
            });

        }
    });
}
function ContestCountdown(dtDate) {
    var dtNow = new Date();
    var intSeconds = (new Date(dtDate - dtNow)).valueOf() / 1000;

    if (intSeconds <= 1) {
        $j('.Contest-TimeLeftCountdown').html('It\'s over');
        return;
    }

    var strDisplay = '';


    intTimeLeftDays = (Math.floor(intSeconds / 86400) % 1000).toString();
    intTimeLeftHours = (Math.floor(intSeconds / 3600) % 24).toString();
    intTimeLeftMinutes = (Math.floor(intSeconds / 60) % 60).toString();
    intTimeLeftSeconds = Math.floor(intSeconds % 60).toString();

    if (intTimeLeftDays > 0) {
        if (intTimeLeftDays > 1) {
            strDisplay += intTimeLeftDays + ' days'
        } else {
            strDisplay += intTimeLeftDays + ' day'
        }
    }

    if (intTimeLeftHours > 0) {
        if (strDisplay != '') { strDisplay += ', '; }
        if (intTimeLeftHours > 1) {
            strDisplay += intTimeLeftHours + ' hours'
        } else {
            strDisplay += intTimeLeftHours + ' hour'
        }
    }

    if (intTimeLeftMinutes > 0) {
        if (strDisplay != '') { strDisplay += ', '; }
        strDisplay += intTimeLeftMinutes + ' min'
    }

    //seconds
    if (intSeconds > 0) {
        if (strDisplay != '') { strDisplay += ', '; }
        if (intTimeLeftSeconds.length == 1) {
            strDisplay += '0' + intTimeLeftSeconds + ' sec'
        } else {
            strDisplay += intTimeLeftSeconds + ' sec'
        }
    }

    $j('.Contest-TimeLeftCountdown').html(strDisplay); ;
    if (intSeconds > 0) {
        setTimeout('ContestCountdown(' + dtDate.valueOf() + ');', 1000);
    }
}

function ContestFaqToggle(strClass) {
    var blnVisible = ($j('.' + strClass).css('display') == 'block');
    $j('.Contest-EnterFaqAnswer').slideUp('fast');

    if (blnVisible != true) {
        $j('.' + strClass).slideDown('fast');
    }
    document.getElementById('HyperLinkFocusHolder').focus();
}

function FacebookShare(u) {
    t = document.title;
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
    return false; 
}