/*
copyright by
F.H.U. Slawomir Kaniecki
e-mail: kontakt (at) www-editor (dot) net
/**/

/* Date.getWeek() comes from: http://javascript.about.com/library/blweekyear.htm */
/* Date.prototype.getWeek = function() {
	var onejan = new Date(this.getFullYear(), 0, 1);
	return Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
} */

//	alert(date("W", mktime(0, 0, 0, 1, 6, 2010)))

var slovek_cal_params = {
	monday_first: true,
	cal_on_right: true,
	mode:					"day"
}
//	alert(slovek_cal_params.mode)
//	alert(slovek_cal_params.mode111)
function slovek_cal(o1, offs, p) {
	prms = (slovek_cal_params) ? slovek_cal_params : {};
	if (p) {
		if (p.monday_first != undefined)	prms.monday_first = p.monday_first;
		if (p.cal_on_right != undefined)	prms.cal_on_right = p.cal_on_right;
		if (p.mode != undefined)					prms.mode = p.mode;
	}
// 	alert(o1.value)

	var of = null;
	if (typeof(o1) == 'string')      { of = oid(o1); }
	else if (typeof(o1) == 'object') { of = o1; if (!offs && of.value != '') { offs = of.value; } }
	else { of = null; }
	if (!of) return;
	of.blur();
	var o = oid('cal1div');
	if (o && of.opened && o.opened == of) {
		of.opened = false;
		if (o.off_to) clearTimeout(o.off_to);
		document.body.removeChild(o);
		return;
	}
	of.opened = true;
	if (!o) { o = document.createElement('div'); o.id = 'cal1div'; document.body.appendChild(o); }
	if (!o) return;
	o.opened = of;
	off_cal(of, o);
	build_cal(of, o, offs, prms);
}

function off_cal(of, o) {
	if (o.off_to) clearTimeout(o.off_to);
	o.off_to = setTimeout(function() {
		if (!o.off) { of.opened = false; document.body.removeChild(o); }
		else { off_cal(of, o); }
	}, 1000);
}

function build_cal(of, o, offs, prms) {
	prms = (prms) ? prms : slovek_cal_params;

	if (!of) return;
	if (!o) return;
	pos = element_position(of);
// 	alert(o + " = " + typeof o + " = " + typeof prms);
	if (typeof o != "string") {
		o.style.position = 'absolute';
		o.style.left = pos.left + ( (prms.cal_on_right) ? of.offsetWidth + 1 : 0 ) + 1;
		o.style.top = pos.top + ( (!prms.cal_on_right) ? of.offsetHeight + 1 : 0 ) + 1;
	}
	
	var now = new Date();
	var txt = '';
	var t, style, y, m, d;
	if (!offs) {
		y = now.getFullYear();
		m = now.getMonth();
		d = now.getDate();
	}
	else {
		t = (offs + '').split('-');
		y = parseInt(t[0]);
		m = parseInt( (t[1].substr(0 , 1) == "0") ? t[1].substr(1) : t[1]) - 1;
		if (t[2]) d = parseInt(t[2]);
	}
	today = (y == now.getFullYear() && m == now.getMonth()) ? now.getDate() : 0;
	var dt = new Date(y, m, 1);
	txt += '<TABLE CLASS="JSCAL" BORDER="0" CELLPADDING="0" CELLSPACING="0">' + "\n";
	//	txt += '<TR><TH id="jscl" COLSPAN="7">zamknij / close &nbsp;x</TH></TR>';

	txt += '<TR CLASS="JSCALH1">' + "\n";
	if (prms.mode == 'week') txt += '<TH>&nbsp;</TH>';
	txt += '<TH COLSPAN="2">';
	txt += '<A id="cpy">&nbsp;&lt;&lt;&nbsp;</A>';
	txt += '<A id="cpm">&nbsp;&lt;&nbsp;</A>';
	txt += '</TH>';
	txt += '<TH COLSPAN="3" style=""><nobr ID="JSCAL_YM">' + dt.getFullYear() + '-' + str_pad((dt.getMonth() + 1).toString(), 2, '0', 'STR_PAD_LEFT') + '</nobr></TH>';
	txt += '<TH COLSPAN="2">';
	txt += '<A id="cnm">&nbsp;&gt;&nbsp;</A>';
	txt += '<A id="cny">&nbsp;&gt;&gt;&nbsp;</A>';
	txt += '</TH>';
	txt += '</TR>' + "\n";
	nie  = '<TH>Nie</TH>';
	txt += '<TR CLASS="JSCALH2">' + "\n";
	if (prms.mode == 'week') txt += '<TH>tyd</TH>';
	if (!prms.monday_first) txt += nie;
	txt += '<TH>Pon</TH><TH>Wto</TH><TH>Śro</TH><TH>Czw</TH><TH>Pią</TH><TH>Sob</TH>';
	if (prms.monday_first) txt += nie;
	txt += '</TR>' + "\n";
	dinit = dt.getDay();
	if (prms.monday_first) dinit = (!dinit) ? 6 : dinit - 1;
	num = 0;
	for (j=0; j<6; j++) {
		txt += '<TR>' + "\n";
		//	current = new Date(dt.getFullYear(), dt.getMonth(), num + 1);
		//	if (prms.mode == 'week') txt += '<TH class="WEEK">' + dt.getMonth() + '</TH>';
		//	cw = date("W", mktime(0, 0, 0, dt.getMonth() + 1, num + 1, dt.getFullYear()))
		wk = (prms.mode == 'week') ? '<TH class="WEEK">??</TH>' : '';

		tyd = '';
		for (i=0; i<7; i++) {
			v = (j == 0 && i < dinit) ? -1 : num++;
			current = new Date(dt.getFullYear(), dt.getMonth(), num);
			if (current.getMonth() != dt.getMonth()) { v = -1; }

			style = (d && d - 1 == v) ? 'font-weight: bold; color: #000000; text-decoration: underline; ' : '';
			style += (today && today - 1 == v) ? 'background: #55FF55; ' : '';
			day = '<TD' + ( (v >= 0) ? ' CLASS="JSCALD1"' : '' ) + ' STYLE="' + style + '">' + ( (v >= 0) ? v + 1 : '&nbsp;') + '</TD>';
			tyd += day;
		}
		txt += wk;
		txt += tyd;
		txt += '</TR>' + "\n";
	}
	txt += '</TABLE>' + "\n";
	o.innerHTML = txt;
	o.onclick = function (e) {
		if (e) { targ = e.target; type = e.type; } // FF
		else if (window.event) { targ = window.event.srcElement; type = window.event.type; } // IE
		if      (targ.id == 'cpy') { build_cal(of, o, parseInt(dt.getFullYear() - 1) + '-' + (dt.getMonth() + 1), prms); }
		else if (targ.id == 'cny') { build_cal(of, o, parseInt(dt.getFullYear() + 1) + '-' + (dt.getMonth() + 1), prms); }
		else if (targ.id == 'cpm') { build_cal(of, o, dt.getFullYear() + '-' + dt.getMonth(), prms); }
		else if (targ.id == 'cnm') { build_cal(of, o, dt.getFullYear() + '-' + (dt.getMonth() + 2), prms); }
		//	else if (targ.id == 'jscl') { document.body.removeChild(o); }
		else if (targ.tagName == 'TD') {
			sel = parseInt(targ.innerHTML)
			ym = oid('JSCAL_YM');
			if (sel > 0 && ym) {
				of.opened = false;
				of.value = ym.innerHTML + '-' + str_pad(sel.toString(), 2, '0', 'STR_PAD_LEFT');
				document.body.removeChild(o);
			}
		}
	}
	o.onmouseover = function (e) {
		if (e) { targ = e.target; type = e.type; } // FF
		else if (window.event) { targ = window.event.srcElement; type = window.event.type; } // IE

		var po = find_o(targ, 'JSCAL');
		if (po) { o.off = true; }

		if      (targ.id == 'cpy') { targ.className = 'JSCALHOVERNAV'; }
		else if (targ.id == 'cny') { targ.className = 'JSCALHOVERNAV'; }
		else if (targ.id == 'cpm') { targ.className = 'JSCALHOVERNAV'; }
		else if (targ.id == 'cnm') { targ.className = 'JSCALHOVERNAV'; }
		else if (prms.mode == 'week' && targ.className == 'WEEK') {
			var t = targ.parentNode;
			if (t.tagName == 'TR') { t.className = 'JSCALTROVER'; }
		}
		else if (targ.tagName == 'TD') {
			if (prms.mode == 'week') {
				var t = targ.parentNode;
				if (t.tagName == 'TR') { t.className = 'JSCALTROVER'; }
			}
			else {
				if (parseInt(targ.innerHTML) > 0) { targ.className = 'JSCALOVER'; }
			}
		}
	}
	o.onmouseout = function (e) {
		if (e) { targ = e.target; type = e.type; } // FF
		else if (window.event) { targ = window.event.srcElement; type = window.event.type; } // IE

		var po = find_o(targ, 'JSCAL');
		if (po) { o.off = false; }

		if      (targ.id == 'cpy') { targ.className = ''; }
		else if (targ.id == 'cny') { targ.className = ''; }
		else if (targ.id == 'cpm') { targ.className = ''; }
		else if (targ.id == 'cnm') { targ.className = ''; }
		else if (prms.mode == 'week' && targ.className == 'WEEK') {
			var t = targ.parentNode;
			if (t.tagName == 'TR') { t.className = ''; }
		}
		else if (targ.tagName == 'TD') {
			if (prms.mode == 'week') {
				var t = targ.parentNode;
				if (t.tagName == 'TR') { t.className = ''; }
			}
			else {
				if (parseInt(targ.innerHTML) > 0) { targ.className = 'JSCALD1'; }
			}
		}
	}
/*
	of.onblur = function (e) {
		//	alert('a')
	}
/**/
}
