// JavaScript Document
function SlideShow(){
	function structGalleryImage(url,caption,width,height){
		var url=url;
		var caption=caption;
		var width=width;
		var height=height;
		var img=new Image();
		img.src=url;
		
		this.URL=url;
		this.Caption=caption;
		this.Width=width;
		this.Height=height;
	}
	
	var images=new Array();
	var currentIndex=-1;
	var xmlPath="/xml/PhotoGallery.xml";
	var imagesXPath="/PhotoGalleryImages/PhotoGalleryImage";
	var urlXPath="URL";
	var captionXPath="Caption";
	var widthXPath="NaturalWidth";
	var heightXPath="NaturalHeight";
	var container=null;
	var show=null;
	var caption=null;
	var countLabel=null;
	var countLabelTextFormat="Image {0} of {1}";
	var next=null;
	var prev=null;
	var showCssClass=null;
	var captionCssClass=null;
	var maxShowWidth=501;
	var maxShowHeight=501;
	
	this.LoadImages=function (){
		var xmlDoc=loadXMLDoc(GetWebSiteRootURL() + xmlPath);
		var imgs=xmlDoc.selectNodes(imagesXPath);
		
		for(i=0;i<imgs.length;i++)
		{
			var imgNode=imgs[i];
			var img=new structGalleryImage(imgNode.selectNodes(urlXPath)[0].childNodes[0].data,imgNode.selectNodes(captionXPath)[0].childNodes[0].data,imgNode.selectNodes(widthXPath)[0].childNodes[0].data,imgNode.selectNodes(heightXPath)[0].childNodes[0].data);
			images.push(img);
		}
	}
	
	this.CreateSlideShow=function (parentElId){
		if(images.length<1)
		{
			alert("No images found");
			return;
		}
		
		var parent=document.getElementById(parentElId);
		
		if(parent==null || typeof(parent)!='object')
		{
			alert("Cannot load slideshow into NULL element");
			return;
		}
		
		currentIndex=0;
		
		var container=document.createElement("div");
		var showContainer=document.createElement("div");
		countLabel=document.createElement("div");
		show=document.createElement("img");		
		caption=document.createElement("div");
		var nav=document.createElement("div");
		next=document.createElement("a");
		prev=document.createElement("a");
		
		showContainer.style.width=maxShowWidth+"px";
		showContainer.style.height=maxShowHeight+"px";
		showContainer.style.margin="10px auto 0px auto";
				
		show.src=GetWebSiteRootURL() + images[0].URL;
		show.height=SizeImageToFit().Height;
		show.width=SizeImageToFit().Width;
		
		if(showCssClass!=null && showCssClass.length>0){
			show.className=showCssClass;
		} /*else if(showStyle!=null && showStyle.length>0){
			var selector=".SlideShow"
			var found=false;
			var idx=-1;
			
			for(r in document.styleSheets[0].cssRules)
			{
				if(document.styleSheets[0].rules[r].selectorText==selector)
				{
					found=true;
					idx=r;
					break;
				}
			}
			
			if(!found){
				document.styleSheets[0].addRule(selector, selector + "{" + showStyle + "}");
			} else {
				document.styleSheets[0].rules[idx].cssText=selector + "{" + showStyle + "}";
			}
		}*/
				
		caption.innerText=images[0].Caption;
		caption.style.width=maxShowWidth+"px";
		caption.style.margin="10px auto";
		caption.style.textAlign="center";
		
		if(captionCssClass!=null && captionCssClass.length>0){
			caption.className=captionCssClass;
		} /*else if(captionStyle!=null && captionStyle.length>0){
			var selector=".SlideShowCaption"
			var found=false;
			var idx=-1;
			
			for(r in document.styleSheets[0].cssRules)
			{
				if(document.styleSheets[0].rules[r].selectorText==selector)
				{
					found=true;
					idx=r;
					break;
				}
			}
			
			if(!found){
				document.styleSheets[0].addRule(selector,selector + "{" + captionStyle + "}");
			} else {
				document.styleSheets[0].rules[idx].cssText=selector + "{" + captionStyle + "}";
			}
		}*/
		
		countLabel.innerText=countLabelTextFormat.replace("{0}",currentIndex+1).replace("{1}",images.length);
		countLabel.style.width=maxShowWidth+"px";
		countLabel.style.margin="10px auto";
		countLabel.style.textAlign="center";
		
		prev.innerText="< Prev";
		prev.href="#";
		prev.style.styleFloat="left";
		prev.style.disabled=(currentIndex<1);
		prev.onclick=this.Prev;
		
		next.innerText="Next >";
		next.href="#";
		next.style.styleFloat="right";
		next.style.disabled=(currentIndex<1 || currentIndex>(images.length-2));
		next.onclick=this.Next;
		
		nav.style.margin="30px auto 10px auto";
		nav.style.width="25%";
		
		showContainer.appendChild(show);
		
		nav.appendChild(prev);
		nav.appendChild(next);
				
		parent.appendChild(countLabel);
		parent.appendChild(showContainer);
		parent.appendChild(caption);
		parent.appendChild(nav);
	}
	
	this.Next=function(){
		if(currentIndex<0) return false;
		
		currentIndex++;
		
		if(currentIndex>(images.length-1)) currentIndex=0;
		
		show.src=GetWebSiteRootURL() + images[currentIndex].URL;
		show.height=SizeImageToFit().Height;
		show.width=SizeImageToFit().Width;
		caption.innerText=images[currentIndex].Caption;
		
		countLabel.innerText=countLabelTextFormat.replace("{0}",currentIndex+1).replace("{1}",images.length);
				
		return true;
	}
	
	this.Prev=function(){
		if(currentIndex<0) return false;
		
		currentIndex--;
		
		if(currentIndex<0) currentIndex=images.length-1;
		
		show.src=GetWebSiteRootURL() + images[currentIndex].URL;
		show.height=SizeImageToFit().Height;
		show.width=SizeImageToFit().Width;
		caption.innerText=images[currentIndex].Caption;
		
		countLabel.innerText=countLabelTextFormat.replace("{0}",currentIndex+1).replace("{1}",images.length);
		
		return true;
	}
	
	function SizeImageToFit(){
		var width=images[currentIndex].Width;
		var height=images[currentIndex].Height;
		
		var widthToHeightProp=width/height;
		var heightToWidthProp=height/width;
		var diff=0;
		
		if(width>maxShowWidth){
			diff=width-maxShowWidth;
			width=maxShowWidth;
			height=Math.round(height-(diff*heightToWidthProp));
		}
		
		if(height>maxShowHeight){
			diff=height-maxShowHeight;
			height=maxShowHeight;
			width=Math.round(width-(diff*widthToHeightProp));
		}
		
		return {Width:width, Height:height}
	}
}