$(document).ready(function(){
	
	//load moduleStyles
	var styles=document.createElement('link');
	styles.rel='stylesheet';
	styles.type='text/css';
	styles.href=basePath+'Modules/projects/css/styles.css';
	var head=document.getElementsByTagName('head');
	head[0].appendChild(styles);
	/*
	$('.colorBoxButton').colorbox({
		inline:true,
		href:"#hiddenProjectText",
		maxWidth: "600px",
		maxHeight: "800px",
		onOpen: function(){stopSlideShow();},
		onClosed: function(){startSlideShow();}
	});
	*/
	preloadImages();
	imagesInterval=window.setInterval("checkImagesPreloaded()",200);
	$('#projectTextFrame').click(function(e){toggleTextFromInside(e);});
});

function toggleVideo(mediaId){
	//alert($('#projectVideoFrame').css('display'));
	if($('#projectVideoFrame').css('display')=='none'){
		if(document.getElementById('infoBtn')){
			document.getElementById('infoBtn').src=basePath+"Modules/projects/images/info.png";
		}
		stopSlideShow();
		$('.slideShowCtrlBtn').hide();
		if($('#projectImages').css('display')!='none'){
			$('#projectImages').hide('fast',function(){$('#projectVideoFrame').show('fast');});
		}else if($('#projectTextFrame').css('display')!='none'){
			$('#projectTextFrame').hide('fast',function(){$('#projectVideoFrame').show('fast');});
		}
		//var videoFrame=document.createElement('iframe');
		document.getElementById('projectVideoIframe').src=basePath+'Modules/projects/templates/html5video.php?mediaId='+mediaId;
	}else{
		if(document.getElementById('infoBtn')){
			document.getElementById('infoBtn').src=basePath+"Modules/projects/images/info.png";
		}
		startSlideShow();
		$('.slideShowCtrlBtnRun').show();
		$('#projectVideoFrame').hide('fast',function(){$('#projectImages').show('fast');});
		document.getElementById('projectVideoIframe').src='';
	}
}

function toggleTextFromInside(e){
	if(e.target.tagName!='a' && e.target.tagName!='A'){
		toggleText();
	}
}

function toggleText(){
	//alert($('#projectVideoFrame').css('display'));
	if($('#projectTextFrame').css('display')=='none'){
		stopSlideShow();
		$('.slideShowCtrlBtn').hide();
		if($('#projectImages').css('display')!='none'){
			
			$('#projectImages').hide('fast',function(){$('#projectTextFrame').show('fast');});
		}else if($('#projectVideoFrame').css('display')!='none'){
			$('#projectVideoFrame').hide('fast',function(){$('#projectTextFrame').show('fast');});
			document.getElementById('projectVideoIframe').src='';
		}
		if(document.getElementById('infoBtn')){
			document.getElementById('infoBtn').src=basePath+"Modules/projects/images/"+(lang=='DE'?'info_schliessen.png':'close_info.png');
		}
	}else{
		startSlideShow();
		$('.slideShowCtrlBtnRun').show();
		$('#projectTextFrame').hide('fast',function(){$('#projectImages').show('fast');});
		if(document.getElementById('infoBtn')){
			document.getElementById('infoBtn').src=basePath+"Modules/projects/images/info.png";
		}
	}
}



var intervalCounter=0;
var imagesInterval=null;
var realPImages=new Array();

function preloadImages(){
	
	for(var i=0;i<pImages.length;i++){
		
			realPImages.push(new Image());
			realPImages[realPImages.length-1].src=pImages[i].image;
			realPImages[realPImages.length-1].alt=pImages[i].alt;
			if(pImages[i].caption!=''){
				realPImages[realPImages.length-1].title=pImages[i].caption;
			}
			realPImages[realPImages.length-1].className='projectImage';
			realPImages[realPImages.length-1].id='projectImage_'+i;
		//if(i>0){
		//	realPImages[realPImages.length-1].style.display='none';
		//}
	}
}

function checkImagesPreloaded(){
	var all=true;
	intervalCounter++;
	for(var i=0;i<realPImages.length;i++){
		if(realPImages[i].complete!=true){
			all=false;
		}
	}
	if(all==true || intervalCounter>100){
		window.clearInterval(imagesInterval);
		preloadImages_callback();
	}
	
}

function preloadImages_callback(){
	$('#projectImages').empty();
	var iWrapper=document.createElement('div');
	iWrapper.id="imagesWrapper";
	var cont=null;
	for(var i=0;i<realPImages.length;i++){
		cont=document.createElement('div');
		if(i>0){
			cont.style.display='none';
		}
		if(realPImages[i].complete==true){
			cont.className='imageContainer';
			cont.id='projectImageContainerDiv_'+i;
			//cont.style.width=realPImages[i].width;
			//cont.style.height=realPImages[i].height;
			cont.appendChild(realPImages[i]);
			imagesCount++;
		}
		iWrapper.appendChild(cont);
	}
	iWrapper.style.width='1024px';
	$('#projectImages').append(iWrapper);
	$('#projectImages').click(function(){toggleText();});
	//alert(iWrapper.style.offsetWidth);
	startSlideShow();
	
	/*
	var imageContainer='';
	imageContainer+='<div class="imageContainer">';
	imageContainer+='<img class="projectImage" src="">';
	imageContainer+='</div>';
	*/
}

var slideShowInterval=null;

function startSlideShow(){
	if(slideShowInterval!=null){
		stopSlideShow();
	}
	if(imagesCount>1){
		slideShowInterval=window.setInterval('getNextImage()',5000);
		$('#slideShowStartButton').hide();
		$('#slideShowStopButton').show();
	}
}

function stopSlideShow(){
	if(slideShowInterval!=null){
		window.clearInterval(slideShowInterval);
		slideShowInterval=null;
		$('#slideShowStopButton').hide();
		$('#slideShowStartButton').show();
	}
}

var visibleImage=0;
var imagesCount=0;
var imageAnimating=false;

function getNextImage(){
	
	if(imageAnimating==false){
		
		imageAnimating=true;
		
		var lastImage=visibleImage;
		var nextImage=visibleImage+1;
		
		if(lastImage==imagesCount-1){
			nextImage=0;
		}
		$('#projectImageContainerDiv_'+lastImage).hide('fast',function(){$('#projectImageContainerDiv_'+nextImage).show('fast',function(){imageAnimating=false;});});
		visibleImage=nextImage;
	
	}
	
}


function getLastImage(){
	
	if(imageAnimating==false){
	
		imageAnimating=true
		var lastImage=visibleImage;
		var nextImage=visibleImage-1;
		if(lastImage==0){
			nextImage=imagesCount-1;
		}
		$('#projectImageContainerDiv_'+lastImage).hide('fast',function(){$('#projectImageContainerDiv_'+nextImage).show('fast',function(){imageAnimating=false;});});
		visibleImage=nextImage;

	}
	
}

function xgetNextImage(){
	
	
	
	var lastImage=visibleImage;
	var nextImage=visibleImage+1;
	
	
	if(lastImage==imagesCount-1){
		var newWrapperMarginLeft='0px';
		nextImage=0;
	}else{
		var newWrapperMarginLeft='-'+(1024*nextImage)+'px';
	}
	var oriWidth=$('#projectImage_'+lastImage).css('width');
	var oriHeight=$('#projectImage_'+lastImage).css('height');
	var newHeight=(parseInt(oriHeight)*0.7)+'px';
	var newWidth=(parseInt(oriWidth)*0.7)+'px';
	var newOpac=0.0;
	var newMarginTop=((parseInt(oriHeight)/2)-(parseInt(newHeight)/2)) +'px';
	var newMarginLeft=((parseInt(oriWidth)/2)-(parseInt(newWidth)/2)) +'px';
	var newMarginRight=((parseInt(oriWidth)/2)-(parseInt(newWidth)/2)) +'px';
	
	//fadeout visible Image
	var options={
		width: newWidth,
		height: newHeight,
		marginTop: newMarginTop,
		marginLeft: newMarginLeft,
		//marginRight: newMarginRight,
		opacity: newOpac
	}
	
	$('#projectImage_'+lastImage).stop(true,false);
	$('#projectImage_'+nextImage).stop(true,false);
	
	$('#projectImage_'+lastImage).animate(options,400,'swing',function(){
		//reset last image and make invisible
		$('#projectImage_'+lastImage).css('visibility','hidden');
		$('#projectImage_'+lastImage).css('width',oriWidth);
		$('#projectImage_'+lastImage).css('height',oriHeight);
		$('#projectImage_'+lastImage).css('marginTop','0px');
		$('#projectImage_'+lastImage).css('marginLeft','0px');
		$('#projectImage_'+lastImage).css('marginRight','0px');
		//$('#projectImage_'+lastImage).css('opacity',1.0);
		//prepare next image
		$('#projectImage_'+nextImage).css('visibility','visible');
		//$('#projectImage_'+nextImage).css('opacity',0.0);
		$('#projectImage_'+nextImage).animate({opacity:1.0},800);
		//animate container
		if(newWrapperMarginLeft=='0px'){
			$('#imagesWrapper').css('marginLeft','1024px');
		}
		$('#imagesWrapper').animate({marginLeft:newWrapperMarginLeft},800,'swing',function(){
			
		});
		
	});
	
	visibleImage=nextImage;
}

function xgetLastImage(){
	
	
	
	var lastImage=visibleImage;
	var nextImage=visibleImage-1;
	
	
	if(lastImage==0){
		nextImage=imagesCount-1;
	}
	
	var newWrapperMarginLeft='-'+(1024*nextImage)+'px';
	var oriWidth=$('#projectImage_'+lastImage).css('width');
	var oriHeight=$('#projectImage_'+lastImage).css('height');
	var newHeight=(parseInt(oriHeight)*0.7)+'px';
	var newWidth=(parseInt(oriWidth)*0.7)+'px';
	var newOpac=0.0;
	var newMarginTop=((parseInt(oriHeight)/2)-(parseInt(newHeight)/2)) +'px';
	var newMarginLeft=((parseInt(oriWidth)/2)-(parseInt(newWidth)/2)) +'px';
	var newMarginRight=((parseInt(oriWidth)/2)-(parseInt(newWidth)/2)) +'px';
	
	//fadeout visible Image
	//if()
	var options={
		width: newWidth,
		height: newHeight,
		marginTop: newMarginTop,
		marginLeft: newMarginLeft,
		//marginRight: newMarginRight,
		opacity: newOpac
	}
	$('#projectImage_'+lastImage).animate(options,400,'swing',function(){
		//reset last image and make invisible
		$('#projectImage_'+lastImage).css('visibility','hidden');
		$('#projectImage_'+lastImage).css('width',oriWidth);
		$('#projectImage_'+lastImage).css('height',oriHeight);
		$('#projectImage_'+lastImage).css('marginTop','0px');
		$('#projectImage_'+lastImage).css('marginLeft','0px');
		$('#projectImage_'+lastImage).css('marginRight','0px');
		$('#projectImage_'+lastImage).css('opacity',1.0);
		//prepare next image
		
		$('#projectImage_'+nextImage).css('opacity',0.0);
		$('#projectImage_'+nextImage).animate({opacity:1.0},800);
		//animate container
		if(lastImage==0){
			$('#imagesWrapper').css('marginLeft','-'+(1024*imagesCount)+'px');
		}
		$('#projectImage_'+nextImage).css('visibility','visible');
		$('#imagesWrapper').animate({marginLeft:newWrapperMarginLeft},800,'swing',function(){
			
		});
		
	});
	
	visibleImage=nextImage;
}
