//*************** Enum ClockTypes ***************//

/// <summary>
/// Конструктор объекта перечисления типов часов
/// </summary>
/// <author date="06.02.2006">MIA</author>
function ClockTypes()
{
    this.Normal = 'Normal';
    this.NormalWithDate = 'NormalWithDate';
    this.Advantaged = 'Advantaged';
    this.AdvantagedWithDate = 'AdvantagedWithDate';
}

//********************************************//

//*************** Class Clocks ***************//

/// <summary>
/// Конструктор экземпляра объекта часов
/// </summary>
/// <param name="name">Имя объекта</param>
/// <param name="parentWindow">Ссылка на окно</param>
/// <param name="typeClock">Тип часов</param>
/// <param name="targetObject">Объект для вывода (должен поддерживать свойство innerHTML)</param>
/// <author date="06.02.2006">MIA</author>
function Clocks(name, parentWindow, typeClock, targetObject, SrvYear, SrvMonth, SrvDay, SrvHour, SrvMinute, SrvSecond)
{
    this.name = name;
    this.parent = parentWindow;
    switch (typeClock)
    {
        case 'AdvantagedWithDate':
        case 'NormalWithDate':
        case 'Advantaged': this.type = typeClock; break;
        default: this.type = 'Normal';
    }
   	var localnow = new Date();
    if(isNaN(SrvYear) || isNaN(SrvMonth) || isNaN(SrvDay) || isNaN(SrvHour) || isNaN(SrvMinute) || isNaN(SrvSecond))
    {
    	this.yearshift = 0;	this.monthshift = 0;  this.dayshift = 0;
    	this.hourshift = 0; this.minuteshift = 0; this.secondshift = 0;
    }
    else
    {
    	this.yearshift = SrvYear - localnow.getFullYear();
    	this.monthshift = SrvMonth - localnow.getMonth();
    	this.dayshift = SrvDay - localnow.getDate();
    	this.hourshift = SrvHour - localnow.getHours();
    	this.minuteshift = SrvMinute - localnow.getMinutes();
    	this.secondshift = SrvSecond - localnow.getSeconds();
    }
    this.target = targetObject;
    this.day = null;
    this.month = null;
    this.year = null;
    this.hours = null;
    this.minutes = null;
    this.seconds = null;
    this.timeout = null;
    
    /// <summary>
	/// Запускает часы
	/// </summary>
    this.run = function ()
    {
    	this.tick();
    	this.timeout = this.parent.setTimeout(this.name + ".tick()", 500);
    }
    
    /// <summary>
	/// Останавливает часы
	/// </summary>
    this.stop = function ()
    {
    	this.parent.clearTimeout(this.timeout);
    }
    
    /// <summary>
	/// Отрабатывает секунду времени
	/// </summary>
    this.tick = function ()
    {
        //Инициализация переменных
        var ap = '';
    	var now = new Date();
    	this.year = now.getFullYear() + this.yearshift;
    	this.month = now.getMonth() + this.monthshift;
    	this.day = now.getDate() + this.dayshift;
        this.hours = now.getHours() + this.hourshift;
        this.minutes = now.getMinutes() + this.minuteshift;
        this.seconds = now.getSeconds() + this.secondshift;
        if(this.seconds < 0) {
        	this.seconds = 60 + this.seconds;
        	this.minutes -= 1;
        }
        else if(this.seconds > 59) {
        	this.seconds = this.seconds - 60;
        	this.minutes += 1;
        }
        if(this.minutes < 0) {
        	this.minutes = 60 + this.minutes;
        	this.hours -= 1;
        }
        else if(this.minutes > 59) {
        	this.minutes = this.minutes - 60;
        	this.hours += 1;
        }
        if(this.hours < 0) {
        	this.hours = 24 + this.hours;
        	this.day -= 1;
        }
        else if(this.hours > 23) {
        	this.hours = this.hours - 24;
        	this.day += 1;
        }
        if(this.day < 0) {
        	this.day = 31 + this.day;
        	this.month -= 1;
        }
        else if(this.day > 31) {
        	this.day = this.day - 31;
        	this.month += 1;
        }
        if(this.month < 0) {
        	this.month = 12 + this.month;
        	this.year -= 1;
        }
        else if(this.month > 12) {
        	this.month = this.month - 12;
        	this.year += 1;
        }
        if (this.seconds < 10) this.target.innerHTML += '0';
        this.target.innerHTML = '';
        //Дата
        if (this.type == 'AdvantagedWithDate' || this.type == 'NormalWithDate')
        {
            if (this.day < 10) this.target.innerHTML += '0';
            this.target.innerHTML += this.day + '.';
            if (this.month < 10) this.target.innerHTML += '0';
            this.target.innerHTML += this.month + '.' + this.year + '&nbsp;';
        }
        //Часы
        switch (this.type)
        {
            case 'AdvantagedWithDate':    
            case 'Advantaged':
                switch (this.hours)
                {
                    case 12:
                        this.target.innerHTML += '00:';
                        ap = ' PM';
                        break;
                    case 24:
                        this.target.innerHTML += '00:';
                        ap = ' AM';
                        break;
                    default:    
                        if (this.hours > 12)
                        {
                            this.target.innerHTML += '0' + (this.hours - 12) + ':';
                            ap = ' PM';
                            break;
                        }
                        if(this.hours < 12)
                        {
                            if (this.hours < 10) this.target.innerHTML += '0';
                            this.target.innerHTML += this.hours + ':';
                            ap = ' AM';
                        }
                }
                break;
            case 'NormalWithDate':
            case 'Normal':
                this.target.innerHTML += this.hours + ":";
                break;
        }
        //Минуты и секунды
        if (this.minutes < 10) this.target.innerHTML += "0";
        this.target.innerHTML += this.minutes + ":";
        if (this.seconds < 10) this.target.innerHTML += "0";
        this.target.innerHTML += this.seconds + " " + ap;
        //Запуск отработки новой секунды
        this.timeout = this.parent.setTimeout(this.name + ".tick()", 1000);
    }
}

//********************************************//

//*************** Class BackClocks ***************//

/// <summary>
/// Конструктор экземпляра объекта часов, идущих назад
/// </summary>
/// <param name="name">Имя объекта</param>
/// <param name="parentWindow">Ссылка на окно</param>
/// <param name="targetObject">Объект для вывода (должен поддерживать свойство innerHTML)</param>
/// <param name="timeStamp">Первоначальное время</param>
/// <author date="06.02.2006">MIA</author>

function BackClocks(name, parentWindow, targetObject, timeStamp)
{
	this.currentTime = timeStamp * 1000;
    this.name = name;
    this.parent = parentWindow;
    this.target = targetObject;
	var timeout;
	
    /// <summary>
	/// Запускает часы
	/// </summary>
    this.run = function ()
    {
    	this.tick();
    }
    
    /// <summary>
	/// Останавливает часы
	/// </summary>
    this.stop = function ()
    {
    	this.parent.clearTimeout(this.timeout);
    }
    
    /// <summary>
	/// Отрабатывает секунду времени
	/// </summary>
    this.tick = function ()
    {
        this.target.innerHTML = '<b>' + this.getTimeString() + '</b>';
        //Запуск отработки новой секунды
        this.timeout = this.parent.setTimeout(this.name + ".tick()", 1000);
    	//this.currentTime -= 1000;
    }
    
    /// <summary>
	/// Возвращает строковое представление текущего времени
	/// </summary>
    this.getTimeString = function ()
    {
    	var temp_var = '';
    	var temp_tag_start = '';
    	var temp_tag_end = '';
    	var temp_date_var = new Date();
    	var var_1 = new Date(this.currentTime);
    	var diff = this.currentTime - temp_date_var.getTime();
    	if (var_1.getDate() < 10) temp_var = '0';
		temp_var += var_1.getDate() + '/';
    	if ((var_1.getMonth() + 1) < 10) temp_var += '0';
		temp_var += (var_1.getMonth() + 1) + '/' + var_1.getFullYear() + '&nbsp;';
    	if (var_1.getHours() < 10) temp_var += '0';
		temp_var += var_1.getHours() + ':';
		if (var_1.getMinutes() < 10) temp_var += '0';
		temp_var += var_1.getMinutes() + '&nbsp;';
    	if (diff < 0)
            //temp_var += '(завершен)';
            temp_var += '';
        else if (diff > 3600)
        {
        	//temp_var += '(больше часа)';
            temp_var += '';
        	temp_tag_start = '<b><font color="green">';
        	temp_tag_end = '</font><b>';
        }
        else if ((diff <= 3600) && (diff > 600))
        {
        	//temp_var += '(меньше часа)';
            temp_var += '';
        	temp_tag_start = '<b><font color="green">';
        	temp_tag_end = '</font><b>';
        }
       	else
       	{
       		//temp_var += '(меньше 10 мин.)';
            temp_var += '';
        	temp_tag_start = '<b><font color="red">';
        	temp_tag_end = '</font><b>';
    	}
       	return temp_tag_start + temp_var + temp_tag_end;
        /*if (this.currentTime < 0)
            temp_var = 'завершен';
        else if (this.currentTime > 3600000)
            temp_var = '<b><font color="green">больше часа</font><b>';
        else if (this.currentTime == 3600000)
        	temp_var = '<b><font color="green">60:00</font><b>';
        else
        {
    	    var tempDate = new Date(this.currentTime);
    	    var minutes = tempDate.getMinutes();
    	    var seconds = tempDate.getSeconds();
    	    temp_var = '<font';
    	    if (minutes < 10) temp_var += ' color="red">0';
    	    else temp_var += ' color="green">';
    	    temp_var += minutes + ':';
    	    if (seconds < 10) temp_var += '0';
    	    temp_var += seconds + '</font>';
    	    if (temp_var == '00:00') temp_var = 'завершен';
    	}
    	return temp_var;*/
    }
}
//********************************************//
