/* 
 	. Created by Lis/2k1. rev# 20.0.2 : 16:4:49
	------------------------------------
	. (c) Multimedia Art, 2002
*/

// CVSinfo
// $Id: cLayerManager.js,v 1.5 2002/07/22 15:10:47 lis Exp $
// $Source: D:/cvsrepo/mres/Kroton/jsLib/cLayerManager.js,v $
// $Revision: 1.5 $


// Define base types
function TPoint ( iTop, iLeft )
{
	this.top = iTop || 0;
	this.left = iLeft || 0;
}

function TSize( iWidth, iHeight )
{
	this.width = iWidth || 0;
	this.height = iHeight || 0;
}

function TRect( iTop, iLeft, iWidth, iHeight )
{
	this.top = iTop || 0;
	this.left = iLeft || 0;
	this.width = iWidth || 0;
	this.height = iHeight || 0;
	this.Expand = function ( iValue ) {
		this.top -= parseInt(iValue/2);
		this.left -= parseInt(iValue/2);
		this.height += iValue;
		this.width += iValue;
		return( this );
	}
}

function TClipRect( iTop, iRight, iBottom, iLeft )
{
	this.top = iTop || 0;
	this.right = iRight || 0;
	this.bottom = iBottom || 0;
	this.left = iLeft || 0;
	this.Expand = function ( iValue ) {
		this.top -= iValue;
		this.right -= iValue;
		this.bottom += iValue;
		this.left += iValue;
		return( this );
	}
}

function TRect2TClipRect( iRect )
{
	var r = new TClipRect();
	r.top = r.left = 0;
	r.right = iRect.width;
	r.bottom = iRect.height;
	return( r );
}

function IsInRange( ix, ixStart, ixEnd )
{
	return( ixStart <= ix && ix <= ixEnd );
}

/**
	Layer object provide and unify simply layers operation.
	Compatibe with: NN4+, IE4+
	
	@param idName - id name for managed layer object.
*/
function cLayerManager( iIdName )
{
	this.bi = new cBrowserInfo();
	this.Stl = null;
	
	this.IsObjectExist = function( iIdName )
	{
		var iObj;

		if ( this.bi.IsDOM )
			iObj = document.getElementById( iIdName );		
		else if( this.bi.IsIE )
			iObj = document.all[iIdName];
		else
			iObj = document.layers[iIdName];
		return( iObj != null );
	}
	
	this.SetObject = function( iIdName )
	{
		this.Obj = this.GetObject( iIdName );
		if ( this.bi.IsIE || this.bi.IsDOM )
			this.Stl = this.Obj.style;
		else
			this.Stl = this.Obj;
		return( this );
	}
	
	/**
		Warning! This method only for competent stuff personal use only!
	*/
	this.SetTrustedObject = function( iObj )
	{
		this.Obj = iObj;
		if ( this.bi.IsIE || this.bi.IsDOM )
			this.Stl = this.Obj.style;
		else
			this.Stl = this.Obj;
		return( this );
	}
		
	this.GetObject = function( iIdName ) 
	{
		var iObj;
		if ( this.bi.IsDOM )
			iObj = document.getElementById( iIdName );
		else if( this.bi.IsIE )
			iObj = document.all[iIdName];
		else 
			iObj = document.layers[iIdName];	
		
		if ( iObj == null ) 
			alert("Can't find layer object with id:'" + iIdName +"'");
		return( iObj );
	}

	this.Hide = function() 
	{
		if ( this.Obj == null )
			return(false);
		
		this.Stl.visibility = ( this.bi.IsIE || this.bi.IsDOM ) ? "hidden" : "hide";
		return(this);
	}
	
	this.Show = function()
	{
		if ( this.Obj == null )
			return(false);
			
		this.Stl.visibility = ( this.bi.IsIE || this.bi.IsDOM ) ? "visible" : "show";
		return(this);
	}
	
	this.IsVisible = function ()
	{
		if ( this.Obj == null )
			return(false);
		var re = new RegExp( "show|visible", "i" );
		return( re.test(this.Stl.visibility) );
	}
	
	this.IsExpand = function ()
	{
		if ( this.Obj == null )
			return(false);
		var re = new RegExp( "block", "i" );
		return( re.test(this.Stl.display) );
	}
	
	this.Collapse = function ()
	{
		if ( this.Obj == null )
			return(false);
		this.Stl.display = "none";
		return(this);
	}
	
	this.Expand = function ()
	{
		if ( this.Obj == null )
			return(false);
		this.Stl.display = "block";
		return(this);
	}
	
	this.SetPosition = function( iLeft, iTop )
	{
		if ( this.Obj == null )
			return(false);

		if ( this.bi.IsIE || this.bi.IsDOM ) {
			this.Obj.style.left = iLeft + "px";
			this.Obj.style.top = iTop + "px";
		}
		else
			this.Obj.moveTo( iLeft, iTop );
		
		return(this);
	}
	
	this.SetSize = function( iWidth, iHeight )
	{
		if ( this.Obj == null )
			return(false);
		if ( this.bi.IsIE || this.bi.IsDOM ) {
			this.Stl.width = iWidth;
			this.Stl.height = iHeight;
		}
		else
			this.Obj.resizeTo( iWidth, iHeight );

		return(this);
	}
	
	this.GetPosition = function() 
	{
		if ( this.Obj == null )
			return(false);
		var r = new function() { this.top = 0; this.left = 0; }
		if ( this.bi.IsIE || this.bi.IsDOM ) {
			r.top = parseInt(this.Obj.offsetTop);
			r.left = parseInt(this.Obj.offsetLeft);
		} else {
			r.top = this.Obj.top;
			r.left = this.Obj.left;
		}
		
		return( r );
	}
	
	this.GetSize = function ()
	{
		if ( this.Obj == null )
			return(false);
		var r = new function() { this.width = 0; this.height = 0; }
		if ( this.bi.IsIE ) {
			r.width = parseInt(this.Obj.clientWidth);
			r.height = parseInt(this.Obj.clientHeight);
		} else if ( this.bi.IsDOM ) {
			r.width = parseInt(this.Obj.offsetWidth);
			r.height = parseInt(this.Obj.offsetHeight);
		} else {
			r.width = this.Obj.document.width;
			r.height = this.Obj.clip.height; 
		}
		return( r );
	}
	
	this.GetRect = function ()
	{
		if ( this.Obj == null )
			return(false);
		
		var p = this.GetPosition();
		var s = this.GetSize();
		var r = new TRect( p.top, p.left, s.width, s.height );
		return( r );
	}
	
	this.GetBordersThickness = function()
	{
		if ( this.Obj == null ) 
			return(false);
		var result = new TClipRect( 
				this.Stl.borderTopWidth,
				this.Stl.borderRightWidth,
				this.Stl.borderBottomWidth,
				this.Stl.borderLeftWidth );

		return( result );
	}
	
	this.ChangeContentTo = function( iHTMLText )
	{
		if ( this.Obj == null ) 
			return(false);
		if ( this.bi.IsIE || this.bi.IsDOM )
			this.Obj.innerHTML = iHTMLText;			
		else
			this.Obj.document.write( iHTMLText );

		return( this );
	}
	
	this.MoveToXCenter = function()
	{
		if ( this.Obj == null )
			return(false);
		var s = this.GetSize();
		var maxX = this.bi.GetMaxX();
		this.SetPosition( (maxX - s.width)/2, this.GetPosition().top );
		return(this);
	}
	
	this.MoveToYCenter = function()
	{
		if ( this.Obj == null )
			return(false);
		var s = this.GetSize();
		var maxY = this.bi.GetMaxY();
		this.SetPosition( this.GetPosition().left, (maxY - s.height)/2  );
		return(this);
	}
	
	this.MoveToCenter = function()
	{
		if ( this.Obj == null )
			return(false);
		var s = this.GetSize();
		var maxX = this.bi.GetMaxX();
		var maxY = this.bi.GetMaxY();
		this.SetPosition( (maxX - s.width)/2, 
			(maxY - s.height)/2  + this.bi.GetScrolledUp() );
		return(this);
	}
	
	this.ParseClipString = function ( iClipString ) 
	{
		var r = new TClipRect();
		
		if ( iClipString == '' ) {
			alert("Script error: Clip string is empty!");
			return(result)
		}
		
		var re = new RegExp("rect\\((.+)\\)","i");
		if (!re.test( iClipString ))
			alert("Error in clip-block declaration");
		var a_pstr = new String( RegExp.$1 ).split(' ');
		r.top = parseInt(a_pstr[0]);
		r.right = parseInt(a_pstr[1]);
		r.bottom = parseInt(a_pstr[2]);
		r.left = parseInt(a_pstr[3]);
		return( r );
	}
	
	this.FormClipRect = function ( iTop, iRight, iBottom, iLeft ) {
		return( "rect("+iTop+"px "+iRight+"px "+iBottom+"px "+iLeft+"px)" );
	}
	
	this.GetClipRect = function () {
		var cr;
		var cp = this.GetSize();
		if ( this.bi.IsDOM ) {
			if ( this.Obj.style.clip == '' )
				this.Obj.style.clip = this.FormClipRect( 0, cp.width, cp.height, 0);
			cr = this.ParseClipString( this.Obj.style.clip );
		}
		else
			cr = this.Obj.clip;
		return( cr );
	}
	
	this.SetClipRect = function ( iR ) // iR as TClipRect
	{
		if ( this.bi.IsDOM )
			this.Obj.style.clip = this.FormClipRect(
				iR.top, iR.right, iR.bottom, iR.left );
		else {
				var g = this.Obj.clip;
				g.top = iR.top;
				g.right = iR.right;
				g.bottom = iR.bottom;
				g.left = iR.left;
		}
		return(this);
	}
		
	this.PseudoScroll = function( iDirrection, iValue )
	{
		this.PseudoScrollEx( iDirrection, this.GetRect(), iValue )
		return( this );
	}
	
	this.IsInRect = function( iR )
	{
		var cr = this.GetRect();
		var result = ( IsInRange( cr.left, iR.left, iR.left + iR.width ) 
				|| IsInRange( cr.left + cr.width, iR.left, iR.left + iR.width ) )
			&& (IsInRange( cr.top, iR.top, iR.top + iR.height ) 
				|| IsInRange( cr.top + cr.height, iR.top, iR.top + iR.height ) );
		return( result );
	}
	
	this.PseudoScrollEx = function ( iDirrection, iWindowRect, iValue )
	{
		scrValue = iValue || 10;
		var cp = this.GetRect();
		var wr = iWindowRect;
		var cr = this.GetClipRect();
				
		switch( iDirrection.toLowerCase() )
		{
			case "up":
				cp.top -= scrValue;
				this.SetPosition( cp.left, cp.top );
				break;
			case "down":
				cp.top += scrValue;
				this.SetPosition( cp.left, cp.top );
				break;
			case "left":
				cp.left -= scrValue;
				this.SetPosition( cp.left, cp.top  );
				break;
			case "right":
				cp.left += scrValue;
				this.SetPosition( cp.left, cp.top  );
				break;
		}

		cr.top = wr.top - cp.top;
		cr.bottom = wr.height + wr.top - cp.top;
		cr.left = wr.left - cp.left;
		//alert("wr.width:"+wr.width );
		cr.right = wr.left + wr.width - cp.left;

		if ( this.bi.IsDOM )
			this.Obj.style.clip = this.FormClipRect( cr.top, cr.right, cr.bottom, cr.left );

		return( this );
	}
	
	this.Obj = null;
	if ( iIdName ) this.SetObject( iIdName );
	return( this );
}
