﻿//=================================================================================================
//	TLIGHTBOX class
//=================================================================================================
TLightBox = function( name, zIndex, top, withImage )
{
	var
		FAutoMove  = true, 
		FElement   = null, 
		FIndex     = lightBoxes.lightBoxes.length, 
		FLeft      = 100, 
		FName      = name, 
		FTop       = 50, 
		FVisible   = false, 
		FZIndex    = 1001 + FIndex, 
		FWithImage = withImage;
	
	if ( typeof( withImage ) == "undefined")
		FWithImage = true;
	
	if ( typeof( zIndex ) != "undefined")
		FZIndex = parseInt( zIndex );
	
	if ( typeof( top ) != "undefined")
		FTop = parseInt( top );
	
	lightBoxes.lightBoxes[FIndex] = this;
	
	//----------------------------------------------------------------
	this.pAllHidden = function()
	{
		for ( var i = 0; i < lightBoxes.lightBoxes.length; i++ )
		{
			var
				lightbox = lightBoxes.lightBoxes[i];
			
			if ( lightbox.pVisible() && lightbox.pWithImage() )
				return false;
		}
		
		return true;
	}
	//----------------------------------------------------------------
	this.pAutoMove = function( value )
	{
		if ( typeof( value ) == "undefined")
			return FAutoMove;
		else
			FAutoMove = Boolean( value );
	}
	//----------------------------------------------------------------
	this.pDoFrame = function()
	{
		if ( this.iframe )
		{
			this.iframe.style.left   = FElement.offsetLeft   + "px";
			this.iframe.style.width  = FElement.offsetWidth  + "px";
			this.iframe.style.height = FElement.offsetHeight + "px";
			
			if ( FAutoMove )
				positionElement( this.iframe, FTop );
			else
				this.iframe.style.top = FElement.offsetTop + "px";
		}
	}
	//----------------------------------------------------------------
	this.pElement = function( value )
	{
		if ( typeof( value ) == "undefined")
			return FElement;
		else 
			FElement = value;
	}
	//----------------------------------------------------------------
	this.pGetRef = function()
	{
		return "lightBoxes.lightBoxes[" + FIndex + "]";
	}
	//----------------------------------------------------------------
	this.pIndex = function()
	{
		return FIndex;
	}
	//----------------------------------------------------------------
	this.pLeft = function( value )
	{
		if ( typeof( value ) == "undefined")
			return FLeft;
		else
		{
			FLeft               = parseInt( value );
			FElement.style.left = FLeft + "px";
			
			this.pDoFrame();
		}
	}
	//----------------------------------------------------------------
	this.pName = function()
	{
		return FName;
	}
	//----------------------------------------------------------------
	this.pSize = function()
	{
		if ( FElement == null )
			return null;
		
		int
			width  = FElement.offsetWidth,
			height = FElement.offsetHeight;
		
		return new Size( width, height );
	}
	//----------------------------------------------------------------
	this.pTop = function( value )
	{
		if ( typeof( value ) == "undefined")
			return FTop;
		else
		{
			FTop = parseInt( value );
			
			if ( FAutoMove )
				positionElement( FElement, FTop );
			else
				FElement.style.top = FTop + "px";
			
			this.pDoFrame();
		}
	}
	//----------------------------------------------------------------
	this.pVisible = function( value )
	{
		if ( typeof( value ) == "undefined")
			return FVisible;
		else
			FVisible = value;
	}
	//----------------------------------------------------------------
	this.pWithImage = function()
	{
		return FWithImage;
	}
	//----------------------------------------------------------------
	this.pZIndex = function()
	{
		return FZIndex;
	}
	//----------------------------------------------------------------
	
	if ( bodyLoaded )
		this.init();
}
//=================================================================================================
TLightBox.prototype.autoMove = function( value )
{
	return this.pAutoMove( value );
}
//=================================================================================================
TLightBox.prototype.element = function()
{
	if ( !this.pElement() )
		this.init();
	
	return this.pElement();
}
//=================================================================================================
TLightBox.prototype.getHtml = function( url )
{
	if ( this.pElement() )
		return getHtml( this.pElement(), url );
	else if ( this.init() )
		return this.getHtml( url );
	
	return false;
}
//=================================================================================================
TLightBox.prototype.height = function( value )
{
	if ( this.pElement() )
	{
		if ( typeof( value ) != "undefined")
		{
			this.pElement().style.height = parseInt( value ) + "px";
			this.pDoFrame();
		}
		else
			return this.pElement().offsetHeight;
	}
}
//=================================================================================================
TLightBox.prototype.hide = function()
{
	if ( this.pElement() )
	{
		if ( this.pVisible() )
		{
			this.pVisible( false );
			
			if ( this.iframe )
				this.iframe.style.display = "none";
			
			this.pElement().style.display = "none";
			
			if ( this.pAllHidden() )
				document.getElementById("imgBack").style.display = "none";
		}
	}
	else if ( this.init() )
		this.hide();
}
//=================================================================================================
TLightBox.prototype.init = function()
{
	if ( this.pElement() == null )
	{
		var
			element = document.getElementById( this.pName() );
		
		if ( element )
		{
			document.body.appendChild( element );
			
			this.pElement( element );
			
			element.style.display  = "none";
			element.style.position = "absolute";
			element.style.zIndex   = this.pZIndex();
			
			if ( isIE6 )
			{
				this.iframe = document.createElement("IFRAME");
				
				this.iframe.src            = "misc/empty.htm";
				this.iframe.style.display  = "none";
				this.iframe.style.position = "absolute";
				this.iframe.style.zIndex   = this.pZIndex() - 1;
				
				document.body.appendChild( this.iframe );
			}
			else
				this.iframe = null;
			
			return true;
		}
		
		return false;		
	}
	else
		return true;
}
//=================================================================================================
TLightBox.prototype.initialized = function()
{
	return this.pElement() != null;
}
//=================================================================================================
TLightBox.prototype.left = function( value )
{
	return this.pLeft( value );
}
//=================================================================================================
TLightBox.prototype.move = function( left, top )
{
	this.pLeft( left );
	this.pTop( top );
}
//=================================================================================================
TLightBox.prototype.name = function()
{
	return this.pName();
}
//=================================================================================================
TLightBox.prototype.position = function( size )
{
	if ( !this.pAutoMove() )
		return;
	
	if ( this.pElement() && this.pVisible() )
	{
		if ( typeof( size ) == "undefined" || typeof( size.width ) == "undefined")
			size = getSize( szPage );
		
		this.pElement().style.left = ( (size.width - this.pElement().offsetWidth) / 2 ) + "px";
		positionElement( this.pElement(), this.pTop() );
		
		this.pDoFrame();
	}
}
//=================================================================================================
TLightBox.prototype.show = function()
{
	if ( this.pElement() )
	{
		if ( !this.pVisible() )
		{
			this.pVisible( true );
			
			if ( this.iframe )
				this.iframe.style.display  = "block";
			
			this.pElement().style.display = "block";
			
			if ( this.pWithImage() )
				document.getElementById("imgBack").style.display = "block";
			
			var
				size = getSize( szWindow ),
				half = Math.floor( (size.height - this.height()) / 2 );
			
			if ( this.top() > half )
				this.top( Math.min( half, 40 ) );
			
			if ( this.pAutoMove() )
				this.position();
			else
			{
				this.left( (winSize.width - this.pElement().offsetWidth) / 2 );
				this.top( this.pTop() );
			}
		}
	} 
	else if ( this.init() )
		this.show();
}
//=================================================================================================
TLightBox.prototype.size = function()
{
	return this.pSize();
}
//=================================================================================================
TLightBox.prototype.top = function( value )
{
	return this.pTop( value );
}
//=================================================================================================
TLightBox.prototype.visible = function()
{
	return this.pVisible();
}
//=================================================================================================
TLightBox.prototype.width = function( value )
{
	if ( this.pElement() )
	{
		if ( typeof( value ) != "undefined")
		{
			this.pElement().style.width = parseInt( value ) + "px";
			this.pDoFrame();
		}
		else
			return this.pElement().offsetWidth;
	}
}
//=================================================================================================
// COLLECTION CLASS FOR LIGHTBOXES
//=================================================================================================
TLightBoxes = function()
{
	this.lightBoxes = [];
}
//=================================================================================================
TLightBoxes.prototype.anyVisible = function()
{
	for ( var i = 0; i < this.lightBoxes.length; i++ )
		if ( this.lightBoxes[i].visible() )
			return true;
	
	return false;
}
//=================================================================================================
TLightBoxes.prototype.init = function()
{
	for ( var i = 0; i < this.lightBoxes.length; i++ )
		this.lightBoxes[i].init();
}
//=================================================================================================
TLightBoxes.prototype.position = function( size )
{
	var
		imgBack = document.getElementById("imgBack");
	
	if ( imgBack )
	{
		if ( !this.anyVisible() )
			imgBack.style.display = "none";
		
		imgBack.style.position = "absolute";
		imgBack.style.zIndex   = 1000;
		imgBack.style.left     = "0px";
		imgBack.style.top      = "0px";
		imgBack.style.width    = size.width  + "px";
		imgBack.style.height   = size.height + "px";
	}
	
	for ( var i = 0; i < this.lightBoxes.length; i++ )
		this.lightBoxes[i].position( size );
}
//=================================================================================================
