var metatags = document.getElementsByTagName("meta");
for(m in metatags)
	if(metatags[m].name=="Language")
		document.write('<script type="text/javascript" src="/js/lang/' + metatags[m].content + '.js"></script>');

function getLangVar(key){
	return langData[key];
}



var panes = new Array();

function setupPanes(containerId, defaultTabId) {
  // go through the DOM, find each tab-container
  // set up the panes array with named panes
  // find the max height, set tab-panes to that height
  panes[containerId] = new Array();
  var maxHeight = 0; var maxWidth = 0;
  var container = document.getElementById(containerId);
  var paneContainer = container.getElementsByTagName("div")[0];
  var paneList = paneContainer.childNodes;
  for (var i=0; i < paneList.length; i++ ) {
    var pane = paneList[i];
    if (pane.nodeType != 1) continue;
    if (pane.offsetHeight > maxHeight) maxHeight = pane.offsetHeight;
    if (pane.offsetWidth  > maxWidth ) maxWidth  = pane.offsetWidth;
    panes[containerId][pane.id] = pane;
    pane.style.display = "none";
  }
//    paneContainer.style.height = maxHeight + "px";
    paneContainer.style.width  = maxWidth + "px";
    document.getElementById(defaultTabId).onclick();
}

function showPane(paneId, activeTab) {
  // make tab active class
  // hide other panes (siblings)
  // make pane visible
  
    for (var con in panes) {
    activeTab.blur();
    activeTab.className = "tab-active";
    if (panes[con][paneId] != null) { // tab and pane are members of this container
      var pane = document.getElementById(paneId);
      pane.style.display = "block";
      var container = document.getElementById(con);
      var tabs = container.getElementsByTagName("ul")[0];
      var tabList = tabs.getElementsByTagName("a")
      for (var i=0; i<tabList.length; i++ ) {
        var tab = tabList[i];
        if (tab != activeTab) tab.className = "tab-disabled";
      }
      for (var i in panes[con]) {
        var pane = panes[con][i];
        if (pane == undefined) continue;
        if (pane.id == paneId) continue;
        pane.style.display = "none"
      }
    }
  }
  return false;    
}








function selectbox_move(fromE, toE, moveAll){
	if(!fromE.nodeType) fromE = document.getElementById(fromE);
	if(!toE.nodeType) 	toE   = document.getElementById(toE);

	move = new Array();

	for(var i = 0;i<fromE.options.length;i++){
		if(fromE.options[i].selected || moveAll)
			move.push(i);
	}

	for(i in move){
/*Ü		try { // only gecko
			toE.options[toE.options.length] = fromE.options[move[i]-i];
		} catch(e){*/
			var option = fromE.options[move[i]-i];
			toE.options[toE.options.length] = new Option (option.text, option.value,false,false);
			fromE.options[move[i]-i]=null;
//		}
	}
}

function selectbox_moveOnKeypress(from,to,event){
	var code = event.which ? event.which : event.keyCode;
	if(code==13)
		selectbox_move(from,to);
}

function selectbox_sort(from,direction){
	options = document.getElementById(from).options;
	
	for(var i = direction<0 ? 0 : options.length-1; direction<0 ? i < options.length : i>=0; direction<0 ? i++ : i--){
		if(!options[i].selected || i+direction < 0 || i+direction >= options.length )
			continue;
		var option2 = new Option(options[i+direction].text, options[i+direction].value,options[i+direction].selected,options[i+direction].selected);
		options[i+direction] = new Option(options[i].text, options[i].value,true,true);
		options[i] = option2;
	}
}

var activeTextbox = null;

function showTextbox(fromE){

	var textbox        = document.getElementById("textbox");
	var selected = null;
	options = fromE.options;
	for(var i = 0;i<options.length;i++){
		if(!options[i].selected)
			continue;
		if(selected){ // avoid multiple checks
			selected = null;
			break;
		}
		if(options[i].value.indexOf("textbox")==0){
			selected = fromE.value;
		}
	}
	
	var activeBefore = activeTextbox;

	if(activeTextbox){
		fromE.form[activeTextbox].value = tinyMCE.getContent();
	}

	activeTextbox    = selected;
	textbox.disabled = !selected;
	textbox.value    = selected && fromE.form[selected] ? fromE.form[selected].value : '';
//	textboxWrapper.style.display='block';
	
	if(activeTextbox){
		if(!activeBefore)
			tinyMCE.execCommand('mceAddEditor','','textbox');
		tinyMCE.setContent(textbox.value);
	}else if(activeBefore)
		tinyMCE.execCommand('mceRemoveEditor','','textbox');
}







function deleteGbEntry(id,e){
	var callback = function(){
		e.parentNode.removeChild(e);
	}
	if(confirm(langData.get('guestbook.deleteQuestion')))
		AJAX_Command("/!XML","guestbook.delete",{id:id},callback);
}

function approveGbEntry(id,e){
	var callback = function(){
		e.parentNode.removeChild(e);
		if(e2 = document.getElementById('allEntries')){
			e = document.getElementById('entry'+id);
			e.parentNode.removeChild(e);
			if(e2.hasChildNodes())
				e2.insertBefore(e,e2.firstChild);
			else
				e2.appendChild(e);
		}
	}
	AJAX_Command("/!XML","guestbook.approve",{id:id},callback);
}


function deleteEntry(blog,entry,e){
	var callback = function(){
		if(!e)
			e = document.getElementById("entry"+entry);
		e.parentNode.removeChild(e);
	}
	if(confirm(langData.get('entry.deleteQuestion')))
		AJAX_Command("/!XML","entry.delete",{blog:blog,entry:entry},callback);
}

function deletePingback(id,e){
	var callback = function(){
		e.parentNode.removeChild(e);		
	}
	if(confirm(langData.get('pingback.deleteQuestion')))
		AJAX_Command("/!XML","pingback.delete",{id:id},callback);
}


function deletePm(id,e){
	var callback = function(){
		e.parentNode.removeChild(e);		
	}
	if(confirm(langData.get('pm.deleteQuestion')))
		AJAX_Command("/!XML","pm.delete",{id:id},callback);
}


function quoteComment(e){
	var cite   = e.getElementsByTagName('cite')[0];
	var block  = e.cloneNode(true);
	
	if(cite.firstChild.data){
		author = cite.firstChild.data.trim();
		if((i = author.trim().indexOf(" ")) > 0)
			author = author.substring(0,i);
	}else{
		author = cite.firstChild.firstChild.data.trim();
	}
	
	block.removeChild(block.getElementsByTagName('cite')[0]);
	var childNodes = block.childNodes;

	for(var i=0; i < childNodes.length; i++)
		if(childNodes[i].tagName=="BLOCKQUOTE" || childNodes[i].tagName=="BR" || childNodes[i].tagName=="A")
			block.removeChild(childNodes[i--]);
	
	value = "<blockquote><cite>"+author+":</cite>" + block.innerHTML.trim() + "</blockquote>";
	
	var textarea = document.getElementById('comment');
	textarea.value = value + "\n";
	textarea.focus();
}

function deleteComment(id,e){
	var callback = function(){
		if(!e)
			e = document.getElementById("comment"+id);
		e.parentNode.removeChild(e);		
	}
	if(confirm(langData.get('comment.deleteQuestion')))
		AJAX_Command("/!XML","comment.delete",{id:id},callback);
}

function approveComment(id,e){
	var callback = function(){
		e.parentNode.removeChild(e);
	}
	AJAX_Command("/!XML","comment.approve",{id:id},callback);
}

function switchPollStatus(id,e){
	var img    = e.firstChild;
	var active = (img.src ? img.src : img.style.filter).indexOf("green.png") != -1; // IE Workaround
	var callback = function(){
		if(img.src)
			img.src          = active ? img.src.replace(/green/,"red") : img.src.replace(/red/,"green");
		else // IE Workaround
			img.style.filter = active ? img.style.filter.replace(/green/,"red") : img.style.filter.replace(/red/,"green");
		e.style.cursor='';
	}
	e.style.cursor='wait';
	AJAX_Command("/!XML","poll.setstatus",{id:id,status:active ? 0 : 1},callback);
}

function deletePoll(id){
	var callback = function(){
		e = document.getElementById('poll'+id);
		e.parentNode.removeChild(e);
	}
	if(confirm("DELETE ?"))
		AJAX_Command("/!XML","poll.delete",{id:id},callback);
}

function submitComment(f){
//	return true;
	var callback = function(xml){
		var p = document.createElement("div");
		p.className='success';
		p.appendChild(document.createTextNode(langData.get('comment.submitConfirmation')));
		f.parentNode.replaceChild(p,f);
	}
	AJAX_Command("/!XML","comment.submit",f,callback);
	return false;
}

function submitGuestbook(f){
//	return true;
	var callback = function(xml){
		var p = document.createElement("div");
		p.className='success';
		p.appendChild(document.createTextNode(langData.get('guestbook.submitConfirmation')));
		f.parentNode.replaceChild(p,f);
	}
	AJAX_Command("/!XML","guestbook.submit",f,callback);
	return false;
}


function cycleRows(table,classes){
	var classes = classes.split(',');
	var rows    = table.tBodies[0].rows;
	for(var i=0; i<rows.length; i++)
		rows[i].className = classes[ i % classes.length];
}


/* AJAX-Klasse */

function AJAX(){

	var ajax = this;
	this.postString = null;

	this.getHttpRequest = function(){
		if ( window.XMLHttpRequest )		// Gecko
			return new XMLHttpRequest() ;
		else if ( window.ActiveXObject )	// IE
			return new ActiveXObject("MsXml2.XmlHttp") ;
	}
	
	this.setPostVars = function(vars){
		if(vars.tagName && vars.tagName=="FORM"){
			var elements = {}
			for(var i=0; i < vars.length; i++)
				if(vars[i].name)
					elements[vars[i].name] = vars[i].value;
			var string = this.serializeObject(elements);
		}else{
			var string = this.serializeObject(vars);
		}
		this.postString = string.length>0 ? string : null;
	}
	
	this.serializeObject = function(obj,prefix){
		var string = "", keystr="";
		for(var key in obj){
			if(string.length>0)
				string += "&";
			keystr = prefix ? prefix+"["+key+"]" : key;
			
			if(typeof obj[key] == "object" || typeof obj[key] == "array")
				string += this.serializeObject(obj[key], keystr);
			else
				string += encodeURIComponent(keystr) + "=" + encodeURIComponent(obj[key]);
		}
		return string;
    }

	this.loadUrl = function(url, onload){
		var request   = this.getHttpRequest();
		var getMethod = this.postString==null;
		
		request.open(getMethod ? "GET" : "POST", url, true) ;

		request.onreadystatechange = function(){
			if (request.readyState != 4)
				return;

			if ( request.status == 200 ){
				if(onload) onload(request) ;
			}else
				alert( 'XML request error: ' + request.statusText + ' (' + request.status + ')' ) ;
		}
		if(!getMethod)
			request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		request.send(this.postString);
	}
}

function AJAX_Command(url, command, vars, onloadFunction){
	url+= "?command="+command;

	var onload = function(xml){
		if(!xml.responseXML)
			alert("XML Request Error:\n"+xml.responseText);
		else{
			
			var recode   = xml.responseXML.selectSingleNode('Response').attributes.getNamedItem('statuscode').value;
			var evalCode = xml.responseXML.selectSingleNode('Response').attributes.getNamedItem('eval');
			if(evalCode)
				eval(evalCode.value);
			if(recode==0){
				if(onloadFunction)onloadFunction(xml.responseXML);
			}else if(recode==100){
				alert(xml.responseXML.selectSingleNode('Response').attributes.getNamedItem('statustext').value);
			}else
				alert("XML Request Error #"+recode+":\n" + xml.responseXML.selectSingleNode('Response').attributes.getNamedItem('statustext').value);
		}
	}

	var ajax = new AJAX();
	if(vars)
		ajax.setPostVars(vars);
	ajax.loadUrl(url, onload);
}



String.prototype.trim = function () {
	return (this.replace(/\s+$/,"").replace(/^\s+/,""));
};


// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/
if( document.implementation.hasFeature("XPath", "3.0") ){
	if( typeof XMLDocument == "undefined" ){ XMLDocument = Document; }
  XMLDocument.prototype.selectNodes = function(cXPathString, xNode){
    if( !xNode ) { xNode = this; } 
		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++){aResult[i] =  aItems.snapshotItem(i);	}
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode){
		if( !xNode ) { xNode = this; } 
		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 ){return xItems[0];	}
		else{return null;	}
	}
	Element.prototype.selectNodes = function(cXPathString){
		if(this.ownerDocument.selectNodes){	return this.ownerDocument.selectNodes(cXPathString, this);}
		else{throw "For XML Elements Only";}
	}
	Element.prototype.selectSingleNode = function(cXPathString){	
		if(this.ownerDocument.selectSingleNode){return this.ownerDocument.selectSingleNode(cXPathString, this);	}
		else{throw "For XML Elements Only";}
	}
}







if(navigator.appName=="Microsoft Internet Explorer"){
	function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
	   {
	   for(var i=0; i<document.images.length; i++)
		  {
		  var img = document.images[i]
		  var imgName = img.src.toUpperCase()
		  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			 {
			 var imgID = (img.id) ? "id='" + img.id + "' " : ""
			 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			 var imgStyle = "display:inline-block;" + img.style.cssText 
			 if (img.align == "left") imgStyle = "float:left;" + imgStyle
			 if (img.align == "right") imgStyle = "float:right;" + imgStyle
			 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
			 
			 var strNewHTML = "<span " + imgID + imgClass + imgTitle
			 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			 + "(src=\'" + img.src + "\', sizingMethod='crop');\"></span>" 
			 img.outerHTML = strNewHTML
			 i = i-1
		 }
	  }
	}
	/*
	for (var ss = 0; ss < document.styleSheets.length; ++ss) with (document.styleSheets[ss])
	{
	for (var rule = 0; rule < rules.length; ++rule) with (rules[rule])
	{
	if (style
	&& style.backgroundImage
	&& style.backgroundImage != "initial")
	{
	// alert ("StyleSheet no. " + ss + ", rule no. " + rule + " has backgroundImage " + style.backgroundImage);
	var path = style.backgroundImage.substr (4, style.backgroundImage.length - 5);
	if (path.substr (path.length-4, path.length-1) == ".png")
	{
	style.filter ="progid:DXImageTransform.Microsoft.AlphaImageLoader (sizingMethod='crop', src='" + path + "')";
	style.backgroundImage = "";
	}
	}
	}
	}*/

	window.attachEvent("onload", correctPNG);
}















/* Google Web Search */

var gWebSearch;
var gWebSearch_query;
var gWebSearch_host;
var gWebSearch_path;

function search_submit(query) {
	gWebSearch_query = query;
	gWebSearch = new GwebSearch();
	gWebSearch.setSiteRestriction(gWebSearch_host);
	gWebSearch.setResultSetSize(GSearch.LARGE_RESULTSET);
	gWebSearch.setSearchCompleteCallback(null, search_onResults);
	gWebSearch.setQueryAddition("inurl:"+gWebSearch_host+"/"+gWebSearch_path);
	gWebSearch.execute(query);
}

function search_onResults() {
	if (!gWebSearch.results) return;
	var searchresults = document.getElementById("searchresults");
	searchresults.innerHTML = "";
	
	var results = "";
	for (var i = 0; i < gWebSearch.results.length; i++) {
		var thisResult = gWebSearch.results[i];
		
		var pos = thisResult.title.lastIndexOf('|');
		if(pos > 5)
			thisResult.title = thisResult.title.substr(0,pos-1);
		
		
		results += "<p>";
		results += "<a href=\"" + thisResult.url + "\">" + thisResult.title + "<\/a><br \/>";
		results += thisResult.content + "<br \/>";
//		results += "<span class=\"url\">" + thisResult.url + "<\/span>";
//		if (thisResult.cacheUrl) {
//			results += " - <a class=\"cached\" href=\"" + thisResult.cacheUrl + "\">Cached <\/a>";
//		}
		results += "<\/p>";
	}
	
	if(gWebSearch.results.length==0){
		results += "<p>Keine Ergebnisse gefunden<\/p>";
	}
	
	if(gWebSearch.results.length==8){
		var query = gWebSearch_query + " site:"+gWebSearch_host+" inurl:"+gWebSearch_host + "/" + gWebSearch_path;
		results += "<p><a href=\"http://www.google.de/search?hl=de&q="+encodeURI(query) +"&start=8\">Mehr Ergebnisse &gt;&gt;</a><\/p>";
	}
	
	searchresults.innerHTML = results;
}
