/* ГАДЖЕТ ОБРАТНОГО ОТСЧЁТА ВРЕМЕНИ */
function __Countdown(id)
{
	this.lang = Locale.Gadgets.Countdown;
	this.Core = new __StdModuleCore(this, id, 'countdown', this.lang.title, this.lang.desc);
	this.updater = null;
	this.counters = {};	//внутреннее хранилище update-данных, получаемых от сервера
	this.numCounters = 0;
	this.finput = {};
	this.fpanel = {};

	this.createPanelSettings();
	this.fpanel.content = ce('UL', this.Content);	//контейнер для городов с часами

	Update.request('Countdown', 'module', 'startup', { id: this.id }, true);
}

__Countdown.prototype.Relay = function(data, part)
{
	var self = this;
	switch(data.event.action)
	{
		case 'startup':
			if(data.variables.events.length)
				for(var i=0; i<data.variables.events.length; i++)
				{
					this.numCounters++;
					this.counters[data.variables.events[i].date] = data.variables.events[i];
				}
			else if(!isEmpty(data.variables.events))
				for(var i in data.variables.events)
				{
					this.numCounters++;
					this.counters[data.variables.events[i].date] = data.variables.events[i];
				}
			this.cdate = new Date();
			this.serverTStamp = (data.variables.ctstamp-0);
//			this.clockShift = checkUserClock(this.serverTStamp);
			this.serverTStamp -= this.cdate.getTimezoneOffset()*60;
			this.userTStamp = Math.round(this.cdate.getTime()/1000);
/*
			var t = Math.abs(checkUserClock(data.variables.ctstamp));
			if(t>3500)
				window.alert('Часы на Вашем компьютере показывают очень неточное время. Вероятно, в операционной системе неправильно настроен часовой пояс');
			else if(t>300)
				window.alert('Возможно, часы на Вашем компьютере показывают очень неточное время');
*/
			this.processInfo();
			break;

		case 'add':
			if(data.variables.status == 'ok')
			{
				if(!isEmpty(data.variables.events))
				{
					if(data.variables.events.replace)
					{	//если имело место редактирование, удаляем старый
						de(this.counters[data.variables.events.replace].obj);
						delete this.counters[data.variables.events.replace];
						this.numCounters--;
					}
					this.numCounters++;
					this.counters[data.variables.events.date] = data.variables.events;
					this.processInfo();
				}
			}
			else if(data.variables.status == 'error')
			{
				var t = '';
				switch(data.variables.msg) 
				{
					case 'Dublicated date':
						t = ': '+ this.lang.msg['EventsCannotDublicate'];
						break;
					case 'Date is incorrect':
						t = ': '+ this.lang.msg['EventDefinedIncorrectly'];
						break;
				}
				window.alert(this.lang.msg['ErrorWhenAddEvent'] + t);
			}
			break;

		default:
			break;
	}
}

__Countdown.prototype.initTimer = function()
{
	var self = this;
	var cdate = new Date(this.serverTStamp*1000);
	window.setTimeout(function()
		{
			self.processInfo();
			self.updater = window.setInterval(function() {self.processInfo()}, 60000);
		},
		(60-cdate.getSeconds())*1000+10
	);
}

//Панель управления гаджетом
__Countdown.prototype.createPanelSettings = function()
{
	var self = this;
	this.tabIds = {toDate: this.id +'_toDate_tab', onInt: this.id +'_inInt_tab'};
	this.Tabs = new __Tabs(this.Options, 'gadget');
	this.Tabs.create(this.tabIds.toDate, Locale['Date']);
	this.Tabs.create(this.tabIds.onInt, Locale['Interval']);
	this.Tabs.show(this.tabIds.toDate);

	this.finput.toDate = {};
	this.finput.onInt = {};

	this.fpanel.formOptType = ce('FORM', this.Options, null, {marginBottom: '10px'});
	this.createFormToDate();
	this.createFormOnInt();
}

//Форма задания события с помощью даты
__Countdown.prototype.createFormToDate = function()
{
	var self = this;
	this.fpanel.formToDate = ce('FORM', this.Tabs.data[this.tabIds.toDate].content, {editCounter: null});
	var oTbody = ce('TBODY', ce('TABLE', this.fpanel.formToDate, {className: 'wform'}));
	this.finput.toDate.date = {};

	var oTd = ce('TD', ce('TR', oTbody));
	var ddmmyyyy = Locale['Time'].format['DD'] +'.'+ Locale['Time'].format['MM'] +'.'+ Locale['Time'].format['YYYY'];
	var hhmm = Locale['Time'].format['hh'] +':'+ Locale['Time'].format['mimi'];
	this.finput.toDate.date = ce('INPUT', oTd, {type: 'text', maxLength: 10, defaultValue: ddmmyyyy, onfocus: function(){if(this.value==ddmmyyyy) this.value='';}}, {width: '80px', marginRight: '8px'});
	this.finput.toDate.time = ce('INPUT', oTd, {type: 'text', maxLength: 5, defaultValue: hhmm, onfocus: function(){if(this.value==hhmm) this.value='';}}, {width: '40px'});

	oTd = ce('TD', ce('TR', oTbody));
	ce('SPAN', oTd, {innerHTML: Locale['Period'] +':'});
	this.finput.toDate.periodType = ce('SELECT', null, null, {marginLeft: '5px'});
		t = ce('OPTION', this.finput.toDate.periodType, {value: false, defaultSelected: true, innerHTML: '-'+ Locale['no'] +'-'});
		t = ce('OPTION', this.finput.toDate.periodType, {value: 'yearly', innerHTML: this.lang['Yearly']});
		t = ce('OPTION', this.finput.toDate.periodType, {value: 'monthly', innerHTML: this.lang['Monthly']});
		t = ce('OPTION', this.finput.toDate.periodType, {value: 'daily', innerHTML: this.lang['atNdays']});
		t = ce('OPTION', this.finput.toDate.periodType, {value: 'hourly', innerHTML: this.lang['atNhours']});
		t = ce('OPTION', this.finput.toDate.periodType, {value: 'minutely', innerHTML: this.lang['atNminutes']});
	oTd.appendChild(this.finput.toDate.periodType);
	this.finput.toDate.periodType.onchange = function()
	{
		if(this.value == 'daily' || this.value == 'hourly' || this.value == 'minutely')
		{
			self.finput.toDate.period.type = 'text';
			self.finput.toDate.period.value = '1';
		}
		else if(self.finput.toDate.period.type == 'text')
		{
			self.finput.toDate.period.type = 'hidden';
			self.finput.toDate.period.value = '0';
		}
	}
	this.finput.toDate.period = ce('INPUT', oTd, {type: 'hidden', value: '0', maxLength: 3}, {width: '20px', marginLeft: '5px'});

	oTd = ce('TD', ce('TR', oTbody));
	ce('SPAN', oTd, {innerHTML: Locale['Event'] +':'});
	this.finput.toDate.title = ce('INPUT', oTd, {type: 'text', maxLength: 90}, {margin: '0px 2px 0px 5px'});
	ce('INPUT', oTd, {type: 'submit', className: 'std_button', value: 'ОК'});

	this.fpanel.formToDate.onsubmit = function()
	{
		if(self.finput.toDate.date.value == '')
		{
			window.alert(this.lang.msg['NeedToEnterDate']);
			self.finput.toDate.date.focus();
			return false;
		}
		if(/^\d{2}[\.\,\;\:]\d{2}[\.\,\;\:]\d{4}$/.test(self.finput.toDate.date.value))
			var dmy = self.finput.toDate.date.value.split(/[\.\,\;\:]/, 3);
		else
		{
			window.alert(self.lang.msg['NeedToEnterCorrectDate'] +' '+ ddmmyyyy);
			self.finput.toDate.date.focus();
			return false;
		}

		var values = {d: dmy[0], m: dmy[1], y: dmy[2]};
		if(/^\d{2}[\.\,\;\:]\d{2}$/.test(self.finput.toDate.time.value))
		{
			var hm = self.finput.toDate.time.value.split(/[\.\,\;\:]/, 2);
			values.h = hm[0];
			values.mn = hm[1];
		}
		else if(self.finput.toDate.time.value == '' || self.finput.toDate.time.value == hhmm || self.finput.toDate.time.value == 0)
		{
			values.h = 0;
			values.mn = 0;
		}
		else
		{
			window.alert(self.lang.msg['NeedToEnterCorrectTime'] +' '+ hhmm);
			self.finput.toDate.time.focus();
			return false;
		}

	
		if(values.d < 1 || values.d > 31)
		{
			window.alert(self.lang.msg['InputedDayIncorrect']);
			self.finput.toDate.date.D.focus();
			return false;
		}
		if(values.m < 1 || values.m > 12)
		{
			window.alert(self.lang.msg['InputedMonthIncorrect']);
			self.finput.toDate.date.M.focus();
			return false;
		}
		if(values.y > 2037 || values.y < 1970)
		{
			window.alert(self.lang.msg['SoDatesMayBeIncorrect']);
		}
		if(values.h < 0 || values.h > 23)
		{
			window.alert(self.lang.msg['InputedHoursIncorrect']);
			self.finput.toDate.date.H.focus();
			return false;
		}
		if(values.mn < 0 || values.mn > 59)
		{
			window.alert(self.lang.msg['InputedMinutesIncorrect']);
			self.finput.toDate.date.Mn.focus();
			return false;
		}

		if(self.finput.toDate.periodType.value == 'daily' || self.finput.toDate.periodType.value == 'hourly' || self.finput.toDate.periodType.value == 'minutely')
		{
			if(/[^\d]/.test(self.finput.toDate.period.value))
			{
				window.alert(self.lang.msg['InputCorrectDate']);
				self.finput.toDate.period.focus();
				return false;
			}
			else if(self.finput.toDate.periodType.value == 'daily' && (self.finput.toDate.value > 365 || self.finput.toDate.value < 1))
			{
				window.alert(self.lang.msg['InputCorrectDaysNum']);
				self.finput.toDate.period.focus();
				return false;
			}
			else if(self.finput.toDate.periodType.value == 'hourly' && (self.finput.toDate.period.value > 24 || self.finput.toDate.period.value < 1))
			{
				window.alert(self.lang.msg['InputCorrectHoursNum']);
				self.finput.toDate.period.focus();
				return false;
			}
			else if(self.finput.toDate.periodType.value == 'minutely' && (self.finput.toDate.period.value > 60 || self.finput.toDate.period.value < 1))
			{
				window.alert(self.lang.msg['InputCorrectMinutesNum']);
				self.finput.toDate.period.focus();
				return false;
			}
		}
		if(self.finput.toDate.title.value.length > 90)
		{
			window.alert(self.lang.msg['TooLongRecord']);
			self.finput.toDate.title.focus();
			return false;
		}

		t = {type: 'toDate', title: self.finput.toDate.title.value, date: values.d +'.'+ values.m +'.'+ values.y +' '+ values.h +':'+ values.mn};
		if(self.finput.toDate.periodType.value)
		{
			t.periodType = self.finput.toDate.periodType.value;
			if(self.finput.toDate.period.value) t.period = self.finput.toDate.period.value;
		}
		if(this.editCounter != null)
		{
			t.replace = this.editCounter;
			this.editCounter = null;
			var tmp = this.getElementsByTagName('INPUT');
			for(var i=0; i<tmp.length; i++)
			{
				if(tmp[i].type == 'text')
					tmp[i].style.color = '';
			}
		}
		Update.request('Countdown', 'module', 'add', {id: self.id}, t, true);
		return false;
	}
}

//Форма задания события с помощью интервала
__Countdown.prototype.createFormOnInt = function()
{
	var self = this;
	this.fpanel.formOnInt = ce('FORM', this.Tabs.data[this.tabIds.onInt].content);
	var oTbody = ce('TBODY', ce('TABLE', this.fpanel.formOnInt, {className: 'wform'}));
	this.finput.onInt.date = {};

	var oTr = ce('TR', oTbody);
	ce('TD', oTr, {innerHTML: '<strong>'+ Locale['Time']['in'] +':</strong>', colSpan: 5})
	oTr = ce('TR', oTbody);
	ce('TD', oTr, {innerHTML: Locale['Time'].format['YYYY'].substr(0, 2)});
	ce('TD', oTr, {innerHTML: Locale['Time'].format['MM']});
	ce('TD', oTr, {innerHTML: Locale['Time'].format['DD']});
	ce('TD', oTr, {innerHTML: Locale['Time'].format['hh']});
	ce('TD', oTr, {innerHTML: Locale['Time'].format['mimi']});
	oTr = ce('TR', oTbody);
	this.finput.onInt.date.Y = ce('INPUT', ce('TD', oTr), {type: 'text', maxLength: 4, defaultValue: '0'}, {width: '20px'});
	this.finput.onInt.date.M = ce('INPUT', ce('TD', oTr), {type: 'text', maxLength: 4, defaultValue: '0'}, {width: '20px'});
	this.finput.onInt.date.D = ce('INPUT', ce('TD', oTr), {type: 'text', maxLength: 6, defaultValue: '0'}, {width: '20px'});
	this.finput.onInt.date.H = ce('INPUT', ce('TD', oTr), {type: 'text', maxLength: 8, defaultValue: '0'}, {width: '20px'});
	this.finput.onInt.date.Mn = ce('INPUT', ce('TD', oTr), {type: 'text', maxLength: 10, defaultValue: '0'}, {width: '20px'});

	oTr = ce('TR', oTbody);
	oTd = ce('TD', oTr, {colSpan: 5, innerHTML: Locale['Event'] +':'});
	this.finput.onInt.title = ce('INPUT', oTd, {type: 'text', maxLength: 90}, {margin: '0px 2px 0px 5px'});
	ce('INPUT', oTd, {type: 'submit', className: 'std_button', value: 'ОК'});

	this.fpanel.formOnInt.onsubmit = function()
	{
		var regx = new RegExp();
		regx.compile('[^0-9\-]');
		if(regx.test(self.finput.onInt.date.D.value))
		{
			window.alert(Locale['InputNum']);
			self.finput.onInt.date.D.focus();
			return false;
		}
		if(!self.finput.onInt.date.D.value) self.finput.onInt.date.D.value = 0;
		if(regx.test(self.finput.onInt.date.M.value))
		{
			window.alert(Locale['InputNum']);
			self.finput.onInt.date.M.focus();
			return false;
		}
		if(!self.finput.onInt.date.M.value) self.finput.onInt.date.M.value = 0;
		if(regx.test(self.finput.onInt.date.Y.value))
		{
			window.alert(Locale['InputNum']);
			self.finput.onInt.date.Y.focus();
			return false;
		}
		if(!self.finput.onInt.date.Y.value) self.finput.onInt.date.Y.value = 0;
		if(regx.test(self.finput.onInt.date.H.value))
		{
			window.alert(Locale['InputNum']);
			self.finput.onInt.date.H.focus();
			return false;
		}
		if(!self.finput.onInt.date.H.value) self.finput.onInt.date.H.value = 0;
		if(regx.test(self.finput.onInt.date.Mn.value))
		{
			window.alert(Locale['InputNum']);
			self.finput.onInt.date.Mn.focus();
			return false;
		}
		if(!self.finput.onInt.date.Mn.value) self.finput.onInt.date.Mn.value = 0;
		if(self.finput.onInt.title.value.length > 90)
		{
			window.alert(self.lang.msg['TooLongRecord']);
			self.finput.onInt.title.focus();
			return false;
		}

		self.updateTimestamps();
		var cdate = new Date(self.serverTStamp*1000);
		var tdate = new Date(self.serverTStamp*1000);
		tdate.setFullYear((cdate.getFullYear() + self.finput.onInt.date.Y.value*1), (cdate.getMonth() + self.finput.onInt.date.M.value*1), (cdate.getDate() + self.finput.onInt.date.D.value*1));
		tdate.setHours((cdate.getHours() + self.finput.onInt.date.H.value*1), (cdate.getMinutes() + self.finput.onInt.date.Mn.value*1));
		var tyear = tdate.getFullYear();
		if(tyear > 2037 || tyear < 1970)
			window.alert(self.lang.msg['SoDatesMayBeIncorrect']);
		Update.request('Countdown', 'module', 'add', {id: self.id}, {type: 'onInt', date: tdate.getDate() +'.'+ (tdate.getMonth()+1) +'.'+ tyear +' '+ tdate.getHours() +':'+ tdate.getMinutes(), title: self.finput.onInt.title.value}, true);

		return false;
	}
}

__Countdown.prototype.printDate = function(d, pre)
{
	if(!(d instanceof Date))
		d = new Date(d);
	console.info(((pre)?pre+': ':'') + addZero(d.getDate()) +'.'+ addZero(d.getMonth()+1) +'.'+ d.getFullYear() +'  '+ addZero(d.getHours()) +':'+ addZero(d.getMinutes()) +':'+ addZero(d.getSeconds()))
}

//Синхронизирует серверный таймштамп с текущим моментом
__Countdown.prototype.updateTimestamps = function()
{
	this.cdate = new Date();
	var t = Math.round(this.cdate.getTime()/1000);
	this.serverTStamp += (t - this.userTStamp);
	this.userTStamp = t;
}

__Countdown.prototype.processInfo = function()
{
	var self = this;
	if(!this.updater) this.initTimer();

	this.updateTimestamps();
	var cdate = new Date(this.serverTStamp*1000);
//this.printDate(cdate, 'cdate')
	var cdate_ts = cdate.getTime();
	this.Content.innerHTML = '';
	this.fpanel.events = ce('TBODY', ce('TABLE', this.Content, {className: 'wborder', width: '100%'}));
	j = 0;
	for(var i in this.counters)
	{
		var tmpdate = new Date((this.counters[i].date + this.cdate.getTimezoneOffset()*60)*1000);	//таймзоны сейчас и на указанный момент могут различаться
		if(tmpdate.getTimezoneOffset() != this.cdate.getTimezoneOffset())
			var tdate = new Date(tmpdate.getTime() + (tmpdate.getTimezoneOffset() - this.cdate.getTimezoneOffset())*60000);	//дата-цель, вычисленная из GMT-штампа с учётом локальной таймзоны
		else
			var tdate = tmpdate;
		var tdate_s = addZero(tdate.getDate()) +'.'+ addZero(tdate.getMonth()+1) +'.'+ tdate.getFullYear();
		var tmp = addZero(tdate.getHours()) +':'+ addZero(tdate.getMinutes());
		if(tmp != '00:00')
			tdate_s += ' '+tmp;
		tdate_s += ' '+ Locale.WeekDaysCtd[gVars.WeekDays[tdate.getDay()]];
		var tdate_ts = tdate.getTime();
		var seconds = Math.round((tdate_ts - cdate_ts)/1000);	//кол-во секунд на отрезке
		if(!j)
		{
			var nearest_date = tdate;
			var nearest_tsdif = Math.abs(seconds);
			var nearest_i = i;
		}
		if(j++ && (Math.abs(seconds) <= nearest_tsdif))	//определяем ближайшее событие
		{
			nearest_date = tdate;
			nearest_tsdif = Math.abs(seconds);
			nearest_i = i;
		}
		if(j % 2 == 0)
			color = '#FFFFFF';
		else
			color = '#DEE9FF';
		this.counters[i].obj = ce('TR', this.fpanel.events);
		ce('TD', this.counters[i].obj, {innerHTML: '<strong>'+ this.counters[i].title +'</strong>', tipInfo: tdate_s, onmouseover: showTip, onmouseout: hideTip}, {padding: '5px', backgroundColor: color});	//название события и дата

		var div = ce('TD', this.counters[i].obj, null, {padding: '5px', backgroundColor: color});
		var oTd = ce('TD', this.counters[i].obj, null, {padding: '5px', backgroundColor: color});
		ce('IMG', oTd, {src: '/img/icons/edit2.gif', className: 'href', i: i, title: Locale['ToEdit'], onclick: function() {self.initEditCounter(this.i);}}, {margin: '3px'});
		ce('IMG', oTd, {src: '/img/icons/x2.gif', className: 'href', i: i, title: Locale['ToDelete'], onclick: delCounter});

		if(seconds>0)
		{	//не наступило
			var days = Math.abs(Math.floor(seconds / 86400));
			tmp = Math.abs(seconds) - days*86400;	//кол-во секунд за вычетом подсчитанных дней
			var hours = Math.floor(tmp/3600);
			var minutes = Math.ceil((tmp - hours*3600)/60);
		}
		else
		{	//наступило
			this.counters[i].obj.style.color = '#808080';
			div.onmouseover = showTip;
			div.onmouseout = hideTip;
			div.tipInfo = this.lang['EventCome'];
			if(tdate.getDate() == cdate.getDate() && tdate.getMonth() == cdate.getMonth() && tdate.getFullYear() == cdate.getFullYear())
			{	//событие произошло сегодня
				div.style.color = '#FF0000';
			}

			var days = Math.abs(Math.ceil(seconds / 86400));
			tmp = Math.abs(seconds) - days*86400;
			var hours = Math.floor(tmp/3600);
			var minutes = Math.floor((tmp - hours*3600)/60);
		}

		if((!days && !hours && !minutes) || (seconds<=0 && this.counters[i].periodType))
		{	//если событие наступило, в случае периодичности, всё равно в первый цикл надо об этом сообщить
			div.innerHTML = '<strong style="color:#FF0000;">'+ this.lang['EventCome'] +'!</strong>';

			if(this.counters[i].periodType)	//Можно применять периодичность - как раз она зацепиться к следующему тику
			{
				tmp = this.counters[i].date*1000;
				var k = 1;
				var tmpdate = new Date(tdate.getTime());
				switch(this.counters[i].periodType)
				{
					case 'yearly':
						tmp = Math.abs(cdate.getFullYear() - tdate.getFullYear());
						if(tmp<1) tmp = 1;
						tdate.setFullYear(cdate.getFullYear()+tmp);
						break;
					case 'monthly':
						tmp = Math.abs(cdate.getMonth() - tdate.getMonth());
						tmp += cdate.getMonth() + tmp * Math.abs(cdate.getFullYear() - tdate.getFullYear())
						tdate.setMonth(tmp);
						break;
					case 'daily':
						k = Math.floor(Math.abs(tdate_ts - cdate_ts) / 86400000 / this.counters[i].period);
						if(k<1) k = 1;
						tdate.setDate(tdate.getDate() + this.counters[i].period * k);
						break;
					case 'hourly':
						k = Math.floor(Math.abs(tdate_ts - cdate_ts) / 3600000 / this.counters[i].period);
						if(k<1) k = 1;
						tdate.setHours(tdate.getHours() + this.counters[i].period * k);
						break;
					case 'minutely':
						k = Math.floor(Math.abs(tdate_ts - cdate_ts) / 60000 / this.counters[i].period);
						if(k<1) k = 1;
						tdate.setMinutes(tdate.getMinutes() + this.counters[i].period * k);
						break;
				}

				if(tdate.getTimezoneOffset() != tmpdate.getTimezoneOffset())	//таймзоны сейчас и на указанный момент могут различаться
					var tdate = new Date(tdate.getTime() + (tdate.getTimezoneOffset() - tmpdate.getTimezoneOffset())*60000);	//дата-цель, вычисленная из GMT-штампа с учётом локальной таймзоны

				this.counters[i].date = Math.round(tdate.getTime()/1000) - this.cdate.getTimezoneOffset()*60;
				Update.request('Countdown', 'module', 'update', {id: this.id}, {'date': i, newTs: this.counters[i].date}, false);
			}
			continue;
		}
/*		else if(seconds <= 0)
			div.innerHTML += ' Прошло: ';
*/

		if(days)
			div.innerHTML += '<strong>'+ days +'</strong>&nbsp;'+ ((Lang == 'RU') ? declineByNum(days, 'день', 'дня', 'дней') : Locale['Time']['abbreviation']['days']) +', ';
		if(days || hours)
			div.innerHTML += '<strong>'+ hours +'</strong>&nbsp;'+ ((Lang == 'RU') ? declineByNum(hours, 'час', 'часа', 'часов') : Locale['Time']['abbreviation']['hours']) +', ';
		div.innerHTML += '<strong>'+ minutes +'</strong>&nbsp;'+ ((Lang == 'RU') ? declineByNum(minutes, 'минута', 'минуты', 'минут')+'.' : Locale['Time']['abbreviation']['minutes']);
	}

	if(j == this.numCounters)
	{	//что-то не уверен, что всё это нужно
/*
		document.getElementById(this.id + '_dateD').value = addZero(nearest_date.getDate());	//заполняем панель настроек
		document.getElementById(this.id + '_dateM').value = addZero(nearest_date.getMonth()+1);
		document.getElementById(this.id + '_dateY').value = nearest_date.getFullYear();
		document.getElementById(this.id + '_dateH').value = addZero(nearest_date.getHours());
		document.getElementById(this.id + '_dateMn').value = addZero(nearest_date.getMinutes());
		document.getElementById(this.id + '_title').value = this.counters[nearest_i].title;
*/
	}
//			this.parent.oLevelATitle.innerHTML = '<strong>'+ this.counters[i].title +'</strong>';

	function delCounter()
	{
		Update.request('Countdown', 'module', 'delete', {id: self.id}, {date: this.i}, true);
		de(self.counters[this.i].obj);
		delete self.counters[this.i];
		self.numCounters--;
		return false;
	}

	function showTip(e)
	{
		if(!this.tipInfo) return;
		e = e || window.event;
		Tooltip.Show(this.tipInfo, Event.element(e), 100);
	}
	function hideTip()
	{
		if(!this.tipInfo) return;
		Tooltip.Hide();
	}
}

__Countdown.prototype.initEditCounter = function(i)
{
	this.Options.style.display = '';
	this.Tabs.show(this.tabIds.toDate);
	var d = new Date((this.counters[i].date + this.cdate.getTimezoneOffset()*60)*1000);
	this.fpanel.formToDate.editCounter = i;
	this.finput.toDate.date.style.color = 'green';
	this.finput.toDate.time.style.color = 'green';
	this.finput.toDate.title.style.color =  'green';
	for(var k in this.counters)
	{
		this.counters[k].obj.childNodes[0].style.textDecoration = '';
		this.counters[k].obj.childNodes[1].style.textDecoration = '';
	}
	this.counters[i].obj.childNodes[0].style.textDecoration = 'blink';
	this.counters[i].obj.childNodes[1].style.textDecoration = 'blink';
	this.finput.toDate.title.focus();
	this.finput.toDate.date.value = addZero(d.getDate()) +'.'+ addZero(d.getMonth()+1) +'.'+ d.getFullYear();
	this.finput.toDate.time.value = addZero(d.getHours()) +':'+ addZero(d.getMinutes());
	this.finput.toDate.title.value = this.counters[i].title;
	if(this.counters[i].periodType)
	{
		for(var j=0; j<this.finput.toDate.periodType.options.length; j++)
		{
			if(this.finput.toDate.periodType.options[j].value == this.counters[i].periodType)
			{
				this.finput.toDate.periodType.options[j].selected = true;
				if(this.counters[i].periodType == 'daily' || this.counters[i].periodType == 'hourly' || this.counters[i].periodType == 'minutely')
				{
					this.finput.toDate.period.type = 'text';
					this.finput.toDate.period.value = this.counters[i].period;
				}
			}
		}
	}
}
