/*
	JavaScript Library
	------------------	
	Author       : Jacky Cheung
	Class        : jScroll
	Description  : JavaScript Shadow
	
	Create Date  : 9 February 2009
	Last Modify  : 26 February 2009
	
	Modify		 : 30 November 2009
*/

/*---| CONSTRUCTOR |---*/
	var jShadow = function ( container, attributes )
	{
		if ( typeof(container) == "string" ) this.CONTAINER = document.getElementById ( container );
		else if ( typeof(container) == "object" ) this.CONTAINER = container;
		
		if ( this.CONTAINER != null )
		{
			var scripts = document.getElementsByTagName('script');
			for ( var i = 0; i< scripts.length; i++ ) 
			{
				var src = scripts[i].getAttribute('src');
				if (!src) continue;
				if ( src.indexOf ( "jShadow.js" ) > -1 )
				{
					this.IMG_PATH = src.replace(/([^\/]+?)\.js(\?.*)?$/, '') + "images/";
					break;
				}
			}
			this.PROP = {};
			this.PROP["ALL"] = ( attributes == undefined ) ? true : false;
			
			if ( attributes != undefined )
			{
				this.PROP["LEFT_TOP"] 			= ( attributes.leftTop 		!= undefined ) ? attributes.leftTop 	: true;
				this.PROP["LEFT"] 				= ( attributes.left			!= undefined ) ? attributes.left		: true;
				this.PROP["LEFT_BOTTOM"]		= ( attributes.leftBottom	!= undefined ) ? attributes.leftBottom	: true;
				this.PROP["TOP"]				= ( attributes.top			!= undefined ) ? attributes.top			: true;
				this.PROP["BOTTOM"]				= ( attributes.bottom		!= undefined ) ? attributes.bottom		: true;
				this.PROP["RIGHT_TOP"]			= ( attributes.rightTop		!= undefined ) ? attributes.rightTop	: true;
				this.PROP["RIGHT"]				= ( attributes.right		!= undefined ) ? attributes.right		: true;
				this.PROP["RIGHT_BOTTOM"]		= ( attributes.rightBottom	!= undefined ) ? attributes.rightBottom	: true;
				this.PROP["RESIZE"]				= ( attributes.resize		!= undefined ) ? attributes.resize		: false;
				this.PROP["WIDTH"]				= ( attributes.width		!= undefined ) ? attributes.width		: 4;
				this.PROP["HEIGHT"]				= ( attributes.height		!= undefined ) ? attributes.height		: 4;
				this.PROP["ON_COMPLETE"]		= ( attributes.onComplete	!= undefined ) ? attributes.onComplete	: null;
				this.PROP["POSITION"]			= ( attributes.position		!= undefined ) ? attributes.position	: "relative";
				this.PROP["MODE"]				= ( attributes.mode			!= undefined ) ? attributes.mode		: ".png";
				if ( attributes.path != undefined ) this.IMG_PATH = attributes.path;
			}

			this.create();
			
			if ( this.PROP["ON_COMPLETE"] != null )
			{
				this.PROP["ON_COMPLETE"]();
			}
		}
	}
	
		/*---| CREATE |---*/
			this.jShadow.prototype.create = function()
			{
				if ( this.PROP["POSITION"] != undefined )
				{
					this.CONTAINER.style.position = this.PROP["POSITION"];
				}
				/* DEBUG : remove function 
					if ( this.CONTAINER.style.position == null )
					{
						this.CONTAINER.style.position = "relative";
					}
				*/
				if ( F )
				{
					if ( this.PROP["ALL"] || this.PROP["LEFT_TOP"] 		) this.attach ( "LEFT_TOP" );
					if ( this.PROP["ALL"] || this.PROP["LEFT"] 			) this.attach ( "LEFT" );
					if ( this.PROP["ALL"] || this.PROP["LEFT_BOTTOM"] 	) this.attach ( "LEFT_BOTTOM" );
					if ( this.PROP["ALL"] || this.PROP["TOP"]				) this.attach ( "TOP" );
					if ( this.PROP["ALL"] || this.PROP["BOTTOM"]			) this.attach ( "BOTTOM" );
					if ( this.PROP["ALL"] || this.PROP["RIGHT_TOP"]		) this.attach ( "RIGHT_TOP" );
					if ( this.PROP["ALL"] || this.PROP["RIGHT"]			) this.attach ( "RIGHT" );
					if ( this.PROP["ALL"] || this.PROP["RIGHT_BOTTOM"]	) this.attach ( "RIGHT_BOTTOM" );
				}
				
				if ( this.PROP["RESIZE"] )
				{
					F.addResize ( this.CONTAINER.id, this.renderResize, this );
				}
			}
			
			
		/*---| ATTACH ELEMENT |---*/
			this.jShadow.prototype.attach = function ( name )
			{
				var FILENAME = this.IMG_PATH + name.toLowerCase() + this.PROP["MODE"];
				var W = this.PROP["WIDTH"];
				var H = this.PROP["HEIGHT"];
				
				var ELEMENT = F.createElement ( "div", "jSHADOW_" + name + "_" + this.CONTAINER.id,	
				{ 
					style:{ backgroundImage:FILENAME, position:"absolute", width:W+"px", height:H+"px", overflow:"hidden", zIndex:F.getStyle ( this.CONTAINER, "zIndex" ) } 
				});
				
				this.CONTAINER.appendChild ( ELEMENT );
				this.setPosition ( name );
				
				if ( F.checkBrowser() == "ie6" )
				{
					ELEMENT.style.backgroundImage = "none";
					ELEMENT.style["filter"] = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\""+FILENAME+"\", sizingMethod=\"scale\")";
				}
			}
	
	
		/*---| RENDER RESIZE |---*/
			this.jShadow.prototype.renderResize = function ( THIS )
			{
				if ( F )
				{
					if ( THIS.PROP["ALL"] || THIS.PROP["LEFT_TOP"] 		) THIS.setPosition ( "LEFT_TOP" );
					if ( THIS.PROP["ALL"] || THIS.PROP["LEFT"] 			) THIS.setPosition ( "LEFT" );
					if ( THIS.PROP["ALL"] || THIS.PROP["LEFT_BOTTOM"] 		) THIS.setPosition ( "LEFT_BOTTOM" );
					if ( THIS.PROP["ALL"] || THIS.PROP["TOP"]				) THIS.setPosition ( "TOP" );
					if ( THIS.PROP["ALL"] || THIS.PROP["BOTTOM"]			) THIS.setPosition ( "BOTTOM" );
					if ( THIS.PROP["ALL"] || THIS.PROP["RIGHT_TOP"]		) THIS.setPosition ( "RIGHT_TOP" );
					if ( THIS.PROP["ALL"] || THIS.PROP["RIGHT"]			) THIS.setPosition ( "RIGHT" );
					if ( THIS.PROP["ALL"] || THIS.PROP["RIGHT_BOTTOM"]		) THIS.setPosition ( "RIGHT_BOTTOM" );
				}
			}
			
		/*---| RESET |---*/
			this.jShadow.prototype.reset = function ( )
			{
				if ( F && this.PROP != undefined )
				{
					if ( this.PROP["ALL"] || this.PROP["LEFT_TOP"] 		) this.setPosition ( "LEFT_TOP" );
					if ( this.PROP["ALL"] || this.PROP["LEFT"] 			) this.setPosition ( "LEFT" );
					if ( this.PROP["ALL"] || this.PROP["LEFT_BOTTOM"] 	) this.setPosition ( "LEFT_BOTTOM" );
					if ( this.PROP["ALL"] || this.PROP["TOP"]				) this.setPosition ( "TOP" );
					if ( this.PROP["ALL"] || this.PROP["BOTTOM"]			) this.setPosition ( "BOTTOM" );
					if ( this.PROP["ALL"] || this.PROP["RIGHT_TOP"]		) this.setPosition ( "RIGHT_TOP" );
					if ( this.PROP["ALL"] || this.PROP["RIGHT"]			) this.setPosition ( "RIGHT" );
					if ( this.PROP["ALL"] || this.PROP["RIGHT_BOTTOM"]	) this.setPosition ( "RIGHT_BOTTOM" );
				}
			}
			
		/*---| SET POSITION |---*/
			this.jShadow.prototype.setPosition = function ( name )
			{
				var obj = document.getElementById ( "jSHADOW_" + name + "_" + this.CONTAINER.id );
				if ( obj )
				{
					var left  = 0;
					var top   = 0;
					var width = this.CONTAINER.offsetWidth;
					var height = this.CONTAINER.offsetHeight;
					var W = parseInt ( obj.style.width );
					var H = parseInt ( obj.style.height );
					var X, Y;
					switch ( name )
					{
						case "LEFT_TOP" 	: X = left - W; 	Y = top - H;							break;
						case "LEFT"			: X = left - W;		Y = top; 				H = height;		break;
						case "LEFT_BOTTOM"	: X = left - W;		Y = top + height;						break;
						case "TOP"			: X = left;			Y = top - H;			W = width;		break;
						case "BOTTOM"		: X = left;			Y = top + height;		W = width;		break;
						case "RIGHT_TOP"	: X = left + width;	Y = top - H;							break;
						case "RIGHT"		: X = left + width; Y = top;				H = height;		break;
						case "RIGHT_BOTTOM"	: X = left + width; Y = top + height;						break;
					}
					obj.style.left = X + "px";
					obj.style.top = Y + "px";
					obj.style.width = W + "px";
					obj.style.height = H + "px";
				}
			}
		
	
	
	
