/**---------------------------------------------------------------------------------------------------
 *
 * File:		layer.js
 * Date:		25.07.2007
 * Description:	Dynamic Adverts
 *
 * Usage:
 *
 *--------------------------------------------------------------------------------------------------*/

var	ie		= document.all,
	dragObj	= null;


/**---------------------------------------------------------------------------------------------------
 *
 * Layer Object
 *
 */
function Layer( name, layerName, parent )
{
	this.obj			= name;
	this.name			= layerName		|| null;
	this.parent			= parent		|| null;
	this.dragStatus		= false;
	this.dragStarted	= false;
	this.constrainX		= false;
	this.constrainY		= false;
	this.layer			= null;
	this.alphaValue		= 100;
	this.visible		= false;
	this.type			= 'layer';
	this.document		= document;
	this.window			= window;
	
	
	/**
	 * Only works in FireFox
	 *
	 */
	this.drag = function()
	{
		if ( this.layer )
			this.layer.setAttribute( "onmousedown", this.obj + ".startDrag()" );
	}
	
	/**
	 * Layer constructor
	 *
	 */
	this.construct = function( id )
	{
		if ( id )
		{
			this.name					= id;
			this.layer					= this.document.getElementById( id );
		//	this.layer.style.position	= this.parent ? "relative" : "absolute";
			this.parent					= this.parent ? this.parent : this.document.body;
		}
		else if ( this.name && this.document.getElementById( this.name ) )
		{
			this.layer					= this.document.getElementById( this.name );
			this.parent					= this.parent ? this.parent : this.document.body;
		}
		else
		{
			this.name					= this.obj;
			this.layer					= this.document.createElement( "DIV" );
			this.layer.style.position	= this.parent ? "relative" : "absolute";
			
			if ( this.parent )
				this.parent.appendChild( this.layer );
			else
			{
				this.parent				= this.document.body;
				this.document.body.appendChild( this.layer );
			}
			
			this.layer.setAttribute( "id", this.name );
		}

		if ( this.layer )
			this.layer.style.display = "none";
	}
	
	/**
	 * Removes the layer from the DOM
	 *
	 */
	this.close = function()
	{
		if ( this.layer )
		{
			this.document.body.removeChild( this.layer );
			this.layer = null;
		}
	}
	
	/**
	 * Moves the layer to position X
	 *
	 * @param int x
	 */
	this.moveX = function( x )
	{
		var	left = this.getBrowserLeft();
		
		if ( ie )
		{
			if ( this.constrainX )
			{
				if ( x > this.parent.clientWidth - this.layer.clientWidth + left )
					x = this.parent.clientWidth - this.layer.clientWidth + left;

				if ( x < left )
					x = left;
			}
		}
		else
		{
			if ( this.constrainX )
			{
				if ( x > window.innerWidth - this.layer.offsetWidth + left )
					x = window.innerWidth - this.layer.offsetWidth + left;
					
				if ( x < left )
					x = left;
			}
		}
		
		if ( this.layer )
			this.layer.style.left = x + 'px';
	}
	
	/**
	 * Moves the layer to position Y
	 *
	 * @param int y
	 */
	this.moveY = function( y )
	{
		var	top = this.getBrowserTop();
		
		if ( ie )
		{
			if ( this.constrainY )
			{
				if ( y > this.parent.clientHeight - this.layer.clientHeight + top )
					y = this.parent.clientHeight - this.layer.clientHeight + top;
					
				if ( y < top )
					y = top;
			}
		}
		else
		{
			if ( this.constrainY )
			{
				if ( y > window.innerHeight - this.layer.offsetHeight + top )
					y = window.innerHeight - this.layer.offsetHeight + top;
					
				if ( y < top )
					y = top;
			}
		}
		
		if ( this.layer )
			this.layer.style.top = y + 'px';
	}
	
	/**
	 * Moves the layer to position X, Y
	 *
	 * @param int x
	 * @param int y
	 */
	this.move = function( x, y )
	{
		this.moveX( x );
		this.moveY( y );
	}
	
	/**
	 * Moves the layer at a given degree to a certain distance
	 *
	 * @param int x
	 * @param in ty
	 */
	this.moveTo = function( degree, distance )
	{
		var	x = distance * Math.cos( (degree - 90) * Math.PI / 180 ),
			y = distance * Math.sin( (degree - 90) * Math.PI / 180 );
			
		this.moveX( this.getX() + x );
		this.moveY( this.getY() + y );
	}
	
	/**
	 * Sets the alpha blending of the layer
	 *
	 * @param int alpha
	 */
	this.alpha = function( alpha )
	{
		this.alphaValue = alpha;
		
		if ( this.layer )
		{
			this.layer.style.opacity	= parseInt( alpha ) / 100;
			this.layer.style.MozOpacity	= parseInt( alpha ) / 100;
			
			if ( ie )
				this.layer.style.filter	= "alpha(opacity=" + alpha + ")";
		}
	}
	
	/**
	 * Writes content to the layer
	 *
	 * @param string str
	 */
	this.write = function( str )
	{
		if ( this.layer )
			this.layer.innerHTML = str;
	}
	
	/**
	 * Outputs content to the layer
	 *
	 * @param string str
	 */
	this.output = function( str )
	{
		if ( this.layer )
			this.layer.innerHTML += str;
	}
	
	/**
	 * Resizes the layer
	 *
	 * @param int xsize
	 * @param int ysize
	 */
	this.resize = function( xsize, ysize )
	{
		if ( this.layer )
		{
			this.layer.style.width	= xsize + 'px';
			this.layer.style.height	= ysize + 'px';
		}
	}
	
	/**
	 * Resizes the layer
	 *
	 * @param int xsize
	 */
	this.resizeX = function( xsize )
	{
		if ( this.layer )
			this.layer.style.width	= xsize + 'px';
	}
	
	/**
	 * Resizes the layer
	 *
	 * @param int ysize
	 */
	this.resizeY = function( ysize )
	{
		if ( this.layer )
			this.layer.style.height	= ysize + 'px';
	}
	
	/**
	 * Displays / Hides the layer
	 *
	 * @param bool flag
	 */
	this.show = function( flag )
	{
		this.visible = flag;
		
		if ( this.layer )
			this.layer.style.display = flag ? "block" : "none";
	}
	
	/**
	 * Gets the absolute position of the layer
	 *
	 * @param object obj
	 */
	this.getPos = function( object )
	{
		var	left	= 0,
			top		= 0;
		
		if ( object )
			obj = object;
		else
			obj = this.layer;

		if ( obj && obj.offsetParent )
		{
			left = obj ? obj.offsetLeft : 0;
			top  = obj ? obj.offsetTop : 0;
			
			if ( obj )
				while( obj = obj.offsetParent )
				{
					left += obj.offsetLeft;
					top  += obj.offsetTop;
				}
		}
		
		return [ left, top, this.getWidth( object ), this.getHeight( object ) ];
	}
	
	/**
	 * Returns the alpha value of the layer
	 *
	 * @return int
	 */
	this.getAlpha = function()
	{
		return parseInt( this.alphaValue );
	}
	
	/**
	 * Returns the X position of the layer
	 *
	 * @return int
	 */
	this.getX = function()
	{
		if ( this.layer.position == undefined || this.layer.position == "relative" )
		{
			pos = this.getPos( this.layer );
			return pos[0];
		}
		
		return parseInt( this.layer.style.left == "" ? 0 : this.layer.style.left );
	}
	
	/**
	 * Returns the Y position of the layer
	 *
	 * @return int
	 */
	this.getY = function()
	{
		if ( this.layer.position == undefined || this.layer.position == "relative" )
		{
			pos = this.getPos( this.layer );
			return pos[1];
		}
			
		return parseInt( this.layer.style.top );
	}
	
	/**
	 * Returns the width of the layer
	 *
	 * @return int
	 */
	this.getWidth = function( obj )
	{
		if ( obj )
			return parseInt( ie ? obj.offsetWidth + 2 : obj.offsetWidth );
		else if ( this.layer )
			return parseInt( ie ? this.layer.clientWidth + 2 : this.layer.offsetWidth );
	}
	
	/**
	 * Returns the height of the layer
	 *
	 * @return int
	 */
	this.getHeight = function( obj )
	{
		if ( obj )
			return parseInt( ie ? obj.offsetHeight + 2 : obj.offsetHeight );
		else if ( this.layer )
			return parseInt( ie ? this.layer.clientHeight + 2 : this.layer.offsetHeight );
	}
	
	this.getBrowserLeft = function()
	{
		return ie ? (this.document.documentElement ? this.document.documentElement.scrollLeft : this.document.body.scrollLeft) : this.window.pageXOffset ;
	}
	
	this.getBrowserTop = function()
	{
		return ie ? (this.document.documentElement ? this.document.documentElement.scrollTop : this.document.body.scrolltop) : this.window.pageYOffset ;
	}
	
	this.getBrowserWidth = function()
	{
	//	return ie ? (this.document.documentElement ? this.document.documentElement.clientWidth : this.document.body.clientWidth) : this.window.innerWidth;
		return ie ? this.document.body.clientWidth : this.window.innerWidth;
	}
	
	this.getBrowserHeight = function()
	{
		return ie ? this.document.body.clientHeight : this.window.innerHeight;
	}
	
	this.getWindowHeight = function()
	{
		return ie ? document.documentElement.clientHeight : this.window.innerHeight;
	}
	
	this.setWidth = function( width )
	{
		if ( this.layer )
			this.layer.style.width = width + 'px';
	}
	
	this.setHeight = function( height )
	{
		if ( this.layer )
			this.layer.style.height = height + 'px';
	}

	this.resize = function( width, height )
	{
		this.setWidth( width );
		this.setHeight( height );
	}
	
	/**
	 * Sets the scroll of the layer
	 *
	 * @param int n
	 */
	this.scrollX = function( n )
	{
		if ( this.layer )
			this.layer.scrollLeft = n;
	}
	
	/**
	 * Sets the scroll of the layer
	 *
	 * @param int n
	 */
	this.scrollY = function( n )
	{
		if ( this.layer )
			this.layer.scrollTop = n;
	}
	
	this.getScrollX = function()
	{
		if ( this.layer )
			return this.layer.scrollLeft;
	}
	
	this.getScrollY = function()
	{
		if ( this.layer )
			return this.layer.scrollTop;
	}
	
	this.getScrollWidth = function()
	{
		if ( this.layer )
			return this.layer.scrollWidth;
	}
	
	this.getScrollHeight = function()
	{
		if ( this.layer )
			return this.layer.scrollHeight;
	}
	
	/**
	 * Starts the layer dragging process
	 *
	 * @param object e Event object
	 */
	this.startDrag = function( e )
	{
		dragObj					= this;
		this.dragStatus			= true;
		this.dragStarted		= true;
		document.onmouseup		= this.endDrag;
		document.onmousemove	= this.doDrag;
		document.onselectstart	= function() { return false; };
		document.onmousedown	= function() { return false; };
	}
	
	/**
	 * Starts the layer resizing process
	 *
	 * @param object a Event object
	 */
	this.startResize = function( e )
	{
		dragObj					= this;
		this.dragStatus			= true;
		this.dragStarted		= true;
		document.onmouseup		= this.endDrag;
		document.onmousemove	= this.doResize;
		document.onselectstart	= function() { return false; };
		document.onmousedown	= function() { return false; };
	}
	
	/**
	 * Ends the layer dragging process
	 *
	 */
	this.endDrag = function()
	{
		this.dragStatus			= false;
		document.onmousemove	= null;
		document.onselectstart	= null;
		document.onmousedown	= null;
	}
	
	/**
	 * Performs the layer dragging
	 *
	 * @param object e Event object
	 */
	this.doDrag = function( e )
	{
		var	top		= document.body.scrollTop,
			left	= document.body.scrollLeft;
				
		if ( dragObj.dragStarted )
		{
			dragObj.dragStartX	= left + parseInt(ie ? window.event.clientX : e.pageX) - parseInt( dragObj.layer.style.left );
			dragObj.dragStartY	= top + parseInt(ie ? window.event.clientY : e.pageY) - parseInt( dragObj.layer.style.top );
			
			dragObj.dragStarted = false;
		}
		
		var	mouseX	= left + parseInt( ie ? window.event.clientX : e.pageX ) - parseInt( dragObj.dragStartX ),
			mouseY	= top + parseInt( ie ? window.event.clientY : e.pageY ) - parseInt( dragObj.dragStartY );
			
		dragObj.move( mouseX, mouseY );
	}
	
	/**
	 * Performs the layer resizing
	 *
	 * @param object a Event object
	 */
	this.doResize = function( e )
	{
		var	top		= document.body.scrollTop,
			left	= document.body.scrollLeft;
			
		if ( dragObj.dragStarted )
		{
			dragObj.dragStartX	= left + parseInt(ie ? window.event.clientX : e.pageX) - parseInt( dragObj.layer.style.left );
			dragObj.dragStartY	= top + parseInt(ie ? window.event.clientY : e.pageY) - parseInt( dragObj.layer.style.top );
			
			dragObj.dragStarted = false;
		}
		
		var	mouseX	= left + parseInt( ie ? window.event.clientX : e.pageX ) - parseInt( dragObj.dragStartX ),
			mouseY	= top + parseInt( ie ? window.event.clientY : e.pageY ) - parseInt( dragObj.dragStartY );
			
		dragObj.resize( mouseX, mouseY );
	}
	
	/**
	 * Changes the z-index of a layer
	 *
	 * @param integer n
	 */
	this.index = function( n )
	{
		if ( this.layer )
			this.layer.style.zIndex = n;
	}
	
	/**
	 * Returns the visibility status of the layer
	 *
	 * @return Bool
	 */
	this.isVisible = function()
	{
		return this.visible;
	}
}


/*--------------------------------------------------------------------------------------------------*/
