function debug(msg) {
	if (debug.arguments.length==2) {
		msg="<textarea rows=10 cols=20 style='width:600px;height:360px'>"+msg.escapeHTML()+"</textarea>";
	}
	try {
		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="";
	try {
		for (z in o) {
			try {
				if (o[z]!="" && o[z]!=null)
					if (z=="innerHTML" || z=="outerHTML") out+=z+" = [..HTML-Code..]; <br>\n";
					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>";
	}
	debug(out)
}
var show_uploadform=false;

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 switchOn(obj) {
	if (show_uploadform) return;
	//$('logo').hide();
	var el = $(obj);
	if ( el.style.zIndex != '4' ) {
		el.style.zIndex = '4';
	}
	else {
		el.style.zIndex = '';
	}
}
function switchBack(obj) {
	if (show_uploadform) return;
	//$('logo').show();
	$(obj).style.zIndex = '2';
}

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');
		for (var z=0; z<para_array.length; z++) {
			if (var_names) {
				var zz= Number(z)+1;
				paras[ para_array[z] ] = para_array[zz].indexOf(';') ? unescape(para_array[zz].substr(para_array[zz].indexOf(';')+1)) : unescape(para_array[zz]);
				z++;
			}
			else {
				paras[ arr_var_names[z] ] = para_array[z].indexOf(';') ? unescape(para_array[z].substr(para_array[z].indexOf(';')+1)) : unescape(para_array[z]);
			}
		}
	}
	else {
		var para_string=url.substr(url.indexOf("?")+1);
		var para_array=para_string.split("&");
		for (z in para_array) {
			value=unescape(para_array[z].substr(para_array[z].indexOf("=")+1));
			paras[para_array[z].substr(0,para_array[z].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);
			}
		}
	});
}
