// JavaScript Document

function clsLightBox(Dir)
	{
	clsLightBox.prototype.Dir = Dir;
	}

clsLightBox.prototype = {
	Booted			: false,
	DefWidth		: 500,
	DefHeight		: 500,
	Margin			: 25,
	Padding			: 25,
	
	BoxWidth		: this.DefWidth + (2 * this.Padding),
	BoxHeight		: this.DefHeight + (2 * this.Padding),
	FileList		: new Array(),
	DefaultImage	: '/img/product/no_image.jpg',

	DisplayTitle	: false,
	TitleHeight		: 35,
	DisplayButtons	: true,
	ButtonsHeight	: 50,
	
	ImgDirSelf		: '/img/lightbox/',
	ImgExit			: 'exit_wr.gif',
	ImgBack			: 'back_wr.gif',
	ImgNext			: 'next_wr.gif',
	
	FactoryName		: 0
	}

clsLightBox.prototype.SetWindowHeight = function()
	{
	clsLightBox.prototype.WindowHeight 	= window.innerHeight;

	if (!isInteger(this.WindowHeight)) { this.WindowHeight = document.body.clientHeight }
	if (!isInteger(this.WindowHeight)) { this.WindowHeight = document.documentElement.clientHeight; }
	if (!isInteger(this.WindowHeight)) { this.WindowHeight = this.DefHeight + (2 * this.Margin) + 2 * (this.Padding); }
	}

clsLightBox.prototype.GetWindowHeight = function()
	{
	return this.WindowHeight;
	}
	
clsLightBox.prototype.SetWindowWidth = function()
	{
	clsLightBox.prototype.WindowWidth 	= window.innerWidth;

	if (!isInteger(this.WindowWidth)) { this.WindowWidth = document.body.clientWidth }
	if (!isInteger(this.WindowWidth)) { this.WindowWidth = document.documentElement.clientWidth; }
	if (!isInteger(this.WindowWidth)) { this.WindowWidth = this.DefWidth + (2 * this.Margin) + 2 * (this.Padding); }
	}

clsLightBox.prototype.GetWindowWidth = function()
	{
	return this.WindowWidth;
	}

clsLightBox.prototype.SetDir = function (Dir)
	{
	clsLightBox.prototype.Dir = Dir;
	}
	
clsLightBox.prototype.BootClass = function ()
	{
	if (!clsLightBox.prototype.Booted)
		{
		clsLightBox.prototype.SetWindowHeight();		
		clsLightBox.prototype.SetWindowWidth();		
		clsLightBox.prototype.Booted = true;
		}
			
	clsLightBox.prototype.InsertDOM_LightBox();		
	clsLightBox.prototype.objBackdrop.style.display	= 'block';
	clsLightBox.prototype.objLightBox.style.display = 'block';
	clsLightBox.prototype.objImagePop.style.display = 'block';
	}

	
clsLightBox.prototype.InsertDOM_LightBox = function ()
	{		
	if (!document.getElementById('Backdrop'))
		{
		clsLightBox.prototype.objBackdrop = document.createElement('div');
		clsLightBox.prototype.objBackdrop.setAttribute('id', 'Backdrop');
		document.body.appendChild(clsLightBox.prototype.objBackdrop);		
		}
	else
		{
		clsLightBox.prototype.objBackdrop 	= document.getElementById('Backdrop');
		}
		
	if (!document.getElementById('LightBox'))
		{
		clsLightBox.prototype.objLightBox = document.createElement('div');
		clsLightBox.prototype.objLightBox.setAttribute('id', 'LightBox');
		document.body.appendChild(clsLightBox.prototype.objLightBox);
		}
	else
		{
		clsLightBox.prototype.objLightBox 	= document.getElementById('LightBox');
		}		

	if (!document.getElementById('ImageBoxWrap'))
		{
		clsLightBox.prototype.objWrapBox = document.createElement('div');
		clsLightBox.prototype.objWrapBox.setAttribute('id','ImageBoxWrap');
		clsLightBox.prototype.objLightBox.appendChild(clsLightBox.prototype.objWrapBox);
		}
	else
		{
		clsLightBox.prototype.objWrapBox 	= document.getElementById('objWrapBox');
		}
		

	if (!document.getElementById('ImageBox'))
		{
		clsLightBox.prototype.objImageBox = document.createElement('div');
		clsLightBox.prototype.objImageBox.setAttribute('id', 'ImageBox');
		clsLightBox.prototype.objWrapBox.appendChild(clsLightBox.prototype.objImageBox);
		}
	else
		{
		clsLightBox.prototype.objImageBox 	= document.getElementById('ImageBox');		
		}
		

	if (!document.getElementById('ImagePop'))
		{
		clsLightBox.prototype.objImagePop = document.createElement('img');
		clsLightBox.prototype.objImagePop.setAttribute('id', 'ImagePop');
		clsLightBox.prototype.objImageBox.appendChild(clsLightBox.prototype.objImagePop);		
		}
	else
		{
		clsLightBox.prototype.objImagePop 	= document.getElementById('ImagePop');
		}
	}

clsLightBox.prototype.PadImage = function()
	{
	if (clsLightBox.prototype.FileList.length == 0)
		{
		clsLightBox.prototype.FileList[0] = clsLightBox.prototype.DefaultImage;
		}
	}
	
clsLightBox.prototype.GalleryFactory = function()
	{
	clsLightBox.prototype.GetTitle 	= function() 
		{
		}
		
	clsLightBox.prototype.FetchFiles = function(Source)
		{
		if (!Source || Source == '') Source = 'filePHP';
			
		if (Source == 'filePHP' && clsLightBox.prototype.Dir)
			{
			var FileString 		= FetchFileAsTxt('/js/pLightbox.php?dir=' + clsLightBox.prototype.Dir + '&ID=' + clsLightBox.prototype.ImageInfo);
			var Files			= FileString.split('@split@');
				
			if (Files.length > 0)
				{
				for (i = 0; i < Files.length; i++)
					{
					Files[1];
					clsLightBox.prototype.FileList[i] = Files[i];
						
					if (clsLightBox.prototype.FileList[i] == clsLightBox.prototype.ImageInfo)
						{
						clsLightBox.prototype.FileCurrentKey = i;
						}
					}
				//alert('test ' + clsLightBox.prototype.ImageInfo + ' ' + clsLightBox.prototype.FileCurrentKey);


				if (clsLightBox.prototype.FileCurrentKey > 0) clsLightBox.prototype.ImageInfo = clsLightBox.prototype.FileList[clsLightBox.prototype.FileCurrentKey];
				else 
					{
					clsLightBox.prototype.FileCurrentKey 	= 0;
					clsLightBox.prototype.ImageInfo 		= clsLightBox.prototype.FileList[0];
					}				
				}
			}
			
		clsLightBox.prototype.PadImage();
		}
	}

clsLightBox.prototype.SetFactory = function ()
	{
	clsLightBox.prototype.GalleryFactory();
	clsLightBox.prototype.FactoryName = 'Gallery';
	}

clsLightBox.prototype.GetFactory = function()
	{
	return clsLightBox.prototype.FactoryName;
	}

clsLightBox.prototype.BuildLightBox = function(ImageInfo)
	{
	clsLightBox.prototype.BootClass();
	clsLightBox.prototype.ImageInfo = ImageInfo;
	clsLightBox.prototype.SetFactory();		
	clsLightBox.prototype.FetchFiles('');
	clsLightBox.prototype.SetLightBox();
	}	

clsLightBox.prototype.SetLightBox = function ()
	{
	//alert(clsLightBox.prototype.ImageInfo);

	clsLightBox.prototype.objImagePop.style.display = 'none';
	clsLightBox.prototype.objImage 					= new Image();
	
	clsLightBox.prototype.objImage.onload = function () 
		{
		clsLightBox.prototype.objImagePop.src = clsLightBox.prototype.objImage.src;	
		
		clsLightBox.prototype.SetButtonBox();
		clsLightBox.prototype.SetTitleBox();
		
		// FETCH THE IMAGE WIDTH & HEIGHT
		var Y_Base			= this.height;
		var X_Base			= this.width;
		
		// CALCULATE THE NEW IMAGE WITH AND HEIGHT
		if (X_Base > Y_Base)
			{
			//alert('INFO FROM PARENT ' + clsLightBox.prototype.WindowWidth + ' ' + clsLightBox.prototype.Margin + ' ' + clsLightBox.prototype.Padding);
			
			var X_Image = clsLightBox.prototype.WindowWidth - (2 * clsLightBox.prototype.Margin) - (2 * clsLightBox.prototype.Padding);
			var Y_Image	= Math.round(Y_Base * (X_Image / X_Base));
			var Y_Space	= clsLightBox.prototype.WindowHeight - (clsLightBox.prototype.Padding * 2) - (clsLightBox.prototype.Margin * 2) - clsLightBox.prototype.ButtonsHeight - clsLightBox.prototype.TitleHeight
			
			//alert('CALCULATE 1 ' + X_Base + ' ' + Y_Base + ' ' + X_Image + ' ' + Y_Image);
			if (Y_Image > Y_Space)
				{
				var Y_Image = clsLightBox.prototype.WindowHeight - (2 * clsLightBox.prototype.Margin) - (2 * clsLightBox.prototype.Padding) - clsLightBox.prototype.ButtonsHeight - clsLightBox.prototype.TitleHeight;
				var X_Image	= Math.round(X_Base * (Y_Image / Y_Base));
				}	
				
			//alert('CALCULATE 2 ' + X_Base + ' ' + Y_Base + ' ' + X_Image + ' ' + Y_Image);
			}
		else
			{
			var Y_Image = clsLightBox.prototype.WindowHeight - (2 * clsLightBox.prototype.Margin) - (2 * clsLightBox.prototype.Padding) - clsLightBox.prototype.ButtonsHeight - clsLightBox.prototype.TitleHeight;
			var X_Image	= Math.round(X_Base * (Y_Image / Y_Base));
			}
				
		// WE DON'T DO UPSIZING
		if (X_Image > X_Base || Y_Image > Y_Base)
			{
			X_Image = X_Base;
			Y_Image = Y_Base;
			}				
				
		//alert(X_Image + ' ' + Y_Image);
				
		clsLightBox.prototype.objImagePop.style.width			= X_Image + 'px'; 
		clsLightBox.prototype.objImagePop.style.height			= Y_Image + 'px';
		clsLightBox.prototype.objImagePop.style.padding			= clsLightBox.prototype.Padding + 'px';
				
		clsLightBox.prototype.objImageBox.style.width			= (X_Image + (clsLightBox.prototype.Padding * 2)) + 'px'; 
		clsLightBox.prototype.objImageBox.style.height			= (Y_Image + (clsLightBox.prototype.Padding * 2)) + 'px';
		clsLightBox.prototype.objImageBox.style.top				= '-' + ((Y_Image / 2) + clsLightBox.prototype.Padding + (clsLightBox.prototype.ButtonsHeight / 2) - (clsLightBox.prototype.TitleHeight / 2)) + 'px';
		clsLightBox.prototype.objImageBox.style.paddingBottom 	= clsLightBox.prototype.ButtonsHeight + 'px';
				
		clsLightBox.prototype.objImagePop.style.display 		= 'block';
		}
	
	clsLightBox.prototype.objImage.src = clsLightBox.prototype.ImageInfo;
	}	

clsLightBox.prototype.SetButtonBox = function()
	{
	if (!clsLightBox.prototype.DisplayButtons)	clsLightBox.prototype.ButtonsHeight = 0;
	else
		{
		if (!document.getElementById('ButtonBox'))
			{
			var objButtonBox = document.createElement('div');
			objButtonBox.setAttribute('id', 'ButtonBox');
			clsLightBox.prototype.objImageBox.appendChild(objButtonBox);	
			}
		else
			{
			var objButtonBox = document.getElementById('ButtonBox');
			}

		objButtonBox.style.position 	= 'absolute';
		objButtonBox.style.bottom		= '15px';
		objButtonBox.style.width		= '100%';
		objButtonBox.style.textAlign	= 'center'; //CloseImageBox();	
		objButtonBox.innerHTML 			= ' ';
		
		//alert(clsLightBox.prototype.FileList.length);

		if (clsLightBox.prototype.FileList.length > 1 && clsLightBox.prototype.FileCurrentKey == 0) 	
			{
			var ImagePrev = clsLightBox.prototype.ImgDirSelf + 'empty.gif';
			var ClickPrev = ' ';
			}
		else if (clsLightBox.prototype.FileList.length > 1)
			{
			var ImagePrev = clsLightBox.prototype.ImgDirSelf + clsLightBox.prototype.ImgBack;		
			var ClickPrev = 'LightBox.ImagePrev()';
			}
		else
			{
			var ImagePrev = clsLightBox.prototype.ImgDirSelf + 'empty.gif';;
			var ClickPrev = '';
			}

		if (clsLightBox.prototype.FileList.length > 1 && clsLightBox.prototype.FileCurrentKey == (clsLightBox.prototype.FileList.length - 1))
			{
			var ImageNext = clsLightBox.prototype.ImgDirSelf + 'empty.gif';
			var ClickNext = ' ';
			}
		else if (clsLightBox.prototype.FileList.length > 1)
			{
			var ImageNext = clsLightBox.prototype.ImgDirSelf + clsLightBox.prototype.ImgNext;
			var ClickNext ='LightBox.ImageNext()';
			}	
		else
			{
			var ImageNext = clsLightBox.prototype.ImgDirSelf + 'empty.gif';;
			var ClickNext =	'';
			}
		
		objButtonBox.innerHTML			= '<span class="LightBoxButton"><img src="' + ImagePrev + '" alt="" onClick="' + ClickPrev + '" /></span><span class="LightBoxButton"><img src="' + clsLightBox.prototype.ImgDirSelf + clsLightBox.prototype.ImgExit + '" alt="" onClick="LightBox.CloseBox()" /></span><span class="LightBoxButton"><img src="' + ImageNext + '" alt="" onClick="' + ClickNext + '"></span>';		
		}
	
	//alert(objButtonBox.innerHTML);
	}

clsLightBox.prototype.SetTitleBox = function()
	{
	if (!clsLightBox.prototype.DisplayTitle) clsLightBox.prototype.TitleHeight = 0;
	}	

clsLightBox.prototype.ImagePrev = function()
	{
	clsLightBox.prototype.FileCurrentKey--;
	if (clsLightBox.prototype.FileCurrentKey < 0) clsLightBox.prototype.FileCurrentKey = 0;
	
	clsLightBox.prototype.ImageInfo = clsLightBox.prototype.FileList[clsLightBox.prototype.FileCurrentKey];
	clsLightBox.prototype.BuildLightBox(clsLightBox.prototype.ImageInfo);	
	}

clsLightBox.prototype.ImageNext = function()
	{
	clsLightBox.prototype.FileCurrentKey++;
	if (clsLightBox.prototype.FileCurrentKey > (clsLightBox.prototype.FileList.length - 1)) clsLightBox.prototype.FileCurrentKey = (clsLightBox.prototype.FileList.length - 1);
	
	clsLightBox.prototype.ImageInfo = clsLightBox.prototype.FileList[clsLightBox.prototype.FileCurrentKey];
	clsLightBox.prototype.BuildLightBox(clsLightBox.prototype.ImageInfo);
	}

clsLightBox.prototype.CloseBox	= function()
	{
	clsLightBox.prototype.objBackdrop.style.display = 'none';
	clsLightBox.prototype.objLightBox.style.display = 'none';
	clsLightBox.prototype.objImagePop.style.display = 'none';
	}

clsLightBox.prototype.FishBoxMove = function(Action,Page)
	{
	var objFishBox = document.getElementById('FishBox');
	clsLightBox.prototype.Page = Page;
	
	if (Action == 'p') Page--;
	if (Action == 'n') Page++;
	
	var FileString 			= FetchFileAsTxt('/js/pFishbox.php?Page=' + Page);
	objFishBox.innerHTML	= FileString;
	}

