function highlight(week, day) {
	var tr_elem = document.getElementById("week"+week);
	var td_elem = document.getElementById("day"+day);

	tr_elem.className = "valittuviikko";
	td_elem.className = "valittupaiva";
}

function unhighlight(week, day) {
	var tr_elem = document.getElementById("week"+week);
	var td_elem = document.getElementById("day"+day);

	tr_elem.className = "";
	td_elem.className = "";
}


function haeTapahtumat(paiva, kk, vuosi) {

	var req = null;
	if(window.XMLHttpRequest) req = new XMLHttpRequest();
	else
	  if(window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");

	req.onreadystatechange = function() {
	  if(req.readyState == 4) {
	    if(req.status == 200) {
		var doc = req.responseXML;

		var aika = document.createElement('div');
		aika.setAttribute('id','kalenteriotsikko');
		aika.setAttribute('class','kalenteriotsikko');
		aika.setAttribute('className','kalenteriotsikko');
		var alkuaika = doc.getElementsByTagName("alkuaika")[0].firstChild.nodeValue;
		var loppuaika = doc.getElementsByTagName("loppuaika")[0].firstChild.nodeValue;
		aika.appendChild(document.createTextNode("Tapahtumat väliltä " + alkuaika + " - " + loppuaika + ":"));

		var kalenteritapahtumat = document.getElementById("kalenteritapahtumat");
		kalenteritapahtumat.innerHTML = "";
		kalenteritapahtumat.appendChild(aika);
		var tapahtumat = doc.getElementsByTagName("tapahtumat");


		for(i = 0; i < tapahtumat[0].childNodes.length; i++) {
//			if(tapahtumat[0].childNode[i].nodeType != 1) continue;

			var tapahtuma = document.createElement('table');
			tapahtuma.setAttribute('class','kalenteritapahtuma');
			tapahtuma.setAttribute('className','kalenteritapahtuma');

			var tmp = document.createElement('tbody');
			tapahtuma.appendChild(tmp);

			var row = document.createElement('tr');

			var alku_pvm = doc.getElementsByTagName("alku_pvm")[i].childNodes[0].nodeValue;
			var container = document.createElement('td');
			container.setAttribute('class','kalenteritapahtumaotsikko');
			container.setAttribute('className','kalenteritapahtumaotsikko');
			var theData = document.createTextNode(alku_pvm + " ");
			container.appendChild(theData);
			row.appendChild(container);

			var alku_klo = doc.getElementsByTagName("alku_klo")[i].childNodes[0].nodeValue;
			theData = document.createTextNode(alku_klo + " - ");
			container.appendChild(theData);
			row.appendChild(container);

			var paikka = doc.getElementsByTagName("paikka")[i].childNodes[0].nodeValue;
			theData = document.createTextNode(paikka);
			container.appendChild(theData);
			row.appendChild(container);
			tmp.appendChild(row);	// lisää otsikkorivi taulukkoon


/*			var otsikko = doc.getElementsByTagName("otsikko")[i].childNodes[0].nodeValue;
			container = document.createElement('td');
			theData = document.createTextNode(otsikko);
			container.appendChild(theData);
			row.appendChild(container);
*/
			row = document.createElement('tr');
			var teksti = doc.getElementsByTagName("teksti")[i].childNodes[0].nodeValue;
			container = document.createElement('td');
//			container = nl2br(teksti);
			container.appendChild(nl2br(teksti));
			container.setAttribute('class','kalenteritapahtumateksti');
			container.setAttribute('className','kalenteritapahtumateksti');
			row.appendChild(container);

			tmp.appendChild(row);

			kalenteritapahtumat.appendChild(tapahtuma);
		}
	    }
	  }

	};
	req.open("POST", "kalenteri.php", true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send("paiva=" + paiva + "&kk=" + kk + "&vuosi=" + vuosi);
}

function nl2br(syote) {
	var palautus = document.createElement('p');
	var palautusteksti = "";
	var teksti = "" + syote;
	var char;
	for(j = 0; j < syote.length; j++) {
		char = syote.charAt(j);
		if(char != '\n') {
			palautusteksti += "" + char;
		} else {
			palautus.appendChild(document.createTextNode(palautusteksti));
			palautusteksti = "";
			palautus.appendChild(document.createElement('br'));
		}
	}
	palautus.appendChild(document.createTextNode(palautusteksti));
	return palautus;
}

function puhe(id) {

	var req = null;
	if(window.XMLHttpRequest) req = new XMLHttpRequest();
	else
	  if(window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");

	req.onreadystatechange = function() {
	  if(req.readyState == 4) {
	    if(req.status == 200) {
		var doc = req.responseXML;

		var window_x, window_y;
		if (self.innerHeight) {
			window_x = self.innerWidth;
			window_y = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			window_x = document.documentElement.clientWidth;
			window_y = document.documentElement.clientHeight;
		} else if (document.body) {
			window_x = document.body.clientWidth;
			window_y = document.body.clientHeight;
		}

		var el = document.getElementById("tausta");
		el.setAttribute('class','tausta');
		el.setAttribute('className','tausta');
		el.style.width = '' + window_x + 'px';
		el.style.height = '' + window_y + 'px';
		el.style.display = '';

		var tulosta = document.getElementById("tulostanappula");
		tulosta.style.display = 'block';
		tulosta.style.left = (window_x / 2 + 150) + 'px';

		var tulostalinkki = document.getElementById("tulostalinkki");
		tulostalinkki.setAttribute('href','puhe.php?id='+id);

		var sulje = document.getElementById("suljenappula");
		sulje.style.display = 'block';
		sulje.style.left = (window_x / 2 + 240) + 'px';

		var puhe = document.getElementById("tekstialusta");
		puhe.setAttribute('class','tekstialusta');
		puhe.setAttribute('className','tekstialusta');
		puhe.style.left = (window_x / 2 - 300) + 'px';
		puhe.style.height = (window_y - 100) + 'px';

		var tekstialusta = nl2br(doc.getElementsByTagName("teksti")[0].childNodes[0].nodeValue);

		puhe.innerHTML = "";
		puhe.appendChild(tekstialusta);
		puhe.style.display = '';
	    }
	  }
	};
	req.open("POST", "puhe.php", true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send("id=" + id);

}

function kolumni(id) {

	var req = null;
	if(window.XMLHttpRequest) req = new XMLHttpRequest();
	else
	  if(window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");

	req.onreadystatechange = function() {
	  if(req.readyState == 4) {
	    if(req.status == 200) {
		var doc = req.responseXML;

		var window_x, window_y;
		if (self.innerHeight) {
			window_x = self.innerWidth;
			window_y = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			window_x = document.documentElement.clientWidth;
			window_y = document.documentElement.clientHeight;
		} else if (document.body) {
			window_x = document.body.clientWidth;
			window_y = document.body.clientHeight;
		}

		var el = document.getElementById("tausta");
		el.setAttribute('class','tausta');
		el.setAttribute('className','tausta');
		el.style.width = '' + window_x + 'px';
		el.style.height = '' + window_y + 'px';
		el.style.display = '';

		var tulosta = document.getElementById("tulostanappula");
		tulosta.style.display = 'block';
		tulosta.style.left = (window_x / 2 + 150) + 'px';

		var tulostalinkki = document.getElementById("tulostalinkki");
		tulostalinkki.setAttribute('href','kolumni.php?id='+id);

		var sulje = document.getElementById("suljenappula");
		sulje.style.display = 'block';
		sulje.style.left = (window_x / 2 + 240) + 'px';

		var kolumni = document.getElementById("tekstialusta");
		kolumni.setAttribute('class','tekstialusta');
		kolumni.setAttribute('className','tekstialusta');
		kolumni.style.left = (window_x / 2 - 300) + 'px';
		kolumni.style.height = (window_y - 100) + 'px';

		var otsikko = document.createElement('h3');
		otsikko.appendChild(document.createTextNode(doc.getElementsByTagName("otsikko")[0].childNodes[0].nodeValue));

		var tekstialusta = nl2br(doc.getElementsByTagName("teksti")[0].childNodes[0].nodeValue);

		var julkaisija = document.createElement('p');
		julkaisija.appendChild(document.createTextNode(doc.getElementsByTagName("julkaisija")[0].childNodes[0].nodeValue + ","));
		julkaisija.appendChild(document.createElement('br'));
		julkaisija.appendChild(document.createTextNode(doc.getElementsByTagName("pvm")[0].childNodes[0].nodeValue));

		kolumni.innerHTML = "";
		kolumni.appendChild(julkaisija);
		kolumni.appendChild(otsikko);
		kolumni.appendChild(tekstialusta);
		kolumni.style.display = '';
	    }
	  }
	};
	req.open("POST", "kolumni.php", true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send("id=" + id);

}

function piilotapuhe() {
		var el = document.getElementById("tausta");
		el.style.display = 'none';

		var tulosta = document.getElementById("tulostanappula");
		tulosta.style.display = 'none';

		var sulje = document.getElementById("suljenappula");
		sulje.style.display = 'none';

		var puhe = document.getElementById("tekstialusta");
		puhe.style.display = 'none';
}

function naytakuva(kuva, kuvateksti) {

	var isokuvatiedosto = new Image();
	isokuvatiedosto.src = 'kuvat/' + kuva;

	var window_x, window_y, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}
	if (self.innerHeight) {
		window_x = self.innerWidth;
		window_y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		window_x = document.documentElement.clientWidth;
		window_y = document.documentElement.clientHeight;
	} else if (document.body) {
		window_x = document.body.clientWidth;
		window_y = document.body.clientHeight;
	}

	var isokuva = document.getElementById("isokuva");
	isokuva.src = 'kuvat/' + kuva;

	if(isokuvatiedosto.height > (window_y * 0.8)) {
		isokuva.style.height = '' + (window_y * 0.8) + 'px';
	}

	var el = document.getElementById("tausta");
	el.setAttribute('class','tausta');
	el.setAttribute('className','tausta');
	el.style.width = '' + window_x + 'px';
	el.style.height = '' + window_y + 'px';
	el.style.top = '' + yScroll + 'px';
	el.style.display = '';

	var kuvateksti_el = document.getElementById("kuvateksti");
	kuvateksti_el.innerHTML = "";
	kuvateksti_el.appendChild(document.createTextNode('' + kuvateksti));

	var kuvatausta = document.getElementById("kuvatausta");
	kuvatausta.style.display = 'block';
	kuvatausta.style.top = '' + (yScroll + 25) + 'px';
	kuvatausta.style.left = '' + (window_x / 2) - (kuvatausta.offsetWidth/2) + 'px';
}

function piilotakuva() {
	var el = document.getElementById("tausta");
	el.style.display = 'none';

	var kuvatausta = document.getElementById("kuvatausta");
	kuvatausta.style.display = 'none';
}

