﻿function ImageGallery(imageContainer, descriptionContainer){
	//private
	var thisRef=this;
	var imagesCollection=new Array();
	var currentIndex=-1;
	var currentOppacity=0;
	
	function ShowImage(){
		//imageContainer.src=imagesCollection[currentIndex].ImageUrl;
		descriptionContainer.innerHTML=imagesCollection[currentIndex].Description;
		
		
		if(imagesCollection[currentIndex].LinkUrl){
			imageContainer.onclick=function(){document.location=imagesCollection[currentIndex].LinkUrl;};
		}
		
		if(currentIndex==(imagesCollection.length-1)) currentIndex=0;
		else currentIndex++;
		
		currentOppacity=0;
		window.setTimeout(AnimateImage,100);
		
	}
	
	function AnimateImage(speed){
	    if(window.event && window.event!=null) return; 
	   
	    
		ChangeOpacity(); 		
		currentOppacity+=10;
		if(currentOppacity>=100) window.setTimeout(DisolveImage,6000);
		else window.setTimeout(AnimateImage,100);
	}
	function DisolveImage(){
	    if(window.event && window.event!=null) return;
		ChangeOpacity(); 		
		currentOppacity-=10;
		if(currentOppacity<0) window.setTimeout(ShowImage,10);//show next image
		else window.setTimeout(DisolveImage,100);
	}
	
	function ChangeOpacity(){
		var perOpacity=currentOppacity/100;
		var cssText="filter:alpha(opacity=" + currentOppacity + ");-moz-opacity:" + perOpacity + ";-khtml-opacity: " + perOpacity + ";opacity: " + perOpacity + ";}";
		cssText+=";background-image:url(" + imagesCollection[currentIndex].ImageUrl + ")";
		imageContainer.style.cssText=cssText; 
	}
	//Public
	this.AddImage=function(imageUrl,description,linkUrl){
		var newImg={ImageUrl:imageUrl,Description:description,LinkUrl:linkUrl};
		imagesCollection.push(newImg);
	}
	
	this.StartAnimation=function(){
		if(imagesCollection.length==0) return;
		currentIndex++;
		ShowImage();
	}
}


