
function start_edit(id) {
	input = document.getElementById(id + '_input');
	caption = document.getElementById(id);

	caption.style.display = 'none';
	input.style.display = 'block';

	edit = document.getElementById(id + '_edit');
	save = document.getElementById(id + '_save');
	if (edit != null) {
		edit.style.display = 'none';
		save.style.display = 'block';
	}

	input.focus();
	input.select();
}

function start_image_edit(id) {
	start_edit(id);
	el = document.getElementById(id + '_loading_layer');
	el.className = 'loading';

}

function nl2br(str) {
	if (typeof (str) == "string")
		return str.replace(/(\r\n)|(\n\r)|\r|\n/g, "<BR>");
	else
		return str;
}

function stop_edit(id) {
	input = document.getElementById(id + '_input');
	caption = document.getElementById(id);
	caption.style.display = 'block';
	input.style.display = 'none';
	escaped = input.value;
	escaped = escaped.replace(/</g, '&lt;');
	escaped = escaped.replace(/>/g, '&gt;');
	escaped = nl2br(escaped);
	caption.innerHTML = escaped;

	if (edit != null) {
		edit = document.getElementById(id + '_edit');
		save = document.getElementById(id + '_save');

		edit.style.display = 'block';
		save.style.display = 'none';
	}
}

/** **** SOME AJAX-STUFF ***** */

// var http_request = false;
function getXMLObject() {
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
	return http_request;
}

queue = new Array();
xmlHttp = getXMLObject();

function getRequestObj() {
	try {
		return new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	}
	try {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) {
	}
	try {
		return new XMLHttpRequest();
	} catch (e) {
	}
	return false;
}
function makePOSTRequest(url, query) {

	var request = getRequestObj();

	request.onreadystatechange = function() {
		alertContents(request)
	};
	request.open('POST', url, true);
	
	
	//charet test
	//request.setRequestHeader("Accept-Charset", "ISO-8859-1");
	//request.setRequestHeader("Transfer-Encoding", "ISO-8859-1"); //
//	request.setRequestHeader("Accept-Encoding", "ISO-8859-1");
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; text/plain; charset=ISO-8859-1");
	
	
//	request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); 
	request.send(query);
}


function getVal(name, xmlHttp) {
	if (xmlHttp == null) {
		return null;
	}
	var responseElement = xmlHttp.responseXML.documentElement;
	var element = responseElement.getElementsByTagName(name)[0];
	if ((element != undefined) && (element.childNodes[0] != undefined)) {
		return element.childNodes[0].nodeValue;
	} else {
		return null;
	}

}

function alertContents(xmlHttp) {

	if (xmlHttp.readyState != 4)
		return;
	if (xmlHttp.status != 200)
		return;

	var valid = getVal('valid', xmlHttp);
	var id = getVal('id', xmlHttp);
	var value = getVal('value', xmlHttp);
	var execute = getVal('execute', xmlHttp);
	var element = document.getElementById(id);

	if ((id != null) && (value != null)) {
		element.innerHTML = value;
	}

	if (element != null) {
		if (valid == 'false') {
			element.style.background = '#ED9824';
		} else {
			element.style.background = 'white';
		}
	}

	if (execute != null) {
		eval(execute);
	}

}

function handle_update(handler, id, params) {
	if (handler == '') {
		return;
	}
	el = document.getElementById(id + '_input');
	if (el != null) {
		value = el.value;
	} else {
		value = '';
	}

	var poststr = "value=" + encodeURI(value) + "&id=" + encodeURI(id)
			+ "&ajax=true" + "&" + params;

	makePOSTRequest(handler, poststr);
}

function handleUpdateOnEnter(e, handler, id, params) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	}
	if (keycode == 13) {
		handle_update(handler, id, params);
	}

}

function handleUpdateAndStopEditOnEnter(e, handler, id, params) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	}
	if (keycode == 13) {
		handle_update(handler, id, params);
		stop_edit(id);
		return false;
	}
}

function getOwnNick() {
	elUsername = document.getElementById('usrname');
	ownNick = elUsername.value;
	return ownNick;
}

function checkForNewMessages(getall) {
	handle_update('chatbox', 'chatcontent', 'check=true&getall=' + getall);

	window.setTimeout("checkForNewMessages('false');", 10000);
}

function keypressHandler(e) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	}
	if (keycode == 13) {
		elChatinput = document.getElementById('chatinput_input');
		elActiveNick = document.getElementById('activenick');
		elUsername = document.getElementById('usrname');
		activeNick = elActiveNick.value;
		if (elChatinput.value != '') {
			processChatMessage(getOwnNick(), activeNick, elChatinput.value);
			handle_update('chatbox', 'chatinput', 'activenick=' + activeNick);
			elChatinput.value = '';
		}
	}
}

function getElementsByClassName(class_name) {
	var all_obj, ret_obj = new Array(), j = 0, teststr;

	if (document.all)
		all_obj = document.all;
	else if (document.getElementsByTagName && !document.all)
		all_obj = document.getElementsByTagName("*");

	for (i = 0; i < all_obj.length; i++) {
		if (all_obj[i].className.indexOf(class_name) != -1) {
			teststr = "," + all_obj[i].className.split(" ").join(",") + ",";
			if (teststr.indexOf("," + class_name + ",") != -1) {
				ret_obj[j] = all_obj[i];
				j++;
			}
		}
	}
	return ret_obj;
}

function update_picture(nickname, url) {
	if (url == '') {
		url = 'images/standard_picture.png';
	}
	url = 'image/?frame=small_avatar&image=' + url;

	var test = new Image();
	test.src = url;
	test.onload = function() {
		imgEl = document.getElementById('tab_img_' + nickname);
		imgEl.src = test.src;
	}

}

function addChatContent(nickname, status) {
	elChatContents = document.getElementById('chatcontents');
	elChatContent = document.createElement('div');
	elChatContent.id = 'chatcontent_' + nickname;
	elChatContent.className = 'content_' + status;
	elChatContents.appendChild(elChatContent);

}

function addChatTab(nickname, status) {
	el = document.getElementById('tab_template');
	newEl = el.cloneNode(true);
	newEl.style.display = 'block';
	newEl.id = 'tab_' + nickname;
	newEl.className = status;
	newEl.onclick = function() {
		switchChatTab(nickname);
	};
	img = newEl.childNodes[1];
	img.id = 'tab_img_' + nickname;

	addChatContent(nickname, status);

	chatElement = document.getElementById('chatElement');
	chatElement.appendChild(newEl);

	if (status == 'active') {
		elActiveNick = document.getElementById('activenick');
		elActiveNick.value = nickname;
	}

	return newEl;
}

function switchChatTab(nickname) {
	var obj = getElementsByClassName('active');
	for (i = 0; i < obj.length; i++) {
		obj[i].className = 'inactive';
	}

	el = document.getElementById('tab_' + nickname);
	if (el == null) {
		el = addChatTab(nickname, 'active');
	}
	el.className = 'active';

	/** Make chatcontent visible* */
	var obj = getElementsByClassName('content_active');
	for (i = 0; i < obj.length; i++) {
		obj[i].className = 'content_inactive';
	}
	el = document.getElementById('chatcontent_' + nickname);
	el.className = 'content_active';
	/** ************************* */

	// change the pic
	// var obj=getElementsByClassName('chat_image');
	// for(i=0;i<obj.length;i++) {
	// if (obj[i].alt != '') {
	// obj[i].src='image/?frame=small_avatar&image=' + obj[i].alt;
	// }
	// }
	// imgEl = document.getElementById( 'tab_img_' + nickname );
	// if (imgEl.alt != '') {
	// imgEl.src = 'image/?frame=small_avatar_green&image=' + imgEl.alt;
	// }
	// ***
	elActiveNick = document.getElementById('activenick');
	elActiveNick.value = nickname;
	makePOSTRequest('chatbox',
			'ajax=true&action=openSession&chatPartner=' + nickname);

	el = document.getElementById('chatinput_input');
	el.focus();
}

function edit_image(id) {
	el = document.getElementById(id + '_img');
	el.className = 'editing';

	TB_show('Upload an image',
			'#TB_inline?height=145&width=300&inlineId=' + id + '_editor', 'no');
}

function cancel_edit_image(id) {
	TB_remove();
}

/** ************************* */
/* Enzian - FILESYSTEM */
/** ************************* */
function trash_picture(id, handler) {
	element = document.getElementById(id);
	trsh = document.getElementById('trash');
	element.style.display = 'none';
	trsh.appendChild(element);
	realId = id.substr(5);
	element.style.display = 'block';
	handle_update(handler, realId, 'action=trash');
}

function untrash_picture(id, handler) {
	element = document.getElementById(id);
	wstbin = document.getElementById('wastebin');
	element.style.display = 'none';
	wstbin.appendChild(element);
	realId = id.substr(5);
	element.style.display = 'block';
	handle_update(handler, realId, 'action=untrash');

}

function delete_trash(handler) {
	makePOSTRequest(handler, 'action=deletetrash&ajax=true');
	trash = document.getElementById('trash');
	trash.innerHTML = '';

}
/** ************************* */
/* Enzian - Album */
/** ************************* */

function Album(ID) {
	this.ID = ID;
	this.width = 410;
	this.current = null;
	this.coming = null;
	this.standing = true;
	this.currentPos = 0;
	this.comingPos = 0;
	this.velocity = 2;
	this.imgContHeight = 0;
	this.blockNumber = 1;
}

Album.prototype.toRight = function() {

	if (this.standing) {
		this.coming = document
				.getElementById("images" + (this.blockNumber + 1));
		this.current = document.getElementById("images" + this.blockNumber);

		this.blockNumber++;

		this.current.style.display = 'block';
		this.coming.style.display = 'block';

		this.comingPos = this.width;
		this.currentPos = 0;

		this.standing = false;

		setTimeout("album" + this.ID + ".toRight()", 40);
	} else {

		if (this.comingPos > 0) {

			if (this.comingPos > (this.width / 2)) {
				this.velocity += 2;
			} else {
				this.velocity -= 2;
			}
			if (this.velocity <= 1) {
				this.velocity = 2;
			}

			this.comingPos -= this.velocity;
			this.currentPos -= this.velocity;

			setTimeout("album" + this.ID + ".toRight()", 40);
		} else {
			this.comingPos = 0;
			this.current.style.display = 'none';
			this.standing = true;
		}
	}
	this.coming.style.left = this.comingPos + 'px';
	this.current.style.left = this.currentPos + 'px';
}

Album.prototype.toLeft = function() {

	if (this.standing) {
		this.coming = document
				.getElementById("images" + (this.blockNumber - 1));
		this.current = document.getElementById("images" + this.blockNumber);
		this.blockNumber--;
		this.current.style.display = 'block';
		this.coming.style.display = 'block';

		this.comingPos = -this.width;
		this.currentPos = 0;

		this.standing = false;

		setTimeout("album" + this.ID + ".toLeft()", 40);
	} else {
		if (this.comingPos < 0) {

			if (this.comingPos < (-this.width / 2)) {
				this.velocity += 2;
			} else {
				this.velocity -= 2;
			}
			if (this.velocity <= 1) {
				this.velocity = 2;
			}

			this.comingPos += this.velocity;
			this.currentPos += this.velocity;

			setTimeout("album" + this.ID + ".toLeft()", 40);
		} else {
			this.comingPos = 0;
			this.current.style.display = 'none';
			this.standing = true;
		}
	}
	this.coming.style.left = this.comingPos + 'px';
	this.current.style.left = this.currentPos + 'px';
}

Album.prototype.openImageContainer = function() {
	if (this.imgContHeight < 320) {
		imageCont = document.getElementById(this.ID + '_imagecontainer');
		imageCont.style.height = this.imgContHeight + 'px';
		this.imgContHeight = this.imgContHeight + 30;
		setTimeout("album" + this.ID + ".openImageContainer()", 40);
	}
}

Album.prototype.closeImageContainer = function() {
	if (this.imgContHeight > 0) {
		imageCont = document.getElementById(this.ID + '_imagecontainer');
		this.imgContHeight = this.imgContHeight - 30;
		imageCont.style.height = this.imgContHeight + 'px';
		setTimeout("album" + this.ID + ".closeImageContainer()", 40);
	}
}

Album.prototype.setAlbumImage = function(imageFile) {
	if ((this.imageFile == imageFile) && (this.imgContHeight > 0)) {
		this.closeImageContainer();
	} else {
		this.imageFile = imageFile;
		albumImageEl = document.getElementById(this.ID + '_image');
		albumImageEl.src = imageFile;
		this.openImageContainer();
	}
}

/** ******** Videolist *********** */

function Clipr(id) {
	this.id = id;
	this.standing = true;
	this.top = 0;
	this.top_target = 0;
	this.height = 240;
	this.velocity = 0;

}

Clipr.prototype.down = function() {
	if (this.standing) {
		this.top_target = this.top_target + this.height;
		this.move();

		el = document.getElementById('flashbox');
		el.innerHTML = "<div id=\"flashoverlay\" >&nbsp;</div>";

		el = document.getElementById('test_clipr');
		el.style.display = 'block';
	}
}

Clipr.prototype.up = function() {
	if (this.standing) {
		this.top_target = this.top_target - this.height;
		this.move();

		el = document.getElementById('flashbox');
		el.innerHTML = "<div id=\"flashoverlay\" >&nbsp;</div>";

		el = document.getElementById('test_clipr');
		el.style.display = 'block';
	}
}

Clipr.prototype.move = function() {
	difference = Math.abs(this.top - this.top_target);
	if ((difference < (this.height / 2)) && (this.velocity > 2)) {
		this.velocity -= 2;
	} else {
		this.velocity += 2;
	}

	if (this.top < this.top_target) {
		if (difference < this.velocity) {
			this.top = this.top_target;
		} else {
			this.top = this.top + this.velocity;
		}
	}
	if (this.top > this.top_target) {
		if (difference < this.velocity) {
			this.top = this.top_target;
		} else {
			this.top = this.top - this.velocity;
		}
	}

	cliprEl = document.getElementById('test_clipr');

	cliprEl.style.top = (-this.top) + 'px';
	if (this.top != this.top_target) {
		setTimeout("clipr" + this.id + ".move()", 40);
	}
}

// Allgemeines Show zum einblenden
function show(id) {
	el = document.getElementById(id);
	el.style.display = 'block';
}

function hide(id) {
	el = document.getElementById(id);
	el.style.display = 'none';
}

/** ********** Für graue input-felder welche den Text ändern ******* */
function getFocus(elementID, text) {
	element = document.getElementById(elementID);

	element.value = element.value.replace(text, '');
	element.style.color = 'black';

}

function looseFocus(elementID, text) {
	element = document.getElementById(elementID);
	if (element.value == '') {
		element.value = text;
		element.style.color = 'gray';
		element.type = 'text';
	}
}

/** ************************* */
/* Enzian - RICHTEXT EDITOR */
/** ************************* */

function editRichtext(id) {
	el = document.getElementById(id + '_frame');
	el.className = 'enz-edit';
}

function saveRichtext(id, handler, column, row) {
	// var editor = tinyMCE.getInstanceById( );
	var content = document.getElementById(id + '_content');
	var value = tinyMCE.get(id + '_editor').getContent();
	content.innerHTML = value;

	var elFrame = document.getElementById(id + '_frame');
	elFrame.className = 'enz-display';

	poststr = "ajax=true&column=" + column + "&row=" + row + "&value="
			+ encodeURIComponent(value);
	makePOSTRequest(handler, poststr);

}

/** ************************* */
/* Enzian - SEARCHBOX */
/** ************************* */

function getSelectedItem(items) {
	for ( var i = 0; i < items.length; i++) {
		if (items[i]['selected']) {
			return i;
		}
	}
	return -1;
}

function selectNextVisible(id, items) {
	var selectedItem = getSelectedItem(items);
	for ( var i = ++selectedItem; i < items.length; i++) {
		if (items[i]['visible']) {
			selectItem(id, items, i);
			return;
		}
	}
}

function selectPrevousVisible(id, items) {
	var selectedItem = getSelectedItem(items);
	for ( var i = --selectedItem; i >= 0; i--) {
		if (items[i]['visible']) {
			selectItem(id, items, i);
			return;
		}
	}
}

function selectItem(id, items, index) {
	for ( var i = 0; i < items.length; i++) {
		var el = document.getElementById(id + '_item_' + i);
		if (i == index) {
			el.className = 'selected';
			items[i]['selected'] = true;
		} else {
			items[i]['selected'] = false;
			if (el != null) {
				el.className = '';
			}
		}
	}
}

function hideSearchbox(id) {
	elResults = document.getElementById(id + '_results');
	elResults.style.display = 'none';
}

function hideSearchboxDelayed(id) {
	window.setTimeout('hideSearchbox( \'' + id + '\' );', 400);
}

function showSearchbox(id) {
	elResults = document.getElementById(id + '_results');
	elResults.style.display = 'block';
	elInput = document.getElementById(id + '_search');
}

function getSelectedItem(items) {
	for ( var i = 0; i < items.length; i++) {
		if (items[i]['selected'] == true) {
			return i;
		}
	}
	return -1;
}

function selectSearch(searchID, itemID, items, handler) {
	elInput = document.getElementById(searchID + '_search');
	elInput.value = items[itemID]['name'];
	
	elId = document.getElementById(searchID + '_id');
	elId.value = items[itemID]['id'];

	elResults = document.getElementById(searchID + '_results');
	elResults.style.display = 'none';

	makePOSTRequest(handler, 'ajax=true&item=' + items[itemID]['id']);
}

function submitSearchBox(e, id, items, handler) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	}

	if (keycode == 13) {
		var result = false;
		var elResults = document.getElementById(id + '_results');
		if (elResults.style.display == "none") {
			result = true;
		}
		selectSearch(id, getSelectedItem(items), items, handler);
		return result;
	}
	return true;
}

function filterSearch(e, id, items, handler) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	}

	if (keycode == 13) {
		selectSearch(id, getSelectedItem(items), items, handler);
		return false;
	}

	if (keycode == 40) {
		selectNextVisible(id, items);
	} else if (keycode == 38) {
		selectPrevousVisible(id, items);
	} else {

		elInput = document.getElementById(id + '_search');
		value = elInput.value;

		elResults = document.getElementById(id + '_results');
		elResults.innerHTML = '';

		for ( var i = 0; i < items.length; i++) {
			if ((items[i]['name'].toLowerCase().indexOf(value.toLowerCase()) != -1)
					|| (items[i]['keywords'].toLowerCase().indexOf(
							value.toLowerCase()) != -1)) {
				if (items[i]['selected']) {
					classStatement = 'class="selected" ';
				} else {
					classStatement = '';
				}
				elResults.innerHTML = elResults.innerHTML + '<div '
						+ classStatement + 'onmouseover="selectItem(\'' + id
						+ '\', items' + id + ', ' + i + ')" id="' + id
						+ '_item_' + i + '" onclick="selectSearch( \'' + id
						+ '\', \'' + i + '\', items' + id + ', \'' + handler
						+ '\')">' + items[i]['name'] + '</div>';
				items[i]['visible'] = true;
			} else {
				items[i]['visible'] = false;
			}
		}

		elResults.style.display = 'block';
	}
}

/** ************************* */
/* Enzian - UPDOWN */
/** ************************* */

function doUp(id) {
	elContent = document.getElementById(id + '_ud_content');
	elInput = document.getElementById(id + '_ud_input');

	elInput.value = parseInt(elInput.value) + 1;
	elContent.innerHTML = elInput.value;
}

function doDown(id) {
	elContent = document.getElementById(id + '_ud_content');
	elInput = document.getElementById(id + '_ud_input');

	elInput.value = parseInt(elInput.value) - 1;
	elContent.innerHTML = elInput.value;
}

/*******************************************************************************
 * Dynamic Ajax Content- © Dynamic Drive DHTML code library
 * (www.dynamicdrive.com) This notice MUST stay intact for legal use Visit
 * Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 ******************************************************************************/

var bustcachevar = 1
// bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects = ""
var rootdomain = "http://" + window.location.hostname
var bustcacheparameter = ""

function ajaxpage(url, containerid) {
	var page_request = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject) { // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} catch (e) {
			try {
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			} catch (e) {
			}
		}
	} else
		return false
	page_request.onreadystatechange = function() {
		loadpage(page_request, containerid)
	}
	if (bustcachevar) // if bust caching of external page
		bustcacheparameter = (url.indexOf("?") != -1) ? "&"
				+ new Date().getTime() : "?" + new Date().getTime()
	page_request.open('GET', url + bustcacheparameter, true)
	page_request.send(null)
}

function loadpage(page_request, containerid) {
	if (page_request.readyState == 4
			&& (page_request.status == 200 || window.location.href
					.indexOf("http") == -1))
		document.getElementById(containerid).innerHTML = page_request.responseText
}

function loadobjs() {
	if (!document.getElementById)
		return
	for (i = 0; i < arguments.length; i++) {
		var file = arguments[i]
		var fileref = ""
		if (loadedobjects.indexOf(file) == -1) { // Check to see if this
													// object has not already
													// been added to page before
													// proceeding
			if (file.indexOf(".js") != -1) { // If object is a js file
				fileref = document.createElement('script')
				fileref.setAttribute("type", "text/javascript");
				fileref.setAttribute("src", file);
			} else if (file.indexOf(".css") != -1) { // If object is a css
														// file
				fileref = document.createElement("link")
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
		}
		if (fileref != "") {
			document.getElementsByTagName("head").item(0).appendChild(fileref)
			loadedobjects += file + " " // Remember this object as being already
										// added to page
		}
	}
}
