Object.extend(Prototype.Browser, {
    IE6:     Prototype.Browser.IE && (typeof window.XMLHttpRequest == "undefined"),
    IE7:     Prototype.Browser.IE && (typeof window.XMLHttpRequest == "object")
});

Shadowbox.init( {
	language :"de-DE",
	players : [ "img", "html", "iframe", "qt", "wmp", "swf", "flv" ]
});

 

document.observe("dom:loaded", function() {  
	// IE6 Workarrounds
	if(Prototype.Browser.IE6){
		$$("#map .item img").each(function(item){
			src = item.src;
			src = src.replace(/\.png/, ".gif");
			item.src = src;
		});
	}
	mapEffect = new MapEffect();
	quicklinks = new Quicklinks();
	content = new Content();

	$('multiLang').observe("change",function() {
		 
		window.location.href= 'http://' + window.location.hostname + '/wEu/'+$F('multiLang');// window.location.pathname.replace(/\/[a-z][a-z]\//,'/'+$F('multiLang')+'/');
		
	});
		 
	$('selectLocale').observe("change",function() {
			if ($F('selectLocale') =='_') return false;
			$('selectLocaleForm').target = '_blank'; 
			$('selectLocaleForm').action = $F('selectLocale');
			$('selectLocaleForm').submit();
			$('selectLocale').value='_';
			
	});
	$$('a[rel="ajaxContent"]').each(
			function(item) {
				item.observe("click", function(event) {
					eval("if(Object.isFunction(" + item.readAttribute("rel")
							+ ")){ new " + item.readAttribute("rel")
							+ "(item, event) }");
					item.blur();
					Event.stop(event);
				});
			});

	$$('.news a.detail').each( function(item) {
		item.observe("click", function(event) {
			if (!item.up().select(".teaser")[0].visible()) {
				item.up().addClassName("on");
				item.up().select(".teaser")[0].appear( {
					duration :0.5
				});
			} else {
				item.up().removeClassName("on");
				item.up().select(".teaser")[0].fade( {
					duration :0.5
				});
			}
			item.blur();
			Event.stop(event);
		});
	});
});

var ajaxContent = {}
ajaxContent = Class.create();
ajaxContent.prototype = {
	initialize : function(element, event) {
		this.element = element;
		this.event = event;
		this.itemLoader = new Element("div", {
			"id" :"loader"
		});
		this.options = Object.extend( {
			loader :true,
			relAttribute :element.readAttribute("rel"),
			revOptions :Object.extend( {
				insertion :"replace",
				cutTarget :true
			}, this.parseRevOptions(this.element.readAttribute("rev"))),
			none :true
		}, arguments[2] || {});

		this.update();
	},

	showLoader : function() {
		if (this.options.revOptions.insertion == "replace") {
			$(this.options.revOptions.destination).update(this.itemLoader);
		} else {
			$(this.options.revOptions.destination).insert( {"top" :this.itemLoader});
		}
	},

	hideLoader : function() {
		this.itemLoader.remove();
	},

	update : function() {
		this.stopEvent();

		if (this.options.revOptions.destination
				&& $(this.options.revOptions.destination)) {
			url = this.element.readAttribute("href");
			new Ajax.Request(
					url,
					{ evalJS: false,
						method :'get',
						onCreate : function(tranport) {
							$(this.options.revOptions.destination).childElements().each(Element.hide);
							this.showLoader();
						}.bind(this),
						onSuccess : function(transport) {
							this.hideLoader();
							text = transport.responseText.stripScripts();
						
							if (this.options.revOptions.cutTarget) {
								tmp = new Element('div');
								tmp.update(text);	
								instance = this; 
								tmp.select("div").each(function(item) {  
									if (item.id == instance.options.revOptions.destination) text= item.innerHTML;
								}
								)
								 
							}
							
							if (this.options.revOptions.insertion == "replace") {

								$(this.options.revOptions.destination).update(text);
							} else {
								$(this.options.revOptions.destination)
										.insert(
												text,
												{
													position :this.options.revOptions.insertion
												});
							}
							 
							if (typeof content == "object") {
								content.setup();
							}
              et_eC_Wrapper( etCode, url, '', 0, '', '', 0, 0, 0, 0, 0, 0, 0);	
						}.bind(this)
					});
		}
	},

	stopEvent : function() {
		Event.stop(this.event);
	},

	parseRevOptions : function(str) {
		if (!str)
			return {};
		var quotes = [], match, rex = /`([^`]*?)`/g;
		while ((match = rex.exec(str)))
			quotes.push(match[1]);
		if (quotes.length)
			str = str.replace(rex, '``');
		str = str.replace(/\s*[:=]\s*/g, ':');
		str = str.replace(/\s*[;&]\s*/g, ' ');
		str = str.replace(/^\s+|\s+$/g, '');
		var pairs = {}, aVars = str.split(' '), i = aVars.length;
		while (i--) {
			var aThisVar = aVars[i].split(':'), name = aThisVar[0], value = aThisVar[1];
			if (typeof value === 'string') {
				if (!isNaN(value))
					value = +value;
				else if (value === 'true')
					value = true;
				else if (value === 'false')
					value = false;
			}
			if (value === '``')
				value = quotes.pop() || '';
			pairs[name] = value;
		}
		return pairs;
	},

	none : function() {
	}
}

var Quicklinks = {}
Quicklinks = Class.create();
Quicklinks.prototype = {
	initialize : function() {
		this.options = Object.extend( {
			itemQuicklink :"#rightContent .item",
			none :true
		}, arguments[0] || {});

		this.showEvent = this.show.bindAsEventListener(this);
		this.hideEvent = this.hide.bindAsEventListener(this);
		$$(this.options.itemQuicklink).each( function(item) {
			Event.observe(item, "mouseover", this.showEvent);
			Event.observe(item, "mouseout", this.hideEvent);
			if (item.select(".active")[0])
				item.select(".active")[0].hide();
		}.bind(this));
	},

	show : function(e) {
		eItem = Event.element(e);
		if (!eItem.hasClassName("item"))
			eItem = Event.element(e).up(".item");

		if (eItem.select(".passive")[0])
			eItem.select(".passive")[0].hide();
		if (eItem.select(".active")[0])
			eItem.select(".active")[0].show();
		eItem.addClassName("on");
	},

	hide : function(e) {
		eItem = Event.element(e);
		if (!eItem.hasClassName("item"))
			eItem = Event.element(e).up(".item");

		if (eItem.select(".passive")[0])
			eItem.select(".passive")[0].show();
		if (eItem.select(".active")[0])
			eItem.select(".active")[0].hide();
		eItem.removeClassName("on");
	},

	none : function() {
	}
}

var Teaser = {}
Teaser = Class.create();
Teaser.prototype = {
	initialize : function() {
		this.items = [];
		this.options = Object.extend( {
			itemTeaser :"#teaser .teaser",
			itemImage :"#mappoints .item",
			classSlideshow: "slideshow"
				
		}, arguments[0] || {});

		$$(this.options.itemTeaser).each( function(item, i) {
			name = item.readAttribute("id");
			name = name.replace(/_teaser/, "");  
			this.items[i] = {
				name :name,
				text :item,
				image :$(name + "_image") ? $(name + "_image") : false
			};
		}.bind(this));

		this.shown = this.items.first().name; 
	},

	next : function() {
		key = null;
		this.items.each( function(item, i) {
			if (item.name == this.shown) {
				if (this.items[i + 1]) {
					key = this.items[i + 1].name;
					cur = this.items[i+1].text;
				} else {
					key = this.items.first().name;
					cur = this.items.first().text;
				}
				if(! cur.hasClassName(this.options.classSlideshow)) this.shown = key;
				return;
			}
		}.bind(this));
		return key;
	},

	getItem : function(key) {
		ret = null;
		this.items.each( function(item, i) {
			if (item.name == key) {
				ret = item;
			}
		}.bind(this));
		return ret;
	},

	rotate : function() {
		if (arguments[0] == "stop") {
			if (this.pe)
				this.pe.stop();
			return;
		}
		this.pe = new PeriodicalExecuter( function(pe) {
			if (this.getItem(this.next()).text.hasClassName("startTeaser") || ! this.getItem(this.next()).image) {
				if (!$("map").visible()) {
					new Effect.Appear($("map"), {
						beforeStart : function(effect) {
							effect.element.setOpacity(0.0);
							effect.element.show();
							new Effect.Opacity('ToolTip',{ from: 0.0, to: 0.4,duration: 2});
						}
					});
					
				}
				if ($("backLink").visible())
					$("backLink").fade();
			 
				$("mappoints").setStyle( {
					zIndex :30
				});
			} else {
				if (!$("backLink").visible())
					$("backLink").appear();
					
				$("mappoints").setStyle( {
					zIndex :40
				});
				if ($("map").visible())
					new Effect.Fade($("map"), {
						afterFinish : function(effect) {
							$("ToolTip").fade();
							effect.element.hide();
							
						}
					});
			}
			this.show(this.next(), "rotate");
		}.bind(this), 13);
	},
    _mapItemIdToKey : function(id) {
		ret = 0;
		this.items.each( function(item,index) {   
			if (id == item.name) ret =  index;
		});
		return ret;
	},
	show : function(key) {
		if (key == this.shown) {
			return;
		}

		temp = this.shown + "_teaser";
		this.items.each( function(item) {
			if (key == item.name) {
				this.shown = key;

				if (this.showEffect)
					this.showEffect.cancel();
				this.showEffect = new Effect.Appear(item.text, {
					beforeStart : function(effect) {
						effect.element.setOpacity(0.5);
						effect.element.show();
						effect.element.setStyle( {
							zIndex :20
						});
					},
					afterFinish : function(effect) {
						effect.element.show();
					},
					from :0.5,
					to :1.0,
					duration :2.0
				});

				if (this.showImageEffect)
					this.showImageEffect.cancel();
				if (item.image)
					this.showImageEffect = new Effect.Appear(item.image, {
						beforeStart : function(effect) {
							$("mappoints").setStyle( {
								zIndex :40
							});
							effect.element.setOpacity(0.0);
							effect.element.show();
							effect.element.setStyle( {
								zIndex :20
							});
						},
						from :0.0,
						to :1.0,
						duration :2.0
					});
			}
		}.bind(this));
		this.hide(temp);
	},

	hide : function() {
		key = arguments[0];
		effect = arguments[1];
		this.items.each( function(item) {
			if (!key || key == item.text.readAttribute("id")) {
				if (!key && this.shown == item.name) {
					return;
				}
				if (effect == "noEffect") {
					item.text.hide();
				} else {
					new Effect.Appear(item.text, {
						beforeStart : function(effect) {
							effect.element.setStyle( {
								zIndex :10
							});
						},
						afterFinish : function(effect) {
							effect.element.hide();
						},
						from :1.0,
						to :0.0,
						duration :2.0
					});
					if (item.image && item.image.visible()) {
						new Effect.Appear(item.image, {
							from :1.0,
							to :0.0,
							duration :2.0
						});
					}
				}
			}

		}.bind(this));
	}
}

var MapEffect = {};
MapEffect = Class.create();
MapEffect.prototype = {
	initialize : function() {
		this.options = Object.extend( {
			itemTeaser :"#teaser .teaser",
			itemMpAnim :"#map .item span",
			itemMp :"#map .item",
			none :true
		}, arguments[0] || {});

		this.showEvent = this.show.bindAsEventListener(this);
		this.hideEvent = this.hide.bindAsEventListener(this);
		this.clickEvent = this.click.bindAsEventListener(this);
		this.stopPEEvent = this.stopPE.bindAsEventListener(this);
		$$("#map .item .location").each( function(item) {

			if (!item.up().hasClassName('edit')) {
				Event.observe(item, "mouseover", this.showEvent);
				Event.observe(item, "mouseout", this.hideEvent);
				Event.observe(item, "click", this.clickEvent);
			} else {
				this.showEdit(item);
			}
		}.bind(this));

		this.backEvent = this.back.bindAsEventListener(this)
		$$("#backLink a, .refBackLink a").each( function(item) {
			Event.observe(item, "click", this.backEvent);
		}.bind(this));

		$$(".teaser .link a").each( function(item) {
			Event.observe(item, "click", this.stopPEEvent);
		}.bind(this));

		this.teaser = new Teaser();
		this.teaser.rotate();
		this.hideInitial();
	},

	stopPE : function(e) {
		this.teaser.rotate("stop");
	},

	show : function(e) {
		if(! $(Event.element(e).up().select("span")[0])) return;
		Event.element(e).up().select("span")[0].show();
		Event.element(e).up().select("span img")[0].setOpacity(0.0);
		new Effect.Appear(Event.element(e).up().select("span img")[0], {
			duration :6.0,
			from :0.0,
			to :0.5,
			transition :Effect.Transitions.pulse
		});
		this.mpPe = new PeriodicalExecuter( function(pe) {
			new Effect.Appear(Event.element(e).up().select("span img")[0], {
				duration :6.0,
				from :0.0,
				to :0.5,
				transition :Effect.Transitions.pulse
			});
		}.bind(this), 6);
		 
	},

	showEdit : function(el) {
		
		el.up().select("span img")[0].setOpacity(0.0);
		curElement = el;
		new PeriodicalExecuter( function(peEdit) {
			curElement.up().select("span")[0].show();
			new Effect.Appear(curElement.up().select("span img")[0], {
				duration :1.7,
				from :0.0,
				to :0.5,
				transition :Effect.Transitions.pulse
			});
		}, 2);
		curElement.setStyle( {
			cursor :'move'
		});
	 
	},

	makeEditPoint : function(el) {
		if($(el)){
		el.addClassName('edit');
		img = el.select('img:not(.h-inner)');
		this.showEdit(img[0]);
		dragInstance = new Draggable(el, {
			onStart : function(el) {
				dragInstance.options.revert = true;
			}
		});
		}
		$$("#map .item .location").each( function(item) {
			item.stopObserving('click', this.clickEvent);
		});
		mapEffect.teaser.rotate("stop");

	},

	back : function(e) {
		this.teaser.rotate("stop");

		this.teaser.show("mp0");
		$("backLink").fade();
		new Effect.Appear($("map"), {
			beforeStart : function(effect) {
				effect.element.setOpacity(0.0);
				effect.element.show();
			}
		});
		new Effect.Opacity('ToolTip',{ from: 0.0, to: 0.4,duration: 2});
		$("mappoints").setStyle( {
			zIndex :30 
		});
		 
		if($$('.refBackLink')[0]) {
			$$(content.options.detailView).each(function (item) {
				item.up().remove();
				
			});
			$("startContent").appear();
		} 
	
	},

	click : function(e) {
		this.teaser.show(Event.element(e).up().readAttribute("id"));
		this.teaser.rotate("stop"); 
		if ($("map").visible() && this.teaser.items[this.teaser._mapItemIdToKey(Event.element(e).up().readAttribute("id"))].image) {
			new Effect.Fade($("map"), {
				afterFinish : function(effect) {
					effect.element.hide();
				}
			});
			$("backLink").appear();
		} else {
			$("backLink").fade();
			if (!$("map").visible())new Effect.Appear($("map"), {
				beforeStart : function(effect) {
					effect.element.setOpacity(0.0);
					effect.element.show();
				}
			});
			$("mappoints").setStyle( {
				zIndex :30
			});
		}
		
	},

	hide : function(e) {
		if(! $(Event.element(e).up().select("span")[0])) return;
		Event.element(e).up().select("span")[0].hide();
		Event.element(e).up().select("span img")[0].setOpacity(0.0);
		if (this.mpPe)
			this.mpPe.stop();
	},

	hideInitial : function() {
		this.teaser.hide(false, "noEffect");

		$("backLink").hide();
		$$(this.options.itemMpAnim).each(Element.hide);
	},

	none : function() {
	}
};

var Content = {}
Content = Class.create();
Content.prototype = {
	initialize : function() {
		this.options = Object.extend( {
			refBackLink :".refBackLink a",
			detailView :".detailView",
			startContent :"startContent",
			none :true
		}, arguments[0] || {});

		this.setup();
	},

	show : function() {
	},

	setup : function() {
		if (typeof Shadowbox == "object")
			Shadowbox.setup();

		this.clickBackLinkEvent = this.clickBackLink.bindAsEventListener(this);
		$$(this.options.refBackLink).each( function(item) {
			Event.observe(item, "click", this.clickBackLinkEvent);
		}.bind(this));
	},

	clickBackLink : function(e) {
		eItem = Event.element(e);
		eItem.up(this.options.detailView).up().remove();
		$("startContent").show();
		if(mapEffect) mapEffect.back();
		Event.stop(e);
	},

	none : function(item) {
	}
}