var ImageViewer = function() {
	var viewer = null;
	var viewerBG = null;
	var viewerRect = null;
	var viewerImg = null;
	var imagePath = null;
	var imageToLoad = null;
	var imageSetLength = null;
	var rectW = null;
	var rectH = null;

	function _construct() {
		viewer = document.getElementById("viewer");
	}
	function buildViewer() {
		var htmlString = '<div id="viewerBG"></div>';
		htmlString += '<div id="viewerRect"><div><img id="viewerImg"></div></div>';
		viewer.innerHTML = htmlString;
	}
	function deleteImage() {
		viewerImg = null;
		viewerRect.innerHTML="";
	}
	function restoreImage() {
		viewerRect.innerHTML = '<div><img id="viewerImg"></div></div>';
		viewerImg = document.getElementById("viewerImg");
		viewerImg.style.opacity="0";
		viewerImg.style.filter="alpha(opacity=0)";
		viewerImg.style.left="-1024px";
	}
	function getViewerElements() {
		viewerBG = document.getElementById("viewerBG");
		viewerRect = document.getElementById("viewerRect");
		viewerImg = document.getElementById("viewerImg");
	}
	function init() {

		// hide everything by making container transparent
		viewer.style.position = "absolute";
		viewer.style.left = "0px";
		viewer.style.top = "0px";
		viewer.style.width = document.body.clientWidth + "px";
		viewer.style.height = "100%";
		viewer.style.opacity = "0";
		viewer.style.filter = 'alpha(opacity=0)';
		viewer.style.zIndex="9998";

		// make backdrop translucent black
		viewerBG.style.position = "relative";
		viewerBG.style.left = "0px";
		viewerBG.style.top = "0px";
		viewerBG.style.width = "100%";
		viewerBG.style.height = "100%";
		viewerBG.style.opacity = "0.80";
		viewerBG.style.filter = 'alpha(opacity=80)';
		viewerBG.style.backgroundColor = "#000000";

		// position and size clipping rect
		viewerRect.style.position = "absolute";
		viewerRect.style.left = document.body.clientWidth / 2 - 50 + "px";
		viewerRect.style.top = document.body.clientHeight / 2 - 50 + "px";
		viewerRect.style.width = "100px";
		viewerRect.style.height = "100px";
		viewerRect.style.opacity = "1.0";
		viewerRect.style.filter = "alpha(opacity=100)";
		viewerRect.style.backgroundColor = "#000000";

		// position image for hidden preloading
		viewerImg.style.position = "relative";
		viewerImg.style.left="-1024px";

	}
	function storeRectSize() {
		rectW = parseInt(viewerRect.style.width);
		rectH = parseInt(viewerRect.style.height);
	}
	function idle() {
		var timeOut = 20;
		var anim = new Animator();
		var obj1 = new MovingObject(viewerRect);
			obj1.loadMotionX(new Tween(	parseInt(viewerRect.style.left),
							parseInt(viewerRect.style.left),
							0,
							timeOut,
							new Spline(timeOut,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ revealImage() }));
		var objArray = [obj1];
		anim.init(0, 60);
		anim.importList(objArray);
		anim.initScene();
		anim.animate();
	}
	function revealImage() {
		if(viewerImg.complete != true) {
			idle();
		} else {
			viewerImg.style.opacity="0";
			viewerImg.style.filter="alpha(opacity=0)";
			viewerImg.style.left="0px";
			
			storeRectSize();
	
			var moveX = (parseInt(viewerImg.offsetWidth) - rectW) / 2;
			var moveY = (parseInt(viewerImg.offsetHeight) - rectH) / 2;
			var moveW = parseInt(viewerImg.offsetWidth) - rectW;
			var moveH = parseInt(viewerImg.offsetHeight) - rectH;
	
			var imgResize = 20;
	
			var fadeIn = 20;
	
			var anim = new Animator();
	
			var obj1 = new MovingObject(viewerRect);
				obj1.loadMotionX(new Tween(	parseInt(viewerRect.style.left),
								parseInt(viewerRect.style.left) - moveX,
								0,
								imgResize,
								new Spline(imgResize,
									[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
										{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
										{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
										{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
										{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
										{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
									null));
			var obj2 = new MovingObject(viewerRect);
				obj2.loadMotionY(new Tween(	parseInt(viewerRect.style.top),
								parseInt(viewerRect.style.top) - moveY,
								0,
								imgResize,
								new Spline(imgResize,
									[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
										{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
										{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
										{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
										{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
										{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
									null));
			var obj3 = new MovingObject(viewerRect);
				obj3.loadMotionW(new Tween(	parseInt(viewerRect.style.width),
								parseInt(viewerRect.style.width) + moveW,
								0,
								imgResize,
								new Spline(imgResize,
									[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
										{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
										{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
										{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
										{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
										{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
									null));
			var obj4 = new MovingObject(viewerRect);
				obj4.loadMotionH(new Tween(	parseInt(viewerRect.style.height),
								parseInt(viewerRect.style.height) + moveH,
								0,
								imgResize,
								new Spline(imgResize,
									[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
										{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
										{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
										{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
										{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
										{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
									null));
			var obj5 = new MovingObject(viewerImg);
				obj5.loadMotionA(new Tween(	0,
								100,
								imgResize,
								fadeIn,
								new Spline(fadeIn,
									[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
										{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
										{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
										{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
										{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
										{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
									function(){ allowClick() }));
			
			var objArray = [obj1, obj2, obj3, obj4, obj5];
			anim.init(0, 60);
			anim.importList(objArray);
			anim.initScene();
			anim.animate();
		}
	}

	function concealImage() {
		var moveX = (parseInt(viewerImg.offsetWidth) - rectW) / 2;
		var moveY = (parseInt(viewerImg.offsetHeight) - rectH) / 2;
		var moveW = parseInt(viewerImg.offsetWidth) - rectW;
		var moveH = parseInt(viewerImg.offsetHeight) - rectH;

		var fadeOut = 20;

		var imgResize = 20;

		var anim = new Animator();

		var obj1 = new MovingObject(viewerImg);
			obj1.loadMotionA(new Tween(	100,
							0,
							0,
							fadeOut,
							new Spline(fadeOut,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ preventClick() }));
		var obj2 = new MovingObject(viewerRect);
			obj2.loadMotionX(new Tween(	parseInt(viewerRect.style.left),
							parseInt(viewerRect.style.left) + moveX,
							fadeOut,
							imgResize,
							new Spline(imgResize,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								null));
		var obj3 = new MovingObject(viewerRect);
			obj3.loadMotionY(new Tween(	parseInt(viewerRect.style.top),
							parseInt(viewerRect.style.top) + moveY,
							fadeOut,
							imgResize,
							new Spline(imgResize,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								null));
		var obj4 = new MovingObject(viewerRect);
			obj4.loadMotionW(new Tween(	parseInt(viewerRect.style.width),
							parseInt(viewerRect.style.width) - moveW,
							fadeOut,
							imgResize,
							new Spline(imgResize,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								null));
		var obj5 = new MovingObject(viewerRect);
			obj5.loadMotionH(new Tween(	parseInt(viewerRect.style.height),
							parseInt(viewerRect.style.height) - moveH,
							fadeOut,
							imgResize,
							new Spline(imgResize,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ showNext() }));
		
		var objArray = [obj1, obj2, obj3, obj4, obj5];
		anim.init(0, 60);
		anim.importList(objArray);
		anim.initScene();
		anim.animate();
	}
	function allowClick() {
		viewerBG.onclick = function() { agImageViewer.closeImage() };
		viewerImg.onclick = function() { agImageViewer.nextImage() };
	}
	function preventClick() {
		viewerBG.onclick = null;
		viewerImg.onclick = null;
	}
	function closeImage() {
		destroy();
	}
	function nextImage() {
		concealImage();
	}
	function getNextImageFile() {
		deleteImage();

		var iNumber = Number(imageToLoad.substring(0,3)) + 1;
		var iPrefix = null;
		var iTemp = null;

		if(iNumber <= imageSetLength) {
			if(iNumber <= 99) {
				if(iNumber < 10) {
					iPrefix = "00";
				} else {
					iPrefix = "0";
				}
			} else {
				iPrefix = "";
			}
			iTemp = iPrefix + iNumber + ".jpg";

		} else {
			iTemp = "001.jpg";
		}

		imageToLoad = iTemp;

		var imgSrc = "images/" + imagePath + "/" + imageToLoad;

		restoreImage();
		viewerImg.src = imgSrc;
	}

	function destroy() {
		viewerImg = null;
		viewerRect = null;
		viewerBG = null;
		viewer.innerHTML = "";
		viewer.style.width = "0px";
		viewer.style.height = "0px";
	}
	function show() {
		buildViewer();
		getViewerElements();
		init();

		var fadeIn = 20;
		var waitForImage = 20;

		var anim = new Animator();

		var obj1 = new MovingObject(viewer);
			obj1.loadMotionA(new Tween(	0,
							100,
							0,
							fadeIn,
							new Spline(fadeIn,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ getImageFile() }));
		var obj2 = new MovingObject(viewer);
			obj2.loadMotionA(new Tween(	100,
							100,
							waitForImage,
							2,
							new Spline(2,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ revealImage() }));
		var objArray = [obj1, obj2];
		anim.init(0, 60);
		anim.importList(objArray);
		anim.initScene();
		anim.animate();
	}
	function showNext() {
		var getFile = 2;

		var waitForImage = 20;

		var anim = new Animator();

		var obj1 = new MovingObject(viewer);
			obj1.loadMotionA(new Tween(	100,
							100,
							0,
							getFile,
							new Spline(getFile,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ getNextImageFile() }));
		var obj2 = new MovingObject(viewer);
			obj2.loadMotionA(new Tween(	100,
							100,
							getFile,
							waitForImage,
							new Spline(waitForImage,
								[	{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.00,  y: 0.00,  c: 1.00,  m:null},
									{x: 0.40,  y: 0.20,  c: 0.50,  m:null},
									{x: 0.60,  y: 0.80,  c: 0.50,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null},
									{x: 1.00,  y: 1.00,  c: 1.00,  m:null}	]),
								function(){ revealImage() }));
		var objArray = [obj1, obj2];
		anim.init(0, 60);
		anim.importList(objArray);
		anim.initScene();
		anim.animate();
	}
	function loadImage(dir, img, max) {
		imagePath = dir;
		imageToLoad = img;
		imageSetLength = max;
		show();
	}
	function getImageFile() {
		var imgSrc = "images/" + imagePath + "/" + imageToLoad;
		viewerImg.src = imgSrc;
	}
	this._construct = _construct;
	this.loadImage = loadImage;
	this.nextImage = nextImage;
	this.closeImage = closeImage;

	_construct();
}