//*************** 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;
    
	this.prevnowstamp = localnow.getTime()/1000-localnow.getTimezoneOffset()*60;
	this.SrvYear = SrvYear;
	this.SrvMonth = SrvMonth;
	this.SrvDay = SrvDay;
	this.SrvHour = SrvHour;
	this.SrvMinute = SrvMinute;
	this.SrvSecond = SrvSecond;

    /// <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();

    	var nowstamp = now.getTime()/1000-now.getTimezoneOffset()*60;
		//Задержка между вызовами - больше минуты.
		//Видимо, пользователь поменял время на своем компьютере
		if( Math.abs(nowstamp-this.prevnowstamp) > 60 )
		{
	    	document.location.reload(true);
		}
		this.prevnowstamp = nowstamp;

    	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, server_tz, date_separator)
{
	this.currentTime = timeStamp * 1000;
    this.name = name;
    this.parent = parentWindow;
    this.target = targetObject;
    this.server_tz = parseInt(server_tz/100)*60 + server_tz%100;
	this.DateSeparator = date_separator;
	this.show_seconds = false;
	
	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 tz_offset = (this.server_tz+temp_date_var.getTimezoneOffset())*60000;
    	var var_1 = new Date(this.currentTime+tz_offset);
    	var diff = this.currentTime+tz_offset - temp_date_var.getTime();
    	if (var_1.getDate() < 10) temp_var = '0';
		temp_var += '' + var_1.getDate() + this.DateSeparator;
    	if ((var_1.getMonth() + 1) < 10) temp_var += '0';
		temp_var += (var_1.getMonth() + 1) + this.DateSeparator + 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();
		if( this.show_seconds )
		{
            temp_var += ':';
    		if (var_1.getSeconds() < 10) temp_var += '0';
    		temp_var += var_1.getSeconds();
		}
   		temp_var += '&nbsp;';
    	if (diff < 0)
    	{
            //temp_var += '(завершен)';
            temp_var += '';
        	temp_tag_start = '<b><font color="black">';
        	temp_tag_end = '</font><b>';
    	}
        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;
    }
}
//********************************************//
