function initGallery(gal)
{

	tds = document.getElementById('galerie').getElementsByTagName('td');

	for (i = 0;i < tds.length;i++) {
		title = tds[i].getElementsByTagName('a')[0].title;
		tds[i].innerHTML = '<a href="/galerie/' + gal + '/' + (i+1) + '.jpg" rel="lightbox[kletr]" title="' + title + '"><img src="/galerie/' + gal + '/_thumbs/' + (i+1) + '.jpg" alt="' + title + '" /></a>';
	}

}

/**
 * URL of the poll script used for display/voting
 */
var url = 'http://kletr.info/poll.php';

/**
 * id of element in which the poll is displayed
 */
var replaceel = 'poll';

function Ajax()
{
	var ajax = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
		try {
			ajax = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				ajax = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (E) {
				ajax = false;
			}
		}
	@end @*/
	if (!ajax && typeof XMLHttpRequest != 'undefined') {
		ajax = new XMLHttpRequest();
	}
	return ajax;
}

function ajaxReplace(id, url)
{
	var ajax = new Ajax();
	if (!ajax) {
		return false;
	}
	ajax.open('GET', url, true);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4) {
			document.getElementById(id).innerHTML = ajax.responseText;
		}
	}
	ajax.send(null);
	return true;
}

function displayPoll() 
{
	req = ajaxReplace(replaceel,url);
}

function vote(vote)
{
	req = ajaxReplace(replaceel,url + '?vote=' + vote);
}

function insertAtCursor(myField, myValue)
{
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}

function addSmiley(smiley)
{
	var area = document.getElementById('kletrchattextarea');
	insertAtCursor(area,  ' ' + smiley + ' ');
}

$(document).ready(function()
{
	$('#kletrchattextarea').autogrow();
	$('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
	//$('table#galerie').unblock();
});