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=no,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=no,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 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;
}
////// Scrolling ///////
var scroll_speed=15;
var scroll_tid=null;

function startScrolling(dir,obj) {
	if (dir=='down') 
		var speed= scroll_speed;
	else 
		var speed= -scroll_speed;
	scroll_tid=setInterval('_scrollDiv("'+obj+'",'+speed+')', 50);
}
function _scrollDiv(obj,speed) {
	obj=document.getElementById(obj);
	var y=obj.style.top;
	if (y=='') y=0;
	else {
		y=parseInt(y.substring(0,y.indexOf('p')));
		if (y+speed > 0 && speed>0) {
			y = -speed;
			stopScrolling();
			return;
		}
		if (obj.scrollHeight+(y+speed) < obj.parentNode.clientHeight && speed<0) {
			y -= speed;
			stopScrolling();
			return;
		}
		
	}

	obj.style.top=(y+speed)+'px';
}
function stopScrolling() {
	clearInterval(scroll_tid);
}

Event.observe(window, 'load', function(){
	
	$$("div").each(function(obj){
		if (obj.id.indexOf('_scroller_box')==-1) return;
		if (obj.scrollHeight < obj.parentNode.clientHeight) {
			$(obj.id+"_up").hide();
			$(obj.id+"_down").hide();
		}
	});
	
	/*if ($('menu_scroller_box').scrollHeight < $('menu_scroller_box').parentNode.clientHeight) {
		$("img_scroll_up").hide();
		$("img_scroll_down").hide();
	}
	*/
});


function debug(msg) {
	if (typeof w == "undefined") {
		w=window.open('','w','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=640,height=400');
	}
	w.document.write(msg+"<br>");
}
function trace(o) {
	var out="";
	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+= z+" = ERROR: "+e+"; <br>";
		}
	}
	debug(out)
}
/////////////////////////////////////////////////////////////////////////////////////
/// AJAX, übernommen aus xkrilyk
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) {
			var target_html=getTextContent(data.responseText, target_element);
			$(target_element).update( target_html );
			$$('#'+target_element+' a').each(function(element){
				if (element.readAttribute('rel') == 'edit') return;
				if (element.readAttribute('rel') == 'maps') return;
				//if (element.readAttribute('rel') == 'lightbox[gal]') return;
				if (!element.href.match("(^http:).*(([^#]$)|([^#.]$))")) return;
				if (element.readAttribute('target') == '_blank') return;
				element.href="javascript:void('"+element.href+"')";
				Event.observe( element, 'click', function(event){
					loadContent(element.href.substring(17,element.href.length-2), target_element);
				});
			});
			/*$$("div").each(function(obj){
				if (obj.id.indexOf('_scroller_box')==-1) return;
				if (obj.scrollHeight < obj.parentNode.clientHeight) {
					$(obj.id+"_up").hide();
					$(obj.id+"_down").hide();
				}
			});
			if (typeof myLightbox != 'undefined') {
				myLightbox.updateImageList();
			}
			*/
			//tourGuide.hoverTourButton();
			$('load').hide();
		},
		onFailure: function() {
			$('load').hide();
			alert('Fehler beim Aufruf der URL \"'+url+'\"');
		}
	});
}

Event.observe(window,'load',function(e) {
	$$('a').each(function(element){
		// lightbox // if (element.readAttribute('rel') == 'edit') return;
		if (!element.href.match("(^http:).*(([^#]$)|([^#.]$))")) return;
		if (element.readAttribute('target') == '_blank') return;

		element.href="javascript:void('"+element.href+"')";
		Event.observe( element, 'click', function(event){
			loadContent(element.href.substring(17,element.href.length-2),'a_all');
		});
	});
});

/////////////////////////////////////////////////////////////////////////////////////
/// Galerie.. mit allem was dazugehört..

var home_fading=false;

function focusGallery() {
	if (home_fading) return;
	home_fading=true;
	
	new Effect.Appear($('back_nav'), { duration: 0.5});
	new Effect.Fade($('a_all'), { duration: 0.5, afterFinish: function(){ home_fading=false;}});
	
	blender.showTour(tourGuide.current_tour); 
}
function blurGallery() {
	if (home_fading) return;
	home_fading=true;
	blender.leaveSingleTour();
	
	new Effect.Appear($('a_all'), { duration: 0.5});
	new Effect.Fade($('back_nav'), {duration: 0.5, afterFinish: function(){ home_fading=false;}});
}
////////////////////////////////////////////////////////////////////////////////////
/// Bilder-Blender, benötigt script.aculo.us, welches wiederum auf prototype.js aufbaut

var Blender=Class.create();
Blender.prototype = {
	USE_SAFARI_HACK: (getBrowser().name=='safari' && getBrowser().version<500) ,
	delay: 10000, // Wartezeit zwischen den Bildern
	single_tour: false,
	initialize: function (tour_guide, el_top, el_bottom) {
		//debug(navigator.userAgent, true);
		//trace(getBrowser());
		this.tour_guide=tour_guide;
		this.layer=[el_bottom, el_top];
		this.next_layer=1;
		this.cur_target='';
		this.fading=false;
		this.ignore_fade_event=false;
		this.trans_paras={
			duration: 0.5, 
			afterFinish: this.queue.bindAsEventListener(this),
			beforeSetup: this.setImage.bindAsEventListener(this)
		};
		this.preloadNextImage();
	},
	
	setImage: function(a) {
		$(this.cur_target).src=this.next_img.src;
	},
	queue: function() {
		//clearTimeout(this.tid);
		//if (!this.single_tour) {
		//	this.tid=setTimeout(this.preloadNextImage.bind(this), 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;
	},
	preloadNextImage: function(a) {
		this.fading=false;
		/*if (this.ignore_fade_event) {
			this.ignore_fade_event=false;
			return;
		} */
		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.fadeIn.bindAsEventListener(this));
			Element.observe(this.next_img,'error', this.fadeIn.bindAsEventListener(this));
		}
		this.tour_guide.getNextPic();
		this.next_img.src = this.tour_guide.getCurrentPic();
	},
	forceNextImage: function() {
		if (!this.tour_guide.getNextPic()) {
			blurGallery();
		}
		
		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));
		}
		this.next_img.src = this.tour_guide.getCurrentPic();
	},
	forcePreviousImage: function() {
		/*if (this.fading) {
			this.ignore_fade_event=true;
		}*/
		if (!this.tour_guide.getPreviousPic()) {
			blurGallery();
			return;
		}
		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 = 	this.tour_guide.getCurrentPic();
	},
	showTour: function(tour_index) {
		var force_next= (this.tour_guide.current_tour != tour_index ); //|| (this.tour_guide.current_tour == tour_index && this.tour_guide.current_pic != 0));
		if (this.tour_guide.current_tour != tour_index) {
			this.tour_guide.setTour(tour_index);
		}
		else {
			this.tour_guide.single_tour_mode=true;
		}
		if (force_next) this.forceNextImage();

		this.single_tour=true;
	},
	leaveSingleTour: function() {
		if (!this.single_tour) return;
		
		this.tour_guide.leaveSingleTour();
		this.single_tour=false;
		this.queue();
	}
	
}

var TourGuide=Class.create();
TourGuide.prototype={
	single_tour_mode: false,
	initialize: function() {
		this.touren=new Array();
		this.buttons=new Array();
		this.current_tour=0;
		this.current_pic=0;
		this.cookieManager=new CookieManager({shelfLife:0.000579});
		var ct=parseInt(this.cookieManager.getCookie('last_tour_index'));
		if (typeof ct != 'undefined' && !isNaN(ct)) {
		//	this.current_tour=ct;
		}
		var cp=parseInt(this.cookieManager.getCookie('last_pic_index'));
		if (typeof cp != 'undefined' && !isNaN(cp)) {
		//	this.current_pic=cp;
		}
	},
	addTour: function(btn, pics) {
		this.touren[this.touren.length] = pics.clone();
		this.buttons[this.buttons.length] = btn;
	},
	hoverTourButton: function() {
		var btn=$(this.buttons[this.current_tour]);
		btn.down("img.pic_link").style.display='none';
		btn.down("img.pic_link_h").style.display='block';
	},
	dehoverTourButton: function() {
		var btn=$(this.buttons[this.current_tour]);
		btn.down("img.pic_link").style.display='block';
		btn.down("img.pic_link_h").style.display='none';
	},
	getNextPic: function() {
		var ret=true;
		this.saveCookie();
		var tour=this.touren[this.current_tour];
		
		if ( typeof tour[++this.current_pic] == "undefined") {
			if (!this.single_tour_mode) {
				this.nextTour();
				tour=this.touren[this.current_tour];
			}
			else {
				this.current_pic=0;
				ret=false;
			}
		}
		if ( typeof tour[this.current_pic] == "undefined") {
			return false;
		}
		//return tour[this.current_pic];
		return ret;
	},
	saveCookie: function() {
		this.cookieManager.setCookie('last_tour_index',this.current_tour);
		this.cookieManager.setCookie('last_pic_index',this.current_pic);
	},
	getPreviousPic: function() {
		var ret=true;
		this.saveCookie();
		var tour=this.touren[this.current_tour];
		if ( --this.current_pic < 0) {
			if (!this.single_tour_mode) {
				this.previousTour();
				tour=this.touren[this.current_tour];
				this.current_pic = tour.length - 1;
			}
			else {
				this.current_pic=0;
				ret = false;
			}
		}
		if ( typeof tour[this.current_pic] == "undefined") {
			return null;
		}
		//return tour[this.current_pic];
		return ret;
	},
	getCurrentPic: function() {
		return this.touren[ this.current_tour ][ this.current_pic ];
	},
	nextTour: function() {
		//this.dehoverTourButton();
		if (typeof this.touren[++this.current_tour] == "undefined") {
			this.current_tour=0;
		}
		this.current_pic=0;
		//this.hoverTourButton();
	},
	previousTour: function() {
		//this.dehoverTourButton();
		if (--this.current_tour < 0) {
			this.current_tour=this.touren.length-1;
		}
		this.current_pic=0;
		//this.hoverTourButton();
	},
	setTour: function(tour_index) {
		if (tour_index<this.touren.length) {
			//this.dehoverTourButton();
			this.current_tour=tour_index;
			this.current_pic=-1;
			this.single_tour_mode=true;
			//this.hoverTourButton();
			this.saveCookie();
		}
	},
	setRandomTour: function() {
		this.current_tour=Math.floor(Math.random()*(this.touren.length));
		this.current_pic=-1;
		//this.hoverTourButton();
		this.saveCookie();
	},
	leaveSingleTour: function() {
		this.single_tour_mode=false;
	}
}

tourGuide=new TourGuide();
var blender;

Event.observe(window, 'load', function(){
	//tourGuide.hoverTourButton();
	tourGuide.setRandomTour();
	blender=new Blender(tourGuide, 'bild_oben','bild_unten');
});
