/* calendar */

var calMonths = {
	ru:["Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентябрь","Октябрь","Ноябрь","Декабрь"],
	ua:["Січень","Лютий","Березень","Квітень","Травень","Червень","Липень","Серпень","Вересень","Жовтень","Листопад","Грудень"],
	en:["January","February","March","April","May","June","July","August","September","October","November","December"]
};

var calDays = {
	ru:["П","В","С","Ч","П","С","В"],
	ua:["П","В","С","Ч","П","С","Н"],
	en:["Mo","Tu","We","Th","Fr","Sa","Su"]
}

function stDay(day) {
	if (day == 0) day = 7;
	return day;
}

function cCalendar (id,lang,path,appendTo) {
	if (id == null) {
		this.inited = false;
	} else {
		this.box = document.getElementById(id);
		this.title = document.getElementById('cal_title');
		this.body = document.getElementById('cal_body');
		this.inited = true;
	}
	this.appendTo = document.getElementById(appendTo) || null;
	this.path = path || 'images/admin/';
	this.current = new Date();
	this.year = this.current.getFullYear();
	this.month = this.current.getMonth();
	this.lang = lang;
	this.targetID = null;
	this.format = "d-m-Y";
	this.changeyear = false;


	this.getMonthTitle = function (month) {
		if (calMonths[this.lang] && calMonths[this.lang][month]) {
			return calMonths[this.lang][month];
		} else return '';
	}
	this.buildMonth = function() {
		var i,td,tr,a,tb;
		var dt = new Date(this.year,this.month,1,12,0,0,0);
		var today = new Date();
		var start = dt.getTime();
		var daystamp = 86400000;
		var msg = '';
		var dow = stDay(dt.getDay());
		var tb = document.createElement('tbody');
		tr = document.createElement('tr');
		if (dow > 1) {
			for (i = 1; i < dow ;i++) {
				td = document.createElement('td');
				tr.appendChild(td);
			}
		}

		this.clear();

		if (this.changeyear) {
			this.title.innerHTML = this.getMonthTitle(this.month) + ' ';
			if (this.inputyear)	this.inputyear.value = this.year;
		} else {
			this.title.innerHTML = this.getMonthTitle(this.month) + ' ' + this.year;
		}

		while (dt.getMonth() == this.month) {
			td = document.createElement('td');
			if ((dt.getMonth() == today.getMonth()) && (dt.getDate() == today.getDate())) {
				td.className = 'selected';
			} else if (dow == 7) td.className = 'red';
			a = document.createElement('a');
			a.href = 'javascript:;';
			addEvent(a,'click',this.chooseDate.bindAsEventListener(this,dt));
			a.appendChild(document.createTextNode(dt.getDate()));
			td.appendChild(a);
			tr.appendChild(td);

			if (dow == 7) {
				tb.appendChild(tr);
				tr = document.createElement('tr');
			}
			dt = new Date((dt.getTime() + daystamp));
			dow = stDay(dt.getDay());
		}

		if (dow != 1) {
			for (i = dow; i <= 7 ; i++) {
				td = document.createElement('td');
				tr.appendChild(td);
			}
			tb.appendChild(tr);
		}
		this.body.appendChild(tb);

	}

	this.hide = function () {
		this.box.style.display = 'none';

	}

	this.show = function (obj,targetID,formatFunc) {
		if (targetID == undefined) {
			this.targetID = obj;
		} else {
			this.targetID = document.getElementById(targetID);
		}

		this.customFormat = formatFunc ? formatFunc : this.customFormat;

		var l,t;
		l = getOffsetLeft(obj);
		t = getOffsetTop(obj);
		if (!this.inited) this.init();
		this.box.style.display = 'block';
		this.box.style.left = (l + obj.offsetWidth) + 'px';
		var tt = (t - this.box.offsetHeight - 10);
		if (tt < 0 ) tt = 10;
		this.box.style.top = tt + 'px';
		this.buildMonth();
	}

	this.yearchange = function () {
		var y = parseInt(this.inputyear.value);
		if ( !isNaN(y) && y > 1930 && y <= this.current.getFullYear()) {
			this.year = y;
			this.buildMonth();
		}
	}

	this.next = function () {
		this.month++;
		if (this.month == 12) {
			this.month = 0;
			this.year++;
		}
		this.buildMonth();
	}

	this.prev = function () {
		this.month--;
		if (this.month == -1) {
			this.month = 11;
			this.year--;
		}
		this.buildMonth();
	}

	this.formatDate = function (dt,test) {
		var v;
		var result = '';
		for (var i = 0; i < this.format.length; i++) {
			switch (this.format.charAt(i)) {
			case 'Y':
				result += dt.getFullYear();
				break;
			case 'y':
				break;
			case 'm':
				v = dt.getMonth();v++;
				if (v < 10) v = '0' + v;
				result += v;
				break;
			case 'd':
				v = dt.getDate();
				if (v < 10) v = '0' + v;
				result += v;
				break;
			default:
				result += this.format.charAt(i);

			}
		}
		return result;
	}

	this.chooseDate = function (e,dt) {
		var dest = this.targetID;
		if (dest) {
			dest.value = this.customFormat ? this.customFormat(dt) : this.formatDate(dt);
		}

		this.customFormat = null;

		this.hide();
	}

	this.clear = function() {
		if (this.body && this.body.tBodies) {
			while(this.body.tBodies.length > 0) {
				this.body.removeChild(this.body.tBodies[0]);
			}
		}
	}

	this.init = function (id) {
		var div,a,el,el2,i;
		this.box = document.createElement('div');
		this.box.className = 'calendar';
		if (id) this.box.id = id;
		div = document.createElement('div');
		div.className = 'head';
		//prev
		a = document.createElement('a');
		a.className = 'left';
		a.href = 'javascript:;';
		el = document.createElement('img');
		el.alt = 'Previous';
		el.src = this.path+'a_calendar_left.gif';
		a.appendChild(el);
		addEvent(a,'click',this.prev.bindAsEventListener(this));
		div.appendChild(a);
		//title
		this.title = document.createElement('span');
		this.title.className = 'center';
		div.appendChild(this.title);
		//Change Year
		if (this.changeyear) {
			this.inputyear = document.createElement('input');
			this.inputyear.id = 'calc_y';
			addEvent(this.inputyear,'keyup',this.yearchange.bindAsEventListener(this));
			div.appendChild(this.inputyear);
		}
		//next
		a = document.createElement('a');
		a.className = 'right';
		a.href = 'javascript:;';
		el = document.createElement('img');
		el.alt = 'Next';
		el.src = this.path+'a_calendar_right.gif';
		a.appendChild(el);
		addEvent(a,'click',this.next.bindAsEventListener(this));
		div.appendChild(a);
		this.box.appendChild(div);
		//days
		div = document.createElement('div');
		div.className = 'days';
		el = document.createElement('div');
		el.className  = 'day';

		for (i in calDays[this.lang]) {
			el2 = document.createElement('div');
			el2.appendChild(document.createTextNode(calDays[this.lang][i]));
			if (i == 6) el2.className = 'red';
			el.appendChild(el2);
		}
		div.appendChild(el);
		this.body = document.createElement('table');
		el = document.createElement('thead');
		el2 = document.createElement('img');
		el2.src = 'images/px.gif';
		el2.alt = '';
		el.appendChild(el2);
		this.body.appendChild(el);
		div.appendChild(this.body);
		this.box.appendChild(div);
		//footer
		div = document.createElement('div');
		div.className = 'submit';
		a = document.createElement('a');
		a.href = 'javascript:;';
		el = document.createElement('img');
		el.src = this.path+'calendar_close.gif';
		el.alt = 'X';
		a.appendChild(el);
		addEvent(a,'click',this.hide.bindAsEventListener(this));
		div.appendChild(a);
		this.box.appendChild(div);
		div = document.createElement('div');
		div.innerHTML = "<!--[if lte IE 6.5]><iframe></iframe><![endif]-->"
		this.box.appendChild(div);
		if (this.appendTo) {
			this.appendTo.appendChild(this.box);
		} else {
			document.body.appendChild(this.box);
		}
		this.inited = true;
	}

}

