// The constructor should be called with
// the parent object (optional, defaults to window).
// It is taken from http://codingforums.com/showthread.php?t=10531

function Timer(){
	//this.ID = Timer.getNew();
	this.obj = (arguments.length)?arguments[0]:window;
    return this;
}

// The set functions should be called with:
// - The name of the object method (as a string) (required)
// - The millisecond delay (required)
// - Any number of extra arguments, which will all be
//   passed to the method when it is evaluated.

Timer.prototype.setInterval = function(func, msec){
    var i = Timer.getNew();
    this.ID = i;
    var t = Timer.buildCall(this.obj, i, arguments);
    Timer.set[i].timer = window.setInterval(t,msec);
    return i;
}
Timer.prototype.setTimeout = function(func, msec){
	var i = Timer.getNew();
	this.ID = i;
    Timer.buildCall(this.obj, i, arguments);
    Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
    return i;
}

// The clear functions should be called with
// the return value from the equivalent set function.

Timer.prototype.clearInterval = function(i){
	if(!Timer.set[i]) return;
    window.clearInterval(Timer.set[i].timer);
    Timer.set[i] = null;
}
Timer.prototype.clearTimeout = function(i){
	if(!Timer.set[i]) return;
    window.clearTimeout(Timer.set[i].timer);
    Timer.set[i] = null;
}

// Private data

Timer.set = new Array();

Timer.buildCall = function(obj, i, args){
	var t = "";
    Timer.set[i] = new Array();
    if(obj != window){
        Timer.set[i].obj = obj;
        t = "Timer.set["+i+"].obj.";
    }
    t += args[0]+"(";
    if(args.length > 2){
        Timer.set[i][0] = args[2];
        t += "Timer.set["+i+"][0]";
        for(var j=1; (j+2)<args.length; j++){
            Timer.set[i][j] = args[j+2];
            t += ", Timer.set["+i+"]["+j+"]";
    }}
    t += ");";
    Timer.set[i].call = t;
    return t;
}
Timer.callOnce = function(i){
	if(!Timer.set[i]) return;
    eval(Timer.set[i].call);
    Timer.set[i] = null;
}
Timer.getNew = function(){
	var i = 0;
	while(Timer.set[i]) i++;
	return i;
}


// External functions

function SetTimeText(timeSeconds, lang, ctrlWhereSet, twoFormat, days, hours, minutes, seconds)
{
	var dd, ddlong;
	var hh, hhlong;
	var mm, mmlong;
	var ss, sslong;
	
	dd = Math.floor(timeSeconds/86400);
	hh = Math.floor((timeSeconds - 86400*dd)/3600);
	mm = Math.floor((timeSeconds - 86400*dd - 3600*hh)/60);
	ss = timeSeconds - 86400*dd - 3600*hh - mm*60;
	var bold_start = "<b>";
	var bold_end = "</b>";
	if (lang == 'ru')
	{
		var last = GetLastDigit(dd);
		if (dd >= 11 && dd <= 19) { ddlong = days[0]; }
		else if (last == 1) { ddlong = days[1]; }
		else if (last >= 2 && last <= 4) { ddlong = days[2]; }
		else if (last >= 5 && last <= 9) { ddlong = days[0]; }
		else if (dd != 0 && last == 0) { ddlong = days[0]; }
		else ddlong = "";
		
		last = GetLastDigit(hh);
		if (hh >= 11 && hh <= 19) { hhlong = hours[0]; }
		else if (last == 1) { hhlong = hours[1]; }
		else if (last >= 2 && last <= 4) { hhlong = hours[2]; }
		else if (last >= 5 && last <= 9) { hhlong = hours[0]; }
		else if ((hh != 0 && last == 0) || (hh == 0 && dd != 0)) { hhlong = hours[0]; }
		else hhlong = "";
		
		if ((twoFormat && dd <= 0) || !twoFormat)
		{
			last = GetLastDigit(mm);
			if (mm >= 11 && mm <= 19) { mmlong = minutes[0]; }
			else if (last == 1) { mmlong = minutes[1]; }
			else if (last >= 2 && last <= 4) { mmlong = minutes[2]; }
			else if (last >= 5 && last <= 9) { mmlong = minutes[0]; }
			else if ((mm != 0 && last == 0) || ((dd != 0 || hh != 0) && mm == 0)) { mmlong = minutes[0]; }
			else mmlong = "";
		}
		else
			mmlong = "";
		
		if ((twoFormat && hh <= 0 && dd <= 0) || !twoFormat)
		{
			last = GetLastDigit(ss);
			if (ss >= 11 && ss <= 19) { sslong = seconds[0]; }
			else if (last == 1) { sslong = seconds[1]; }
			else if (last >= 2 && last <= 4) { sslong = seconds[2]; }
			else if (last >= 5 && last <= 9) { sslong = seconds[0]; }
			else if (last == 0) { sslong = seconds[0]; }
			else sslong = "";
		}
		else
			sslong = "";
	}
	else
	{
		if (dd == 1) { ddlong = days[2]; }
		else if (dd > 1) { ddlong = days[0]; }
		else { ddlong = ""; }
	
		if (hh == 1) { hhlong = hours[1]; }
		else if (hh > 1) { hhlong = hours[0]; }
		else if (hh == 0 && dd != 0) { hhlong = hours[0]; }
		else hhlong = "";
		
		if ((twoFormat && dd <= 0) || !twoFormat)
		{
			if (mm == 1) { mmlong = minutes[1]; }
			else if (mm > 1) { mmlong = minutes[0]; }
			else if (mm == 0 && (hh != 0 || dd != 0)) { mmlong = minutes[0]; }
			else mmlong = "";
		}
		else
			mmlong = "";
		
		if ((twoFormat && hh <= 0 && dd <= 0) || !twoFormat)
		{	
			if (ss == 1) { sslong = seconds[1]; }
			else if (ss > 1 || ss == 0) { sslong = seconds[0]; }
			else sslong = "";
		}
		else
			sslong = "";
	}
	
	if (ddlong != "")
			ddlong = bold_start + dd + bold_end + " " + ddlong + " ";
	if (hhlong != "")
			hhlong = bold_start + hh + bold_end + " " + hhlong + " ";
	if (mmlong != "")
			mmlong = bold_start + mm + bold_end + " " + mmlong + " ";
	if (sslong != "")
			sslong = bold_start + ss + bold_end + " " + sslong;
	
	
	if (minutes[0] == ":")
	{
		ddlong = "";
		hhlong = hh + ":";
		
		if (mm < 10)
			mmlong = "0" + mm + ":";
		else
			mmlong = mm + ":";
		
		if (ss < 10)
			sslong = "0" + ss;
		else
			sslong = ss;
	}			
	
	ctrlWhereSet.innerHTML = ddlong + hhlong + mmlong + sslong;
}

function GetLastDigit(val)
{
	val = val + "";
	return val.charAt(val.length-1);
}

function TickHandlerStub(timer, seconds)
{
	return true;
}