var eSpirit = {
	slider:{
		MAX_VISIBLE_TABS:3,
		SLIDE_CHANGE_SPEED:"fast",
		TAB_MOVE_SPEED:"fast",
		currentActiveTabIndex:0,
		tabWidth:126,
		activeTabPos: 126,
		slider: null,
		slideDiv: null,
		galleryIndex: null,
		init: function () {
			var sliderConfig = this;
			jQuery('div.singleStudy').each(function (numSlider) {
				jQuery.extend({},sliderConfig).initSingle(this);
			});
		},
		initSingle: function (sliderTarget){
			var sliderConfig = this;
			this.slideDiv = jQuery(sliderTarget);
			this.galleryIndex = this.slideDiv.find('div.galleryIndex');
			this.slider = this.slideDiv.siblings('div.slideContainer').children('ul.slider');
			this.slider.css("margin:0");
			this.slider.children('li').children('a').bind('click', sliderConfig, sliderConfig.tabClickHandler);
			this.slideDiv.find('img.slideLeft').parent("a").click(function (e){
				sliderConfig.moveSlides(-1);		
				sliderConfig.moveTabs(-1);		
				return false;
			});	
			this.slideDiv.find('img.slideRight').parent("a").click(function (e){
				sliderConfig.moveSlides(1);				
				sliderConfig.moveTabs(1);				
				return false;
			});	
			this.galleryIndex.find('ul.thumbIndex li a').click(function (){
				var myPos = jQuery(this).parent().prevAll("li").length;
				eSpirit.slider.showDivWithNumber(myPos);
				eSpirit.slider.displayThumbIndex(myPos);
				return false;
			});
			this.displayThumbIndex(1);
		},
		displayThumbIndex:function (pos){
			this.galleryIndex.find('ul.thumbIndex li').removeClass("high").eq(pos).addClass("high");
		},
		tabClickHandler:function (e){
			var me = jQuery(this);
			var sliderConfig = e.data;
			me.blur();
			// if already active nothing happens
			var meIndex = me.parent("li").prevAll("li").length; // position of clicked tab
			// the left tab or the right tab?
			if (meIndex == 0){
				sliderConfig.moveTabs(-1);
				sliderConfig.moveSlides(-1);
			} else if (meIndex == 2){
				sliderConfig.moveTabs(1);
				sliderConfig.moveSlides(1);
			}
			return false;
		},
		showDivWithNumber: function (index){
			var sliderConfig = this;
			sliderConfig.slideDiv.find("div.high").fadeOut(sliderConfig.SLIDE_CHANGE_SPEED, function () {
				jQuery(this).removeClass("high");
				sliderConfig.slideDiv.find("div.case").eq(index).fadeIn(sliderConfig.SLIDE_CHANGE_SPEED,function (){
					jQuery(this).addClass("high");
				});
			});
		},
		moveTabs: function(direction){
			var sliderConfig = this;
			var tabUL = sliderConfig.slider;
			if (direction > 0){
				tabUL.find("li").eq(2).addClass("high");
				tabUL.find("li").eq(1).removeClass("high");
				tabUL.animate({
					marginLeft:"-"+sliderConfig.tabWidth
				},sliderConfig.TAB_MOVE_SPEED,function (){
					tabUL.hide();
					var firstLi = tabUL.children("li:first").appendTo(tabUL);
					tabUL.css("marginLeft",0);
					tabUL.show();
				});
			}else {
				tabUL.find("li").eq(0).addClass("high");
				tabUL.find("li").eq(1).removeClass("high");
				tabUL.hide();
				tabUL.css("marginLeft",-sliderConfig.tabWidth);
				tabUL.children("li:last").prependTo(tabUL);
				tabUL.show();
				tabUL.animate({
					marginLeft:0
				},sliderConfig.TAB_MOVE_SPEED);	
			}
		},
		moveSlides:function (direction){
			var sliderConfig = this;
			var currentSlide = sliderConfig.slideDiv.find("div.high");
			if (direction > 0){
				if (currentSlide.nextAll("div.case").length > 0){
					var nextSlide = currentSlide.next("div.case");
				}else {
					// if the active tab is the last in the row, the next is the first
					var nextSlide = sliderConfig.slideDiv.find("div.case").eq(0);
				}
			}else {
				if (currentSlide.prevAll("div.case").length > 0) {
					var nextSlide = currentSlide.prev("div.case");
				}else {
					var numberOfElements = sliderConfig.slideDiv.find("div.case").length;
					var nextSlide = sliderConfig.slideDiv.find("div.case").eq(numberOfElements-1);
				}
			}
			if (currentSlide && nextSlide){
				var pos = nextSlide.prevAll("div.case").length;
				sliderConfig.displayThumbIndex(pos);	
				sliderConfig.exchangeByFade(currentSlide,nextSlide);
			}
		},
		exchangeByFade: function (oldElement,newElement){
			var sliderConfig = this;
			jQuery(oldElement).fadeOut(sliderConfig.SLIDE_CHANGE_SPEED, function (){
				jQuery(this).removeClass("high");
				jQuery(newElement).fadeIn(sliderConfig.SLIDE_CHANGE_SPEED,function(){
					jQuery(this).addClass("high");
				});
			})
		}
	},
	teasingHover:{
		TEASER_DELAY_TIME: 500,
		TEASER_STAND_TIME: 1000,
		TEASER_FADE_OUT_SPEED: 1,
		TEASER_FADE_IN_SPEED: 500,
		openTeaser: null,
		openMenu: null,
		over:null,
		fadeOutTimer:null,
		fadeDelayTimer:null,
		init:function (){
		// find all subemenue
		var allNaviLis = jQuery("#navigation>li.normal");
			jQuery("#navigation>li.normal").hover(function (e){
				if (eSpirit.teasingHover.fadeOutTimer){
					clearTimeout(eSpirit.teasingHover.fadeOutTimer);
				}
				
				if (eSpirit.teasingHover.over == this){
					return;
				}
				var that = jQuery(this);
				var second = that.find("div.secondLevel")

				jQuery("#navigation>li.normal").removeClass("active");
				
				// hiding currently open menus
				if (eSpirit.teasingHover.openMenu){
					if (e.originalTarget != this){
						if (eSpirit.teasingHover.openTeaser != that.find("div.navLayer")){
							eSpirit.teasingHover.openMenu.parent("li").removeClass("active");	
							eSpirit.teasingHover.openMenu.hide();
							eSpirit.teasingHover.openTeaser.hide();
						}
					}
				}
				second.show();
				
				eSpirit.teasingHover.fadeDelayTimer = setTimeout(function(anElement){
					// IE doesnt seem to deliver the expected element, so we have to get it by hand
					if (!anElement || typeof anElement.find !== "function"){
						anElement = jQuery("#navigation>li.active");
					}
					if (!anElement || typeof anElement.find !== "function"){
						return;
					}
					anElement.find("div.navLayer").fadeIn(eSpirit.teasingHover.TEASER_FADE_IN_SPEED);
				 },eSpirit.teasingHover.TEASER_DELAY_TIME);
				 // tmp activated
				//jQuery(this).find("div.navLayer").fadeIn(eSpirit.teasingHover.TEASER_FADE_IN_SPEED,jQuery(this));
				that.show().addClass("active");
				if (!that.data("cornerOn")) {
					setTimeout(function () {
						that.corner("6px,top");
						that.data("cornerOn", true);
					}, 0);
				}
				eSpirit.teasingHover.openTeaser = that.find("div.navLayer");
				eSpirit.teasingHover.openMenu = that.find("div.secondLevel");
				eSpirit.teasingHover.over = this;
				jQuery(".cornerTL").corner("tl 6px");	
			}, function (e){
					var that = jQuery(this);
					if (that.find("div.navLayer").length>0){
						clearTimeout(eSpirit.teasingHover.fadeDelayTimer);
 						eSpirit.teasingHover.fadeOutTimer = setTimeout(function(anElement){
							// IE doesnt seem to deliver the expected element, so we have to get it by hand
							if (!anElement  || typeof anElement.find !== "function"){
								anElement = jQuery("#navigation>li.active");
							}
							if (!anElement || typeof anElement.find !== "function"){
								return;
							}
							anElement.find("div.navLayer").fadeOut(eSpirit.teasingHover.TEASER_FADE_OUT_SPEED);
							anElement.find("div.secondLevel").fadeOut(eSpirit.teasingHover.TEASER_FADE_OUT_SPEED,function(){
								jQuery(this).parent().removeClass("active");
							});
							eSpirit.teasingHover.openMenu = null;
							eSpirit.teasingHover.openTeaser = null;
							eSpirit.teasingHover.over = null;
						},eSpirit.teasingHover.TEASER_STAND_TIME,that);
						jQuery(".cornerTL").corner("tl 6px");
					}else {
						that.removeClass("active");
						that.find("div.secondLevel").hide().parent().removeClass("active");
						eSpirit.teasingHover.openMenu = null;
						eSpirit.teasingHover.openTeaser = null;
						eSpirit.teasingHover.over = null;
						jQuery(".cornerTL").corner("tl 6px");
					}
			});
		}
	},
	contentNavigator: {
		 currentPage: -1,
  		 allNaviLinks:null,
		init:function(){
			// initialize pageNavigation
			if (jQuery('div.contentNavigator').length == 0 ){
				return;
			}
			eSpirit.contentNavigator.allNaviLinks = jQuery("ul.pageList li:not(.scrollUp,.scrollDown) a");

			// looking for named anchor
			// read called anchor
			var calledHash = self.document.location.hash;
			if (calledHash && calledHash.equals != ""){
				calledHash = calledHash.replace("#","");
				// look for called hash in all content
				var hashAnchor = jQuery("div.contentNavigator a[name='"+calledHash+"']").eq(0);
				if (hashAnchor){
				// in which section is it?
					var foundIndex = hashAnchor.parent("div.contentArea").prevAll("div.contentArea").length;
					eSpirit.contentNavigator.displayPage(foundIndex);
				}
			}else {
				eSpirit.contentNavigator.displayPage(0);
			}
			for(var c=0,l=eSpirit.contentNavigator.allNaviLinks.length;c<l;++c){
				eSpirit.contentNavigator.allNaviLinks[c].linkNumber = c;
				jQuery(eSpirit.contentNavigator.allNaviLinks[c]).click(function(){
					eSpirit.contentNavigator.displayPage(this.linkNumber);
					return false;
				});
			}
	
			// initialize Scrollbuttons 	
			jQuery("ul.pageList li.scrollUp a").click(function (){
				if (eSpirit.contentNavigator.currentPage > 0){
					jQuery(this).blur();
					eSpirit.contentNavigator.displayPage(eSpirit.contentNavigator.currentPage-1);
					return false;
				}
			});
			jQuery("ul.pageList li.scrollDown a").click(function (){
				  if (eSpirit.contentNavigator.currentPage < eSpirit.contentNavigator.allNaviLinks.length-1){
					jQuery(this).blur();
					try{
					eSpirit.contentNavigator.displayPage(eSpirit.contentNavigator.currentPage + 1);
					} catch(e){};
					return false;
				}
			});
	
	 		jQuery('a.nextButton').click(function (){
				eSpirit.contentNavigator.displayPage(eSpirit.contentNavigator.currentPage+1);
				return false;
			});
	 		jQuery('a.backButton').click(function (){
				eSpirit.contentNavigator.displayPage(eSpirit.contentNavigator.currentPage-1);
				return false;
			});
	}, 
		displayPage: function(numberOfPage){
			if (numberOfPage < 0){
				//numberOfPage = 0;
				// --> wenn active, hat der "PageBack"-Button am ende der Seite (grafisch) keine Funktion mehr
			}
	

			if (numberOfPage == eSpirit.contentNavigator.currentPage){
				return;
			}
			eSpirit.contentNavigator.currentPage = numberOfPage;
			// hide currentPage
			jQuery('.contentArea').not(".hide").addClass("hide");
			
			// display numberOfPage-Page
			
			jQuery(jQuery('.contentArea')[numberOfPage]).removeClass("hide");
			jQuery(jQuery('.contentArea')[numberOfPage]).show();
			
			// setNavigation
			// disable currentPoint
			//jQuery("ul.pageList2 li.selected").removeClass("selected").corner("0").corner("4px");
			// --> wenn active, markiert KEINEN eintrag 
			
			// set nextPoint
			//jQuery('.pageList2 li').not(".scrollUp,.scrollDown").eq(numberOfPage).addClass("selected").corner("0").corner("4px");
			// --> wenn active, markiert immer den ERSTEN Eintrag
			
			
		}
	}
	
};

jQuery.noConflict();
jQuery(document).ready(function($){
	// case-study-tabslider
	eSpirit.slider.init();
	eSpirit.teasingHover.init();
	//eSpirit.contentNavigator.init();
});
