/*
	Author:		Robert Hashemian (http://www.hashemian.com/)
	Modified by:	Munsifali Rashid (http://www.munit.co.uk/)
*/

// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0'); if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}

function refresh()
{
	window.location.reload();
}

function countdown(obj)
{
	this.obj		= obj;
	this.Div		= "clock";
	this.TargetDate		= "12/31/2020 5:00 AM";
	this.NowDate		= Date();
	this.DisplayFormat	= "%%D%%d %%H%%h %%M%%m %%S%%s";
	this.CountActive	= true;
	this.RefreshActive	= true;
	
	this.DisplayStr;

	this.Calcage		= cd_Calcage;
	this.CountBack		= cd_CountBack;
	this.Setup		= cd_Setup;
}

function pausecomp(millis){
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}

function getPage(){
	var sPath = window.location.pathname;
	//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	return sPage;
}

function cd_Calcage(secs, num1, num2)
{
	  s = ((Math.floor(secs/num1))%num2).toString();
	  return (s);
}

function cd_CountBack(secs)
{
  if(secs <= 0){  
  	this.DisplayStr = this.DisplayFormat.replace(/%%D%%d %%H%%h %%M%%m %%S%%s/g, "-");
	this.CountActive	= false;
	//refresh();
	page = getPage();
	if (page != "messages.php" && page != "profile.php" && this.RefreshActive){
		//pausecomp(2000);
		refresh();
	}
  }else if(secs >= 1 && secs <= 59){
    this.DisplayStr = this.DisplayFormat.replace(/%%D%%d %%H%%h %%M%%m/g, "");
 	this.DisplayStr = this.DisplayStr.replace(/%%S%%/g, this.Calcage(secs,1,60));
  }else if(secs >= 60 && secs <= 3599){
    this.DisplayStr = this.DisplayFormat.replace(/%%D%%d %%H%%h/g, "");
  	this.DisplayStr = this.DisplayStr.replace(/%%M%%/g, this.Calcage(secs,60,60));
 	this.DisplayStr = this.DisplayStr.replace(/%%S%%/g, this.Calcage(secs,1,60));
  }else if(secs >= 3600 && secs <= 86399){
    this.DisplayStr = this.DisplayFormat.replace(/%%D%%d/g, "");
	this.DisplayStr = this.DisplayStr.replace(/%%H%%/g, this.Calcage(secs,3600,24));
  	this.DisplayStr = this.DisplayStr.replace(/%%M%%/g, this.Calcage(secs,60,60));
  	this.DisplayStr = this.DisplayStr.replace(/%%S%%s/g, "");
  }else if(secs >= 86400){  
 	this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(secs,86400,30));
  	this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
 	this.DisplayStr = this.DisplayStr.replace(/%%M%%m/g,		"");
  	this.DisplayStr = this.DisplayStr.replace(/%%S%%s/g,		"");
  }
  document.getElementById(this.Div).innerHTML = this.DisplayStr;
  if (this.CountActive) setTimeout(this.obj +".CountBack(" + (secs-1) + ")", 990);
}

function cd_Setup(NowDate)
{
	if (!this.TargetDate){
		document.getElementById(this.Div).innerHTML = "-";
	}else{
		var dthen	= new Date(this.TargetDate * 1000);
  		var dnow	= new Date(NowDate * 1000);
		ddiff		= new Date(dthen-dnow);
		gsecs		= Math.floor(ddiff.valueOf()/1000);
		this.CountBack(gsecs);
	}
}


/////// RESOURCES

function res(obj)
{
	this.obj		= obj;
	this.Div;
	this.Hourly = "0";
	this.Max = "0";
	
	this.DisplayStr;

	this.Setup		= cu_Count;
	this.Count	= cu_Count;
}

function cu_Count(res)
{
	this.DisplayStr = formatNumber(res,0,',','','','','-','');
	document.getElementById(this.Div).innerHTML = this.DisplayStr;
	
	if (res < this.Max){
		if (this.Hourly == 0){
			// do nothing
		}else if (this.Hourly < 0){
			Tick = this.Hourly * -1;
			setTimeout(this.obj +".Count(" + (res-1) + ")", (3600000/Tick));
		}else{
			setTimeout(this.obj +".Count(" + (res+1) + ")", (3600000/this.Hourly));
		}
	}
}