var jsCallendar = {
	day: 		new Array(),
	month: 		new Array(),
	year: 		new Array(),
	weekFormat: null,
	monthNames:	new Array(),
	weekDays:	new Array(),
	links:		new Array(),
	onclicks:	new Array(),
	forArchive:	null,
	dateDump:	new Array(),
	currDate:	new Array(),

	isLeapYear: function (year)
	{
		if (year % 4 == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	},

	getDays: function (month, year)
	{
		 var ar = new Array(13);
		 ar[0] = 31 ;
		 ar[1] = (this.isLeapYear(year)) ? 29 : 28;
		 ar[2] = 31 ;
		 ar[3] = 30 ;
		 ar[4] = 31 ;
		 ar[5] = 30 ;
		 ar[6] = 31;
		 ar[7] = 31 ;
		 ar[8] = 30 ;
		 ar[9] = 31 ;
		 ar[10] = 30 ;
		 ar[11] = 31 ;

		 return ar[month];
	},

	Print: function(callId)
	{
		var container = document.getElementById('callendar_container_'+callId);
		container.innerHTML = '';
		var displDate = new Date();

		this.setCurDate(callId);

		displDate.setFullYear(this.year[callId]);
		displDate.setMonth(this.month[callId]);
		displDate.setDate(1);

		var dayInWeek = displDate.getDay();

		if(this.weekFormat == 'ru') var dayOffset = dayInWeek == 0 ? 6 : dayInWeek - 1;
			else var dayOffset = dayInWeek;

		var dayInMonth = this.getDays(this.month[callId], this.year[callId]);

		var monYear = document.createElement('div');
		var table = document.createElement('table');
		var body = document.createElement('tbody');

		this.generateHead(monYear, body, callId);
		this.generateBody(body, dayInMonth, dayOffset, callId);

		container.appendChild(monYear);
		table.appendChild(body);
		container.appendChild(table);
	},

	setCurDate: function(callId)
	{
		if(typeof(this.dateDump[callId]) == 'undefined')
		{
			this.dateDump[callId] = new Array();

			this.dateDump[callId]['day'] = this.day[callId];
			this.dateDump[callId]['month'] = this.month[callId];
			this.dateDump[callId]['year'] = this.year[callId];
		}

		if(typeof(this.currDate[callId]) == 'undefined')
		{
			this.currDate[callId] = new Array();
			var dt = new Date();

			this.currDate[callId]['day'] = dt.getDate();
			this.currDate[callId]['month'] = dt.getMonth();
			this.currDate[callId]['year'] = isIE ? parseInt(dt.getYear()) : 1900 + parseInt(dt.getYear());
		}
	},

	generateBody: function(body, dayInMonth, dayOffset, callId)
	{
		var link = this.links[callId];
		var onclick = this.onclicks[callId];

		var dayCounter = 1;
		var tr = document.createElement('tr');
		tr.className = 'row';

		for(var i = -dayOffset; i < dayInMonth + 7; i++)
		{

			if(dayCounter % 8 == 0) body.appendChild(tr);

			if(dayCounter % 8 == 0)
			{
				var tr = document.createElement('tr');
				tr.className = 'row';
				dayCounter = 1;
			}

			var td = document.createElement('td');

			if(i >= 0 && i < dayInMonth)
			{
				var d = i + 1;
				var m = this.month[callId];
				var y = this.year[callId];

				if(this.forArchive == 'yes')
				{
					if((d <= this.currDate[callId]['day'] && m == this.currDate[callId]['month'] && y == this.currDate[callId]['year'])
					|| (m < this.currDate[callId]['month'] || y < this.currDate[callId]['year']))
					{
						var subTd = document.createElement('a');
						this.setDayClass(subTd, dayCounter, 'we_day_p', 'smpl_day_p');

						if(d == this.dateDump[callId]['day'] && m == this.dateDump[callId]['month'] && y == this.dateDump[callId]['year'])
						{
							td.className = "selected";
						}

						if(d == this.currDate[callId]['day'] && m == this.currDate[callId]['month'] && y == this.currDate[callId]['year'])
						{
							td.className = td.className == '' ? "current" : td.className+" current";
						}

						subTd.setAttribute('href', onclick + y + '/' + (parseInt(m) + 1) + '/' + d + '.html' + link);
					}
					else
					{
						var subTd = document.createElement('span');
						this.setDayClass(subTd, dayCounter, 'we_day_p', 'smpl_day_p');
					}
				}
				else
				{
					if(d == this.dateDump[callId]['day'] && m == this.dateDump[callId]['month'] && y == this.dateDump[callId]['year'])
					{
						td.className = "selected";
					}

					if(d == this.currDate[callId]['day'] && m == this.currDate[callId]['month'] && y == this.currDate[callId]['year'])
					{
						td.className = td.className == '' ? "current" : td.className+" current";
					}

					var subTd = document.createElement('a');
					this.setDayClass(subTd, dayCounter, 'we_day_p', 'smpl_day_p');

					if(link.indexOf("?") != -1) subTd.setAttribute('href', link + '&day=' + d + '&month=' + (parseInt(m) + 1) + '&year=' + y);
						else subTd.setAttribute('href', link + '?day=' + d + '&month=' + (parseInt(m) + 1) + '&year=' + y);
				}

				subTd.appendChild(document.createTextNode(d));
			}
			else
			{
				var subTd = document.createTextNode('');
			}

			td.appendChild(subTd);
			tr.appendChild(td);
			dayCounter++;
		}
	},

	setDayClass: function(element, dnum, wks, sds)
	{
		if(this.weekFormat == 'ru')
		{
			if(dnum % 7 == 0 || (dnum + 1) % 7 == 0) element.className = wks;
				else element.className = sds;
		}
		else
		{
			if(dnum == 1 || dnum == 7 ) element.className = wks;
				else element.className = sds;
		}
	},

	generateHead: function(monYear, body, callId)
	{
		monYear.className = 'top-cal';
		var aPrev = document.createElement('a');
		aPrev.className = 'arch_prev_mon';

		if(isIE)	aPrev.onclick = function(){jsCallendar.Prev(callId)};
			else aPrev.setAttribute('onclick', 'jsCallendar.Prev(\''+callId+'\'); return false;');

		aPrev.appendChild(document.createTextNode('«'));
		var aNext = document.createElement('a');
		aNext.className = 'arch_next_mon';

		if(isIE) aNext.onclick = function(){jsCallendar.Next(callId)};
			else aNext.setAttribute('onclick', 'jsCallendar.Next(\''+callId+'\'); return false;');

		aNext.appendChild(document.createTextNode('»'));

		monYear.appendChild(aPrev);
		monYear.appendChild(document.createTextNode(' ' + this.monthNames[this.month[callId]] + ' ' + this.year[callId] + ' '));
		monYear.appendChild(aNext);

		var row = document.createElement('tr');
		row.className = 'arch_head';

		for(var i = 0; i < this.weekDays.length; i++)
		{
			var th = document.createElement('th');
			this.setDayClass(th, i + 1, 'h_we_day', 'h_smpl_day');
			th.appendChild(document.createTextNode(this.weekDays[i]));
			row.appendChild(th);
		}

		body.appendChild(row);
	},

	Prev: function(callId)
	{

		if (this.month[callId] == 0)
		{
			this.month[callId] = 11
			this.year[callId]--;
		}
		else
		{
			this.month[callId]--;
		}

		this.Print(callId);
	},

	Next: function(callId)
	{
		if (this.month[callId] == 11)
		{
			this.month[callId] = 0;
			this.year[callId]++;
		}
		else
		{
			this.month[callId]++;
		}

		this.Print(callId);
	},

	Reset: function(callId)
	{
		this.day[callId]		= document.getElementById('callendar_c_day_'+callId).value;
		this.month[callId]		= document.getElementById('callendar_c_month_'+callId).value;
		this.year[callId]		= document.getElementById('callendar_c_year_'+callId).value;
		this.month_name[callId] 	= this._month[this.month[callId]];

		this.Print(callId);
	},

	Set: function(callId, curr_day)
	{
		var _day		= document.getElementById('callendar_day_'+callId);
		var _month		= document.getElementById('callendar_month_'+callId);
		var _year		= document.getElementById('callendar_year_'+callId);

		_day.value		= curr_day;
		_month.value	= this.month[callId];
		_year.value		= this.year[callId];

//		SetClasses();
	}
}

function checkvalue(obj, str)
{
	if (obj.value == str) {obj.value=''}
}

function setSearchGroups(obj)
{
	var items = obj.getElementsByTagName('item');
	var el = document.getElementById('catalogue_group_select');
	el.options.length = 0;

	var c=0;
	for (c = 0; c!=items.length; c++)
	{
		var opt = document.createElement('option');
		opt.value = items[c].getAttribute('id');
		opt.text = items[c].getAttribute('title');
		el.options.add(opt);
	}
}

function hintShow_my()
{
	if (!isBlocked)
	{
		var hint = document.getElementById('drop_down');

		var hintHeight = hint.clientHeight;

		if (isIE) {mouseX = window.event.x + hint.clientWidth + 30; /*- document.body.scrollLeft*/}
		if (isIE) {mouseY = window.event.y;}

		var limit = document.body.clientHeight + window.pageYOffset;
		var bottom = hintHeight + mouseY + oY;

		var hint = document.getElementById('drop_down');
		hint.style.left = (oX + mouseX) + 'px';

		if ((bottom)<limit) hint.style.top = (oY + mouseY) + 'px';
			else hint.style.top = (mouseY) + 'px';
	}
}

var currentInf = new Array();

function hide_inf(grp)
{
	var inf = document.getElementById('informer_one_' + currentInf[grp]);

	inf.className = 'hide';
}

function show_inf(grp, index)
{
	var inf = document.getElementById('informer_one_' + index);
	inf.className = 'show';
	currentInf[grp] = index;
}