/*
	Author:		Robert Hashemian (http://www.hashemian.com/)
	Modified by:	Munsifali Rashid (http://www.munit.co.uk/)
*/
// var predPrefix = 'ctl00_ctl05_'; // Predifined values of Year, Month, Day, Minute and Second. Translatable.
// var localPrefix = 'ctl00_CPH_'; // Prefix for the local controls

function countdown(obj)
{
	this.obj		= obj;
	this.CountActive = true;
	this.cap1 = "";
	this.cap2 = "";
	this.dat1 = "";
	this.dat2 = "";
	this.label = "";

	this.Calcage		= cd_Calcage;
	this.CountBack		= cd_CountBack;
	this.Setup		= cd_Setup;
}

function cd_Calcage(secs, num1, num2)
{
  s = ((Math.floor(secs/num1))%num2).toString();
  // if (s.length < 2) s = "0" + s;
  return (s);
}
function cd_CountBack(secs) {

//    this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g, this.Calcage(secs, 86400, 100000) < 0 ? 0 : this.Calcage(secs, 86400, 100000));
//    this.DisplayStr = this.DisplayStr.replace(/%%H%%/g, this.Calcage(secs, 3600, 24) < 0 ? 0 : this.Calcage(secs, 3600, 24));
//    this.DisplayStr = this.DisplayStr.replace(/%%M%%/g, this.Calcage(secs, 60, 60) < 0 ? 0 : this.Calcage(secs, 60, 60));
//    this.DisplayStr = this.DisplayStr.replace(/%%S%%/g, this.Calcage(secs, 1, 60) < 0 ? 0 : this.Calcage(secs, 1, 60));
//    
    var DD = this.Calcage(secs, 86400, 100000) < 0 ? 0 : this.Calcage(secs, 86400, 100000);
    var HH = this.Calcage(secs, 3600, 24) < 0 ? 0 : this.Calcage(secs, 3600, 24);
    var MM = this.Calcage(secs, 60, 60) < 0 ? 0 : this.Calcage(secs, 60, 60);
    var SS = this.Calcage(secs, 1, 60) < 0 ? 0 : this.Calcage(secs, 1, 60);


    // setLabel('testDiv', getValue('HdnMinutes'));
    // document.getElementById('testDiv').innerHTML = 'THIS IS IT';

    if (HH == 0 && DD == 0) { // Minutes & Seconds
        setLabel(this.cap1, MM == 0 ? '' : getValue(predPrefix + 'HdnMinute' + (MM == 1 ? '' : 's')));
        setLabel(this.cap2, getValue(predPrefix + 'HdnSecond' + (SS == 1 ? '' : 's')));

        setLabel(this.dat1, MM == 0 ? '' : MM);
        setLabel(this.dat2, SS);
    }

    if (HH > 0 && DD == 0) { // Hours & Minutes
        setLabel(this.cap1, getValue(predPrefix + 'HdnHour' + (HH == 1 ? '' : 's')));
        setLabel(this.cap2, MM == 0 ? '' : getValue(predPrefix + 'HdnMinute' + (MM == 1 ? '' : 's')));

        setLabel(this.dat1, HH);
        setLabel(this.dat2, MM == 0 ? '' : MM);
    }

    if (DD > 0) { // Days and Hours
        setLabel(this.cap1, getValue(predPrefix + 'HdnDay' + (DD == 1 ? '' : 's')));
        setLabel(this.cap2, HH == 0 ? '' : getValue(predPrefix + 'HdnHour' + (HH == 1 ? '' : 's')));

        setLabel(this.dat1, DD);
        setLabel(this.dat2, HH == 0 ? '' : HH);
    }

    if (DD == 0 && HH == 0 && MM < 15) {
        ShowConnect(this.label, this.div, this.EID);
//        setLabel(this.label, getValue(localPrefix + 'hdnLiveBiddingIsOpen'));
//        setLabel(this.div, '<table><tr><td style="border: none 0px #ffffff; margin: 0px 0px 0px 0px; padding: 0px 0px 0px 0px;"><a href="/Events/Connect.aspx?EventID=' + this.EID + '" class="buttonDefault"><span>' + getValue(localPrefix + 'hdnConnect') + '</span></a></td></tr></table>');
        this.CountActive = false; 
    }   
    

  // document.getElementById(this.Div).innerHTML = this.DisplayStr;
    if (this.CountActive) {
        setTimeout(this.obj + ".CountBack(" + (secs - 1) + ")", 990);
    }
}
function cd_Setup()
{
	var dthen	= new Date(this.TargetDate);
  	var dnow	= new Date(this.CurrentDate);
  	ddiff = new Date(dthen - dnow);
  	// document.writeln('Diff: ' + String(dthen - dnow) + '<br>');
  	gsecs = Math.floor(ddiff.valueOf() / 1000);
	this.CountBack(gsecs);
}

