/*
	JavaScript Library
	------------------	
	Author       : Jacky Cheung
	Class        : F
	Description  : Common Functions
	
	Create Date  : 2 February 2009
	Last Modify  : 4 May 2009
*/

/*
	Functions Summary
	-----------------
	- createElement                 : 3 JAN 2009 by Jacky
	- popup                         : 2 FEB 2009 by Jacky
	- include                       : 3 FEB 2009 by Jacky
	- confirmDelete					: 4 FEB 2009 by Jacky
	
	- getAbsoluteOffsetTop			: 26 FEB 2009 by Jacky
	- getAbsoluteOffsetLeft			: 26 FEB 2009 by Jacky
	- getBodyScrollTop				: 26 FEB 2009 by Jacky
	- getBodyScrollLeft				: 26 FEB 2009 by Jacky
	
	- changeClass					: 4 May 2009 by Jacky
*/

var F = 
{
	/*___| CREATE ELEMENT ~ 3 JAN 2009 by JACKY |___*/
		createElement : function ( type, id, attributes, mouseEvent )
		{
			var element = document.createElement ( type );
			for ( var attr in attributes ) 
			{ 
				if ( attr.toUpperCase() == "STYLE" )
				{
					for ( var style in attributes [ attr ] ) 
					{ 
						switch ( style.toUpperCase() )
						{
							case "BACKGROUNDIMAGE":
								element.style.backgroundImage = "url(" + attributes [ attr ] [ style ] + ")";
								break;
							case "BACKGROUNDCOLOR":
							case "BGCOLOR":
								element.style.backgroundColor = attributes [ attr ] [ style ];
								break;
							default :
								element.style [ style ] = attributes [ attr ] [ style ]; 
								break;
						}
					}
				} else {
					element [ attr ] = attributes [ attr ]; 
				}
			}
			if ( id != null ) element.setAttribute ( "id", id );
			if ( mouseEvent != undefined )
			{
				if ( mouseEvent.mousedown != undefined ) { element.onmousedown = mouseEvent.mousedown; }
				if ( mouseEvent.mouseup   != undefined ) { element.onmouseup   = mouseEvent.mouseup;   }
				if ( mouseEvent.mouseover != undefined ) { element.onmouseover = mouseEvent.mouseover; }
				if ( mouseEvent.mouseout  != undefined ) { element.onmouseout  = mouseEvent.mouseout;  }
			}
			
			switch ( type.toUpperCase() )
			{
				case "INPUT":
				case "TEXTAREA":
					element.name = id;
					break;
			}
			
			return element;
		},
		
	/*___| REMOVE ELEMENT ~ 5 SEP 2009 by JACKY |___*/
		removeElement : function ( id )
		{
			var child = document.getElementById(id);
			var parent = child.parentNode;
			parent.removeChild ( child );
		},

	/*___| POPUP ~ 2 FEB 2009 by JACKY |___*/
		popup : function ( url, attributes )
		{
			var parameters = "";
			for ( var attr in attributes ) { if ( attr != "target" ) { parameters += ( parameters != "" ) ? "," : ""; parameters += attr + "=" + attributes [ attr ]; }	}
			if ( attributes == null ) attributes = {};
			if ( attributes != null ) { if ( attributes.target == undefined ) target = ""; 	}
			window.open(url, target, parameters);
		},
		
	/*___| INCLUDE ~ 3 FEB 2009 by JACKY |___*/
		include : function ( filename )
		{
			var SCRIPT = this.createElement ( "script", null, { type:"text/javascript", src:filename } );
			document.getElementsByTagName ( "head" )[0].appendChild ( SCRIPT );
		},
		
	/*___| CONFIRM DELETE MESSAGE BOX ~ 4 FEB 2009 by JACKY |___*/
		confirmDelete : function ( ) 
		{ 
			if(confirm("Delete this order?"))
			{
				return true; 
			} else { return false; }
		},
		
	/*___| GET ABSOLUTE OFFSET LEFT ~ 26 FEB 2009 by JACKY |___*/
		getAbsoluteOffsetLeft : function ( obj )
		{
			var LEFT = obj.offsetLeft;
			var PARENT = obj.offsetParent;
			while (PARENT != null) {
				LEFT += PARENT.offsetLeft;
				PARENT = PARENT.offsetParent;
			}
			return LEFT;
		},
		
	/*___| GET ABSOLUTE OFFSET TOP ~ 26 FEB 2009 by JACKY |___*/
		getAbsoluteOffsetTop : function ( obj )
		{
			var TOP = obj.offsetTop;
			var PARENT = obj.offsetParent;
			while (PARENT != null) {
				TOP += PARENT.offsetTop;
				PARENT = PARENT.offsetParent;
			}
			return TOP;
		},
		
	/*___| GET BODY SCROLL TOP ~ 26 FEB 2009 by JACKY |___*/
		getBodyScrollTop : function ( )
		{
			if ( window && window.pageYOffset != undefined ) return window.pageYOffset;
			if ( document.documentElement && document.documentElement.scrollTop != undefined ) return document.documentElement.scrollTop;
			if ( document.body && document.body.scrollTop != undefined ) return document.body.scrollTop;
		},
		
	/*___| GET BODY SCROLL LEFT ~ 26 FEB 2009 by JACKY |___*/
		getBodyScrollLeft : function ( )
		{
			if ( window && window.pageXOffset != undefined ) return window.pageXOffset;
			if ( document.documentElement && document.documentElement.scrollLeft != undefined ) return document.documentElement.scrollLeft;
			if ( document.body && document.body.scrollLeft != undefined ) return document.body.scrollLeft;
		},

	/*___| GET SCREEN HEIGHT ~ 14 JUNE 2009 by JACKY |___*/
		getScreenHeight : function ( )
		{
			if ( window && window.innerHeight != undefined ) return window.innerHeight;
			if ( document.documentElement && document.documentElement.clientHeight != undefined ) return document.documentElement.clientHeight;
			if ( document.body && document.body.clientHeight != undefined ) return document.body.clientHeight;
		},
		
	/*___| CHANGE CLASS NAME ~ 4 MAY 2009 by JACKY |___*/
		changeClass : function ( obj, name )
		{
			obj.className = name;
		},
		
	/*___| CHECK BROWSER |___*/
		checkBrowser : function ( )
		{
			if(window.XMLHttpRequest){ //Mozilla, Safari, IE7  
				if(!window.ActiveXObject){ // Mozilla, Safari,    
					return "mozilla";
				}else{    
					return "ie7";
				}
			}else {    
				return "ie6";
			}  
		},	
		
	/*___| GET CURRENT STYLE |___*/
		getStyle : function ( obj, style )
		{
			if ( obj.currentStyle ) return obj.currentStyle [ style ];
			else if ( window.getComputedStyle )
			{
				style = style.replace ( /([A-Z])/g, "-$1" ).toLowerCase();   
				return window.getComputedStyle ( obj, null ).getPropertyValue ( style );
			} else { return null; }
		},
		
		resizeArray : new Array( ),
		
		addResize : function ( name, func, params )
		{
			this.resizeArray.push ( { name:name, func:func, params:params } );
		},
		
		renderResize : function ( )
		{
			for ( var i = 0; i < this.resizeArray.length; i++ )
			{
				this.resizeArray[i].func ( this.resizeArray[i].params );
			}
		},
		
		over : function ( id, suffix )
		{
			var c = ( suffix != undefined ) ? suffix : "_o";
			var obj = document.getElementById(id);
			obj.className = obj.className.replace ( c, "" ) + c;
		},
		out : function ( id, suffix )
		{
			var c = ( suffix != undefined ) ? suffix : "_o";
			var obj = document.getElementById(id);
			obj.className = obj.className.replace ( c, "" );
		}
		
};

window.onresize = function () { F.renderResize(); };