var background_active = "#0B6AAC";
var background_idle = "#F3F3F3";
var color_active = "#FFFFFF";
var color_idle = "#000000";
var color_past = "#9B9B9B";

function getObj(obj) {
    if(document.getElementById) { return document.getElementById(obj); }
    if(document.all) { return document.all[obj]; }
    if(document.layers) { return document.layers[obj]; }
}

function isChild(src, dest) {
	while(src) {
		if(src == dest) return true;
		src = src.parentNode;
	}
	return false;
}

function checkClick(e) {
	e ? evt = e : evt = event;
	cse = evt.target ? evt.target : evt.srcElement;
	if(getObj('calendar') && !isChild(cse, getObj('calendar'))) getObj('calendar').style.clip='rect(0px 0px 0px 0px)';
}

function zeroPad(num, count) {
  var result = num + '';
  while(result.length < count)
    result = "0" + result;
  return result;
}

// HTML kód kalendáře
document.write('<div id="calendar"><table><tr>');
document.write('<td onClick="javascript:return calendarSubMonth();"><img src="design/arrow-month-left.gif"></td>');
document.write('<th colspan="5" id="calendar_month_select"></th>');
document.write('<td onClick="javascript:calendarAddMonth();"><img src="design/arrow-month-right.gif"></td>');
document.write('</tr><tr>');
document.write('<td class="weekend">Ne</td>');
document.write('<td class="weekday">Po</td>');
document.write('<td class="weekday">Út</td>');
document.write('<td class="weekday">St</td>');
document.write('<td class="weekday">Čt</td>');
document.write('<td class="weekday">Pá</td>');
document.write('<td class="weekend">So</td></tr>');

// Tabulka dnů
for(row = 1; row <= 6; row++) {
	document.write('<tr>');
	for(col = 1; col <= 7; col++)
		document.write('<td class="day" id="v' + (7 * (row - 1) + col) + '">&nbsp;</td>');
	document.write('</tr>');
}

document.write('</table></div>');
document.all ? document.attachEvent('onclick', checkClick) : document.addEventListener('click', checkClick, false);




// Calendar script
var now = new Date;
var sccm = now.getMonth();
var sccy = now.getFullYear();
var ccm = now.getMonth();
var ccy = now.getFullYear();

var obj_day;
var obj_month;
var obj_year;

function lcs(button, day, month, year) {
  // Datová pole
  obj_day = getObj(day);
  obj_month = getObj(month);
  obj_year = getObj(year);
  // Nastavení pozice
  button = getObj(button);
  button_pos = findElementPos(button);
	getObj('calendar').style.clip = "rect(0px auto auto 0px)";
	getObj('calendar').style.left = (button_pos[0] + 18) + 'px';
	getObj('calendar').style.top = button_pos[1] + 'px';
  // Sestavení kalendáře
	isdt = true;
	ccm = obj_month.value - 1;
	ccy = obj_year.value;
	prepcalendar(obj_day.value, obj_month.value - 1, obj_year.value);
}

function evtTgt(e)
{
	var el;
	if(e.target) el = e.target;
	else if(e.srcElement) el = e.srcElement;
	if(el.nodeType == 3) el = el.parentNode; // defeat Safari bug
	return el;
}
function EvtObj(e) { if(!e) e = window.event; return e; }
function cs_over(e) { evtTgt(EvtObj(e)).style.background = background_active; }
function cs_out(e) { evtTgt(EvtObj(e)).style.background = background_idle; }

/**
 * Výběr hodnoty v kalendáři a nastavení příslušných polí
 * @param e Událost
 */
function calendar_select_click(e) {
  array = calendar_values[evtTgt(EvtObj(e)).id.substring(1, evtTgt(EvtObj(e)).id.length)];
  obj_day.value = zeroPad(array[0], 2);
  obj_month.value = zeroPad(array[1], 2);
  obj_year.value = array[2];
	getObj('calendar').style.clip = 'rect(0px 0px 0px 0px)';
}

var mn = new Array('Leden','Únor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Řijen','Listopad','Prosinec');
var calendar_values = new Array(42);

function f_cps(obj) {
	obj.style.background = background_idle;
	obj.style.color = color_idle;
	obj.style.font = '10px Arial';
	obj.style.textAlign = 'center';
	obj.style.textDecoration = 'none';
	obj.style.cursor = 'pointer';
}

function f_cpps(obj) {
	obj.style.background = background_idle;
	obj.style.color = color_past;
	obj.style.font = '10px Arial';
	obj.style.textAlign = 'center';
	obj.style.cursor = 'pointer';
}

function f_hds(obj) {
	obj.style.background = background_active;
	obj.style.color = color_active;
	obj.style.font = 'bold 10px Arial';
	obj.style.textAlign = 'center';
	obj.style.cursor = 'pointer';
}

// day selected
function prepcalendar(hd, cm, cy) {
	today = new Date();
	selected_day = today.getDate();
	if(cy<sccy - 2) {
    cy = sccy;
    cm = sccm;
  }
  
	first = new Date();
	first.setFullYear(cy, cm, 1);
	
	current_day = first.getDay();
	
	getObj('calendar_month_select').innerHTML = mn[cm]+ ' ' + cy;
	month_length = ((cy % 4) == 0)
    ? new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
    : new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	for(var d = 1; d <= 42; d++) {
		f_cps(getObj('v' + parseInt(d)));
		if(d > current_day && d <= current_day + month_length[cm]) {
      cur = d == current_day && cm == sccm && cy == sccy;
			past = d - current_day < selected_day && cm == sccm && cy == sccy || cm < sccm && cy == sccy || cy < sccy;
			high = hd != '' && d - current_day == hd;
			if(past) f_cpps(getObj('v' + parseInt(d)));
			if(cur) f_cps(getObj('v' + parseInt(d)));
			else if(high) f_hds(getObj('v' + parseInt(d)));
			getObj('v' + parseInt(d)).onmouseover = (high) ? null : cs_over;
			getObj('v' + parseInt(d)).onmouseout = (high) ? null : cs_out;
			getObj('v' + parseInt(d)).onclick = calendar_select_click;
			getObj('v' + parseInt(d)).innerHTML = d - current_day;
      // Pole hodnot [den, měsíc, rok]
			calendar_values[d] = [(d - current_day), (cm - (-1)), cy];
		}
		else {
			getObj('v'+d).innerHTML = '&nbsp;';
			getObj('v'+parseInt(d)).onmouseover = null;
			getObj('v'+parseInt(d)).onmouseout = null;
			getObj('v'+parseInt(d)).style.cursor = 'default';
		}
	}
}

prepcalendar('', ccm, ccy);

function calendarAddMonth() {
	ccm += 1;
	if(ccm >= 12) ccm = 0, ccy++;
	prepcalendar('', ccm, ccy);
}

function calendarSubMonth() {
	ccm -= 1;
	if(ccm < 0) ccm = 11, ccy--;
	prepcalendar('', ccm, ccy);
}
