$(function(){

	// Tabs
	Bb_Tabs.init();

	// Accordion
	Bb_Accordion.init();
	
	//Footer-Navigation
	$(".footer .tabs li").hover(function () {
		$(this).find('.holder').width($(this).closest('ul').width()-2);
	});
		
	$(".slider").each(function(){Slider.init(this)});

	if($.browser.msie && parseInt($.browser.version)<=6){
		
		//ie6_autobottom
		var footer=$(".footer");
		var fh=footer.height()+10;
		window.onscroll=function(){footer.css({top:$(window).height()+document.documentElement.scrollTop-fh}); footer.css({bottom:0, top:"auto"})};
		window.onscroll();
		
		//ie6 hover
		$(".footer .tabs li").hover(function(){$(this).addClass("active")}, function(){$(this).removeClass("active")});
	}
	
	// AdTicket Kalendermaske (nicht Startseite) + Eventliste
	if ($('#eventcalendar-en').length) {
		ADticket.load({module: 'event-calendar', container: '#eventcalendar-en', id: 6011, width: 500, height: 205, lang: 'en'});
	} else {
		ADticket.load({module: 'event-calendar', container: '#eventcalendar', id: 6011, width: 500, height: 205, lang: 'de'});
	}

	if ($('#eventlist-en').length) {
		ADticket.load({module: 'event-list', container: '#eventlist-en', id: 6011, layout: 'table', lang: 'en'});
	} else {
		ADticket.load({module: 'event-list', container: '#eventlist', id: 6011, layout: 'table', lang: 'de'});
	}


	ieFooter.init();
});

//--------------------------------------------------

var Class=function(proto){
	var obj=function(){this.init.apply(this, arguments)};
	obj.prototype=proto;
	obj.prototype.constructor=obj;
	return obj;
};


var Smooth=new Class({
	dyn:0,
	speed:1,
	from:0,
	to:100,
	init:function(opt){
		this.dyn=(opt.dyn!=undefined?opt.dyn:-50)/100;
		this.speed=opt.speed?(opt.speed>0?opt.speed:-1/opt.speed):1;
		this.from=opt.from!=undefined?opt.from:this.from;
		this.to=opt.to!=undefined?opt.to:this.to;
		this.onstart=opt.onstart || this.onstart;
		this.onplay=opt.onplay || this.onplay;
		this.onend=opt.onend || this.onend;
		if(opt.autostart) opt.autostart>0?this.start():this.toogle();
	},
	start:function(from, to){
		if(from!=undefined) this.from=from;
		if(to!=undefined) this.to=to;
		this.d=this.from<this.to?1:-1; //direction
		this.startPos=this.from;
		this.endPos=this.to;
		this.range=0;
		if(this.tm){
			clearTimeout(this.tm);
			this.tm=null;
		}
		else{
			this.onstart(this.d);
			this.curPos=this.from;
		}
		this.onplay(this.curPos);
		this.play(); 
	},
	play:function(){
		var _this=this;
		if(this.dyn!=0) this.range=Math.round((this.dyn>0?(this.endPos-this.curPos)*this.dyn:-(this.curPos-this.startPos)*this.dyn)*this.speed);
		if(this.range*this.d<1) this.range=this.d*this.speed;
		this.curPos+=this.range;
		if(this.endPos*this.d<this.curPos*this.d) return this.end();
		this.onplay(this.curPos, this.curPos/this.endPos);
		this.tm=setTimeout(function(){_this.play()}, 25);
	},
	end:function(){
		clearTimeout(this.tm);
		this.tm=null;
		this.curPos=this.endPos;
		this.onend(this.d);
		return false;
	},
	toogle:function(){
		var t=this.from;
		this.from=this.to;
		this.to=t;	
		this.start();
	},
	onstart:function(d){},
	onplay:function(curPos){},
	onend:function(d){}
});


var Slider = {
	slideshowInterval:6500,
	
	init:function (obj) {
		var _this={};
		_this.root=$(obj);
		_this.img=$(obj.parentNode).find('img:first');
		_this.hint=$(obj.parentNode).find('.hint');
		_this.scroll=_this.root.find('.scroll');
		_this.pane=_this.root.find('.pane');
		_this.cur=null;
		_this.num=0;
		_this.page=1;
		_this.arr=[];
		_this.slideshowTimeoutId = -1;
		_this.items=_this.scroll.find('a').click(function(){
			return Slider.set(_this, this);
		}).each(function(i){
			this.i = i;
			_this.arr[i]=$(this);
		});
		_this.root.find('.scroll a:first').click();
		_this.scrollWidth = _this.scroll.width();
		_this.slidePerPage = Math.floor(_this.scrollWidth / $(_this.items[0]).outerWidth());
		
		if (_this.items.length > _this.slidePerPage) {
			Slider.addButtons(_this);
		}
		
		_this.smooth = new Smooth({
			dyn:50, 
			speed:-1.5,
			onstart:function(){},
			onplay:function(pos){_this.scroll.get(0).scrollLeft=pos;},
			onend:function(){}
		});
	},
	
	addButtons:function(_this){
		_this.root.prepend($('<a class="arr_l"></a>').click(function(){Slider.go(_this, -1)}).bind('mousedown', function () {return false;}));
		_this.root.append($('<a class="arr_r"></a>').click(function(){Slider.go(_this, 1)}).bind('mousedown', function () {return false;}));
	},
	
	set:function(_this, obj){
		obj = $(obj);
		if(_this.cur && _this.cur.get(0)==obj.get(0)) {
			return false;
		}
		_this.num=obj.get(0).i;
		
		_this.hint.html(obj.find("img:first").attr("alt"))
		_this.img.attr({
			src : obj.attr("rel"),
			alt : obj.find("img:first").attr("alt")
		});
		
		obj.addClass("active");
		
		if(_this.cur) {
			_this.cur.removeClass("active");
		}
		
		_this.cur = obj;
		this.startAutoSlide(_this);
		return false;
	},
	
	go:function(_this, d){
		_this.num += d;
		if (_this.num < 0) {
			_this.num=_this.arr.length-1;
		}
		if (_this.num > _this.arr.length-1) {
			_this.num = 0;
		}
		_this.arr[_this.num].click();
		this.pageCheck(_this, d);
	},
	
	pageCheck:function(_this, d){
		if(_this.num >= _this.slidePerPage * _this.page || _this.num + _this.slidePerPage < _this.slidePerPage * _this.page) {
			_this.page = Math.floor(_this.num / _this.slidePerPage) + 1;
			var end_pos = _this.scrollWidth * (_this.page - 1);
			_this.smooth.start(_this.scroll.get(0).scrollLeft, end_pos);
		}
	},
	
	startAutoSlide: function (_this) {
		window.clearTimeout(_this.slideshowTimeoutId);
		_this.slideshowTimeoutId = window.setTimeout(function () {Slider.doAutoSlide(_this)}, this.slideshowInterval);
	}, 
	
	doAutoSlide: function (_this) {
		this.go(_this, 1);
	}
};

var Bb_Tabs = {
	// Initialisierung aller jQuery UI Tab-Elemente
	init: function() {
		$('.bb_jquerytab_wrapper').tabs();

		$('.bb_jquerytab_wrapper .csc-default').each(function(){
			var id = $(this).attr('id');
			var header = $(this).find('h1, h2, h3, h4, h5, h6').first();
			var title = header.html();
			header.remove();
			$(this).closest('.bb_jquerytab_wrapper').tabs('add', '#' + id, title);
		});
	}
};

var Bb_Accordion = {
	init: function() {
		var accordions = $('.bb_jqueryaccordion_wrapper');
		var item_counter = 1;
		accordions.each(function(){
			var accordion = $(this);
			var elements = accordion.children('div.csc-default');
			var elements_count = elements.length;
			elements.each(function(index, e){
				var e = $(e);
				var header = e.find('h1, h2, h3, h4, h5, h6').first();
				var header_html = header.html();
				var escaped_header = escape(header_html);

				header.parent().remove();

				var html = e.html();

				e.remove();

				accordion.append('<div class="item' + ((index+1 == elements_count) ? ' last': '') + '"><h3 id="acc' + item_counter + '">'+header_html+'</h3><div class="item_body" style="display: none;">'+html+'</div></div>');

				item_counter++;
			});

		});

		// Öffne alle in der URL übergebenen Accorion-Items
		var opened = Bb_Accordion._getOpenedItemsFromUrl();
		for (var i=0; i<opened.length; i++) {
			Bb_Accordion._toggle(opened[i], true);
		}

		$(".bb_jqueryaccordion_wrapper h3").click($.proxy(Bb_Accordion, "_toggle")).bind('mousedown', function () {return false;});
	},

	/**
	 * Klappt ein Accordion-Item ein oder aus.
	 * */
	_toggle: function(e, keepUrl) {
		element = typeof e == "string" ? $('#'+e) : $(e.target);
		this.slideToggle(element.toggleClass("active").next(".item_body").stop(true));
		if (!keepUrl) {
			this._toggleItemInURL(element.attr('id'));
		}
		return false;
	},

	/**
	 * Parsed die offenen Items aus der URL.
	 * */
	_getOpenedItemsFromUrl: function() {
		return (window.location.hash.indexOf('#show:') == -1) ? [] : window.location.hash.split('#show:').join('').split(',');
	},

	/**
	 * Toggled ein Item in der URL.
	 * */
	_toggleItemInURL: function(item) {
		var opened = Bb_Accordion._getOpenedItemsFromUrl();
		var found = false;
		var i;
		
		for (i = 0; i<opened.length; i++) {
			if (opened[i] == item) {
				found = true;
				opened.splice(i,1);
				break;
			}
		}
		
		if (!found) {
			opened.push(item);
		}
		
		opened.sort();
		
		window.location.hash = opened.length ? ('#show:' + opened.join(',')) : '-';
	},
	
	slideToggle: function (el, bShow){
		var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
		
		// if the bShow isn't present, get the current visibility and reverse it
		if( arguments.length == 1 ) bShow = !visible;
		
		// if the current visiblilty is the same as the requested state, cancel
		if( bShow == visible ) return false;
		
		// get the original height
		if( !height ) {
			// get original height
			height = $el.show().height();
			// update the height
			$el.data("originalHeight", height);
			// if the element was hidden, hide it again
			if( !visible ) $el.hide().css({height: 0});
		}
			
		// expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
		if( bShow ){
			$el.show().animate({height: height}, {duration: 500});
		} else {
			$el.animate({height: 0}, {duration: 500, complete:function (){$el.hide();}});
		}
	}
	
};

var ieFooter = {
	init: function () {
		if ($.browser.msie) {
			$(".footer .tabs > li:first-child").hover(function () {$(this).toggleClass('show')});
		}
	}
}

