var XMLHttp = ajax ();
var fix = false;
var fixEl = false;

/* Ajax */
function ajax (handler)
{
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp = new XMLHttpRequest ();
		if (objXMLHttp.overrideMimeType) {
			objXMLHttp.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { // For IE
		try {
			objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	return objXMLHttp;
}

// Calendar
function pushCalendar()
{
	if (4 == XMLHttp.readyState) {
		if ( 200 == XMLHttp.status) {
			document.getElementById ('calendar').innerHTML = XMLHttp.responseText;
			return;
		}
		fix = false;
	}
}

function calendarMinus(month,year)
{
	if (1 == month)
	{
		month=13;
		year--;
	}
	month--;
	XMLHttp.open ("GET", "/calendar.php?y="+year+"&m="+month, true);
	XMLHttp.onreadystatechange = pushCalendar;
	XMLHttp.send (null);
	return false;
}

function calendarPlus(month,year)
{
	if (12 == month)
	{
		month=0;
		year++;
	}
	month++;
	XMLHttp.open ("GET", "/calendar.php?y="+year+"&m="+month, true);
	XMLHttp.onreadystatechange = pushCalendar;
	XMLHttp.send (null);
	return false;
}

function fixEvent(elId)
{
	if (!fix)
	{
		document.getElementById('cevents-'+elId).style.display='';
		fix = true;
	} else {
		fix = false;
	}
	
	return false;
}

function showEvent(elId)
{
	if (!fix)
		document.getElementById('cevents-'+elId).style.display='';
}

function hideEvent(elId)
{
	if (!fix)
	document.getElementById('cevents-'+elId).style.display='none';
}

function replyto(id, text)
{
	var reply_to = document.getElementById('reply_to');
	var comment_text = document.getElementById('comment_text');
	if (-1 != reply_to.value && '' != comment_text)
	{
		if(!confirm('Už jsi na nějaký komentář reagoval. Opravdu reagovat na nově zvolený?'))
			return false;
	}
	reply_to.value = id;
	if('' != comment_text.value)
	{
		comment_text.value += "\n\n";
	}
	comment_text.value += text + "\n\n";
	comment_text.focus();
	return false;
}

var lastFmContainer = new Array();
function getLastFm()
{
	var http = ajax ();
	http.open("GET", "/lastfm.xml", true);
	http.onreadystatechange = function() {
		if (4 == http.readyState && 200 == http.status) {
			var x = http.responseXML;
			var container = x.getElementsByTagName('note');
			for(var z in container) {
				try {
					lastFmContainer.push(container[z].firstChild.data);
				} catch(e) {
					break;
				}
			}
			showLastFm();
			return;
		}
	};
	http.send(null);
}
// Last.fm
function getLastFmOld()
{
	var http = ajax ();
	http.open("GET", "/lastfm.txt", true);
	http.onreadystatechange = function() {
		if (4 == http.readyState && 200 == http.status) {
			var txt = http.responseText;
			var lines = txt.split("\n");
			for (var z in lines)
			{
				var line = lines[z].split("#", 2);
				if (line[0] == "now")
				{
					lastFmContainer.push('<img src="/layout/icon_lastfm.png" alt="LastFm" style="border:0"/> Právě si pouštím <a href="http://www.last.fm/user/georgo10"><strong>' + line[1] + '</strong></a>');
				}
				else if (line[0] == "today")
				{
					lastFmContainer.push('<img src="/layout/icon_lastfm.png" alt="LastFm" style="border:0"/> Dnes jsem poslouchal <a href="http://www.last.fm/user/georgo10"><strong>' + line[1] + '</strong></a>');
				}
				else if (line[0] == "DPP")
				{
					lastFmContainer.push('<img src="/layout/icon_dpp.png" alt="DPP" title="DPP" style="border:0"/>&nbsp;' + line[1] + '</a>');
				}
				else if (line[0] == "DPMO")
				{
					lastFmContainer.push('<img src="/layout/icon_dpmo.png" alt="DPMO" title="DPMO" style="border:0"/>&nbsp;' + line[1] + '</a>');
				}
				else if (line[0] == "ČD")
				{
					lastFmContainer.push('<img src="/layout/icon_cd.png" alt="ČD" title="ČD" style="border:0"/>&nbsp;' + line[1] + '</a>');
				}
				else if (line[0] == "DPMB")
				{
					lastFmContainer.push('<img src="/layout/icon_dpmb.png" alt="DPMB" title="DPMB" style="border:0"/>&nbsp;' + line[1] + '</a>');
				}
				else if (line[0] == "SA")
				{
					lastFmContainer.push('<img src="/layout/icon_sa.png" alt="SA" title="Student Agency" style="border:0"/>&nbsp;' + line[1] + '</a>');
				}

			}
			showLastFm();
			return;
		}
	};
	http.send(null);
}

function showLastFm()
{
	var lfm = document.getElementById('lastfm');
	lfm.style.display = "none";
	if(lastFmContainer.length)
	{
		var code = lastFmContainer.shift();
		lfm.innerHTML = code;
		lfm.style.display = "block";
		setTimeout("showLastFm();",10000);
	} else {
		lfm.innerHTML = "";	
	}
	return;
}
