// PopUps
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();
}
// 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;
}


// Menü
var rect_height=0;
var cur_effect;
var cur_direction;
function initMenu() {
	rect_height = $("sub_kasten").getHeight();
	
	//cur_direction = "open";
	//closeMenu();
	cur_direction = "close";
	$("sub_kasten").setStyle({top: "-"+rect_height+"px"});
	
	Event.observe($("menu"), "mouseover", openMenu);
	Event.observe($("menu"), "mouseout", closeMenu);
}
function openMenu() {
	if (cur_direction=="open") return;
	
	if (cur_direction=="close" && Try.these(function() {return cur_effect.state != "finished";}, function(){return false;})) {
		cur_effect.cancel();
	}
	//debug(Position.cumulativeOffset("sub_kasten"));
	cur_effect = new Effect.Move("sub_kasten", {
		x:0, y:0, 
		duration: 0.5, 
		mode:"absolute",
		transition: Effect.Transitions.sinoidal
	});
	cur_direction="open";
}
function closeMenu(ev) {
	if (cur_direction=="close") return;
	if (Prototype.Browser.IE) {
		Position.prepare();
		if (Position.withinIncludingScrolloffsets($("menu"), Event.pointerX(ev), Event.pointerY(ev)))	return;
	}
	if (cur_direction=="open" && Try.these(function() {return cur_effect.state != "finished";}, function(){return false;})) {
		cur_effect.cancel();
	}
	
	cur_effect = new Effect.Move("sub_kasten", {
		x:0, y:-rect_height, 
		duration: 0.5, 
		mode:"absolute",
		transition: Effect.Transitions.sinoidal
	});
	cur_direction="close";
}

Event.observe(document, "click", function(ev){
	var e=Event.element(ev);
	if (e.nodeName.toLowerCase()=="a" && e.readAttribute('href').indexOf("javascript")==-1 && e.readAttribute('href')!= location.href && cur_direction=="open") {
		closeMenu();
		Event.stopObserving($("menu"), "mouseover", openMenu);
	}
});


// wird jetzt inline aufgerufen für unmittelbares Verstecken
//Event.observe(window, 'load', initMenu);

/////////////////////////
////// Scrolling ///////
/*
var scroll_speed=13;
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=$(obj);
	var y=obj.getStyle("top");
	if (y==null || y=='') y=0;
	else {
		y=parseInt(y.substring(0,y.indexOf('p')));
		if (y+speed > 0 && speed>0) {
			y = -speed;
			stopScrolling();
		}
		if (obj.scrollHeight+(y) < obj.parentNode.clientHeight && speed<0) {
			y -= speed;
			stopScrolling();
		}
	}

	obj.setStyle({top: (y+speed)+'px'});
}
function stopScrolling() {
	clearInterval(scroll_tid);
}

Event.observe(window, 'load', function(){
	try {
		var o=$('a_box_scroller_box');
		if (o.scrollHeight < o.parentNode.clientHeight) {
			$$(".a_box_navi").invoke('hide');
	
		}
	}
	catch(e) {};
});

Event.observe(window, 'load', function(){
	try {
		var o=$('c_cont_scroller_box');
		if (o.scrollHeight < o.parentNode.clientHeight) {
			$$(".c_cont_navi").invoke('hide');
		}
	}
	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();
var gallery_counter=0;

Blender.prototype = {
	USE_SAFARI_HACK: (getBrowser().name=='safari' && getBrowser().version<500) ,
	initialize: function (pics, el_top, el_bottom) {
		//debug(navigator.userAgent, true);
		//trace(getBrowser());
		//trace(pics, true);
		this.showColorPic=false;
		this.pictures=pics.clone();
		this.layer=[el_bottom, el_top];
		this.next_layer=1;
		this.cur_color_img = new Image();
		this.cur_bw_img = new Image();
		this.first_execution=true;
		this.cur_pic_index=-1;
		this.num_display_element=null;
		this.autoplay=true;
		this.autoplay_display=null;
		this.loading_img=null;
		this.effect_instance=null;
		this.pics_loaded=0;
		this.forced_change=true;
		this.std_blend_duration=4;
		this.tid_ts=new Date();
		this.cur_target=false;
		delay=2000; // Wartezeit zwischen den Bildern
		this.fading=false;
		this.ignore_fade_event=false;
		this.trans_paras={
			duration: this.std_blend_duration,
			beforeSetup: this.setImage.bindAsEventListener(this)
		};
		if (this.pictures.length>1) {
			this.trans_paras.afterFinish=this.preloadNextImage.bindAsEventListener(this);
		}
		//alert("starte mit Bild "+this.cur_pic_index);
		this.tid=setTimeout(this.preloadNextImage.bind(this), gallery_counter*4000);
		//this.preloadNextImage();
		gallery_counter++;
	},
	newPictureList:function(pics) {
		this.pictures=pics.clone();
	},
	setImage: function(a) {
		if (!this.cur_target) return;
		$(this.cur_target).src= this.showColorPic ? this.next_img.src : this.next_bw_img.src;
		this.cur_color_img.src = this.next_img.src;
		this.cur_bw_img.src = this.next_bw_img.src;
		this.updateNumberDisplay();
	},
	queue: function() {
		//if (++this.pics_loaded < 2) return;
		//this.pics_loaded=0;
		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);
				this.tid_ts = new Date();
			}
		}
		if (this.loading_img) this.loading_img.hide();
	},
	
	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;
		}
		this.effect_instance = new Effect.Fade(top, paras.merge(this.trans_paras));
		this.fading=true;
		this.trans_paras.duration = this.std_blend_duration;
		
		//$(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();
		this.next_bw_img = new Image();
		if (this.USE_SAFARI_HACK) {
			this._tid_counter=0;
			this._tid_counter_2=0;
			//this._tid=setInterval(function(obj){if(obj.next_img.complete || ++obj._tid_counter>8){ clearInterval(obj._tid); obj.queue();  } }, 500, this);
			this._tid2=setInterval(function(obj){if(obj.next_bw_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));
			Element.observe(this.next_bw_img,'load', this.queue.bindAsEventListener(this));
			Element.observe(this.next_bw_img,'error', this.queue.bindAsEventListener(this));
		}
		this.next_img.src = this.pictures[ this.cur_pic_index ];
		this.next_bw_img.src = (location.host.indexOf("plus.it")==-1 ? "lib" : "") + "/img_bw.php?i="+this.pictures[ this.cur_pic_index ];
	},
	forceNextImage: function(event) {
		/*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;
		}
		*/
		if (this.fading) {
			this.accelerateFading();
			return;
		}
		clearTimeout(this.tid);
		
		this.trans_paras.duration=0.7;
		if (this.loading_img) this.loading_img.show();
		
		Try.these(function(){this.next_img.src=""; this.next_bw_img.src="";});
		this.next_img = new Image();
		this.next_bw_img = new Image();
		if (this.USE_SAFARI_HACK) {
			this._tid_counter=0;
			this._tid=setInterval(function(obj){if(obj.next_bw_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_bw_img,'load', this.fadeIn.bindAsEventListener(this));
			Element.observe(this.next_bw_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 = this.pictures[ this.cur_pic_index ];
		this.next_bw_img.src = (location.host.indexOf("plus.it")==-1 ? "lib" : "") + "/img_bw.php?i="+this.pictures[ this.cur_pic_index ];
		Event.element(event).blur();
	},
	forcePreviousImage: function(event) {
		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);
		this.trans_paras.duration=0.7;
		if (this.loading_img) this.loading_img.show();
		
		Try.these(function(){this.next_img.src=""; this.next_bw_img.src="";});
		this.next_img = new Image();
		this.next_bw_img = new Image();
		if (this.USE_SAFARI_HACK) {
			this._tid_counter=0;
			this._tid=setInterval(function(obj){if(obj.next_bw_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_bw_img,'load', this.fadeIn.bindAsEventListener(this));
			Element.observe(this.next_bw_img,'error', this.forcePreviousImage.bindAsEventListener(this));
		}
		this.next_img.src = this.pictures[ this.cur_pic_index ];
		this.next_bw_img.src = (location.host.indexOf("plus.it")==-1 ? "lib" : "") + "/img_bw.php?i="+this.pictures[ this.cur_pic_index ];
		Event.element(event).blur();
	},
	setNumberDisplay: function(e) {
		this.num_display_element = $(e);
	},
	updateNumberDisplay: function() {
		if (this.num_display_element!=null) {
			this.num_display_element.update( this.cur_pic_index+1 );
		}
	},
	switchAutoplay: function(event) {
		if (this.autoplay) {
			this.autoplay=false;
			clearTimeout(this.tid);
			if (this.autoplay_display) {
				this.autoplay_display.addClassName("autoplay").blur();
			}
		}
		else {
			this.autoplay=true;
			this.queue();
			if (this.autoplay_display) {
				this.autoplay_display.removeClassName("autoplay").blur();
			}
		}
	},
	setAutoplayDisplay: function(e) {
		this.autoplay_display=e;
	},
	accelerateFading: function() {
		var pos = this.effect_instance.position;
		var to = this.effect_instance.fromToDelta > 0 ? 1 : 0.001;
		var element=this.effect_instance.element;
		this.effect_instance.cancel();
		var paras =  {
			from: pos,	to: to,	duration: 0.25, 
			afterFinish: this.preloadNextImage.bindAsEventListener(this)
		}
		this.effect_instance = new Effect.Fade( element, paras);
	},
	showColorImage: function() {
		if (!this.cur_target) return;
		$(this.cur_target).src=this.cur_color_img.src;
		this.showColorPic=true;
		if (this.fading) {
			this.accelerateFading();
		}
		
		/*if (this.autoplay) {
			this.time_left = delay-((new Date())-this.tid_ts);
			clearTimeout(this.tid);
		}
		*/
	},
	showBWImage: function() {
		if (!this.cur_target) return;
		$(this.cur_target).src=this.cur_bw_img.src;
		this.showColorPic=false;
		/*if (this.autoplay && typeof this.time_left != "undefined") {
			this.tid=setTimeout(this.fadeIn.bind(this), this.time_left);
		}
		*/
	}
}

function initGalleries() {
	var galls = $$(".galerie");
	//trace(galls, true);
	if (galls.length==0) return;
	galls.each(function(g){
		var blender = Try.these(function(){
			var picArray = $w(g.down('.picture_array').innerHTML);
			return new Blender( picArray, g.down(".img_oben"), g.down(".img_unten"));
		}, function(){ return false; });
		
		if (!blender) { 
			return;
		}
		Element.observe( g.down(".rahmen"), "mouseover", blender.showColorImage.bindAsEventListener(blender) );
		Element.observe( g.down(".rahmen"), "mouseout", blender.showBWImage.bindAsEventListener(blender) );
		if (blender.pictures.length>1) {
			Element.observe( g.down("a.next"), "click", blender.forceNextImage.bindAsEventListener(blender) );	
			Element.observe( g.down("a.prev"), "click", blender.forcePreviousImage.bindAsEventListener(blender) );
			blender.setNumberDisplay( Try.these(function(){ return g.down(".num_display"); }, function() {return null;}));
			blender.setAutoplayDisplay( Try.these(
				function(){ 
					Element.observe( g.down(".autostop"), "click", blender.switchAutoplay.bindAsEventListener(blender) );
					return g.down(".autostop");
				}, 
				function() {return null;}
			));
			blender.loading_img = $(g.down("img.loading_animation"));
		}
	});
}
Event.observe(window, "load", initGalleries);

////////////////////////////////////////
/// Textfelder-Focus //////////////////

var SmartTextInput=Class.create();
SmartTextInput.prototype={
	element: null,
	labelname: "",
	textarea: false,
	initialize: function(e) {
		this.element=e;
		this.textarea = this.element.tagName.toLowerCase() == 'textarea';
		this.labelname=$F(this.element);
		Event.observe(this.element, "focus", this.onFocus.bindAsEventListener(this));
		Event.observe(this.element, "blur", this.onBlur.bindAsEventListener(this));
	},
	onFocus: function() {
		if ($F(this.element)==this.labelname) {
			this.element.clear();
		}
	},
	onBlur: function() {
		if ($F(this.element)==this.labelname || $F(this.element).blank()) {
			this.element.value = this.labelname;
		}
	}
};

Event.observe(window, "load", function(){
	$$("form").each(function(form){
		form.getInputs("text").each(function(f_element){
			new SmartTextInput($(f_element));
		});
	});
	$$("textarea").each(function(f_element){
		new SmartTextInput($(f_element));
	});
});

