var gMonthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
                   	'August', 'September', 'October', 'Novermber', 'December'];

var gWeek = ['Mo','Tu','We','Th','Fr','Sa','So'];

function Calendar() {
}

Calendar.prototype = {

day   : 0,
month : 0,
year  : 0,

first_day : 0,
last_day  : 0,

destination : null,

created : false,
showed  : false,

timer : 0,

others : null,

contener : null,

OnHide : function() {},

Init : function() {
	
	if (!this.created) {
		//var tmp    = $(this.destination).value.split('-');
		
		var tmp    = $(this.destination).getAttribute("dzisiaj").split('-');
		this.year  = tmp[0];
		this.month = tmp[1].substr(0,1) == '0' ? tmp[1].substr(1,1) : tmp[1];
		this.month = parseInt(this.month) - 1;
		this.day   = tmp[2].substr(0,1) == '0' ? tmp[2].substr(1,2) : tmp[2];
	}

	if (this.month < 0) {
		this.month = 11;
		this.year--;
	}

	if (this.month > 11) {
		this.month = 0;
		this.year++;
	}

	this.first_day = new Date(this.year, this.month, 1).getDay();
	this.last_day  = new Date(this.year, parseInt(this.month) + 1, 0).getDate();
	
	this.contener = $(this.contener);
},

Create : function() {

	if (this.contener == null) eval('Calendar contener isn\'t set');

	this.Init();

	this.contener.innerHTML = '';
	this.contener.className = this.contener.className.replace('calendar', '');
	this.contener.className += ' calendar';
	
	var fieldset = DomCreateElement('fieldset');
	var legend   = DomCreateElement('legend');
	var legend_label = DomCreateTextNode('Calendar');
	
	legend.appendChild(legend_label);
	fieldset.appendChild(legend);
	
	var form = DomCreateElement('div', {css:'calendar_form'});
	
	var select = DomCreateElement('select');
	form.appendChild(select);

	var toSelect = 0;
	
	for (var i = 0; i < gMonthNames.length; i++) {

		if (i == this.month) {
			toSelect = i;
		}

		select.options[i] = new Option(gMonthNames[i], i);
	}

	select.options[toSelect].selected = true;

        var input = DomCreateElement('input', {readonly:'readonly', value:this.year});
        form.appendChild(input);
        
        fieldset.appendChild(form);

	this.contener.appendChild(fieldset);
	
	var arrows = DomCreateElement('div', {css:'arrows'});
	
	var arrow_up_year   = DomCreateElement('div', {css:'arrow_up'});
	var arrow_down_year = DomCreateElement('div', {css:'arrow_down'});
	
	arrows.appendChild(arrow_up_year);
	arrows.appendChild(arrow_down_year);
	
	fieldset.appendChild(arrows);
	
	var outside_frame = DomCreateElement('div', {css:'month_outside_frame'});
	var inside_frame  = DomCreateElement('div', {css:'month_inside_frame'});
	
	var week = DomCreateElement('div', {css:'week'});
	var days = DomCreateElement('div', {css:'days'});
	var day;
	var day_nr;

	for (var i = 0; i < 7; i++) {
		day = DomCreateElement('div', {css:'dh'});
		day.appendChild(DomCreateTextNode(gWeek[i]));
		week.appendChild(day);
	}

	for (i = 0; i < this.first_day - 1; i++) {
		day = DomCreateElement('div', {css:'day'});
		days.appendChild(day);
	}

	var selectedcss;

	for (i = 1; i <= this.last_day; i++) {
		selectedcss = (i == this.day) ? 'day_selected' : 'day_nr';
		
		day = DomCreateElement('div', {css:'day f_l'});
		day_nr = DomCreateElement('div', {css:selectedcss})
		day_nr.appendChild(DomCreateTextNode((i)));
		day.appendChild(day_nr);
		days.appendChild(day);
		day.onclick = function() {
			thisObj.day = this.childNodes[0].firstChild.nodeValue;
			thisObj.InsertDate();
			thisObj.Hide();
		}
	}

	inside_frame.appendChild(week);
	inside_frame.appendChild(days);
	
	outside_frame.appendChild(inside_frame);
	fieldset.appendChild(outside_frame);

	var thisObj = this; 

	select.onchange = function() {
		thisObj.month = this.options[this.selectedIndex].value;
		thisObj.Create();
	}

	arrow_up_year.onclick = function() {
		thisObj.year++;
		thisObj.Create();
	}

	arrow_down_year.onclick = function() {
		thisObj.year--;
		thisObj.Create();
	}

	this.created = true;
	
	this.contener.style.opacity = 95 * 0.01;
	this.contener.style.filter = 'alpha(opacity=' + 95 + ', #fbfbfd)';
},

InsertDate : function() {
	var date;
	var month = parseInt(this.month) + 1;
	
	date = this.year + '-';
	date += month < 10 ? '0' + month : month;
	date += '-';
	date += this.day < 10 ? '0' + this.day : this.day;
	$(this.destination).value = date;
},

Show : function() {

	this.Create();

	for (var i in this.others) {
		SetDisplay(this.others[i].contener, 'none');
	}

	SetDisplay(this.contener, 'block');
	this.showed = true;
},

Hide : function() {
	if (!this.showed) return;
	SetDisplay(this.contener, 'none');
	clearTimeout(this.timer);
	this.showed = false;
	this.OnHide();
},

SetTimer : function() {
	var thisObj = this;
	this.timer = setTimeout(function() {
		thisObj.Hide();
	}, 500);
},

AutoHide : function() {
	var thisObj = this;
	this.contener.onmouseout = function() {
		thisObj.DocumentClick();
	}
	
	this.contener.onmouseover = function() {
		document.onclick = null;
	}
	
	setTimeout(function(){
		thisObj.DocumentClick()
	}, 10);
},

DocumentClick : function() {
	var thisObj = this;
	document.onclick = function() {
		document.onclick = null;
		thisObj.Hide();
	}
},

Synch : function(obj) {

	if (this.others == null) {
		this.others = new Array;
	}

	this.others[this.others.length] = obj;
},

Manager : function() {
	document.onclick = null;
	
	if (this.showed) {
		this.Hide();
	} else {
		this.Show();
		this.AutoHide();
	}
}

};

function DomCreateElement(element, attributes)
{
	var domElement = document.createElement(element);
	if (attributes !== undefined) DomSetAttribute(domElement, attributes);
	
	return domElement;
}

function DomCreateTextNode(text)
{
	var domTextNode = document.createTextNode(text);
	return domTextNode;
}

function DomSetAttribute(domElement, params)
{
	var value;
	for (var i in params) {
		value = params[i];
		if (i == 'css') {
			domElement.className = value;
		} else {
			if (i.substr(0,1) == '_') {
				i = i.substr(1);
			}
			domElement.setAttribute(i, value);
		}
	}
}

function $(id)
{
	if ( typeof(id) == 'string' )
		if ( !document.getElementById(id) ) return;/* not found */
		else return document.getElementById(id);
	else return id;
}

function SetDisplay(obj, value)
{
	$(obj).style.display = value;
}
