/* DivScroller */ 


function DivScroller(contentDivId) {
	this.contentDivObject=document.getElementById(contentDivId);
	this.currentXPos=0;
	this.targetXPos=0;
	this.interval=null;
}

DivScroller.prototype.setPosition=function (targetPosition) {
	window.clearInterval(this.interval);
	var animationObject=this;
	this.targetXPos=targetPosition;
	function animateScroll() {
		var stepDistance=(animationObject.targetXPos-animationObject.currentXPos)/3;
		if (Math.abs(stepDistance)>0.3) {
			animationObject.currentXPos+=stepDistance;
			animationObject.contentDivObject.style.left=-animationObject.currentXPos+"px";
		} else {
			animationObject.currentXPos=animationObject.targetXPos;
			animationObject.contentDivObject.style.left=-animationObject.currentXPos+"px";
			window.clearInterval(animationObject.interval);
		}
	}
	this.interval=window.setInterval(animateScroll,50);
}


/* PhotoGalleryThumbScroll Class */ 


function PhotoGalleryThumbScroll(pictureContainerDiv,scrollContainerDiv,thumbForwardButtonId,thumbBackwardButtonId,thumbWidth,numOfThumbsPerPage) {
	this.thumbScroller=new DivScroller(scrollContainerDiv);
	this.numOfThumbsPerPage=numOfThumbsPerPage;
	this.photoContainerDivObject=document.getElementById(pictureContainerDiv);
	this.scrollContainerDivObject=document.getElementById(scrollContainerDiv);
	this.thumbForwardButtonObject=document.getElementById(thumbForwardButtonId);
	this.thumbBackwardButtonObject=document.getElementById(thumbBackwardButtonId);
	this.thumbnailWidth=thumbWidth;
	this.thumbnailFileNames=new Array();
	this.thumbnailPictureObjects= new Array();
	this.photoFileNames=new Array();
	this.currentThumbSet=null;
	this.numOfThumbSets=null;
	this.currentPhoto=null;
	this.numberOfPhotos=0;
}

PhotoGalleryThumbScroll.prototype.addPicture= function (thumbFileName,photoFileName) {
	this.thumbnailFileNames[this.numberOfPhotos]=thumbFileName;
	this.photoFileNames[this.numberOfPhotos]=photoFileName;
	this.numberOfPhotos++;
}

PhotoGalleryThumbScroll.prototype.init= function () {
	this.initThumbs();
	this.currentThumbSet=1;
	this.numOfThumbSets=Math.ceil(this.numberOfPhotos/this.numOfThumbsPerPage);
	this.currentPhoto=1;
	this.displayPhoto(this.currentPhoto);
}

PhotoGalleryThumbScroll.prototype.getThumbSetNum= function (photoNum) {
	return Math.ceil(photoNum/this.numOfThumbsPerPage); 
}

PhotoGalleryThumbScroll.prototype.getCurrentPhoto= function () {
	return this.currentPhoto; 
}

PhotoGalleryThumbScroll.prototype.calculateScrollPosition=function (targetThumbSet) {
	var numOfPhotosInLastSet;
	numOfPhotosInLastSet=this.numberOfPhotos % this.numOfThumbsPerPage;
	
	if (this.numOfThumbSets==targetThumbSet && this.numOfThumbSets>1 && numOfPhotosInLastSet!=0) {
		return ((targetThumbSet-2)*this.numOfThumbsPerPage+numOfPhotosInLastSet)*this.thumbnailWidth;
	}	else {
		return (targetThumbSet-1)*this.thumbnailWidth*this.numOfThumbsPerPage;
	}
}

PhotoGalleryThumbScroll.prototype.setThumbSet=function (numOfThumbSet) {
	this.currentThumbSet=numOfThumbSet;
	this.thumbScroller.setPosition(this.calculateScrollPosition(numOfThumbSet));
}

PhotoGalleryThumbScroll.prototype.initThumbs= function () {
	var photoGalleryThumbScrollObj;
	photoGalleryThumbScrollObj=this;
	DivUtils.clear(this.scrollContainerDivObject);
	for (var i=0;i<this.numberOfPhotos;i++) {
		this.thumbnailPictureObjects[i]=document.createElement("IMG");
		this.thumbnailPictureObjects[i].num=i+1;
		this.thumbnailPictureObjects[i].setAttribute("src",this.thumbnailFileNames[i]);
		this.thumbnailPictureObjects[i].style.cursor="pointer";
		this.scrollContainerDivObject.appendChild(this.thumbnailPictureObjects[i]);
		this.thumbnailPictureObjects[i].onclick=function () {
			photoGalleryThumbScrollObj.displayPhoto(this.num);
		}
	}
	this.thumbForwardButtonObject.onclick= function () {
		if (photoGalleryThumbScrollObj.currentThumbSet!=photoGalleryThumbScrollObj.numOfThumbSets) {
			photoGalleryThumbScrollObj.setThumbSet(photoGalleryThumbScrollObj.currentThumbSet+1);
		} else {
			photoGalleryThumbScrollObj.setThumbSet(1);
		}
		
	}
	
	this.thumbBackwardButtonObject.onclick= function () {
		if (photoGalleryThumbScrollObj.currentThumbSet>1) {
			photoGalleryThumbScrollObj.setThumbSet(photoGalleryThumbScrollObj.currentThumbSet-1);
		} else {
			photoGalleryThumbScrollObj.setThumbSet(photoGalleryThumbScrollObj.numOfThumbSets);
		}
	}
	
	this.scrollContainerDivObject.style.width=this.numberOfPhotos*this.thumbnailWidth+"px";
}

PhotoGalleryThumbScroll.prototype.displayPhoto=function(photoNum) {
	DivUtils.clear(this.photoContainerDivObject);
	var photoElement=document.createElement("IMG");
	photoElement.setAttribute("src",this.photoFileNames[photoNum-1]);
	this.photoContainerDivObject.appendChild(photoElement);
	this.currentPhoto=photoNum;
}




/* ZoomSlideShow Class */ 

function ZoomSlideShow(zoomSlideshowContainerDivId,contentContainerDivId,photoContainerDivId,nextButtonId,previousButtonId,closeButtonId,backgroundDivId) {
	this.zoomSlideshowContainerDivObj=document.getElementById(zoomSlideshowContainerDivId);
	this.contentContainerDivObj=document.getElementById(contentContainerDivId);
	this.photoContainerDivObj=document.getElementById(photoContainerDivId);
	this.nextButton=document.getElementById(nextButtonId);
	this.previousButton=document.getElementById(previousButtonId);
	this.closeButton=document.getElementById(closeButtonId);
	this.backgroundDivObj=document.getElementById(backgroundDivId);
	this.photoFileNames=new Array();
	this.currentPhoto=null;
	this.numOfPhotos=0;
}

ZoomSlideShow.prototype.addPhoto=function (photoFileName) {
	this.photoFileNames[this.numOfPhotos]=photoFileName;
	this.numOfPhotos++;
}

ZoomSlideShow.prototype.show=function() {
	this.zoomSlideshowContainerDivObj.style.display="block";
	this.zoomSlideshowContainerDivObj.style.height=getDocumentHeight()+"px";
	this.centerPhoto(870,100);
	DivUtils.clear(this.photoContainerDivObj);
}

ZoomSlideShow.prototype.hide=function() {
	this.zoomSlideshowContainerDivObj.style.display="none";
}

ZoomSlideShow.prototype.getCurrentPhoto=function () {
	return this.currentPhoto;
}

ZoomSlideShow.prototype.displayPhoto=function (photoNumber) {
	var obj=this;
	this.centerPhoto(870,100);
	DivUtils.clear(this.photoContainerDivObj);
	var photoImg=new Image();
	photoImg.onload=display;
	photoImg.src=this.photoFileNames[photoNumber-1];
	function display() {
		DivUtils.clear(obj.photoContainerDivObj);
		obj.centerPhoto(this.width,this.height);
		var photoElement=document.createElement("IMG");
		photoElement.setAttribute("src",obj.photoFileNames[photoNumber-1]);
		obj.photoContainerDivObj.appendChild(photoElement);		
		obj.centerPhoto(this.width,this.height);
		obj.currentPhoto=photoNumber;
	}
}

ZoomSlideShow.prototype.centerPhoto=function(photoWidth,photoHeight) {
	this.contentContainerDivObj.style.width=(photoWidth+45)+"px";
	this.contentContainerDivObj.style.height=photoHeight+"px";
	this.nextButton.parentNode.style.left=(photoWidth-20)+"px"		
	this.nextButton.parentNode.style.top=Math.round((photoHeight-this.nextButton.parentNode.offsetHeight)/2)+"px"
	DivUtils.centerOnPage(this.contentContainerDivObj);	
}

ZoomSlideShow.prototype.displayNextPhoto=function() {
	if (this.currentPhoto!=this.numOfPhotos) {
		this.currentPhoto++;
		this.displayPhoto(this.currentPhoto);
	} else {
		this.displayPhoto(1);
		this.currentPhoto=1;
	}
}

ZoomSlideShow.prototype.displayPreviousPhoto=function() {
	if (this.currentPhoto!=1) {
		this.currentPhoto--;
		this.displayPhoto(this.currentPhoto);
	} else {
		this.displayPhoto(this.numOfPhotos);
		this.currentPhoto=this.numOfPhotos;
	}
}

ZoomSlideShow.prototype.init=function() {
	var SlideShowObject;
	SlideShowObject=this;
	this.currentPhoto=1;
	if (this.numOfPhotos!=1) {
		this.nextButton.onclick=function() {
			SlideShowObject.displayNextPhoto();
		}
		this.previousButton.onclick=function() {
			SlideShowObject.displayPreviousPhoto();
		}
		this.nextButton.style.display="block";
		this.previousButton.style.display="block";
	} else {
		this.nextButton.style.display="none";
		this.previousButton.style.display="none";
	}
	this.backgroundDivObj.onclick=hideZoom;
	this.closeButton.onclick=hideZoom;
	function hideZoom() {
		SlideShowObject.hide();
	}
}

/* MATRAGallery Class */

function MATRAGallery(photoGalleryThumbScrollObj,zoomSlideShowObj,zoomBtnId,closeBtnId,backgroundDivId) {
	this.photoGalleryThumbScrollObj=photoGalleryThumbScrollObj;
	this.zoomSlideShowObj=zoomSlideShowObj;
	this.zoomBtnObj=document.getElementById(zoomBtnId);
	this.closeBtnObj=document.getElementById(closeBtnId);
	this.backgroundDivObj=document.getElementById(backgroundDivId);
	this.currentPhoto=null;
}

MATRAGallery.prototype.add= function (thumbFile,photoFile,photozoomfile) {
	this.photoGalleryThumbScrollObj.addPicture(thumbFile,photoFile);
	this.zoomSlideShowObj.addPhoto(photozoomfile);
}

MATRAGallery.prototype.init= function () {
	var matraGalleryObj=this;
	this.currentPhoto=1;
	this.photoGalleryThumbScrollObj.init();
	this.zoomSlideShowObj.init();
	this.zoomBtnObj.onclick=function() {
		matraGalleryObj.zoomSlideShowObj.init();
		matraGalleryObj.zoomSlideShowObj.show();
		matraGalleryObj.currentPhoto=matraGalleryObj.photoGalleryThumbScrollObj.getCurrentPhoto();
		matraGalleryObj.zoomSlideShowObj.displayPhoto(matraGalleryObj.currentPhoto);
		matraGalleryObj.closeBtnObj.onclick=hideZoom;
		matraGalleryObj.backgroundDivObj.onclick=hideZoom;
	}
	
	function hideZoom() {
		matraGalleryObj.zoomSlideShowObj.hide();
		matraGalleryObj.currentPhoto=matraGalleryObj.zoomSlideShowObj.getCurrentPhoto();
		matraGalleryObj.photoGalleryThumbScrollObj.displayPhoto(matraGalleryObj.currentPhoto);
		matraGalleryObj.photoGalleryThumbScrollObj.setThumbSet(matraGalleryObj.photoGalleryThumbScrollObj.getThumbSetNum(matraGalleryObj.currentPhoto));	
	}
}

/* zoomGallery onclick handler */

function onclickGallery(galleryObj) {
	if (galleryObj!=null) {
		galleryObj.show();
		galleryObj.init();
		galleryObj.displayPhoto(1);
	}
}