(function($){
	var IBV = window.IBV = function() {
		return {
			init: function(){
				IBV.headerSlider.init();
				IBV.upcommingSlider.init();
				IBV.tabs.init();
				$('#content a').zoomimage({controls: false});
			}
		}
	}();
	IBV.headerSlider = function(){
		var slides, current = 0, timer,
			goTo = function(ev) {
				var el = $(ev.target), newIndex = 0;
				if (el && el.is('a')) {
					if (el.is('.sliderGoPrev')) {
						newIndex = current -1;
					} else if (el.is('.sliderGoNext')) {
						newIndex = current +1;
					} else {
						newIndex = $('#headerSliderNavigationSlides a').index(el.get(0));
					}
					if (newIndex < 0) {
						newIndex = slides.size() -1;
					} else if (newIndex >= slides.size()) {
						newIndex = 0;
					}
					goToIndex(newIndex);
				}
				return false;
			},
			goToIndex = function(newIndex) {
				$('#headerSliderNavigationSlides a').removeClass('active').eq(newIndex).addClass('active');
				slides
					.eq(current)
						.stop()
						.fadeTo(500, 0, function(){
							$(this).hide();
						})
						.end()
					.eq(newIndex)
						.show()
						.stop()
						.fadeTo(500, 1);
				current = newIndex;
				clearTimeout(timer);
				timer = setTimeout(function(){
					goTo.apply($('#headerSliderNavigation').get(0), [{target: $('#headerSliderGoNext').get(0)}])
				}, 5000);
			};
		return {
			init: function() {
				var html = [];
				slides = $('#headerSliderSlides a')
						.each(function(){
							html.push('<a href="#" title="');
							html.push($(this).attr('title'));
							html.push('"></a>');
						}).css('opacity', 0).hide();
				$('#headerSliderNavigationSlides').append(html.join(''));
				$('#headerSliderNavigation')
					.bind('click', goTo);
				goToIndex(0);
			}
		}
	}();
	IBV.upcommingSlider = function(){
		var slides, current = 0, timer,
			goTo = function(ev) {
				var el = $(ev.target), newIndex = 0;
				if (el && el.is('a')) {
					if (el.is('.sliderGoPrev')) {
						newIndex = current -1;
					} else if (el.is('.sliderGoNext')) {
						newIndex = current +1;
					} else {
						newIndex = $('#upcommingEventsNavigationSlides a').index(el.get(0));
					}
					if (newIndex < 0) {
						newIndex = slides.size() -1;
					} else if (newIndex >= slides.size()) {
						newIndex = 0;
					}
					goToIndex(newIndex);
				}
				return false;
			},
			goToIndex = function(newIndex) {
				$('#upcommingEventsNavigationSlides a').removeClass('active').eq(newIndex).addClass('active');
				$('#upcommingEventsSlides>ul').stop().animate({top: -newIndex*210}, 500);
				current = newIndex;
				clearTimeout(timer);
				timer = setTimeout(function(){
					goTo.apply($('#upcommingEventsNavigation').get(0), [{target: $('#upcommingEventsGoNext').get(0)}])
				}, 5000);
			};
		return {
			init: function() {
				if (window.location.hash == '#slider') {
					$('#upcommingEvents, #infoBox').show();
					var html = [];
					slides = $('#upcommingEventsSlides>ul>li')
							.each(function(){
								html.push('<a href="#" title="');
								html.push($('>span',this).text());
								html.push('"></a>');
							});
					$('#upcommingEventsNavigationSlides').append(html.join(''));
					$('#upcommingEventsNavigation')
						.bind('click', goTo);
					goToIndex(0);
				}
			}
		}
	}();
	IBV.tabs = function(){
		var setActive = function(){
			var tabIndex = $(this)
				.parent().parent()
				.find('a')
				.removeClass('active')
				.index(this);
			$(this)
				.addClass('active')
				.parent().parent().parent()
				.find('div.boxTabsPage')
				.hide()
				.eq(tabIndex)
				.show();
			this.blur();
			return false;
		};
		return {
			init: function(){
				$('div.boxTabs')
					.find('ul.boxTabstabs a')
						.bind('click', setActive)
							.filter(':first')
								.addClass('active')
								.end()
						.end()
					.find('div.boxTabsPage:first')
						.show();
			}
		}
	}();
	jQuery(document).ready(IBV.init);
})(jQuery)