$(document).ready(function(){

if($('.carouselVertical').length >0){
	var verticalCarousels = $('.carouselVertical');
	for (var i=1;i<verticalCarousels.length+1;i++){
	
		//Specify the carousel's height
		window["carouselHeight_"+i] = parseInt(verticalCarousels[i-1].style.height);
		//Specify the carousel's slide speed (larger is faster 1-10)
		var vContainerSpeed=10;
		
		var vCarouselObj = verticalCarousels[i-1];
		//alert(carouselObj.id);
		var vItemNum = $("#hBoxContainer" + i +" .paternosterItem").length;
		
		window["actualheight_"+i]=(vItemNum*80);
		//Specify the carousel's height (in pixels)
		//window["carouselheight_"+i]=vItemNum*80*2;

        window["calculateHeight_"+i]=( window["actualheight_"+i]*(-1)+window["carouselHeight_"+i]);
        
		window.carouselUp = function(tempId){
			if (parseInt(window["vSlide_"+tempId].style.top)<18) {
			window["vSlide_"+tempId].style.top=parseInt(window["vSlide_"+tempId].style.top)+vContainerSpeed+"px"
            }
		}
		
		window.carouselDown = function(tempId){
			if ( parseInt( window["vSlide_"+tempId].style.top ) > ( parseInt( window["calculateHeight_"+tempId] ) ) ){
			    window["vSlide_"+tempId].style.top=parseInt(window["vSlide_"+tempId].style.top)-vContainerSpeed+"px";
            }
		}
		
		var topButton = $("#" +vCarouselObj.id + " .scrollUp");
		topButton[0].tempId = i;
		var downButton = $("#" +vCarouselObj.id + " .scrollDown");
		downButton[0].tempId = i;
		
		var vWrapContainer = $("#" +vCarouselObj.id + " .wrapContainer");
		
		window["vSlide_"+i]=document.getElementById("hBoxContainer"+i);
		vWrapContainer[0].style.height = window["carouselHeight_"+i]+"px";

		//window["slide_"+i+"_2"].style.top=window["actualheight_"+i]+"px";
		
		topButton[0].onmouseover=function(){		
			vTimer=setInterval("carouselUp("+ this.tempId + ")",30);
		}
		downButton[0].onmouseover=function(){
			vTimer=setInterval("carouselDown("+ this.tempId + ")",30);
		}
		topButton[0].onmouseout = downButton[0].onmouseout = function(){
			clearInterval(vTimer);
		}
	} // for Schleife carouselVertical ende
} // if-abfrage nach carouselVertical ende

if($('.carouselHorizontal').length >0){
	//console.log($('.thaliaCarousel').length);

	//Specify the carousel's height
	var carouselheight="235px";
	//Specify the carousel's slide speed (larger is faster 1-10)
	var containerSpeed=10;
	
	var measureItemNum = $("#tempBoxContainer1 .articleBox").length;
	
	var itemNum = measureItemNum;
	
	//console.log("itemNum: "+ $('#tempBoxContainer1 .articleBox').length);
	//var itemNum = 5;
	
	//actualwidth=itemlist.length*156;
	actualwidth=(itemNum*158);
	//Specify the carousel's width (in pixels)
	carouselwidth=itemNum*158*2;
	
	
	window.carouselleft = function(){
		if (parseInt(slide.style.left)<(actualwidth+8))
		slide.style.left=parseInt(slide.style.left)+containerSpeed+"px"
		else
		slide.style.left=parseInt(slide2.style.left)-actualwidth+containerSpeed+"px"
		
		if (parseInt(slide2.style.left)<(actualwidth+8))
		slide2.style.left=parseInt(slide2.style.left)+containerSpeed+"px"
		else
		slide2.style.left=parseInt(slide.style.left)-actualwidth+"px"
	}
	
	window.carouselright = function(){
		if (parseInt(slide.style.left)>(actualwidth*(-1)+8))
		slide.style.left=parseInt(slide.style.left)-containerSpeed+"px"
		else
		slide.style.left=parseInt(slide2.style.left)+actualwidth-containerSpeed+"px"
		
		if (parseInt(slide2.style.left)>(actualwidth*(-1)+8))
		slide2.style.left=parseInt(slide2.style.left)-containerSpeed+"px"
		else
		slide2.style.left=parseInt(slide.style.left)+actualwidth+"px"
	}
	
	var leftButton = document.getElementById("mycarousel-prev");
	var rightButton = document.getElementById("mycarousel-next");
	var lowLevel = 100;
	var highLevel = 1000;
	var wrapContainer = document.getElementById("wrapContainer");
	
	var slide=document.getElementById("tempBoxContainer1");
	var slide2=document.getElementById("tempBoxContainer2");
	
	wrapContainer.style.width = carouselwidth+"px";
	slide2.style.left=actualwidth+"px";
	
	leftButton.onmouseover=function(){
		timer=setInterval("carouselleft()",30);
	}
	rightButton.onmouseover=function(){
		timer=setInterval("carouselright()",30);
	}
	leftButton.onmouseout = rightButton.onmouseout = function(){
		clearInterval(timer);
	}
	leftButton.onclick = rightButton.onclick = function(){
		return false;
	}
	
			
		pullLinks = $(".carouselHorizontal .pullout");
		//console.log("pullLinks: " + pullLinks);
		for (var i=0;i<pullLinks.length;i++){
	//		console.log("pullLinks: " + pullLinks[i].parentNode.parentNode.parentNode.id);
			pullLinks[i].parentNode.parentNode.parentNode.opened = false;
			pullLinks[i].layer = $("#"+parseInt(pullLinks[i].id) +"article-box-infos");
			pullLinks[i].counter = parseInt(pullLinks[i].id);
			
			pullLinks[i].onclick=function(){
				lowLevel+=1;
				highLevel+=1;
				//console.log("layer: " + this.counter);
				var box = this.parentNode.parentNode.parentNode;
				var containerLeftPos = this.parentNode.parentNode.parentNode.parentNode.offsetLeft;
				var leftPos = this.parentNode.parentNode.parentNode.offsetLeft;
				
				if(!box.opened){
				//console.log("open layer: " + this.layer.css('z-index'));
					this.parentNode.parentNode.parentNode.parentNode.style.zIndex = highLevel;
					this.parentNode.parentNode.parentNode.style.zIndex = highLevel;					
					if (parseInt(leftPos+containerLeftPos) < 380){
						this.layer.animate({ 
							left:"157px"
						}, 500 );
					}else{
						this.layer.animate({ 
							left:"-157px"
						}, 500 );
					}
					box.opened = true;
				}else{
					this.parentNode.parentNode.parentNode.parentNode.style.zIndex = highLevel;
					this.parentNode.parentNode.parentNode.style.zIndex = highLevel;
					//console.log("close layer: " + this.layer.css('z-index'));
					this.layer.animate({ 
					left:"0px"
					}, {duration:500,complete: function(){this.parentNode.style.zIndex = lowLevel;}} );
					box.opened = false;
				}
				
				// close all layers
				for(var j=0;j<pullLinks.length;j++){
		//			console.log("pullLinks[j]: " + pullLinks[j]);
					if (this!=pullLinks[j]){
						if(pullLinks[j].parentNode.parentNode.parentNode.opened){
							pullLinks[j].layer.animate({ 
								left:"0px"
							}, {duration:500,complete: function(){;this.parentNode.opened = false;this.parentNode.style.zIndex = 100;}} );
						}
					}
				}
				
				return false;
			};
		}
	} // if-abfrage nach carouselHorizontal ende
});
