var lightbox = new lbControl();

// Gallery object
function lbGallery(WareWItem, WItemPhoto, ButtonAlts)
{
	// data
	this.WareID = 0;
	this.WItemID = 0;
	this.WareWItem = WareWItem;
	this.WareWItemPos = null;
	this.WItemPhoto = WItemPhoto;
	this.WItemPhotoPos = null;
	this.ButtonAlts = ButtonAlts;
	
	this.lbControl = null;
	
	// methods
	// init
	this.init = function(LbControl)
	{
		this.lbControl = LbControl;
		if(!this.WareWItemPos)
		{
			this.WareWItemPos = new Array();
			for( cWareID in this.WareWItem)
			{
				this.WareWItemPos[cWareID] = 0;
			}
		}
		if(!this.WItemPhotoPos)
		{
			this.WItemPhotoPos = new Array();
			for( cWItemID in this.WItemPhoto)
			{
				this.WItemPhotoPos[cWItemID] = 0;
			}
		}
	};
	
	// clean position pointers
	this.clear = function()
	{
		this.WareWItemPos = null;
		this.WItemPhotoPos = null;
		this.lbControl = null;
	};
	
	
	// next WItem photo
	this.nextPhoto = function()
	{	
		if(! (this.WItemPhotoPos[this.WItemID] == (this.WItemPhoto[this.WItemID].length-1)) )
		{
			this.WItemPhotoPos[this.WItemID]++;
		}
		this.redraw();
	};
	
	// next WItem 
	this.nextWItem = function()
	{
		if(! (this.WareWItemPos[this.WareID] == this.WareWItem[this.WareID].length-1) )
		{
			this.WareWItemPos[this.WareID]++;
			this.WItemID = this.WareWItem[this.WareID][this.WareWItemPos[this.WareID]];
		}
		this.redraw();
	};
	
	// previous WItem Photo
	this.prevPhoto = function() 
	{
		if(! (this.WItemPhotoPos[this.WItemID] == 0) )
		{
			this.WItemPhotoPos[this.WItemID]--;
		}
		this.redraw();
	};
	// previous WItem
	this.prevWItem = function()
	{
		if( ! (this.WareWItemPos[this.WareID] == 0) )
		{
			this.WareWItemPos[this.WareID]--;
			this.WItemID = this.WareWItem[this.WareID][this.WareWItemPos[this.WareID]];
		}		
		this.redraw();
	};
	// set default Ware and WItem
	this.setDefault = function(WareID, WItemID, PhotoID)
	{
		this.WareID = WareID;
		for(var i=0; i < this.WareWItem[this.WareID].length; i++)
		{
			if(this.WareWItem[this.WareID][i] == WItemID)
			{
				this.WareWItemPos[this.WareID] = i;
				this.WItemID = WItemID;
				for(var j=0; j < this.WItemPhoto[this.WItemID].length; j++)
				{
					if(this.WItemPhoto[this.WItemID][j] == PhotoID)
					{
						this.WItemPhotoPos[this.WItemID] = j;
					}
				}
				return; 
			}
		} 	
	};
	
	// change image src
	this.redraw = function()
	{
		this.redrawButtons();
		this.lbControl.changeImageSrc(this.WItemPhoto[this.WItemID][this.WItemPhotoPos[this.WItemID]]);
	}; 
	
	// show hide buttons
	this.redrawButtons = function()
	{
		if(this.WareWItemPos[this.WareID]==0) {
			PrevWItem = 0;
		} else {
			PrevWItem = 1;
		}
		if(this.WItemPhotoPos[this.WItemID]==0) {
			PrevPhoto = 0;
		} else {
			PrevPhoto = 1;
		}
		if(this.WareWItemPos[this.WareID] == this.WareWItem[this.WareID].length-1) {
			NextWItem = 0;
		} else {
			NextWItem = 1;
		}
		if(this.WItemPhotoPos[this.WItemID] == this.WItemPhoto[this.WItemID].length-1) {
			NextPhoto = 0;
		} else {
			NextPhoto = 1;
		}
		this.lbControl.redrawButtons(PrevWItem, PrevPhoto, NextWItem, NextPhoto);
	}; 
}


// control object
function lbControl()
{
	// settings
	this.div = 'lightbox';
	this.bgdiv = 'lb-background';
	this.img = 'lightboxphoto';
	this.scrollTop = 0;
	this.btnPrevWItem = 'lightboxBtnPrevWItem';
	this.btnPrevPhoto = 'lightboxBtnPrevPhoto';
	this.btnNextWItem = 'lightboxBtnNextWItem';
	this.btnNextPhoto = 'lightboxBtnNextPhoto';
	this.photoPath = '/photo.php?type=show&action=big&id=';
	this.blankImagePath = '/gfx/blank.gif';
	// current gallery
	this.gallery = null;
	
	// methods
	
	// show gallery
	this.show = function(lbGallery, WareID, WItemID, PhotoID, PrevItemAlt, PrevPhotoAlt, NextItemAlt, NextPhotoAlt)
	{
		if(typeof(lbGallery)=='undefined')
		{
			return false;
		}
		if( getElem(this.div) && getElem(this.img))
		{
			this.gallery = lbGallery;
			this.gallery.init(this);
			this.gallery.setDefault(WareID, WItemID, PhotoID);
			// background-height
			if( !oldIE() ) {
				getElem(this.bgdiv).style.height = getDocumentHeight()+'px';
			} else {
				this.scrollTop = document.documentElement.scrollTop;
				document.documentElement.scrollTop = 0;
			}
			// button alts
			this.setButtonAlts();
			// visible
			this.gallery.redraw();
			getElem(this.div).className = "visible";
		}
	};

	// hide gallery
	this.hide = function()
	{
		this.redrawButtons(0,0,0,0);
		if( getElem(this.div) && getElem(this.img))
		{
			this.setBlankImageSrc();
			getElem(this.div).className = "hidden";
			if( !oldIE() ) {
				getElem(this.bgdiv).style.height = '0px';
			} else {
				document.documentElement.scrollTop = this.scrollTop;
				this.scrollTop = 0;
			}
		}
		this.gallery.clear();
		this.gallery = null;
	};
	
	// navigation
	this.nextWItem = function()
	{
		this.setBlankImageSrc();
		this.gallery.nextWItem();
	};
	this.nextPhoto = function()
	{
		this.setBlankImageSrc();
		this.gallery.nextPhoto();
	};
	this.prevWItem = function()
	{
		this.setBlankImageSrc();
		this.gallery.prevWItem();
	};
	this.prevPhoto = function()
	{
		this.setBlankImageSrc();
		this.gallery.prevPhoto();
	};
	

	
	// button redraw
	this.redrawButtons = function (PrevWItem, PrevPhoto, NextWItem, NextPhoto)
	{
		if(PrevWItem==1) {
			this.showBtnPrevWItem();
		} else {
			this.hideBtnPrevWItem();
		}
		if(PrevPhoto==1) {
			this.showBtnPrevPhoto();
		} else {
			this.hideBtnPrevPhoto();
		}
		if(NextWItem==1) {
			this.showBtnNextWItem();
		} else {
			this.hideBtnNextWItem();
		}
		if(NextPhoto==1) {
			this.showBtnNextPhoto();
		} else {
			this.hideBtnNextPhoto();
		}
	};
	
	this.setButtonAlts = function ()
	{
		getElem(this.btnPrevWItem).alt = this.gallery.ButtonAlts.PrevItemAlt;
		getElem(this.btnPrevWItem).title = this.gallery.ButtonAlts.PrevItemAlt;
		getElem(this.btnPrevPhoto).alt = this.gallery.ButtonAlts.PrevPhotoAlt;
		getElem(this.btnPrevPhoto).title = this.gallery.ButtonAlts.PrevPhotoAlt;
		getElem(this.btnNextWItem).alt = this.gallery.ButtonAlts.NextItemAlt;
		getElem(this.btnNextWItem).title = this.gallery.ButtonAlts.NextItemAlt;
		getElem(this.btnNextPhoto).alt = this.gallery.ButtonAlts.NextPhotoAlt;
		getElem(this.btnNextPhoto).title = this.gallery.ButtonAlts.NextPhotoAlt;
	};
	
	
	// change photo
	this.changeImageSrc = function (PhotoID)
	{
		getElem(this.img).src = this.photoPath+PhotoID;
		getElem(this.img).style.display = 'inline';
	};
	
	// change photo to blank
	this.setBlankImageSrc = function ()
	{
		getElem(this.img).src = this.blankImagePath;
		getElem(this.img).style.display = 'none';
	};
	
	
	// show hide buttons
	this.showBtnPrevWItem = function()
	{
		showElem(getElem(this.btnPrevWItem));
	};
	this.hideBtnPrevWItem = function()
	{
		hideElem(getElem(this.btnPrevWItem));
	};
	this.showBtnPrevPhoto = function()
	{
		showElem(getElem(this.btnPrevPhoto));
	};
	this.hideBtnPrevPhoto = function()
	{
		hideElem(getElem(this.btnPrevPhoto));
	};
	this.showBtnNextWItem = function()
	{
		showElem(getElem(this.btnNextWItem));
	};
	this.hideBtnNextWItem = function()
	{
		hideElem(getElem(this.btnNextWItem));
	};
	this.showBtnNextPhoto = function()
	{
		showElem(getElem(this.btnNextPhoto));
	};
	this.hideBtnNextPhoto = function()
	{
		hideElem(getElem(this.btnNextPhoto));
	};
}