var tracedepth=0;
var max_trace_depth=1;

function debug(msg) {
	if (debug.arguments.length==2) {
		msg="<textarea rows=10 cols=20 style='width:600px;height:360px'>"+msg.escapeHTML()+"</textarea>";
	}
	try {
		//throw new Exception();
		if (typeof w == "undefined" || w.closed) {
			w=window.open('','w','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=640,height=400');
			w.moveTo(screen.availWidth-640, 0);
		}
		w.document.write(msg+"<br>");
	}
	catch (e) {
		try{ $('debug_container').innerHTML += msg+"<br>"; }
		catch(e) {
			var div=document.createElement('div');
			div.id='debug_container';
			div.innerHTML=msg+'<br />';
			document.body.appendChild(div);
		}
	}
}

function trace(o) {
	var out="";
	var nofunctions = (trace.arguments.length>1 && trace.arguments[1]);
	var do_return  = (trace.arguments.length>2 && trace.arguments[2]);
	
	try {
		for (z in o) {
			try {
				if (o[z]!="" && o[z]!=null)
					if (z=="innerHTML" || z=="outerHTML") out+=z+" = [..HTML-Code..]; <br>\n";
					if (typeof o[z] == "function" && nofunctions) {
						//out += z+": [..Function..]<br>\n";
						continue;
					}
					if (typeof o[z] == "object") {
						out += z+" : {<div style='margin-left: 20px'>";
						if (++tracedepth>max_trace_depth || z.indexOf('parent')!=-1 || z.indexOf('own')!=-1) out += " [ object ] ";
						else out += trace(o[z], nofunctions, true);
						tracedepth--;
						out += "</div>}<br>";
					}
					else out+=z+" = "+o[z]+"; <br>";
			}
			catch(e) {
				out+="<i>Fehler in "+z+": "+e+"</i><br />\n";
			}
		}
	}
	catch(ee) {
		out+="<b>Fatal Error in tracing: "+ee+"</b>";
	}
	if (do_return) return out;
	else debug(out)
}
var show_uploadform=false;
// Flash-Detection
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion() {
	var version, axo, e;
	try {
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {}

	if (!version){
		try {
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			version = "WIN 6,0,21,0";
			axo.AllowScriptAccess = "always";
			version = axo.GetVariable("$version");

		} catch (e) {	}
	}
	if (!version) {
		try {
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {}
	}
	if (!version) {
		try {
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) { }
	}
	if (!version) {
		try {
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	if (version && version!=-1) {
		if( /([^ ]*) ([0-9]*),(.*)/.exec(version) ) {
			version=parseInt(RegExp.$2);
		}
	}	
	return version;
}
function GetSwfVer(){
	var flashVer = -1;	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var descArray = navigator.plugins["Shockwave Flash" + swVer2].description.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var flashVer = tempArrayMajor[0];
		}
	}
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}
function getBrowser() {
	if (!window.RegExp) return false;
	var ret={ name:'', version: 0 };
	var AGENTS = ["opera","msie","safari","firefox","netscape","mozilla"];
	var agent = navigator.userAgent.toLowerCase();
	for (var i = 0; i < AGENTS.length; i++) {
		var agentStr = AGENTS[i];
		if (agent.indexOf(agentStr) != -1) {
			var versionExpr = new RegExp(agentStr + "[ \/]?([0-9]+(\.[0-9]+)?)");
			var version = 0;
			if (versionExpr.exec(agent) != null) {
				ret.version = parseFloat(RegExp.$1);
				ret.name=agentStr;
				return ret;
			}
		}
	}
	return ret;
}

function popupOpen() {		// parameter.reihenfolge: URL, WIDTH, HEIGHT
	var w,h,url,aw,ah;
	var a=popupOpen.arguments;
	if (a.length==0) return;
	url=a[0];
	if (a.length>1) {
		w=a[1]; h=a[2];
	}
	else {
		w=500; h=400
	}
	win=window.open(url,'win_front','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+w+',height='+h);
	if (screen) {
		aw=screen.availWidth;
		ah=screen.availHeight;
		win.moveTo(((aw/2)-(w/2)),((ah/2)-(h/2)));
	}
	win.focus();
}

function openPicture() {		// parameter.reihenfolge: URL, WIDTH, HEIGHT
	var w,h,url,aw,ah;
	var a=openPicture.arguments;
	if (a.length==0) return;
	url=a[0];
	if (a.length>1) {
		w=a[1]; h=a[2];
	}
	else {
		w=500; h=400
	}
	win_pic=window.open(url,'win_pic','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+w+',height='+h);
	if (screen) {
		aw=screen.availWidth;
		ah=screen.availHeight;
		win_pic.moveTo(((aw/2)-(w/2)),((ah/2)-(h/2)));
	}
	win_pic.focus();
}


function getURLParameter(url) {
	var paras=new Array();
	var value;
	url=unescape(url);
	if (url.indexOf('?')==-1) {
		var para_string=url.substr(Math.max(url.indexOf(".php")+5, url.indexOf(".php5")+6));
		var var_names= para_string.indexOf('node_id') != -1;
		var para_array=para_string.split("/");
		var arr_var_names=new Array('node_id','lang_id','scope','element_id','cmd','subcmd');
		var str, str2;
		for (var z=0; z<para_array.length; z++) {
			str = String(para_array[z]);
			if (var_names) {
				var zz= Number(z)+1;
				str2 = String(para_array[zz]);
				paras[ str ] = str2.indexOf(';') ? unescape(str2.substr(str2.indexOf(';')+1)) : unescape(str2);
				z++;
			}
			else {
				paras[ arr_var_names[z] ] = str.indexOf(';') ? unescape(str.substr(str.indexOf(';')+1)) : unescape(str);
			}
		}
	}
	else {
		var para_string=url.substr(url.indexOf("?")+1);
		var para_array=para_string.split("&");
		var str;
		for (z in para_array) {
			str = String(para_array[z]);
			value=unescape(str.substr(str.indexOf("=")+1));
			paras[str.substr(0,str.indexOf("="))] = value.indexOf(';') != -1 ? value.substr(value.indexOf(';')+1) : value;
		}
	}
	return paras;
}
////////////////////////////////////////////////////////////////////////////////////
/// Typo Blender, benötigt script.aculo.us, welches wieder auf prototype.js aufbaut

// generelle Fehlerbehandlung in Ajax-Dingen 
/*Ajax.Responders.register({
	onException: function(data, ex){ debug('<strong>Eine Ausnahme ist aufgetreten:</strong>'); trace(ex);},
	onFailure: function(){ alert('Something went wrong...');}
});
*/

var Blender=Class.create();
Blender.prototype = {
	USE_SAFARI_HACK: (getBrowser().name=='safari' && getBrowser().version<500) ,
	initialize: function (pics, el_top, el_bottom) {
		//debug(navigator.userAgent, true);
		//trace(getBrowser());
		this.pictures=pics.clone();
		this.layer=[el_bottom, el_top];
		this.next_layer=1;
		this.first_execution=true;
		this.cookieManager=new CookieManager({});
		var cpi=parseInt(this.cookieManager.getCookie('last_blender_image'));
		if (typeof cpi == 'undefined' || isNaN(cpi)) {
			this.cur_pic_index=-1;
		}
		else this.cur_pic_index=cpi;
		this.cur_target='';
		delay=10000; // Wartezeit zwischen den Bildern
		this.fading=false;
		this.ignore_fade_event=false;
		this.trans_paras={
			duration: 0.8, 
			afterFinish: this.preloadNextImage.bindAsEventListener(this),
			beforeSetup: this.setImage.bindAsEventListener(this)
		};
		//alert("starte mit Bild "+this.cur_pic_index);
		this.preloadNextImage();
	},
	newPictureList:function(pics) {
		this.pictures=pics.clone();
	},
	setImage: function(a) {
		$(this.cur_target).src=this.next_img.src;
	},
	queue: function() {
		if (this.first_execution) {
			this.first_execution=false;
			this.fadeIn();
		}
		else {
			clearTimeout(this.tid);
			this.tid=setTimeout(this.fadeIn.bind(this), delay);
		}
	},
	
	fadeIn: function() {
		this.cur_target = this.layer[this.next_layer];
		var top = this.layer[1];
		
		if (this.next_layer==1) {
			var paras=$H({from: 0.001, to: 1});
			this.next_layer=0;
		}
		else {
			var paras=$H({to: 0.001, from: 1});
			this.next_layer=1;
		}
		new Effect.Fade(top, paras.merge(this.trans_paras));
		this.fading=true;
		this.cookieManager.setCookie('last_blender_image',this.cur_pic_index);
		//$(tg).src=this.next_img.src;
	},
	preloadNextImage: function(a) {
		this.fading=false;
		if (this.ignore_fade_event) {
			this.ignore_fade_event=false;
			return;
		}
		this.cur_pic_index = this.cur_pic_index+1 >= this.pictures.length ? 0 : this.cur_pic_index+1;
		this.next_img = new Image();
		if (this.USE_SAFARI_HACK) {
			this._tid_counter=0;
			this._tid=setInterval(function(obj){if(obj.next_img.complete || ++obj._tid_counter>8){ clearInterval(obj._tid); obj.queue();  } }, 500, this);
		}
		else {
			Element.observe(this.next_img,'load', this.queue.bindAsEventListener(this));
			Element.observe(this.next_img,'error', this.queue.bindAsEventListener(this));
		}
		this.next_img.src = pic_folder+this.pictures[ this.cur_pic_index ];
	},
	forceNextImage: function() {
		if (this.fading) {
			this.ignore_fade_event=true;
			this.cur_pic_index = this.cur_pic_index+1 >= this.pictures.length ? 0 : this.cur_pic_index+1;
		}
		clearTimeout(this.tid);
		Try.these(function(){this.next_img.src="";});
		this.next_img = new Image();
		if (this.USE_SAFARI_HACK) {
			this._tid_counter=0;
			this._tid=setInterval(function(obj){if(obj.next_img.complete){ clearInterval(obj._tid); obj.fadeIn();}
										 else if(++obj._tid_counter>8){clearInterval(obj._tid); obj.forceNextImage();} }, 100, this);
		}
		else {
			Element.observe(this.next_img,'load', this.fadeIn.bindAsEventListener(this));
			Element.observe(this.next_img,'error', this.forceNextImage.bindAsEventListener(this));
		}
		//Element.observe(this.next_img,'error', function(e){trace(e);debug('url:'+this.pictures[ this.cur_pic_index ])});
		this.next_img.src = pic_folder+this.pictures[ this.cur_pic_index ];
	},
	forcePreviousImage: function() {
		if (this.fading) {
			this.ignore_fade_event=true;
			this.cur_pic_index = this.cur_pic_index-1 < 0 ? this.pictures.length-1 : this.cur_pic_index-1;
		}
		else {
			this.cur_pic_index = this.cur_pic_index-2 < 0 ? this.pictures.length+(this.cur_pic_index-2) : this.cur_pic_index-2;
		}
		clearTimeout(this.tid);
		Try.these(function(){this.next_img.src="";});
		this.next_img = new Image();
		if (this.USE_SAFARI_HACK) {
			this._tid_counter=0;
			this._tid=setInterval(function(obj){if(obj.next_img.complete){ clearInterval(obj._tid); obj.fadeIn();}
										 else if(++obj._tid_counter>8){clearInterval(obj._tid); obj.forcePreviousImage();}}, 100, this);
		}
		else {
			Element.observe(this.next_img,'load', this.fadeIn.bindAsEventListener(this));
			Element.observe(this.next_img,'error', this.forcePreviousImage.bindAsEventListener(this));
		}
		this.next_img.src = pic_folder+this.pictures[ this.cur_pic_index ];
	}
}
var blender;
var pic_folder="";
function loadBlenderPictures(url) {
	pic_folder=url.substr(0, url.lastIndexOf('/')+1);
	// umgeht Browser-caching
	new Ajax.Request(url+"?"+Math.random()*1000, {
		method: 'get',
		onSuccess: function (data) {
			var pics=data.responseText.split(',');
			pics.reverse();
			if (typeof blender == "undefined") {
				blender=new Blender(pics, 'bild_oben','bild_unten');
			}
			else {
				blender.newPictureList(pics);
			}
		}
	});
}
////////////////////////////////////////////////////////////
/////  Flash-Navi Ajax ////////////////////////////////////

lightbox_excludes=$w("bottomNavClose nextLink prevLink");

myHistory = new Array();

function getTextContent(data, needle) {
	if (typeof data == 'string') {
		var dat=document.createElement('div');
		dat.innerHTML=data;
		data=dat;
	}
	if (data.id==needle) {
		var output=data.innerHTML;
		// im IE steht hinzugefügter Text manchmal hinter dem _text_content_-SPAN, der wird hier wieder angehängt
		if (data.nextSibling && data.children) {
			if (data.nextSibling.nodeType == 3 && data.nextSibling.nodeValue !='') {
				data.innerHTML+=data.nextSibling.nodeValue;
			}
			
			for (var d=data.nextSibling; d!=null; d=d.nextSibling) {
				if (d.nodeType==3) output+=d.nodeValue;
				else if (d.nodeType==1) {
					if (d.nodeName=='BR') output += '<BR>';
					else {
						output += '<' + d.nodeName + '>' + d.innerHTML + '</' + d.nodeName + '>';
					}
				}
			}
		}
		return output;
	}
	for (var child=data.firstChild; child!=null; child=child.nextSibling) {
		var res=getTextContent(child, needle);
		if (res) return res;
	}
	return false
}


function loadContent(url, target_element) {
	//$('load').show();
	new Ajax.Request(url, {
		parameters:{ using_xml_object: 'true'},
		onSuccess: function(data) {
			myHistory.push(url);
			var target_html=getTextContent(data.responseText, target_element);
			$(target_element).update( target_html );
			//debug(target_html, true);
			$$('a').each(function(element){
				var h=element.href;
				var alt=false;
				h = h.substr(h.indexOf("/", h.indexOf(location.hostname)));
				if (element.readAttribute('rel') == 'lightbox[gal]') alt=true;
				if (element.readAttribute('rel') == 'local') alt=true;
				if (!alt && lightbox_excludes.indexOf(element.readAttribute('id')) !== -1) alt=true;
				if (!alt && (h.substr(0, base_url.length) != base_url || location.protocol != element.protocol)) alt=true;
				if (!alt && element.href.substr(element.href.lastIndexOf('/')+1) == "#") alt=true;
				if (alt) {
					Event.observe( element, 'click', setClickTS);
					return;
				}
				element.href="javascript:void('"+element.href+"')";
				Event.observe( element, 'click', function(event){
					var murl=element.href.substring(17,element.href.length-2);
					loadContent(murl, 'content');
					var paras = getURLParameter(murl);
					Try.these(function(){
						$('SWFNavigation').setNavigation(paras['node_id'], paras['lang_id']);
					});
					setClickTS();
				});
			});
			if (typeof myLightbox != "undefined") {
				myLightbox.updateImageList();
			}
			else {
				myLightbox = new Lightbox();
			}
			try {
				$("iframe_").remove();
			} catch(e) { }
			//$('load').hide();
		},
		onFailure: function() {
			$('load').hide();
			alert('Fehler beim Aufruf der URL \"'+url+'\"');
		}
	});
}

function jumpTo(node_id, lang_id, ssl) {
	//debug(node_id+"-"+lang_id+"-"+ssl+" - "+arguments[3]);
	var arg_3 = (arguments.length==4) ? arguments[3] : false;
	var my_paras = "node_id="+node_id+"&lang_id="+lang_id;
	var protocol = (arg_3 && arg_3!="_reload_") ? "http:" : "https:";
	//var url = (node_id==1 && arg_3!="_reload_") ? protocol+"//"+location.hostname : protocol+"//"+location.hostname+base_url+"?"+my_paras;
	var url = protocol+"//"+location.hostname+base_url+"?"+my_paras;
	
	if (parent.length) {
		Try.these(function(){
			$("iframe_").remove();
		});
	}
	
	if (node_id==0 && lang_id==0 && arg_3) {
		window.open(arg_3);
		return;
	}
	else if (jumpTo.arguments.length==4 || location.protocol=="http:" || node_id==1) {
		//alert("url: "+location.protocol+"//"+location.hostname+base_url+"?"+my_paras);	
		location.href = url;
	}
	else {
		loadContent(url, "content");
	}
}
var lastClick=new Date();
function setClickTS() {
	lastClick = new Date();
}

if (GetSwfVer() >= 9) {
	Event.observe(window,'load',function(e) {
		myHistory.push(location.href);
		$$('a').each(function(element){
			var h=element.href;
			var alt=false;
			h = h.substr(h.indexOf("/", h.indexOf(location.hostname)));
			if (element.readAttribute('rel') == 'lightbox[gal]') alt=true;
			if (!alt && lightbox_excludes.indexOf(element.readAttribute('id')) !== -1) alt=true;
			if (!alt && (h.substr(0, base_url.length) != base_url || location.protocol != element.protocol)) alt=true;
			if (!alt && element.href.substr(element.href.lastIndexOf('/')+1) == "#") alt=true;
			if (alt) {
				Event.observe( element, 'click', setClickTS);
				return;
			}
			element.href="javascript:void('"+element.href+"')";
			Event.observe( element, 'click', function(event){
				var murl=element.href.substring(17,element.href.length-2);
				loadContent(murl, 'content');
				var paras = getURLParameter(murl);
				Try.these(function(){
					$('SWFNavigation').setNavigation(paras['node_id'], paras['lang_id']);
				});
				setClickTS();
			});
		});
	});
}
/*
Event.observe(window, Event.BACK, function(e){
	var curDate = new Date();
	if(curDate - lastClick>1000) {
		trace(location);
	}
}, true);
*/
