var newsproject = { // NAMESPACE
	archive : {},
	getObject : function(id) {
		if (typeof this.archive[id] == "undefined") {
			this.archive[id] = new this.calendar(id);
		}
		return this.archive[id];
	}
};

newsproject.calendar = function(id) { // CONSTRUCTOR
	// Vars
	this.id = id;

	this.curYear = 0;
	this.curMohth = 0;
	this.curDay = 0;

	this.todayYear = 0;
	this.todayMohth = 0;
	this.todayDay = 0;

	this.showYear = 0;
	this.showMohth = 0;

	this.minYear = 1999;
	this.minMonth = 1;
	// HTML-elements
	this.nextMonth = null;
	this.prevMonth = null;
	this.yearList = null;
	this.curDateBlock = null;
	this.calendarTable = null;
	// Flags
	this.isTodayMonth = true;
	this.isCurMonth = true;
	this.isMinMonth = false;
	this.debugMode = false;
}

newsproject.calendar.prototype = { // PROTOTYPE
// Const
	MONTHES : [
		"Январь", "Февраль",
		"Март", "Апрель", "Май",
		"Июнь", "Июль", "Август",
		"Сентябрь", "Октябрь", "Ноябрь",
		"Декабрь", "Январь"
	],
	ERRORS : {
		1 : "Ошибка при инициализации объекта",
		2 : "Контейнер nextMonth не найден",
		3 : "Контейнер prevMonth не найден",
		4 : "Контейнер yearList не найден",
		5 : "Контейнер curDate не найден",
		6 : "Контейнер calendarTable не найден"
	},
	NEXT_MONTH_PREFIX : "nextMonth_",
	YEAR_LIST_PREFIX : "calendarYears_",
	CUR_DATE_PREFIX : "curDate_",
	PREV_MONTH_PREFIX : "prevMonth_",
	CALENDAR_PREFIX : "calendar_",
// Methods
	// Default
	gebi : function(id) {
		return document.getElementById(id);
	},
	addHandler : function(object, event, handler, useCapture) {
		if (object.addEventListener) {
			object.addEventListener(event, handler, useCapture ? useCapture : false);
		} else if (object.attachEvent) {
			object.attachEvent('on' + event, handler);
		} else alert(this.errorArray[9]);
	},
	// Init
	debug : function(keys) {
		if (!this.debugMode) return;
		var mes = "";
		for (var i = 0; i < keys.length; i++) mes += this.ERRORS[keys[i]] + " : ";
		mes = mes.substring(0, mes.length - 3);
		alert(mes);
	},
	printp : function() {
		if (!this.debugMode) return;
		var mes = "", type;
		for (var i in this) {
			type = typeof this[i];
			if (type != "function" && type != "object") mes += i + " = " + this[i] + "\n"
		}
		alert(mes);
	},
	findElements : function() {
		this.nextMonth = this.gebi(this.NEXT_MONTH_PREFIX + this.id);
		if (this.nextMonth == null) {
			this.debug([1,2]);
				return;
		}
		this.prevMonth = this.gebi(this.PREV_MONTH_PREFIX + this.id);
		if (this.prevMonth == null) {
			this.debug([1,3]);
				return;
		}
		this.yearList = this.gebi(this.YEAR_LIST_PREFIX + this.id);
		if (this.yearList == null) {
			this.debug([1,4]);
				return;
		}
		this.curDateBlock = this.gebi(this.CUR_DATE_PREFIX + this.id);
		if (this.curDateBlock == null) {
			this.debug([1,5]);
				return;
		}
		this.calendarTable = this.gebi(this.CALENDAR_PREFIX + this.id);
		if (this.calendarTable == null) {
			this.debug([1,6]);
				return;
		}
	},
	init : function(obj) {
		try {
			this.findElements();
			var today = new Date();
			if (typeof obj == "undefined") obj = {};

			this.todayYear = obj.todayYear || today.getFullYear();
			this.todayMohth = obj.todayMonth || today.getMonth();
			this.todayDay = obj.todayDay || today.getDate();

			this.todayMohth-=1;//фикс
			if(!this.todayMohth)this.todayMohth=12;//фикс

			this.curYear = obj.year || this.todayYear;
			this.curMohth = obj.month || this.todayMohth;
			this.curDay = obj.day || this.todayDay;

			if(!this.curMohth)this.curMohth=12; //фикс

			this.showYear = this.curYear;
			this.showMohth = this.curMohth;

			this.drawHeader();
			//this.printp(); // debug too

			this.addHandler(
				document,
				"click",
				function() {
					newsproject.getObject('archive').yearList.style.display = "none";
				}
			);
		} catch(e) {
			this.debug([1]);
		}
	},
	// Functionality
	drawHeader : function() {
		this.detectMonthState();
//		this.nextMonth.innerHTML = this.isTodayMonth ? this.MONTHES[(this.showMohth + 1) % 12] : "<a onclick=\"return newsproject.getObject('archive').showNext();\" href=\"#\">" + this.MONTHES[(this.showMohth + 1) % 12] + "</a>";
//		this.prevMonth.innerHTML = this.isMinMonth ? this.MONTHES[(this.showMohth - 1 + 12) % 12] : "<a onclick=\"return newsproject.getObject('archive').showPrev();\" href=\"#\">" + this.MONTHES[(this.showMohth - 1 + 12) % 12] + "</a>"
//		this.curDateBlock.innerHTML = this.MONTHES[this.showMohth] + " <a href=\"#\" onclick=\"return newsproject.getObject('archive').showYears(event);\">" + this.showYear + "</a>";
		this.curDateBlock.innerHTML = this.MONTHES[this.showMohth] + ", "+ this.showYear;
		var yearHtml = "";
		var top = -1;
		for (var i = this.todayYear; i >= this.minYear; i--) {
			if (i > this.showYear) top = top - 18;
			yearHtml += "<a onclick=\"return newsproject.getObject('archive').setShowYear(" + i + ");\" href=\"#\"" + (i == this.showYear ? " class=\"act\"" : "") + ">" + i + "</a>\n";
		}
		this.yearList.getElementsByTagName('td')[0].innerHTML = yearHtml;
		this.yearList.style.top = top - 3 + "px";
		this.drawTable();
	},
	drawTable : function() {
		var html = "<table class=\"calendar\">";
/*		var html = "<table class=\"calendar\">\
			<tr class=\"week\">\
				<th>Пн</th>\
				<th>Вт</th>\
				<th>Ср</th>\
				<th>Чт</th>\
				<th>Пт</th>\
				<th>Сб</th>\
				<th>Вс</th>\
			</tr>";*/
		var buferArray = [];
		var cdate = new Date(this.showYear, this.showMohth, 1);
		var ndate = new Date(this.showYear, this.showMohth + 1, 1);

		var cdateDay = cdate.getDay();
		cdateDay = cdateDay == 0 ? 7 : cdateDay; // fix for sunday
		for (var i = 1; i < cdateDay; i++) buferArray[i] = false;

		var dayInMonth = ndate.getTime() - cdate.getTime();
		dayInMonth = dayInMonth / (1000 * 60 * 60 * 24);
		if (this.showMohth == 9) dayInMonth = dayInMonth - 1; // october fix
		for (i = 0; i < dayInMonth; i++) buferArray[i + cdateDay] = i + 1;

		var cdate = new Date(this.showYear, this.showMohth, dayInMonth);
		var cdateDay = cdate.getDay();
		cdateDay = cdateDay == 0 ? 7 : cdateDay; // fix for sunday
		for (i = cdateDay + 1; i <= 7; i++) buferArray[buferArray.length] = false;

		var isLink, className;
		for (i = 1; i < buferArray.length; i++) {
			isLink = true;
			if (i % 7 == 1) html += "<tr>\n\t";
			if (buferArray[i]) {
				isLink = this.isTodayMonth && buferArray[i] > this.todayDay ? false : true;
				className = "";
				if (this.isTodayMonth && buferArray[i] == this.todayDay) className += "on ";
				if (this.isCurMonth && buferArray[i] == this.curDay) className += "act ";
				if (isLink) html += "<td"+(className ? " class=\""+className+"\"" : "")+"><a href=\"/news/archive/" + rubric + this.showYear + "/" + (this.showMohth + 1==13?1:this.showMohth + 1) + "/" + buferArray[i] + "\"" + (buferArray[i] < 10 ? " class=\"onedig\"" : "") + ">" + (buferArray[i] ? buferArray[i] : "") + "</a></td>";
				else html += "<td class=\"old\">" + (buferArray[i] ? buferArray[i] : "") + "</td>";
			} else {
				html += "<td></td>";
			}
			if (i % 7 == 0) html += "</tr>\n";
		}
		html += "</table>";
		//alert(html);

		this.calendarTable.innerHTML = html;
	},
	detectMonthState : function() {
		this.isTodayMonth = this.showYear == this.todayYear && this.showMohth == this.todayMohth;
		this.isCurMonth = this.showYear == this.curYear && this.showMohth == this.curMohth;
		this.isMinMonth = this.showYear == this.minYear && this.showMohth == this.minMonth-1;
	},
	showNext : function() {
	   if(this.isTodayMonth)return false;
		this.showMohth = (this.showMohth + 1) % 12;
		this.showYear = this.showMohth == 0 ? this.showYear + 1 : this.showYear;
		this.drawHeader();
		return false;
	},
	showPrev : function() {
	   if(this.isMinMonth)return false;
		this.showMohth = (this.showMohth - 1 + 12) % 12;
		this.showYear = this.showMohth == 11 ? this.showYear - 1 : this.showYear;
		this.drawHeader();
		return false;
	},
	setShowYear : function(year) {
		this.showYear = year;
		if (this.showYear == this.todayYear && this.showMohth > this.todayMohth) {
			this.showMohth = this.todayMohth;
		}
		this.yearList.style.display = "none";
		this.drawHeader();
		return false;
	},
	showYears : function(evt) {
		evt = evt || window.event;
		evt.cancelBubble = true;
		this.yearList.style.display = "";
		return false;
	}
}