// Fader
var containerElements=["fade_logo", "fade_navi", "fade_teaser", "fade_container"];
if (typeof FADE_ALL != "undefined") {
	Event.observe(window, "load", function() {
		containerElements.each( function(el, index) {
			try {
				new Effect.Fade($(el), {from: 0.001, to: 1, duration: 1, delay: 0.5+index*0.5, beforeUpdate:function(){
					$(el).style.visibility="visible";
				}});
			}
			catch(e) {}
		});
		setTimeout(initGalleries, 2500);
	});
}
else {
	Event.observe(window, "load", function() {
		initGalleries();
		containerElements.each( function(el, index) {
			if (el=="fade_container") return;
			try {
				$(el).style.visibility="visible";
			}
			catch(e) {}
		});
		new Effect.Fade($("fade_container"), {from: 0.001, to: 1, duration: 1, beforeSetup:function(){$("fade_container").style.visibility="visible";}});
		
		//$$(".a_navi_main ul li").each(function(element) {
		$$(".navi_hide").each(function(element) {
			//if (element.down("a").href != location.href) return;
			try {
				new Effect.BlindDown(element, {duration: (element.childNodes.length-1)*0.1, beforeSetup:function(){element.style.visibility="visible";}});
			} catch(e) { }
		});
	});
}
///////////////////////////////////////
///////////// Galerien ///////////////

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;
}

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.cur_img = new Image();
		this.first_execution=true;
		this.cur_pic_index=-1;
		
		this.autoplay=true;
		
		this.pics_loaded=0;
		
		this.cur_target='';
		delay=4000; // Wartezeit zwischen den Bildern
		this.fading=false;
		
		this.trans_paras={
			duration: 1, 
			afterFinish: this.preloadNextImage.bindAsEventListener(this),
			beforeSetup: this.setImage.bindAsEventListener(this)
		};
		this.preloadNextImage();
	},
	newPictureList:function(pics) {
		this.pictures=pics.clone();
	},
	setImage: function(a) {
		$(this.cur_target).src=this.next_img.src;
		this.cur_img.src = this.next_img.src;
	},
	queue: function() {
		if (this.first_execution) {
			this.first_execution=false;
			this.fadeIn();
		}
		else {
			clearTimeout(this.tid);
			if (this.autoplay) {
				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=Object.clone(this.trans_paras);
			paras.from=0.001;
			paras.to=1;
			this.next_layer=0;
		}
		else {
			var paras=Object.clone(this.trans_paras);
			paras.from=1;
			paras.to=0.001;
			this.next_layer=1;
		}
		
		new Effect.Fade(top, paras);
		this.fading=true;
	},
	preloadNextImage: function(a) {
		this.fading=false;
		
		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_counter_2=0;
			this._tid2=setInterval(function(obj){if(obj.next_img.complete || ++obj._tid_counter_2>8){ clearInterval(obj._tid2); 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 = this.pictures[ this.cur_pic_index ];
	}
}
function initGalleries() {
	var galls = $$(".galerie");
	if (galls.length==0) return;
	galls.each(function(g){
		var blender = Try.these(function(){
			return new Blender( $w(g.down('.picture_array').innerHTML), g.down(".img_oben"), g.down(".img_unten"));
		}, function(){ return false; });
		
		if (!blender) { 
			return;
		}
	});
}


// JavaScript Document
var tracedepth=0;
var max_trace_depth=0;

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="";
	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)
}

