/////////////////////////////////////////////////////
//	NGalleryStaticClassStaticClass object
//
function NGalleryStaticClass()
{
	//	Initialization
	NGalleryStaticClass.prototype.Initialize = function()
	{
		NEventSinkService.BodyLoaded.Subscribe(this.initDefaultImages);

		this.leftLightArrowImg = new Image(5);
		this.leftLightArrowImg.src = "gallery/FullGalleries/galleryLeftLightArrow.gif";

		this.leftDarkArrowImg = new Image(5);
		this.leftDarkArrowImg.src = "gallery/FullGalleries/galleryLeftDarkArrow.gif";

		this.rightLightArrowImg = new Image(5);
		this.rightLightArrowImg.src = "gallery/FullGalleries/galleryRightLightArrow.gif";

		this.rightDarkArrowImg = new Image(5);
		this.rightDarkArrowImg.src = "gallery/FullGalleries/galleryRightDarkArrow.gif";
	}

	//	Operarations
	NGalleryStaticClass.prototype.RegisterZoomSlot = function(zoomSlotId, defaultThumbId)
	{
		this.defaultImagesArray.push(defaultThumbId);
		this.zoomSlotsArray.push(zoomSlotId);
	}
	
	NGalleryStaticClass.prototype.RegisterThumbnail = function(thumbId)
	{
		this.allThumbnailsArray.push(thumbId);
	}
	
	NGalleryStaticClass.prototype.OnArrowHover = function(imgId, makeDark)
	{
		var arrowImg = document.getElementById(imgId);
		if(imgId.indexOf("Left") != -1)
		{
			var textSpan = document.getElementById("galleryLeftNavSpan");
			if(makeDark)
			{
				arrowImg.src = this.leftDarkArrowImg.src;
				textSpan.className = "galleryNavTextLeftDark";
			}
			else
			{
				arrowImg.src = this.leftLightArrowImg.src;
				textSpan.className = "galleryNavTextLeft";
			}
			return;
		}
			
		var textSpan = document.getElementById("galleryRightNavSpan");
		if(makeDark)
		{
			arrowImg.src = this.rightDarkArrowImg.src;
			textSpan.className = "galleryNavTextRightDark";
		}
		else
		{
			arrowImg.src = this.rightLightArrowImg.src;
			textSpan.className = "galleryNavTextRight";
		}
	}

	//	Tools
	NGalleryStaticClass.prototype.updateNavText = function(imgId, makeDark)
	{
		var leftLink = document.getElementById("galleryLeftArrowLink");
		var rightLink = document.getElementById("galleryRightArrowLink");

		var textSpan = document.getElementById("galleryNavTextSpan");
		if(!makeDark)
		{
			var html = "";
			html += "<table width='100%'><tr><td class='galleryNavTextLeft'>previous gallery</td><td class='galleryNavTextRight'>next gallery</td></tr></table>"
			textSpan.innerHTML = html;
			return;
		}
		
		var className = "galleryNavTextDarkRight";
		if(imgId.indexOf("Left") != -1)
			className = "galleryNavTextDarkLeft";
		
		var html = "";
		html += "<table width='100%'><tr><td class='" + className + "'>" + text + "</td></tr></table>";
		textSpan.innerHTML = html;
	}
	
	NGalleryStaticClass.prototype.loadImage = function(thumbSlotName, zoomSlotName)
	{
		var thumbSlotImg = document.getElementById(thumbSlotName);
		var zoomSlotImg = document.getElementById(zoomSlotName);
		
		var path = thumbSlotImg.src;
		
		var regexpPath = new RegExp(this.thumbsPath, "i");
		var regexpExt = new RegExp(this.thumbsExt, "i");
		
		path = path.replace(regexpPath, this.zoomedPath);
		path = path.replace(regexpExt, this.zoomedExt);
			
		var validationImg = document.createElement('img');
		validationImg.style.width = NBrowserCaps.ToPixelLength(this.thumbWidth);
		validationImg.style.height = NBrowserCaps.ToPixelLength(this.thumbHeight);
		
		validationImg.src = path;
		
		if(! validationImg.complete)
		{
			zoomSlotImg.src = this.loadingPath;
	
			var newImg = document.createElement('img');
			newImg.style.width = NBrowserCaps.ToPixelLength(this.thumbWidth);
			newImg.style.height = NBrowserCaps.ToPixelLength(this.thumbHeight);
			if(this.loadingImagePerZoomSlotArray[zoomSlotImg.id] != null &&
				this.loadingImagePerZoomSlotArray[zoomSlotImg.id] != "undefined")
				newImg = this.loadingImagePerZoomSlotArray[zoomSlotImg.id];
			
			this.loadingImagesObjArray[path] = newImg;
			this.loadingImagePerZoomSlotArray[zoomSlotImg.id] = newImg;
	
			this.loadingImagesArray[path] = zoomSlotName;
	
			newImg.onload = this.onImageLoaded;
			newImg.src = path;
	
			if(newImg.complete)
			{
				zoomSlotImg.src = path;
				this.loadingImagePerZoomSlotArray[zoomSlotImg.id] = null;
			}
		}
		else
		{
			zoomSlotImg.src = path;
		}
	
		this.showText(thumbSlotName, zoomSlotName);
	}

	NGalleryStaticClass.prototype.animateFrame = function(thumbSlotName, zoomSlotName)
	{
		var thumbSlot = document.getElementById(thumbSlotName + "div");
		var zoomSlot = document.getElementById(zoomSlotName + "div");
		
		var thumbSlotOffset = NBrowserCaps.GetOffset(thumbSlot);
		this.startX = thumbSlotOffset.offsetLeft;
		this.startY = thumbSlotOffset.offsetTop;
		this.startW = thumbSlot.offsetWidth;
		this.startH = thumbSlot.offsetHeight;
		
		var zoomSlotOffset = NBrowserCaps.GetOffset(zoomSlot);
		this.endX = zoomSlotOffset.offsetLeft;
		this.endY = zoomSlotOffset.offsetTop;
		this.endW = zoomSlot.offsetWidth;
		this.endH = zoomSlot.offsetHeight;
		
		this.movingFrame.style.display = "block";
	
		this.movingFrame.style.left = NBrowserCaps.ToPixelLength(this.startX);
		this.movingFrame.style.top = NBrowserCaps.ToPixelLength(this.startY);
		
		this.movingFrame.style.width = NBrowserCaps.ToPixelLength(this.startW);
		this.movingFrame.style.height = NBrowserCaps.ToPixelLength(this.startH);
	
		var movingFrameOffset = NBrowserCaps.GetOffset(this.movingFrame);
		
		this.movingFrame.style.borderStyle = "solid";
		
		this.xStep = (this.endX - this.startX) / this.frameIterationsCount;
		this.yStep = (this.endY - this.startY) / this.frameIterationsCount;
		this.wStep = (this.endW - this.startW) / this.frameIterationsCount;
		this.hStep = (this.endH - this.startH) / this.frameIterationsCount;
	
		this.frameIterator = 0;
		
		if(this.timerIsRunning == true)
		{
			clearInterval(this.timerID);
			this.timerIsRunning = false;
		}
		
		this.timerID = setInterval("NGallery.moveFrame('" + thumbSlotName + "', '" + zoomSlotName + "')", this.frameIterationTimeout);
		this.timerIsRunning = true;
	}

	NGalleryStaticClass.prototype.markSelected = function(thumbSlotName, zoomSlotName)
	{
		for(var i = 0; i < this.allThumbnailsArray.length; i ++)
		{
			var thumbSlotId = this.allThumbnailsArray[i];
			var thumbSlot = document.getElementById(thumbSlotId + "div");
			if( thumbSlotId.indexOf(zoomSlotName) != -1)
			{
				thumbSlot.className = "thumbnailDiv";
			}
		}
		
		var slectedSpan = document.getElementById(thumbSlotName + "div");
		slectedSpan.className = "thumbnailSelectedDiv";
	}

	NGalleryStaticClass.prototype.enableThumbnailClick = function()
	{
		for(var i = 0; i < this.allThumbnailsArray.length; i ++)
		{
			var thumbSlotSpan = document.getElementById(this.allThumbnailsArray[i]);
			thumbSlotSpan.className = "thumbnailDiv";
		}
	}

	NGalleryStaticClass.prototype.showText = function(thumbSlotName, zoomSlotName)
	{
		var textSrcSlotName = thumbSlotName + this.textSlotExtention;
		var textDestSlotName = zoomSlotName + this.textSlotExtention;
		var titleSrcSlotName = thumbSlotName + this.titleSlotExtention;
		var titleDestSlotName = zoomSlotName + this.titleSlotExtention;
	
		var thumbSlotSpan = document.getElementById(textSrcSlotName);
		var zoomSlotElement = document.getElementById(textDestSlotName);
		var titleThumbSlotElement = document.getElementById(titleSrcSlotName);
		var titleZoomSlotElement = document.getElementById(titleDestSlotName);
		
		if(titleThumbSlotElement != null && titleZoomSlotElement != null)
			titleZoomSlotElement.innerHTML = titleThumbSlotElement.innerHTML;
		else if(titleZoomSlotElement != null)
			titleZoomSlotElement.innerHTML = "";
		
		if(thumbSlotSpan != null && zoomSlotElement != null)
			zoomSlotElement.innerHTML = thumbSlotSpan.innerHTML;
		else if(zoomSlotElement != null)
			zoomSlotElement.innerHTML = "";
	}

	//	Event Handlers
	NGalleryStaticClass.prototype.onImageLoaded = function()
	{
		var zoomSlotImg = document.getElementById(NGallery.loadingImagesArray[this.src]);
		NGallery.loadingImagePerZoomSlotArray[zoomSlotImg.id] = null;
		zoomSlotImg.src = this.src;
	}

	NGalleryStaticClass.prototype.initDefaultImages = function()
	{
		NGallery.movingFrame = NBrowserCaps.AddBox(document.body, 0, 0, 1, 1, "movingFrame");
		
		NGallery.galleryLoaded = true;
		NGallery.enableThumbnailClick();
		
		for(var i = 0; i < NGallery.zoomSlotsArray.length; i ++)
		{
			NGallery.loadImage(NGallery.defaultImagesArray[i], NGallery.zoomSlotsArray[i]);
			NGallery.markSelected(NGallery.defaultImagesArray[i], NGallery.zoomSlotsArray[i]);
		}
	}
	
	NGalleryStaticClass.prototype.moveFrame = function(thumbSlotName, zoomSlotName)
	{
		if(NGallery.frameIterator >= NGallery.frameIterationsCount - 1 && NGallery.timerIsRunning == true)
		{
			clearInterval(this.timerID);
			NGallery.timerIsRunning = false;
			
			NGallery.movingFrame.style.display = "none";
			NGallery.loadImage(thumbSlotName, zoomSlotName);
		}
		else
		{
			var movingFrameOffset = NBrowserCaps.GetOffset(NGallery.movingFrame);
			NGallery.movingFrame.style.left = NBrowserCaps.ToPixelLength(movingFrameOffset.offsetLeft + NGallery.xStep);
			NGallery.movingFrame.style.top = NBrowserCaps.ToPixelLength(movingFrameOffset.offsetTop + NGallery.yStep);
			
			NGallery.movingFrame.style.width = NBrowserCaps.ToPixelLength(NGallery.movingFrame.offsetWidth + NGallery.wStep);
			NGallery.movingFrame.style.height = NBrowserCaps.ToPixelLength(NGallery.movingFrame.offsetHeight + NGallery.hStep);
		}
	
		NGallery.frameIterator ++;
	}

	NGalleryStaticClass.prototype.ThumbClicked = function(thumbSlotName, zoomSlotName)
	{
		if(!NGallery.galleryLoaded)
			return;
			
		NGallery.animateFrame(thumbSlotName, zoomSlotName);
		NGallery.markSelected(thumbSlotName, zoomSlotName);
	}

	//	Fields
	this.length += 16;
	this.thumbsPath = "thumbs";
	this.thumbsExt = "jpg";
	this.zoomedPath = "images";
	this.zoomedExt = "png";
	
	this.thumbWidth = 110;
	this.thumbHeight = 82;
	
	this.loadingPath = "gallery/FullGalleries/imageload.gif";
	this.loadingOnStartupPath = "gallery/FullGalleries/imageload-onstartup.gif";
	
	this.textSlotExtention = "_txt";
	this.titleSlotExtention = "_title";
	
	this.loadingImagesArray = new Array();
	this.loadingImagesObjArray = new Array();
	this.loadingImagePerZoomSlotArray = new Array();
	this.allThumbnailsArray = new Array();
	this.defaultImagesArray = new Array();
	this.zoomSlotsArray = new Array();
	
	this.galleryLoaded = false;
	this.movingFrame = null;

	//	animation state
	this.length += 22;
	this.frameIterator = 0;
	this.frameIterationsCount = 14;
	this.frameIterationTimeout = 12;
	this.timerID = null;
	this.timerIsRunning = false;
	
	this.xStep = 0;
	this.yStep = 0;
	this.wStep = 0;
	this.hStep = 0;
			
	this.startX = 0;
	this.startY = 0;
	this.startW = 0;
	this.startH = 0;
	
	this.endX = 0;
	this.endY = 0;
	this.endW = 0;
	this.endH = 0;
	
	this.leftLightArrowImg = new Image(5);
	this.leftDarkArrowImg = new Image(5);
	this.rightLightArrowImg = new Image(5);
	this.rightDarkArrowImg = new Image(5);

	//	Initialize object
	this.Initialize();
}
//	Inheritance
var NGallery = new NGalleryStaticClass();
