var MESSAGE = null;
var MONTHS = new Array('leden',
											 'únor',
											 'březen',
											 'duben',
											 'květen',
											 'červen',
											 'červenec',
											 'srpen',
											 'září',
											 'říjen',
											 'listopad',
											 'prosinec');

function openForm(date) {
	e = document.getElementById('inner-left');
	
	forms = e.getElementsByTagName('form');
	
	for (var i = 0; i < forms.length; i++) {
		forms[i].className = 'hidden';
	}
	
	e = document.getElementById('tasting-form-' + date);
	e.className = '';
}

function XMLDoc() {
	var me = this;
	var req = null;
	
	if (window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = null;
		}
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = null;
			}
		}
	} else {
		alert("Tento prohlížeč nepodporuje XML, některé funkce stránky budou nedostupné.");
	}
	
	this.request = req;
	
	this.loadXMLDoc = function(url, loadHandler) {
		if (this.request) {
			this.request.open("GET", url, true);
			this.request.onreadystatechange = function() {
				loadHandler(me);
			};
			this.request.setRequestHeader("Content-Type", "text/xml");
			this.request.send(null);
		}
	};
};

function setCalendar(year, month) {
	var calendarRequest = new XMLDoc();
	if (year && month) {
		calendarRequest.loadXMLDoc("calendar.php?Y=" + year + "&M=" + month, updateCalendar);
	} else {
		calendarRequest.loadXMLDoc("calendar.php", updateCalendar);
	}
};

function updateCalendar(req) {
	req = req.request;
	if (req.readyState == 4 && req.status == 200) {
		var year 		= req.responseXML.getElementsByTagName("calendar")[0].getAttribute("year");
		var month 	= req.responseXML.getElementsByTagName("calendar")[0].getAttribute("month");
		var Y				= parseInt(year, 10);
		var M				= parseInt(month, 10);
		var today 	= req.responseXML.getElementsByTagName("calendar")[0].getAttribute("today");
		var data 		= req.responseXML.getElementsByTagName("month")[0];
		var numdays = data.getAttribute("numdays");
		var begin		= data.getAttribute("begin");
		var events 	= new Array(numdays);
		var tasting = new Array(numdays);
		var date;
		
		for (var i = 0; i < data.childNodes.length; i++) {
			if (data.childNodes[i].nodeType == 1) {
				date = data.childNodes[i].getAttribute("date");
				if (data.childNodes[i].nodeName.toUpperCase() == 'EVENT') {
					events[date] = data.childNodes[i].firstChild.nodeValue;
				}
				
				if (data.childNodes[i].nodeName.toUpperCase() == 'TASTING') {
					tasting[date] = new Array(data.childNodes[i].firstChild.nodeValue, data.childNodes[i].getAttribute("time"), data.childNodes[i].getAttribute("price"));
				}
			}
		}
		
		var html = new Array();
		var count;
		var day;
		
		html.push('<table border="0" cellpadding="0" cellspacing="1">');
		
		for (var y = 0; y < 6; y++) {
			html.push("<tr>");
			for (var x = 0; x < 7; x++) {
				count = (y * 7) + (x + 1);
				day 	= count - begin + 1;
				date	= year + "-" + month + "-" + (day < 9 ? "0" + day : day);
				
				if (count < begin || day > numdays) {
					html.push('<td><img src="images/calendar/blank.gif" alt="" /></td>');
				} else if (events[date] != undefined || tasting[date] != undefined) {
					html.push('<td onmouseover="displayDate(this);" onmouseout="displayDate(this);">');
					html.push('<img src="images/calendar/e' + day + '.gif" alt="' + day + '" width="26" height="26" />');
					html.push('<div style="position: relative;">');
					html.push('<div class="event hidden">');
					html.push('<div class="event-top"><!-- --></div>');
					html.push('<div class="event-body">');
					if (tasting[date] != undefined) {
						html.push('<strong>' + tasting[date][0] + '</strong>');
						html.push('<br /><br />');
						html.push('<strong style="width: 3em; display: block; float: left;">Čas:</strong>&nbsp;&nbsp;' + tasting[date][1]);
						html.push('<br style="clear:both;" />');
						html.push('<strong style="width: 3em; display: block; float: left;">Cena:</strong>&nbsp;&nbsp;' + tasting[date][2]);
						html.push('<br style="clear:both;" /><br />');
						html.push('<a href="?go=tasting&amp;date=' + date + '#form-' + date + '">&rsaquo; zúčastnit se</a>');
					}
					if (events[date] != undefined && tasting[date] != undefined) {
						html.push('<span class="divider"></span>');
					}
					if (events[date] != undefined) {
						html.push('<strong>' + events[date] + '</strong>');
					}
					html.push('</div>');
					html.push('</div>');
					html.push('</div>');
				} else if (date == today) {
					html.push('<td><img src="images/calendar/t' + day + '.gif" alt="' + day + '" width="26" height="26" /></td>');
				} else {
					html.push('<td><img src="images/calendar/d' + day + '.gif" alt="' + day + '" width="26" height="26" /></td>');
				}
			}
			html.push("</tr>");
		}
		
		html.push("<tr>");
		
		if (M == 1) {
			html.push('<td class="dates"><a href="javascript:void(0);" onclick="setCalendar(' + (Y - 1) + ', 12);"><img src="images/calendar/prev.gif" alt="předcházející měsíc" onmouseover="this.src=\'images/calendar/prev2.gif\';" onmouseout="this.src=\'images/calendar/prev.gif\';" /></a></td>');
		} else {
			html.push('<td class="dates"><a href="javascript:void(0);" onclick="setCalendar(' + Y + ', ' + (M - 1) + ');"><img src="images/calendar/prev.gif" alt="předcházející měsíc" onmouseover="this.src=\'images/calendar/prev2.gif\';" onmouseout="this.src=\'images/calendar/prev.gif\';" /></a></td>');
		}
		
		html.push('<td colspan="5" class="dates">');
		html.push('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="134" height="26">');
		html.push('<param name="movie" value="swf/dates_v2.swf?dates=' + MONTHS[M - 1] + '%20%E2%80%99' + year.substring(2) + '" />');
		html.push('<param name="quality" value="high" />');
		html.push('<param name="wmode" value="transparent" />');
		html.push('<param name="menu" value="false" />');
		html.push('<!--[if !IE]>-->');
		html.push('<object type="application/x-shockwave-flash" data="swf/dates_v2.swf?dates=' + MONTHS[M - 1] + '%20%E2%80%99' + year.substring(2) + '" width="134" height="26">');
		html.push('<param name="quality" value="high" />');
		html.push('<param name="wmode" value="transparent" />');
		html.push('<param name="menu" value="false" />');
		html.push('<!--<![endif]-->');
		html.push(MONTHS[M - 1] + " ’" + year.substring(2));
		html.push('<!--[if !IE]>-->');
		html.push('</object>');
		html.push('<!--<![endif]-->');
		html.push('</object>');
		html.push('</td>');
		
		if (M == 12) {
			html.push('<td class="dates"><a href="javascript:void(0);" onclick="setCalendar(' + (Y + 1) + ', 1);"><img src="images/calendar/next.gif" alt="následující měsíc" onmouseover="this.src=\'images/calendar/next2.gif\';" onmouseout="this.src=\'images/calendar/next.gif\';" /></a></td>');
		} else {
			html.push('<td class="dates"><a href="javascript:void(0);" onclick="setCalendar(' + Y + ', ' + (M + 1) + ');"><img src="images/calendar/next.gif" alt="následující měsíc" onmouseover="this.src=\'images/calendar/next2.gif\';" onmouseout="this.src=\'images/calendar/next.gif\';" /></a></td>');
		}
		html.push("</tr>");
		html.push("</table>");
		
		document.getElementById("calendar").innerHTML = html.join("");
	}
};

function displayDate(e) {
	var div = e.getElementsByTagName("div")[1];
	div.className != "event" ? div.className = "event" : div.className = "event hidden";
};

function checkMailForm() {
	var f = document.getElementById('mail-form');
	var e	= new Array();
	var r = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if (f['mail-name'].value == '') e.push('— Zadejte prosím jméno a příjmení.');
	if (f['mail-address'].value == '' || !r.test(f['mail-address'].value)) e.push('— Zadejte prosím platnou e-mailovou adresu.');	
	if (f['mail-agree'].checked != true) e.push('— Zaškrtněte prosím souhlas se zpracováním osobních údajů.');
	if (e.length > 0) {
		alert(e.join("\n"));
		return false;
	} else {
		return true;
	}
};

function checkTastingForm(f) {
	var e	= new Array();
	var r = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if (f['tasting-name'].value == '') e.push('— Zadejte prosím jméno a příjmení.');
	if (f['tasting-address'].value == '' || !r.test(f['tasting-address'].value)) e.push('— Zadejte prosím platnou e-mailovou adresu.');
	r = /^(\+420)? ?\d{3} ?\d{3} ?\d{3}$/;
	if (f['tasting-phone'].value == '' || !r.test(f['tasting-phone'].value)) e.push('— Zadejte prosím platné telefonní číslo.');
	if (e.length > 0) {
		alert(e.join('\n'));
		return false;
	} else {
		return true;
	}
};

function checkMessage() {
	if (MESSAGE != null) {
		alert(MESSAGE);	
	}
};

function setSize() {
	var e = document.getElementById('window');
	if (e.className != 'hidden') {
		var db = document.body;
		var dde = document.documentElement;
		var docHeight = Math.max(db.scrollHeight, dde.scrollHeight, db.offsetHeight, dde.offsetHeight, db.clientHeight, dde.clientHeight)
		e.style.height = docHeight + 'px';
	}
};

function openWindow(date) {
	var html = new Array();
	
	html.push('<div id="window">');
	html.push('<div id="window-base">');
	html.push('<div id="window-top"><!-- --></div>');
	html.push('<div id="window-body">');
	html.push('<h3><span>Degustace</span></h3>');
	html.push('<p><strong style="width: 4em; display: block; float: left;">Datum:</strong> ' + date + '<br style="clear:both;" /><strong style="width: 4em; display: block; float: left;">Čas:</strong> 18.30 hod.<br style="clear:both;" /><strong style="width: 4em; display: block; float: left;">Cena:</strong> 800,— Kč<br style="clear:both;" /><br /><strong>Ochutnávka francouzských vín z vinařství De Neuve a Château Le Bernat</strong></p>');
	html.push('<span class="divider"></span>');
	html.push('<div id="step1">');
	html.push('<p>Prosím vyplňte následující formulář, budeme Vás kontaktovat pro potvrzení Vaší účasti.</p>');
	html.push('<table border="0" cellpadding="0" cellspacing="0" style="margin-top: 2em; width: 60%;">');
	html.push('<tr>');
	html.push('<td><strong>Jméno a příjmení:</strong></td>');
	html.push('<td><input name="tasting-name" id="tasting-name" type="text" class="full input" /></td>');
	html.push('</tr>');
	html.push('<tr>');
	html.push('<td colspan="2">&nbsp;</td>');
	html.push('</tr>');
	html.push('<tr>');
	html.push('<td><strong>Počet osob:</strong></td>');
	html.push('<td>');
	html.push('<select name="tasting-count" id="tasting-count" class="input">');
	html.push('<option value="1">1</option>');
	html.push('<option value="2">2</option>');
	html.push('<option value="3">3</option>');
	html.push('<option value="4">4</option>');
	html.push('</select>');
	html.push('</td>');
	html.push('</tr>');
	html.push('<tr>');
	html.push('<td colspan="2">&nbsp;</td>');
	html.push('</tr>');
	html.push('<tr>');
	html.push('<td><strong>E-mail:</strong></td>');
	html.push('<td><input name="tasting-address" id="tasting-address" type="text" class="full input" /></td>');
	html.push('</tr>');
	html.push('<tr>');
	html.push('<td colspan="2">&nbsp;</td>');
	html.push('</tr>');
	html.push('<tr>');
	html.push('<td><strong>Telefon:</strong></td>');
	html.push('<td><input name="tasting-phone" id="tasting-phone" type="text" class="full input" /></td>');
	html.push('</tr>');
	html.push('</table>');
	html.push('<input name="tasting-submit" value="true" type="image" src="_img/submit.png" alt="Odeslat" onmouseover="this.src=\'_img/submit2.png\'" onmouseout="this.src=\'_img/submit.png\'" style="position: absolute; left: 25px; bottom: 25px;" onclick="checkTastingForm();" />');
	html.push('<input name="tasting-cancel" value="true" type="image" src="_img/cancel.png" alt="Storno" onmouseover="this.src=\'_img/cancel2.png\'" onmouseout="this.src=\'_img/cancel.png\'" style="position: absolute; right: 25px; bottom: 25px;" onclick="closeWindow();" />');
	html.push('</div>');
	
	html.push('<div id="step2" class="hidden">');
	html.push('<p>Děkujeme za Váš zájem a věříme, že u nás budete spokojeni.<br />Brzy Vás budeme kontaktovat pro potvrzení Vaší účasti.<br /><br />Vaše Malostranská vinotéka —<br />The Wine Bar</p>');
	html.push('<input name="tasting-close" value="true" type="image" src="_img/close.png" alt="Odeslat" onmouseover="this.src=\'_img/close2.png\'" onmouseout="this.src=\'_img/close.png\'" style="position: absolute; left: 25px; bottom: 25px;" onclick="closeWindow();" />');
	html.push('</div>');
	html.push('</div>');
	html.push('</div>');
	html.push('</div>');
	
	var d = document.createElement('div');
	d.innerHTML = html.join("");
	document.body.appendChild(d);
	
	var db = document.body;
	var dde = document.documentElement;
	var docHeight = Math.max(db.scrollHeight, dde.scrollHeight, db.offsetHeight, dde.offsetHeight, db.clientHeight, dde.clientHeight)

	var e = document.getElementById('window');
	e.style.height = docHeight + 'px';
};

function closeWindow() {
	w = document.getElementById('window').parentNode;
	w.parentNode.removeChild(w);
};

if (window.addEventListener) {
	window.addEventListener('load', checkMessage, false);	
	window.addEventListener('load', setCalendar, false);
	window.addEventListener('resize', setSize, false);
}
else if (window.attachEvent) {
	window.attachEvent('onload', checkMessage);
	window.attachEvent('onload', setCalendar);
	window.attachEvent('onresize', setSize);	
}
