coverflow = {
	thumbWidth : 46, //this is inclusive borders (+2)
	totalWidth : 0,
	speed : 160, //160
	flowTo : 0,
	fadeRatio: 0.5,
	canScrollLeft : false,
	positions : new Array(),
	itemsInFlowbox : 9,
	numItemsInFlowbox:0,
	currIndex:0,
	noLeftFade: false,
	noRightFade: false,
	noScrolling: false,
	init : function(){
		$(".cf-goleft").hover(coverflow.goLeftOver, coverflow.goLeftOut);
		$(".cf-goright").hover(coverflow.goRightOver, coverflow.goRightOut);
		$(".cf-images li").each(function(){
			coverflow.positions.push($(this).position().left);
		});
		$(".cf-images li img").hover(coverflow.itemOver, coverflow.itemOut);
		$(".cf-images").children("li").click(coverflow.itemClick);
		coverflow.numItemsInFlowbox = $(".cf-images li").length;
		coverflow.totalWidth = coverflow.numItemsInFlowbox*coverflow.thumbWidth;
		coverflow.flowTo = coverflow.totalWidth - $(".cf-images-wrapper").width();
		//$(".project-menu li").eq(0).addClass("menu-active");
		$(".project-menu li").hover(coverflow.menuOver, coverflow.menuOut);
		$("#project-image-large").hover(coverflow.imgLargeOver, coverflow.imgLargeOut);
		$("#project-header h1").click(function(){$("#project-image-large").trigger('click');});
		coverflow.selectItem(0);

	},
	selectItem : function(itemIndex){
		var selected = $(".cf-images li").eq(itemIndex);
		$("#project-image-large img.project-image").attr("src", $(selected).attr("pihlproject:imgpath"));
		$(selected).addClass("active");
		$(selected).children("img").fadeTo(1,1);
		if(coverflow.numItemsInFlowbox > coverflow.itemsInFlowbox){
			$(".cf-goleft").fadeTo(1,coverflow.fadeRatio);
			$(".cf-goright").fadeTo(1,0.1);
		}else{
			$(".cf-goleft").fadeTo(1,0.01);
			$(".cf-goright").fadeTo(1,0.01);
			coverflow.noScrolling = true;
		}
	},
	itemOver : function(){
		//if(!$(this).parent().hasClass('active')){
			$(this).fadeTo(120,1, function(){
				/*
				var leftposText = $(this).position().left+$(".cf-images").position().left+35;
				leftposText = leftposText - ($(".item-over-sub-container div").width()+70)/4;
				$(".item-over-sub-container div").css({"width":$(".item-over-sub-container div").width()+70+"px","left": leftposText+"px"});
				*/
				$(".item-over-sub-container div").text($(this).parent().attr("pihlproject:caption"));
				$(".item-over-sub-container div").show();
			});
		//}

		//$(".item-over-sub-container div").stop(true, true);
	/*	$(".item-over-sub-container div").text($(this).attr("pihlproject:caption").substr(0,20));
		var leftposText = $(this).position().left+$(".cf-images").position().left+35;
		leftposText = leftposText - Math.floor(($(".item-over-sub-container div").width()+70)/4);
		$(".item-over-sub-container div").css({"width":$(".item-over-sub-container div").width()+70+"px","left": leftposText+"px"});
		$(".item-over-sub-container div").show();*/
	},
	itemOut : function(){
		if(!$(this).parent().hasClass('active')){
			$(this).fadeTo(120,coverflow.fadeRatio, function(){
				/*$(".item-over-sub-container div").css({"width":"auto"});*/
				$(".item-over-sub-container div").hide();
			});
		}

		//$(".item-over-sub-container div").stop(true, true);
		//
		//$(".item-over-sub-container div").text("");
		//
	},
	itemClick : function(event){

		$("li.active").removeClass('active');
		var act = $(this);
		$(".cf-images li img").stop(true,true);
		$(".cf-images li img").fadeTo(1, coverflow.fadeRatio, function(){
			$(act).addClass('active');
			$(act).children('img').fadeTo(1,1);
		});

		if($(this).attr("pihlproject:prtitle").length > 0){
			$('#project-header h1').text($(this).attr("pihlproject:prtitle"));
			$('#project-header p').text($(this).attr("pihlproject:prshort"));
		}
		//$('#project-image-large img.project-image').stop(true, true);

		if($('.preloader').length == 1){
			$('.preloader').remove();
		}
		var newPic = new Image();
  		newPic.src = $("li.active").attr('pihlproject:imgpath');

		$('#project-image-large img.project-image').fadeTo(100,0.01, function(){
			$(this).attr('src', $("li.active").attr('pihlproject:imgpath'));
			$(this).fadeTo(300, 1);
		});
		event.preventDefault();

	},
	goLeftOver: function(){
		if($(".cf-images").position().left > -1*coverflow.flowTo && !coverflow.noScrolling){
			//$(".cf-goleft").stop(true, true);
			$(".cf-goleft").fadeTo(100,1);
			//$(".cf-goright").stop(true, true);
			$(".cf-goright").fadeTo(100,coverflow.fadeRatio);
			//var thisSpeed = coverflow.totalWidth + $(".cf-images").position().left * coverflow.speed;
			var thisSpeed = (coverflow.numItemsInFlowbox-5-coverflow.currIndex)*coverflow.speed;
			$(".cf-images").animate({left:'-'+coverflow.flowTo+'px'},thisSpeed, "linear");
		}else{
			coverflow.noLeftFade = true;
		}
	},
	doGoOn : function(){
		var currLeft = $(".cf-images").position().left;
		if(-1*currLeft != coverflow.flowTo && currLeft != 0){
			var gotoNearest = coverflow.nearestPos(currLeft);
			$(".cf-images").animate({left:'-'+gotoNearest+'px'},400,"easeInSine", function(){$(".cf-images").stop();});
			coverflow.noLeftFade = false;
			coverflow.noRightFade = false;
		}else{
			//$(".cf-goright").stop(true, true);
			//$(".cf-goleft").stop(true, true);
			if(currLeft == 0 && !coverflow.noScrolling){
				coverflow.noRightFade = true;
				coverflow.noLeftFade = false;
				$(".cf-goright").fadeTo(100,0.1);
				$(".cf-goleft").fadeTo(100,coverflow.fadeRatio);
				coverflow.currIndex = 0;
			}else if(!coverflow.noScrolling){
				coverflow.noLeftFade = true;
				coverflow.noRightFade = false;
				$(".cf-goright").fadeTo(100,coverflow.fadeRatio);
				$(".cf-goleft").fadeTo(100,0.1);
				coverflow.currIndex = coverflow.positions.length-1;
			}
		}
	},
	nearestPos: function(aLeftPos){
		aLeftPos = -1*aLeftPos;
		if(aLeftPos == 0){
			coverflow.currIndex = 0;
			return 0;
		}
		if(aLeftPos == coverflow.flowTo){
			coverflow.currIndex = coverflow.positions.length-1;
			return coverflow.positions[coverflow.positions.length-1];
		}
		for(var i = 0;i < coverflow.positions.length;i++){
			var tmpPos1 = coverflow.positions[i];
			var tmpPos2 = coverflow.positions[i+1];

			if(tmpPos2 > coverflow.flowTo){
				coverflow.currIndex = i;
				return tmpPos1;
			}
			if(aLeftPos >= tmpPos1 && aLeftPos <= tmpPos2){
				if(aLeftPos-tmpPos1 >= tmpPos2-aLeftPos){
					coverflow.currIndex = i+1;
					return tmpPos2;
				}
				else{
					coverflow.currIndex = i;
					return tmpPos1;
				}
			}
		}
	},
	goLeftOut: function(){
		$(".cf-images").stop();
		coverflow.doGoOn();
		if(!coverflow.noLeftFade)
			$(".cf-goleft").fadeTo(100,coverflow.fadeRatio);
	},
	goRightOver: function(){
		if($(".cf-images").position().left < 0 && !coverflow.noScrolling){
			$(".cf-goright").fadeTo(100,1);
			$(".cf-goleft").fadeTo(100,coverflow.fadeRatio);
			var thisSpeed = (coverflow.numItemsInFlowbox-(coverflow.numItemsInFlowbox-coverflow.currIndex))*coverflow.speed;
			//var thisSpeed = (coverflow.totalWidth - $('.cf-images').position().left - $(".cf-images-wrapper").width()) * coverflow.speed;
			$(".cf-images").animate({left:'0px'},thisSpeed, "linear");
		}else{
			coverflow.noRightFade = true;
		}
	},
	goRightOut: function(){
		$(".cf-images").stop();
		coverflow.doGoOn();
		if(!coverflow.noRightFade)
			$(".cf-goright").fadeTo(100,coverflow.fadeRatio);
	},
	menuOver: function(){
		if(!$(this).hasClass('menu-active')){
			$(this).addClass('menu-over');
		}
	},
	menuOut: function(){
		if(!$(this).hasClass('menu-active')){
			$(this).removeClass('menu-over');
		}
	},
	imgLargeOver: function(){
		$(".project-image-overlay").fadeTo(200, 0.7);
	},
	imgLargeOut: function(){
		$(".project-image-overlay").fadeTo(200, 0.3);
	}
}

$(document).ready(function(){
	$(".cf-images li img").each(function(){
		if(!$(this).hasClass("active"))
			$(this).fadeTo(1,coverflow.fadeRatio);
	});

	coverflow.init();
});
img = new Image();
//Changed by Jan-erik
//img.src = 'typo3conf/ext/pihl_project/pi1/res/images/generator.gif';
img.src = 'typo3conf/ext/pihl_project/pi1/res/images/ajax-loader.gif';