/*
Asterop GeoIntelligence Intranet/Internet Framework

(c) Asterop 1999-2007

*/

/* ------------------------------------------------------------------------------------------- */

function LoadJSScript(url)
{
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);
}

/* ------------------------------------------------------------------------------------------- */

var 	CTools = 
{
	GetWidth: function(self)
	{
		var		dx = 0;
		if (self.innerHeight) // all except Explorer
		{
			dx = self.innerWidth;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			dx = document.documentElement.clientWidth;
		}
		else if (document.body) // other Explorers
		{
			dx = document.body.clientWidth;
		}
		
		return dx;
	},
	
	GetHeight: function (self)
	{
		var		dy = 0;
		if (self.innerHeight) // all except Explorer
		{
			dy = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			dy = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			dy = document.body.clientHeight;
		}
		
		return dy;
	},
	
	InsertDivBeforeParent: function(self)
	{
		var		outerdiv = document.createElement("div");
		var		parent   = self.parent;
		
		outerdiv.appendChild(self);
		if (parent)
			parent.appendChild(outerdiv);
		else
			document.body.appendChild(outerdiv);
		
		return outerdiv;
	},
	
	SetElementPosition: function(elem, X, Y, Width, Height)
	{
		var Style = elem.style;
		
		if (Style)
		{
			Style.left 	= String(X) + "px";
			Style.top	= String(Y) + "px";;
			if (Width && Height)
			{
				Style.width 	= String(Width) + "px";;
				Style.height	= String(Height) + "px";;
			}
		}
	},
	SetElementPositionFromRect: function(elem, pRect)
	{
		var Style = elem.style;
		
		if (Style)
		{
			Style.left 	= String(pRect.m_X1) + "px";
			Style.top	= String(pRect.m_Y1) + "px";;
			Style.width 	= String(pRect.m_X2 - pRect.m_X1) + "px";;
			Style.height	= String(pRect.m_Y2 - pRect.m_Y1) + "px";;
		}
	},
	
	GetWindowBottomRight: !(window.attachEvent && !window.opera)?function()
	{
		// other browser
		return new CPoint(window.innerWidth, window.innerHeight);
	}:function()
	{
		// IE
		var pt = new CPoint();
		if (document.documentElement && document.documentElement.clientWidth)
		{
			pt.m_X = document.documentElement.clientWidth;
			pt.m_Y = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			pt.m_X = document.body.clientWidth;
			pt.m_Y = document.body.clientHeight;
		}
		return pt;
	}
}

/* ------------------------------------------------------------------------------------------- */

var CRect = Class.create();

CRect.prototype = 
{
	initialize: function(x1, y1, x2, y2)
	{
		this.m_X1 	= Math.min(x1,x2)||0;
		this.m_X2	= Math.max(x1,x2)||0;
		this.m_Y1 	= Math.min(y1,y2)||0;
		this.m_Y2	= Math.max(y1,y2)||0;
	},
	GetWidth: function()
	{
		return (this.m_X2 - this.m_X1)
	},
	GetHeight: function()
	{
		return (this.m_Y2 - this.m_Y1)
	}
};

/* ------------------------------------------------------------------------------------------- */

var CPoint = Class.create();

CPoint.prototype = 
{
	initialize: function(x, y)
	{
		this.m_X 	= x||0;
		this.m_Y	= y||0;
	},
	InRect: function(rect)
	{
		return (this.m_X >= rect.m_X1) && (this.m_X <= rect.m_X2) && (this.m_Y >= rect.m_Y1) && (this.m_Y <= rect.m_Y2);
	}
};

/* ------------------------------------------------------------------------------------------- */

var CObject = Class.create();

CObject.prototype = 
{
	initialize: function(Name) 
	{
		this.m_Name  = Name;
	},

	GetName: function() 
	{
		return this.m_Name;
	}
}

/* ------------------------------------------------------------------------------------------- */

var	WM_LBUTTONDOWN		= 1;
var WM_LBUTTONUP		= 2;
var WM_RBUTTONDOWN		= 3;
var WM_RBUTTONUP		= 4;
var WM_MOUSEOVER		= 5;
var WM_MOUSEOUT			= 6;
var	WM_MOUSECLICK		= 7;
var	WM_MOUSEDBLCLK		= 8;
var WM_MOUSEMOVE		= 9;
var WM_MOUSEWHEEL		= 10;

var WM_COMMAND			= 20;
var WM_SETFOCUS			= 30;
var WM_KILLFOCUS		= 31;
var WM_CHANGE			= 32;
var WM_SELECT			= 33;
var WM_KEYPRESS			= 34;
var WM_KEYUP			= 35;
var WM_KEYDOWN			= 36;

var WM_DESTROYWINDOW	= 200;
var WM_CLOSEWINDOW		= 201;
var WM_SHOWWINDOW		= 202;

var WM_USER				= 10000;

var BN_CLICKED			= 1;
var BN_CHECKED			= 2;
var EN_CHANGE			= 3;	
var CBN_SELCHANGE		= 4;
var LBN_SELCHANGE		= 5;
var TBN_SELCHANGE		= 6;
var MNN_SELCHANGE		= 7;	// CMenu
var LVN_ITEMCHANGED		= 8; 	// CListCtrl

var CWindow = Class.create();
 
Object.extend(Object.extend(CWindow.prototype, CObject.prototype),
{
	initialize: function(name, ID, Parentwnd, Width, Height) 
	{
		CObject.prototype.initialize.call(this, name, ID, Parentwnd);
		this.m_Children = null;
		this.m_X		= 10;
		this.m_Y		= 100;			
		this.m_Width	= Width;
		this.m_Height	= Height;
		this.m_Style	= 0;
		this.m_Class    = "window";
		this.m_ID		= ID;
		this.m_Disabled 	= false;
		this.m_TempDisabled = false;
		this.m_ScriptName	= "";
		this.m_ScriptItem	= null;
		if(Parentwnd && Parentwnd.AddChild)
			Parentwnd.AddChild(this);
	},
	
	AddChild: function(childwnd)
	{
		if (this.m_Children == null)
			this.m_Children = new Array();
		this.m_Children[this.m_Children.length] = childwnd;
		childwnd.m_Parent = this;		
	},
  
	OnDraw: function()
	{
		return "The name of this window is "+this.m_Name+" Width = "+this.m_Width+" Height = "+this.m_Height+"<br>";
	},
	
	// Event functions (to be overloaded)
	OnLButtonDown: function()
	{
	},
	OnRButtonDown: function()
	{
	},
	OnLButtonUp: function()
	{
	},
	OnRButtonUp: function()
	{
	},
	OnMouseClick: function()
	{
	},
	OnMouseWheel: function(param1, param2)
	{
	},
	OnMouseDoubleClick: function()
	{
	},
	OnMouseOver: function()
	{
	},
	OnMouseOut: function()
	{
	},
	OnMouseMove: function()
	{
	},
	OnSetFocus: function()
	{
	},
	OnKillFocus: function()
	{
	},
	OnButtonClick: function(ctrlid)
	{
	},
	OnEditChange: function(ctrlid)
	{
	},
	OnCloseWindow: function()
	{
	},
	OnDestroyWindow: function()
	{
	},
	OnShowWindow: function(bShow)
	{
	},

	OnEnableWindow: function()
	{
	},

	OnDisableWindow: function()
	{
	},

	OnTempDisableWindow: function(bDisabled)
	{
	},

	OnDisableWindow: function()
	{
	},
	
	OnChange: function()
	{
	},

	OnSelChange: function()
	{
	},
	
	OnMenuChange: function()
	{
	},

	OnListCtrlChange: function()
	{
	},

	OnSelect: function()
	{
	},
	
	OnKeyPress: function()
	{
	},
	
	OnKeyUp: function()
	{
	},

	OnKeyDown: function()
	{
	},
	
	// ----------------------------------------------------------
	OnEvent: function()
	{
		if (!this.m_Disabled && !this.m_TempDisabled)
		{
			var args = $A(arguments), message = args.shift();
			switch(message)
			{
			case WM_LBUTTONDOWN :
				this.OnLButtonDown();
				break;
			case WM_RBUTTONDOWN :
				this.OnRButtonDown();
				break;
			case WM_LBUTTONUP :
				this.OnLButtonUp();
				break;
			case WM_RBUTTONUP :
				this.OnRButtonUp();
				break;
			case WM_MOUSEOVER :
				this.OnMouseOver();
				break;
			case WM_MOUSEOUT :
				this.OnMouseOut();
				break;
			case WM_MOUSEMOVE :
				this.OnMouseMove.apply(this, args);
				break;
			case WM_MOUSECLICK :
				this.OnMouseClick();
				break;
			case WM_MOUSEWHEEL:
				this.OnMouseWheel.apply(this, args);
				break;
			case WM_SETFOCUS :
				this.OnSetFocus();
				break;
			case WM_KILLFOCUS :
				this.OnKillFocus();
				break;
			case WM_MOUSEDBLCLK :
				this.OnMouseDoubleClick();
				break;
			case WM_CHANGE :
				this.OnChange();
				break;
			case WM_SELECT :
				this.OnSelect();
				break;
			case WM_KEYPRESS :
				this.OnKeyPress();
				break;
			case WM_KEYUP :
				this.OnKeyUp();
				break;
			case WM_KEYDOWN :
				this.OnKeyDown();
				break;
			case WM_COMMAND:
				var param1 = args.shift();
				if (param1 == BN_CLICKED)
					this.OnButtonClick.apply(this, args);
				else if (param1 == EN_CHANGE)
					this.OnEditChange.apply(this, args);
				else if (param1 == CBN_SELCHANGE)
					this.OnSelChange.apply(this, args);
				else if (param1 == LBN_SELCHANGE)
					this.OnSelChange.apply(this, args);
				else if (param1 == TBN_SELCHANGE)
					this.OnSelChange.apply(this, args);
				else if ((param1 == MNN_SELCHANGE) && this.OnMenuChange)
					this.OnMenuChange.apply(this, args);
				else if ((param1 == LVN_ITEMCHANGED) && this.OnListCtrlChange)
					this.OnListCtrlChange.apply(this, args);
				break;
			case WM_DESTROYWINDOW:
				this.OnDestroyWindow();
				this.DestroyWindow();
				break;
			case WM_CLOSEWINDOW:
				this.OnCloseWindow();
				break;
			case WM_SHOWWINDOW:
				this.OnShowWindow.apply(this, args);
				break;
			};
		};
	},
	
	// ---- Internal events handlers
	OnEIMouseClick: function(e)
	{
//		Event.stop(e);
		this.m_CurrentEvent = e;
		this.OnEvent(WM_MOUSECLICK, 0, 0);
	},

	OnEIMouseDoubleClick: function(e)
	{
//		Event.stop(e);
		this.m_CurrentEvent = e;
		this.OnEvent(WM_MOUSEDBLCLK, 0, 0);
	},

	OnEIMouseUp: function(e)
	{
//		Event.stop(e);
		this.m_CurrentEvent = e;
		this.OnEvent(WM_LBUTTONUP, 0, 0);
	},

	OnEIMouseDown: function(e)
	{
//		Event.stop(e);
		this.m_CurrentEvent = e;
		this.OnEvent(WM_LBUTTONDOWN, 0, 0);
	},

	OnEIMouseOver: function(e)
	{
//		Event.stop(e);
		this.m_CurrentEvent = e;
		this.OnEvent(WM_MOUSEOVER, 0, 0);
	},

	OnEIMouseOut: function(e)
	{
//		Event.stop(e);
		this.m_CurrentEvent = e;
		this.OnEvent(WM_MOUSEOUT, 0, 0);
	},

	OnEIMouseMove: function(e)
	{
//		Event.stop(e);
		this.m_CurrentEvent = e;
		this.OnEvent(WM_MOUSEMOVE, 0, 0);
	},

	OnEISetFocus: function(e)
	{
//		Event.stop(e);
		this.m_CurrentEvent = e;
		this.OnEvent(WM_SETFOCUS, 0, 0);
	},

	OnEIKillFocus: function(e)
	{
//		Event.stop(e);
		this.m_CurrentEvent = e;
		this.OnEvent(WM_KILLFOCUS, 0, 0);
	},

	OnEIChange: function(e)
	{
		this.m_CurrentEvent = e;
		this.OnEvent(WM_CHANGE, 0, 0);
	},
	
	OnEISelect: function(e)
	{
//		Event.stop(e);
		this.m_CurrentEvent = e;
		this.OnEvent(WM_SELECT, 0, 0);
	},
	
	OnEIKeyPress: function(e)
	{
		this.m_CurrentEvent = e;
		this.OnEvent(WM_KEYPRESS, 0, 0);
	},
	
	OnEIKeyUp: function(e)
	{
		this.m_CurrentEvent = e;
		this.OnEvent(WM_KEYUP, 0, 0);
	},

	OnEIKeyDown: function(e)
	{
		this.m_CurrentEvent = e;
		this.OnEvent(WM_KEYDOWN, 0, 0);
	},
	OnEIMouseWheel: function(e)
	{
		Event.stop(e);
		this.m_CurrentEvent = e;
		
		if (this.OnEvent)
		{
			var delta 	= 0;
			var pt;	

			//Bug in FF : clientX / clientY are wrong on event MouseWheel
			if (Prototype.Browser.Gecko)
			{
				pt = new CPoint(e.screenX, e.screenY);
				this.ScreenPointToClientPoint(pt);
			}	
			else
				pt = this.GetClientCursorPos();
			
			if (e.wheelDelta) 
			{
				delta = e.wheelDelta/120; 
				if (window.opera) 
					delta = -delta;
			} 
			else if (e.detail) 
				delta = -e.detail/3;
			if (delta)
			{
				if (e.preventDefault)
	        		e.preventDefault();
				e.returnValue = false;
				this.OnEvent(WM_MOUSEWHEEL, pt, delta);
			}
		}
	},
	OnEndCreate: function()
	{
		var		Style;
		
		Style = this.m_ScriptItem.style;
		Style.backgroundColor  	= "white";
		Style.borderStyle  		= "solid";
		Style.borderWidth  		= "1px";
	},

	SetEventHandlers: function()
	{
		if (this.m_ScriptItem)
		{
			Event.observe(this.m_ScriptItem, 'click', this.m_hMouseClick = this.OnEIMouseClick.bindAsEventListener(this)); 			
			Event.observe(this.m_ScriptItem, 'dblclick', this.OnEIMouseDoubleClick.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'mouseup', this.OnEIMouseUp.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'mousedown', this.OnEIMouseDown.bindAsEventListener(this)); 

			Event.observe(this.m_ScriptItem, 'mouseover', this.OnEIMouseOver.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'mousemove', this.OnEIMouseMove.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'mouseout', this.OnEIMouseOut.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'focus', this.OnEISetFocus.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'blur', this.OnEIKillFocus.bindAsEventListener(this)); 

			Event.observe(this.m_ScriptItem, 'change', this.OnEIChange.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'select', this.OnEISelect.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'keypress', this.OnEIKeyPress.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'keyup', this.OnEIKeyUp.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'keydown', this.OnEIKeyDown.bindAsEventListener(this)); 
			Event.observe(this.m_ScriptItem, 'keydown', this.OnEIKeyDown.bindAsEventListener(this));
			//MouseWheel For IE
			Event.observe(this.m_ScriptItem, 'mousewheel', this.OnEIMouseWheel.bindAsEventListener(this));
			//MouseWheel For FF
			Event.observe(this.m_ScriptItem, 'DOMMouseScroll', this.OnEIMouseWheel.bindAsEventListener(this));
		};
	},
	Create: function()
	{
		this.m_ScriptName = this.m_Class+"_"+this.m_ID;
		this.m_ScriptItem = document.createElement("div");
		
		if (this.m_Parent)
			this.m_Parent.m_ScriptItem.appendChild(this.m_ScriptItem);
		else
			document.body.appendChild(this.m_ScriptItem);
		
		Element.extend(this.m_ScriptItem);		
		
		this.m_ScriptItem.id 			= this.m_ScriptName;
		this.m_ScriptItem.name 			= this.m_ScriptName;
		
		var		Style;
		
		Style = this.m_ScriptItem.style;
			
		Style.position 	   		= 'absolute';		
		this.SetEventHandlers();
		
		if (this.m_Children != null)
		{
			var i = 0;
			for (i = 0;i<this.m_Children.length;i++)
			{
				if (this.m_Children[i] != null)
					this.m_Children[i].Create();
			};
		};

		this.OnEndCreate();
		this.m_Width = (this.m_Width == undefined)?800:this.m_Width;
		this.m_Height = (this.m_Height == undefined)?600:this.m_Height;
		
//		var 	X2 = parentWidth - this.m_X - this.m_Width+1;
//		var 	Y2 = parentHeight - this.m_Y - this.m_Height+1;
		
		Style.left    			= this.m_X+"px";
//		Style.right   			= this.m_Width+1+"px";
		Style.top     			= this.m_Y+"px";
//		Style.bottom  			= this.m_Height+1+"px";
		Style.margin			= "0px";
		Style.padding			= "0px";

/*		if (Style.pixelWidth != null)
			Style.pixelWidth = this.m_Width;
		if (Style.pixelHeight != null)
			Style.pixelHeight = this.m_Height;*/
		Style.width = this.m_Width+'px';
		Style.height = this.m_Height+'px';
	},
  
	SendMessage: function(message, param1, param2)
	{
		this.OnEvent(message, param1, param2);
	},

	EnableWindow: function(bEnable)
	{
		this.m_Disabled = !bEnable;
		if (bEnable)
			this.OnEnableWindow();
		else
			this.OnDisableWindow();
	},

	TempDisableWindow: function(bDisable)
	{
		this.m_TempDisabled = bDisable;
		this.OnTempDisableWindow(bDisable);
	},
	
	ShowWindow: function(bShow)
	{
		this.SendMessage(WM_SHOWWINDOW, bShow);
	},
	
	MoveWindow: function(dX, dY)
	{
		this.m_X += dX;
		this.m_Y += dY;
		
		if (this.m_ScriptItem)
		{
			var		Style;
			
			Style = this.m_ScriptItem.style;
			Style.left    			= this.m_X+"px";
			Style.top     			= this.m_Y+"px";
		};
	},
	
	SetPosition: function(X, Y, Width, Height)
	{
		this.m_X = X;
		this.m_Y = Y;
		
		if ((Width != undefined) && (Height != undefined))
		{
			this.m_Width  = Width;
			this.m_Height = Height;
		};
		
		if (this.m_ScriptItem)
		{
			var		Style;
			
			Style = this.m_ScriptItem.style;
		
			// Move it now
			var		parentWidth = 0;
			var		parentHeight = 0;
			
			if (this.m_Parent != null)
			{
				parentWidth = this.m_Parent.m_Width;
				parentHeight = this.m_Parent.m_Height;
			}
			else
			{
				parentWidth = CTools.GetWidth(window);
				parentHeight = CTools.GetHeight(window);
			};
				
			
			var 	X2 = parentWidth - this.m_X - this.m_Width+1;
			var 	Y2 = parentHeight - this.m_Y - this.m_Height+1;
			
			Style.left    			= this.m_X+"px";
//			Style.right   			= X2+"px";
			Style.top     			= this.m_Y+"px";
//			Style.bottom  			= Y2+"px";
/*			if (Style.pixelWidth != null)
				Style.pixelWidth = this.m_Width;
			if (Style.pixelHeight != null)
				Style.pixelHeight = this.m_Height;*/
			Style.width = this.m_Width+'px';
			Style.height = this.m_Height+'px';
		};
	},	
	
	TempEnableWindow: function(bEnable)
	{
		this.TempDisableWindow(!bEnable);
		if (this.m_Children)
		{
			for (var i = 0;i<this.m_Children.length;i++)
			{
				if (this.m_Children[i] != null)
					this.m_Children[i].TempEnableWindow(bEnable);
			};
		};
	},
	
	DestroyWindow: function()
	{
		if (this.m_Children)
		{
			for(var i = 0;i<this.m_Children.length;i++)
			{
				if (this.m_Children[i] != null)
					this.m_Children[i].DestroyWindow();
			};
		};
		if (this.m_ScriptItem) this.m_ScriptItem.parentNode.removeChild(this.m_ScriptItem);
		for(property in this) { delete this[property]; }
	},
	
	SetBkColor: function(color)
	{
		if (this.m_ScriptItem)
			this.m_ScriptItem.style.backgroundColor = color;
	},
	GetClientCursorPos: function()
	{
		var pPoint = new CPoint();
		
		if (this.m_CurrentEvent)
		{
			pPoint.m_X = Event.pointerX(this.m_CurrentEvent);
			pPoint.m_Y = Event.pointerY(this.m_CurrentEvent);
			this.ScreenPointToClientPoint(pPoint);
		}
		return pPoint;
	},
	GetControlPosition: function(Control)
	{
		var pos;
		var pPoint = new CPoint();
		
		pos = Position.cumulativeOffset($(Control));
		if (pos.length > 0)
			pPoint.m_X = pos[0];
		if (pos.length > 1)
			pPoint.m_Y = pos[1];
		
		return pPoint; 
	},
	ScreenPointToClientPoint: function(pPoint)
	{
		var pControlPoint = this.GetControlPosition(this.m_ScriptItem);
		
		pPoint.m_X = pPoint.m_X - pControlPoint.m_X;
		pPoint.m_Y = pPoint.m_Y - pControlPoint.m_Y;
	},
	ClientPointToScreenPoint: function(pPoint)
	{
		var pControlPoint = this.GetControlPosition(this.m_ScriptItem);
		
		pPoint.m_X = pPoint.m_X + pControlPoint.m_X;
		pPoint.m_Y = pPoint.m_Y + pControlPoint.m_Y;
	},
	ClientRectToScreenRect: function(rc)
	{
		var	pPoint = new CPoint();
		
		pPoint.m_X = rc.m_X1;
		pPoint.m_Y = rc.m_Y1;
		
		this.ClientPointToScreenPoint(pPoint);
		rc.m_X2 = pPoint.m_X + (rc.m_X2 - rc.m_X1);
		rc.m_X1 = pPoint.m_X;
		rc.m_Y2 = pPoint.m_Y + (rc.m_Y2 - rc.m_Y1);
		rc.m_Y1 = pPoint.m_Y;
	},
	ScreenRectToClientRect: function(rc)
	{
		var	pPoint = new CPoint();
		
		pPoint.m_X = rc.m_X1;
		pPoint.m_Y = rc.m_Y1;
		
		this.ScreenPointToClientPoint(pPoint);
		rc.m_X2 = pPoint.m_X + (rc.m_X2 - rc.m_X1);
		rc.m_X1 = pPoint.m_X;
		rc.m_Y2 = pPoint.m_Y + (rc.m_Y2 - rc.m_Y1);
		rc.m_Y1 = pPoint.m_Y;
	}
});

/* ------------------------------------------------------------------------------------------- */

var CControl = Class.create();

Object.extend(Object.extend(CControl.prototype, CWindow.prototype),
{
	initialize: function(name, ID, Parentwnd, Width, Height)
	{
		CWindow.prototype.initialize.call(this, name, ID, Parentwnd, Width, Height);
		this.m_Class = "control";
	},

	OnDraw: function()
	{
		return "<b>"+this.m_Name+"</b>";
	}
});

/* ------------------------------------------------------------------------------------------- */

var CButton = Class.create();

CButton.BSTT_UP			= 1;
CButton.BSTT_DOWN		= 2;
CButton.BSTT_DISABLED	= 4;

CButton.BS_PUSHBUTTON	= 0;
CButton.BS_CHECKBOX		= 1;
CButton.BS_RADIO		= 2;

Object.extend(Object.extend(CButton.prototype, CWindow.prototype),
{
	initialize: function(name, ID, Parentwnd, Width, Height, strCaption, iStyle)
	{
		CWindow.prototype.initialize.call(this, name, ID, Parentwnd, Width, Height);
		this.m_Class = "button";
		this.m_strCaption = strCaption;
		this.m_iState = CButton.BSTT_UP;
		this.m_iStyle = iStyle || CButton.BS_PUSHBUTTON;
	},

	OnEndCreate: function()
	{
		if (this.m_ScriptItem)
		{
			var input = elemt = document.createElement('input');
			var width = 0;
			
			this.m_ScriptItem.style.borderStyle = 'none';
			this.SetBkColor('transparent');
			
			if(input)
			{
				switch(this.m_iStyle)
				{
					case CButton.BS_PUSHBUTTON:
						input.type = 'button';
						input.value = this.m_strCaption;
						break;
					case CButton.BS_CHECKBOX:
						input.type = 'checkbox';
						this.m_ScriptItem.appendChild(input);
						width = input.offsetWidth + 10;
						elemt = document.createElement('span');
						elemt.innerHTML += this.m_strCaption;
						break;
					case CButton.BS_RADIO:
						input.type = 'radio';
						this.m_ScriptItem.appendChild(input);
						width = input.offsetWidth + 10;
						elemt = document.createElement('span');
						elemt.innerHTML += this.m_strCaption;
						break;
				}	
				
				this.m_ScriptItem.appendChild(elemt);
				this.m_ctrlBtn = input;
				this.m_ScriptItem.style.cursor = 'default';
				this.m_Width = this.m_Width || (width + elemt.offsetWidth);
				this.m_Height = this.m_Height || Math.max(input.offsetHeight, elemt.offsetHeight);
				this.SetState(this.m_iState);
			}
		}
	},
	
	OnMouseClick: function()
	{
		if(this.m_iStyle != CButton.BS_PUSHBUTTON)
			Event.stop(this.m_CurrentEvent);
	},
	
	OnLButtonUp: function()
	{
		if(this.m_iState != CButton.BSTT_DISABLED)
		{
			if(this.m_iStyle != CButton.BS_PUSHBUTTON)
			{
				this.m_iState = (this.m_iState == CButton.BSTT_UP)?CButton.BSTT_DOWN:CButton.BSTT_UP;
				this._updateDisplay(this.m_iState);
				Event.stop(this.m_CurrentEvent);
			}
			if(this.m_Parent != null)
				this.m_Parent.SendMessage(WM_COMMAND, BN_CLICKED, this.m_ID);
		}
	},
	
	_updateDisplay: function(iState)
	{
		if(this.m_ctrlBtn)
		{
			this.m_ctrlBtn.disabled = (iState == CButton.BSTT_DISABLED);
			this.m_ctrlBtn.checked = (iState == CButton.BSTT_DOWN);
		}
	},

	SetState: function(iState)
	{
		if((this.m_iStyle != CButton.BS_PUSHBUTTON) || (iState != CButton.BSTT_DOWN))
			this.m_iState = iState;
		this._updateDisplay(this.m_iState);
	},
	
	GetCheck: function()
	{
		return (this.m_iState == CButton.BSTT_DOWN);
	}
});

/* ------------------------------------------------------------------------------------------- */

var CBitmapButton = Class.create();

Object.extend(Object.extend(CBitmapButton.prototype, CButton.prototype), 
{
	handlers: Array(
		function()
		{
			if(this.m_iState == CButton.BSTT_UP)
			{
				var obj = this;
	
				this._updateDisplay(CButton.BSTT_DOWN);	
				Event.stopObserving(this.m_ScriptItem, 'click', this.m_hMouseClick);
				setTimeout(
					function() 
					{
						Event.observe(obj.m_ScriptItem, 'click', obj.m_hMouseClick);
						obj._updateDisplay(CButton.BSTT_UP);	
					}, 150);
				return true;
			}
			return false;
		},
		
		function()
		{
			if(this.m_iState != CButton.BSTT_DISABLED)
			{
				var iState = (this.m_iState == CButton.BSTT_UP)?CButton.BSTT_DOWN:CButton.BSTT_UP;
				this.SetState(iState);
				return true;
			}
			return false;
		},
		
		function()
		{
			if(this.m_iState == CButton.BSTT_UP)
			{
				this.SetState(CButton.BSTT_DOWN);
				return true;
			}
			return false;
		}
	),
		
	initialize: function(name, ID, Parentwnd, iWidth, iHeight, strRsceUrl, strCaption, iStyle, strRsceSelUrl, strRsceDisabledUrl, strRsceFocusUrl)
	{
		CButton.prototype.initialize.call(this, name, ID, Parentwnd, iWidth, iHeight, strCaption, iStyle);
		this.m_Class = 'bitmapbutton';

		var imgSel, imgDisabled, imgFocus;
		
		if(strRsceSelUrl)
		{
			imgSel = new CBitmap(strRsceSelUrl);
		}
		if(strRsceDisabledUrl)
		{
			imgDisabled = new CBitmap(strRsceDisabledUrl);
		}
		if(strRsceFocusUrl)
		{
			imgFocus = new CBitmap(strRsceFocusUrl);
		}
	    this.m_images = 
		{
	    	dflt: new CBitmap(strRsceUrl),
	    	selected: imgSel,
	    	disabled: imgDisabled,
	    	focus: imgFocus
		}
	},

	OnEndCreate: function()
	{
		this.m_Width = Math.max(this.m_Width, this.m_images.dflt.GetWidth());
		this.m_Height = Math.max(this.m_Height, this.m_images.dflt.GetHeight());
		if(this.m_images.selected)
		{
			this.m_Width = Math.max(this.m_Width, this.m_images.selected.GetWidth());
			this.m_Height = Math.max(this.m_Height, this.m_images.selected.GetHeight());
		}
		if(this.m_images.disabled)
		{
			this.m_Width = Math.max(this.m_Width, this.m_images.disabled.GetWidth());
			this.m_Height = Math.max(this.m_Height, this.m_images.disabled.GetHeight());
		}
		if(this.m_images.focus)
		{
			this.m_Width = Math.max(this.m_Width, this.m_images.focus.GetWidth());
			this.m_Height = Math.max(this.m_Height, this.m_images.focus.GetHeight());
		}
		
		if(this.m_strCaption)
		{
			var div = document.createElement('div');
			div.innerHTML += this.m_strCaption;
			this.m_ScriptItem.appendChild(div);
			div.style.position = 'absolute';
			var x = (this.m_Width - div.offsetWidth)>>1,
				y = (this.m_Height - div.offsetHeight)>>1;
			div.style.left = x + 'px';
			div.style.top = y + 'px';
		}
		this.SetBkColor('transparent');
		this.m_ScriptItem.style.borderStyle = 'none';
		this.m_ScriptItem.style.backgroundRepeat = 'no-repeat';
		this.m_ScriptItem.style.backgroundPosition = 'center center';
		this._updateDisplay(this.m_iState);
	},

	OnLButtonUp: CWindow.prototype.OnLButtonUp,

	OnMouseClick: function()
	{
		if(this.handlers[this.m_iStyle].call(this) && (this.m_Parent != null))
			this.m_Parent.SendMessage(WM_COMMAND, BN_CLICKED, this.m_ID);
	},
	
	_updateDisplay: function(iState)
	{
		if(this.m_ScriptItem)
		{
			var img;
			var cursor = 'pointer'; 
			switch(iState)
			{
				case CButton.BSTT_UP:
					img = (this.m_bIn)?(this.m_images.focus || this.m_images.dflt):this.m_images.dflt;
					break;
				case CButton.BSTT_DOWN:
					img = this.m_images.selected || this.m_images.dflt;
					break;
				case CButton.BSTT_DISABLED:
					cursor = 'default';
					img = this.m_images.disabled || this.m_images.dflt;
					break;
			}
			this.m_ScriptItem.style.backgroundImage = img.GetBackgroundImage();
			this.m_ScriptItem.style.cursor = cursor;
		}
	},
	
	OnMouseOver: function()
	{
		this.m_bIn = true;
		this._updateDisplay(this.m_iState);
	},
	
	OnMouseOut: function()
	{
		this.m_bIn = false;
		this._updateDisplay(this.m_iState);
	}
});

/* ------------------------------------------------------------------------------------------- */

var CTooltipCtrl = Class.create();

CTooltipCtrl.GetCoord = function(elem, x, y)
{
	var pt = CTools.GetWindowBottomRight();
	var w = elem.offsetWidth;
	var h = elem.offsetHeight;
	pt.m_X = (x+15+w) < pt.m_X?x+15:Math.max(x-w-5,0);
	pt.m_Y = (y+15+h) < pt.m_Y?y+15:Math.max(y-h-5,0);
	return pt;
}


CTooltipCtrl.prototype =
{
	m_iDefaultDelay: 1000,
	
	m_oStyle: 
	{
		border: '1px solid #A5CFE9',
		padding: '1px',
		color: '#333333',
		backgroundColor: '#FFFF66',
		filter: 'alpha(opacity=85)', // IE
		opacity: '0.85', // FF
		fontFamily: 'Arial, Helvetica, sans-serif',
		fontWeight: 'normal',
		fontSize: '11px'
	},
	
	initialize: function(iDelay) 
	{
		this.m_Class = 'CTooltipCtrl';
		this.m_X = 0;
		this.m_Y = 0;
		this.m_iTimer = 0;
		this.m_TooltipSet = {};
		this.m_iDelay = iDelay || this.m_iDefaultDelay;
	},
	
	SetHandler: function(id, variant)
	{
		// variant is a string or a function ...
		if(id && !this.m_TooltipSet[id])
		{
			this.m_TooltipSet[id] = (variant instanceof Function)?variant:function(ctrl) {	ctrl.m_ScriptItem.innerHTML = variant;	};
		}
	},
	
	OnMouseMove: function(Parentwnd, id, x, y, bForce)
	{
		if(bForce || (this.m_X != x) || (this.m_Y != y))
		{
			if(this.m_ScriptItem)
				this.m_ScriptItem.style.visibility = 'hidden';
			clearTimeout(this.m_iTimer);
			this.m_X = x;
			this.m_Y = y;
			this.m_iTimer = setTimeout(this.OnTimeout.bind(this, Parentwnd, id), this.m_iDelay);
		}
	},
	
	OnMouseOut: function()
	{
		if(this.m_ScriptItem)
			this.m_ScriptItem.style.visibility = 'hidden';
		clearTimeout(this.m_iTimer);
	},
	
	OnTimeout: function(Parentwnd, id)
	{
		if(this.m_TooltipSet[id])
		{
			if(!this.m_ScriptItem)
			{
				CTooltipCtrl.prototype.m_ScriptItem = document.createElement('div');
				document.body.appendChild(this.m_ScriptItem);
				this.m_ScriptItem.style.position = 'absolute';
				this.m_ScriptItem.style.visibility = 'hidden';
				Object.extend(this.m_ScriptItem.style, this.m_oStyle);
			}
			
			this.m_TooltipSet[id](this, Parentwnd, id);
			
			var pt = CTooltipCtrl.GetCoord(this.m_ScriptItem, this.m_X, this.m_Y);			
			CTools.SetElementPosition(this.m_ScriptItem, pt.m_X, pt.m_Y);
			this.m_ScriptItem.style.visibility = 'visible';
		}		
	}
}

/* ------------------------------------------------------------------------------------------- */

var CToolBar = Class.create();

Object.extend(Object.extend(CToolBar.prototype, CWindow.prototype),
{
	initialize: function(name, ID, Parentwnd, Width, Height)
	{
		CWindow.prototype.initialize.call(this, name, ID, Parentwnd, Width, Height);
		this.m_Class = 'toolbar';
		this.m_Width = Width || 1;
		this.m_Height = Height || 1;
	},
	
	OnButtonClick: function(id)
	{
		if(id)
		{
			var i;
			var aTmp;
			var bFound = false;
			
			for(property in this.m_RadioSet)
			{
				aTmp = this.m_RadioSet[property];
				if(aTmp instanceof Array)
				{
					for(i = 0; !bFound && (i < aTmp.length); ++i)
					{
						bFound = (aTmp[i].m_ID == id);
					}
					if(bFound)
					{	
						for(i = 0; i < aTmp.length; ++i)
						{
							if(aTmp[i].m_ID != id)
							{
								aTmp[i].SetState(CButton.BSTT_UP);
							}
						}
					}
				}
			}
			this.m_Parent.SendMessage(WM_COMMAND, BN_CLICKED, id);
		}
	},

	Register: function(strGroupName, button)
	{
		var aTmp;
		var aGroup;
		var remove = function(button)
		{
			var obj = button;
			return this.select(
				function(value) 
				{
					return (value != obj);
		    	});
  		}
		
		if(!this.m_RadioSet)
			 this.m_RadioSet = {};
		for(property in this.m_RadioSet)
		{
			aTmp = this.m_RadioSet[property];
			if(aTmp instanceof Array)
			{
				this.m_RadioSet[property] = remove.call(aTmp, button);
				if(property == strGroupName)
				{
					aGroup = this.m_RadioSet[property];
				}
			}
		}
		if(!aGroup)
		{
			this.m_RadioSet[strGroupName] = aGroup = new Array();
		}
		for(var i = 0, bLoop = button.GetCheck(); bLoop && (i < aGroup.length); ++i)
		{
			if(aGroup[i].GetCheck())
				aGroup[i].SetState(CButton.BSTT_UP);
		}
		aGroup.push(button);
	},	
	
	OnEndCreate: function()
	{
		var obj;
		var x = 0;
		
		if(this.m_Children instanceof Array)
		{
			for(i = 0; i < this.m_Children.length; ++i)
			{
				obj = this.m_Children[i];
				obj.SetPosition(x, 0);
				x += obj.m_Width;
				this.m_Height = Math.max(this.m_Height, obj.m_Height);
			}
		}
		this.m_Width = Math.max(this.m_Width, x);
		if(this.m_pendingTooltips)
		{
			var window;
			for(i = 0; i < this.m_pendingTooltips.length; ++i)
			{
				window = this.m_pendingTooltips[i];
				Event.observe(window.m_ScriptItem, 'mousemove', this.OnMouseMove.bindAsEventListener(this, window));
				Event.observe(window.m_ScriptItem, 'mouseout', this.OnMouseOut.bind(this));
			}
			delete this.m_pendingTooltips;
		}
	},
	
	SetTooltip: function(variant, window)
	{
		if(variant && window)
		{
			if(!this.m_oTooltipCtrl)
			{
				this.m_oTooltipCtrl = new CTooltipCtrl();
			}
			this.m_oTooltipCtrl.SetHandler(window.m_ID, variant);
			if(window.m_ScriptItem)
			{
				var handler = this.OnMouseMove.bindAsEventListener(this, window);
				Event.observe(window.m_ScriptItem, 'mousemove', handler);
				Event.observe(window.m_ScriptItem, 'mouseover', handler);
				Event.observe(window.m_ScriptItem, 'mouseout', this.OnMouseOut.bind(this));
			}
			else
			{
				if(!this.m_pendingTooltips)
					this.m_pendingTooltips = new Array();
				this.m_pendingTooltips.push(window);
			}
		}
	},
	
	OnMouseMove: function(e, window)
	{
		if (!this.m_Disabled && !this.m_TempDisabled)
		{
			if(window && this.m_oTooltipCtrl)
			{
				var bodyScrollTop = document.documentElement&&document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;
				var bodyScrollLeft = document.documentElement&&document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft;
				
				var mouseX=e.pageX?e.pageX:e.clientX+bodyScrollLeft;
				var mouseY=e.pageY?e.pageY:e.clientY+bodyScrollTop;
	
				this.m_oTooltipCtrl.OnMouseMove(this, window.m_ID, mouseX, mouseY);
			}
		}
	},
	
	OnMouseOut: function()
	{
		if(this.m_oTooltipCtrl)
		{
			this.m_oTooltipCtrl.OnMouseOut();
		}		
	}	
});

/* ------------------------------------------------------------------------------------------- */

var CBitmap = Class.create();

Object.extend(CBitmap,
{
	m_oBitmaps: {},

	m_iCounter: 0,
	
	m_vCallBacks: new Array(),
	
	_register: function(oImage)
	{
		if(!oImage.complete && !this.m_oBitmaps[oImage.src])
		{
			++CBitmap.m_iCounter;
			CBitmap.m_oBitmaps[oImage.src] = oImage;
			oImage.onload = oImage.onabort = oImage.onerror = function() {CBitmap._loaded(oImage);};
		}
	},
	
	_loaded: function(oImage)
	{
		delete CBitmap.m_oBitmaps[oImage.src];
		--CBitmap.m_iCounter;
		if(!CBitmap.m_iCounter)
		{
			for(var i = 0; i < CBitmap.m_vCallBacks.length; ++i)
				CBitmap.m_vCallBacks[i]();
			CBitmap.m_vCallBacks = new Array();
		}
	},
	
	_loadImages: function(callBack)
	{
		if(CBitmap.m_iCounter)
			CBitmap.m_vCallBacks.push(callBack);
		else
			callBack();
	}
});

CBitmap.prototype =
{
	initialize: function(url)
	{
		this.m_Image = new Image();
		this.m_Image.src = url;
		CBitmap._register(this.m_Image);
	},
	
	LoadBitmap: function(url)
	{
		this.m_Image.src = url;
	},
	
	GetWidth: function()
	{
		return this.m_Image.width || 20;
	},
	
	GetHeight: function()
	{
		return this.m_Image.height || 20;
	},
	
	GetBackgroundImage: function()
	{
		return 'url(' +this.m_Image.src+ ')';
	}
};

/* ------------------------------------------------------------------------------------------- */

var CStatic = Class.create();

Object.extend(Object.extend(CStatic.prototype, CControl.prototype),
{
	initialize: function(name, ID, Parentwnd, Width, Height)
	{
		CControl.prototype.initialize.call(this, name, ID, Parentwnd, Width, Height);
		this.m_Class = "static";
		this.m_Text = name;
	},
	
	SetText: function(text)
	{
		this.m_Text = text;
		if (this.m_ScriptItem)
			this.m_ScriptItem.innerHTML = this.m_Text;
	},

	OnEndCreate: function()
	{
		if(this.m_ScriptItem)
		{
			this.m_ScriptItem.innerHTML += this.m_Text;
			this.m_Width = this.m_Width || this.m_ScriptItem.offsetWidth;
			this.m_Height = this.m_Height || this.m_ScriptItem.offsetHeight;
		}
	}	
});

/* ------------------------------------------------------------------------------------------- */

var CEdit = Class.create();

Object.extend(Object.extend(CEdit.prototype, CControl.prototype),
{
	initialize: function(name, ID, Parentwnd, Width, Height)
	{
		CControl.prototype.initialize.call(this, name, ID, Parentwnd, Width, Height);
		this.m_Class = "edit";
		this.m_Text = name;
	},
	
	SetText: function(text)
	{
		if (this.m_ctrlEdit)
			this.m_ctrlEdit.innerText = text;
		this.m_Text = text;
	},

	GetText: function()
	{
		if (this.m_ctrlEdit)
		{
			this.m_Text = this.m_ctrlEdit.value;
			return this.m_ctrlEdit.value;
		}
		else
			return this.m_Text;
	},
	
	OnTempDisableWindow: function(bDisabled)
	{
		if (this.m_ctrlEdit)
			this.m_ctrlEdit.disabled = bDisabled;
	},
	
	OnDisableWindow: function()
	{
		OnTempDisableWindow(true);
	},

	OnEnableWindow: function()
	{
		OnTempDisableWindow(false);
	},	
	
	OnChange: function()
	{
		if (this.m_Parent != null)
			this.m_Parent.SendMessage(WM_COMMAND, EN_CHANGE, this.m_ID);
	},

	OnKeyPress: function()
	{
		if (this.m_Parent != null)
			this.m_Parent.SendMessage(WM_COMMAND, EN_CHANGE, this.m_ID);
	},

	OnDraw: function()
	{
	},
	
	OnEndCreate: function()
	{
		// Construct the html code?
		if (this.m_ScriptItem)
		{
			// Create an edit control with the sizes of the surrounding object
			this.m_ctrlEdit = document.createElement("INPUT");
			
			this.m_ScriptItem.style.borderStyle = "none";
			this.SetBkColor("transparent");
			
			if (this.m_ctrlEdit)
			{
				this.m_ctrlEdit.type = "text";
				this.m_ScriptItem.appendChild(this.m_ctrlEdit);
				Event.observe(this.m_ctrlEdit, 'change', 	this.OnEIChange); 
			};
		};
		return null;
	}
});

/* ------------------------------------------------------------------------------------------- */

var CCombo = Class.create();

Object.extend(Object.extend(CCombo.prototype, CControl.prototype),
{
	initialize: function(name, ID, Parentwnd, Width, Height)
	{
		CControl.prototype.initialize.call(this, name, ID, Parentwnd, Width, Height);
		this.m_Class 	= "combo";
		this.m_Content 	= new Array();
	},
	
	OnTempDisableWindow: function(bDisabled)
	{
		if (this.m_ctrlCombo)
		{
			this.m_ctrlCombo.disabled = bDisabled;
			if (bDisabled)
				this.m_ctrlCombo.hide(bDisabled);
			else
				this.m_ctrlCombo.show();
		};
	},
	
	OnDisableWindow: function()
	{
		OnTempDisableWindow(true);
	},

	OnEnableWindow: function()
	{
		OnTempDisableWindow(false);
	},	
	
	OnChange: function()
	{
		if (this.m_Parent != null)
			this.m_Parent.SendMessage(WM_COMMAND, CBN_SELCHANGE, this.m_ID);
	},

	AddString: function(text, id)
	{
		if (id == null)
			id = text;
			
		this.m_Content[this.m_Content.length] = new Option(text, id);
		if (this.m_ctrlCombo)
			this.m_ctrlCombo.options[this.m_ctrlCombo.options.length] = new Option(text, id);
	},
	
	ResetContent:function()
	{
		this.m_Content.length = 0;
		if (this.m_ctrlCombo)
			this.m_ctrlCombo.option.length = 0;
	},
	
	GetLBText:function(index)
	{
		if (index < this.m_Content.length)
			return this.m_Content[index];
		else
			return "";
	},
	
	GetCount:function()
	{
		return this.m_Content.length;
	},
	
	OnDraw: function()
	{
	},
	
	OnEndCreate: function()
	{
		// Contrusct the html code?
		if (this.m_ScriptItem)
		{
			// Create an edit control with the sizes of the surrounding object
			this.m_ctrlCombo = document.createElement("SELECT");
			
			this.m_ScriptItem.style.borderStyle = "none";
			this.SetBkColor("transparent");
			
			if (this.m_ctrlCombo)
			{
				var i = 0;
				for (i = 0;i<this.m_Content.length;i++)
				{
					this.m_ctrlCombo.options[i] = new Option(this.m_Content[i].text, this.m_Content[i].value);
				};
				
				var			Style = this.m_ctrlCombo.style;
				if (Style.pixelWidth != null)
					Style.pixelWidth = this.m_Width;
				if (Style.pixelHeight != null)
					Style.pixelHeight = this.m_Height;

				Style.width 	= this.m_Width+"px";
				//Style.height 	= this.m_Height+"px";

				this.m_ScriptItem.appendChild(this.m_ctrlCombo);
				Event.observe(this.m_ctrlCombo, 'change', this.OnEIChange); 
			};
		};
		return null;
	}
});

/* ------------------------------------------------------------------------------------------- */

var CListbox = Class.create();

Object.extend(Object.extend(CListbox.prototype, CControl.prototype),
{
	initialize: function(name, ID, Parentwnd, Width, Height)
	{
		CControl.prototype.initialize.call(this, name, ID, Parentwnd, Width, Height);
		this.m_Class = "listbox";
		this.m_Content = new Array();
	},
	
	OnTempDisableWindow: function(bDisabled)
	{
		if (this.m_ctrlList)
		{
			this.m_ctrlList.disabled = bDisabled;
			if (bDisabled)
				this.m_ctrlList.hide();
			else
				this.m_ctrlList.show();
		};
	},
	
	OnDisableWindow: function()
	{
		OnTempDisableWindow(true);
	},

	OnEnableWindow: function()
	{
		OnTempDisableWindow(false);
	},	
	
	OnChange: function()
	{
		if (this.m_Parent != null)
			this.m_Parent.SendMessage(WM_COMMAND, LBN_SELCHANGE, this.m_ID);
	},

	AddString: function(text, id)
	{
		if (id == null)
			id = text;
			
		this.m_Content[this.m_Content.length] = new Option(text, id);
		if (this.m_ctrlList)
			this.m_ctrlList.options[this.m_ctrlList.options.length] = new Option(text, id);
	},
	
	ResetContent:function()
	{
		this.m_Content.length = 0;
		if (this.m_ctrlList)
			this.m_ctrlList.option.length = 0;
	},
	
	GetCount:function()
	{
		return this.m_Content.length;
	},
	
	OnDraw: function()
	{
	},
	
	OnEndCreate: function()
	{
		if (this.m_ScriptItem)
		{
			// Create an edit control with the sizes of the surrounding object
			this.m_ctrlList = document.createElement("SELECT");
			
			this.m_ScriptItem.style.borderStyle = "none";
			this.SetBkColor("transparent");
			
			if (this.m_ctrlList)
			{
				this.m_ctrlList.multiple = true;
				
				if (this.m_ctrlList)
				{
					var i = 0;
					for (i = 0;i<this.m_Content.length;i++)
					{
						this.m_ctrlList.options[i] = new Option(this.m_Content[i].text, this.m_Content[i].value);
					};

					// Resize the control - IE Style
					var			Style = this.m_ctrlList.style;
					if (Style.pixelWidth != null)
						Style.pixelWidth = this.m_Width;
					if (Style.pixelHeight != null)
						Style.pixelHeight = this.m_Height;

					Style.width 	= this.m_Width+"px";
					Style.height 	= this.m_Height+"px";
						
					this.m_ScriptItem.appendChild(this.m_ctrlList);
					Event.observe(this.m_ctrlList, 'change', this.OnEIChange); 
				};
			};
		};
		return null;
	}
});

/* ------------------------------------------------------------------------------------------- */

var 	CTabCtrl = Class.create();

Object.extend(Object.extend(CTabCtrl.prototype, CControl.prototype),
{
	initialize: function(name, ID, Parentwnd, Width, Height)
	{
		CControl.prototype.initialize.call(this, name, ID, Parentwnd, Width, Height);
		this.m_Class = "tabctrl";
		this.m_Tabs  = new Array();
		this.m_CurrentTab = "";
		this.m_TabWidth   = 150;
	},
	
	AddItem: function(name, id)
	{
		if (id == null)
			id = name;
			
		this.m_Tabs[this.m_Tabs.length] = new Option(name, id);
		
		if (this.m_ScriptItem)
		{
			var				button;
			var				i = this.m_Tabs[i].length;
			
			button = new CButton(this.m_Tabs[i].text, this.m_Tabs[i].value, this);
			button.SetPosition(i*this.m_TabWidth, 0, this.m_TabWidth, this.m_Height);
			this.AddChild(button);
		};
	},
	
	OnButtonClick: function(ctrlid)
	{
		if (this.m_Parent != null)
		{
			// Activate this tab
			// Check that the tab exists
			var				bFound = false;
			var				i;
			
			for (i = 0;i<this.m_Tabs.length && !bFound;i++)
			{
				if (this.m_Tabs[i].value == ctrlid)
					bFound = true;
			};
			
			if (bFound)
			{
				this.m_CurrentTab  = ctrlid;
				this.m_Parent.SendMessage(WM_COMMAND, TBN_SELCHANGE, this.m_ID);
			};
		};
	},
	
	GetCurSel: function()
	{
		return this.m_CurrentTab;
	},
	
	OnEndCreate: function()
	{
		if (this.m_ScriptItem)
		{
			this.m_ScriptItem.style.borderStyle = "none";
			this.SetBkColor("transparent");
			
			var i = 0;
			var	lXPos = 0;
			for (i = 0;i<this.m_Tabs.length;i++)
			{
				// Create a button for each tabs
				var				button;
				
				button = new CButton(this.m_Tabs[i].text, this.m_Tabs[i].value, this);
				button.SetPosition(lXPos, 0, this.m_TabWidth, this.m_Height);
				this.AddChild(button);
				lXPos = lXPos + this.m_TabWidth;
			};
			
		};
		return null;
	}	
});

/* ------------------------------------------------------------------------------------------- */

var CDialog = Class.create();

Object.extend(Object.extend(CDialog.prototype, CWindow.prototype),
{
	initialize: function(name, ID, Parentwnd, Width, Height)
	{
		CWindow.prototype.initialize.call(this, name, ID, Parentwnd, Width, Height);
		this.m_Owner = Parentwnd;
		this.m_Class = "dialog";
	},

	OnInitDialog: function()
	{
	},
	
	OnEndCreate: function()
	{
		this.SetBkColor("#ffeeff");
		if (this.m_ScriptItem)
		{
			// Create a div inside the div and give it the focus... (sort of)
			//this.m_OuterDiv = CTools.InsertDivBeforeParent(this.m_ScriptItem);
			
			if (this.m_OuterDiv)
			{
				var		Style = this.m_OuterDiv.style;
				var		Style1 = this.m_ScriptItem.style;
											
				Style.position			= "absolute";
				Style.left    			= Style1.left;
				Style.right   			= Style1.right;
				Style.top     			= Style1.top;
				Style.bottom  			= Style1.bottom;
				Style.margin			= "0px";
				Style.padding			= "0px";
				if (Style.pixelWidth != null)
					Style.pixelWidth = Style1.pixelWidth;
				if (Style.pixelHeight != null)
					Style.pixelHeight = Style1.pixelHeight;	
					
				Style.backgroundColor  	= Style1.backgroundColor;
				Style.borderStyle  		= "solid";
				Style.borderWidth  		= "1px";

				Style1.position 		= "absolute";
				Style1.backgroundColor  = "transparent";
				Style1.left = "0px";
				Style1.top  = "0px";
				
				settings = 
				{
					tl: { radius: 10 },
					tr: { radius: 10 },
					bl: { radius: 2 },
					br: { radius: 2 },
					antiAlias: true,
					autoPad: false,
					validTags: ["div"]
				};
				var		myCC = new curvyCorners(settings, this.m_OuterDiv);
				myCC.applyCornersToAll();			
			}
			else
			{
				var		Style = this.m_ScriptItem.style;
											
				Style.borderStyle  		= "solid";
				Style.borderWidth  		= "1px";
				Style.margin			= "0px";
				Style.padding			= "0px";
			};

		};
	},
	
	OnDraw: function()
	{
	},
	
	OnCloseWindow: function()
	{
		if (this.m_Owner != null)
			this.m_Owner.TempEnableWindow(true);
		this.DestroyWindow();
		if (this.m_OuterDiv)
		{
			Element.remove(this.m_OuterDiv);
			this.m_OuterDiv = null;
		};
	},
	
	DoModal: function(owner)
	{
		this.m_Owner = owner;
		if (this.m_Owner != null)
			this.m_Owner.TempEnableWindow(false);
		this.Create();
		this.OnInitDialog();
	},
	
	EndDialog: function()
	{
		this.SendMessage(WM_CLOSEWINDOW);
	}
});

/* ------------------------------------------------------------------------------------------- */

var CMenu = Class.create();

CMenu.MF_CHECKED		= 0x10000000;
CMenu.MF_UNCHECKED		= 0;
CMenu.MF_DISABLED		= 0x00000010;
CMenu.MF_ENABLED		= 0;
CMenu.MF_GRAYED			= 0x00000001;
CMenu.MF_MENUBARBREAK	= 0x01000000;
CMenu.MF_MENUBREAK		= 0x00100000;
CMenu.MF_POPUP			= 0x00010000;
CMenu.MF_OWNERDRAW		= 0x00000100;
CMenu.MF_SEPARATOR		= 0x00001000;
CMenu.MF_STRING			= 0;


Object.extend(Object.extend(CMenu.prototype, CObject.prototype), 
{
	initialize: function(name)
	{
		CObject.prototype.initialize.call(this, name);
	    this.m_Class = 'CMenu';
	    this.hide = this._hideHandler.bindAsEventListener(this);
	},
	
	CreateMenu: function()
	{
		this.m_bPopup = false;
	    if(this.m_ScriptItem)
	    {
	    	this.m_ScriptItem.parentNode.removeChild(this.m_ScriptItem);
	    	delete this.m_ScriptItem;
	    }
	},

	CreatePopupMenu: function()
	{
		this.m_bPopup = true;
	    if(this.m_ScriptItem)
	    {
	    	this.m_ScriptItem.parentNode.removeChild(this.m_ScriptItem);
	    	delete this.m_ScriptItem;
	    }
	},
	
	TrackPopupMenu: function(x, y, wnd)
	{
		if(!this.m_ScriptItem)
		{
			if(this.m_bPopup)
			{
				this.m_ScriptItem = document.createElement('table');
				this.m_ScriptItem.cellPadding = this.m_ScriptItem.cellSpacing = 0;
				this.m_ScriptItem.style.border = '1px solid #858585';
				this.m_ScriptItem.style.padding = '3px';
				this.m_ScriptItem.style.width = '170px';
				this.m_ScriptItem.style.backgroundColor = '#fcfcfc';
				this.m_ScriptItem.style.position = 'absolute';
				this.m_ScriptItem.style.cursor = 'pointer';
				if(!this.m_ScriptItem.tBodies.length)
					this.m_ScriptItem.appendChild(document.createElement('tbody'));
				document.body.appendChild(this.m_ScriptItem);
				
				for(var i = 0; i < this.m_pendingMenus.length; ++i)
				{
					var aTmp = this.m_pendingMenus[i];
					var tr = this._getPopupItem(aTmp[0], aTmp[1]);
					if(aTmp[2])
						tr.firstChild.style.backgroundImage = aTmp[2].GetBackgroundImage(); 
					this.m_ScriptItem.tBodies[0].appendChild(tr);
				}
				
				delete this.m_pendingMenus;
			}
		}

		var pt = CTooltipCtrl.GetCoord(this.m_ScriptItem, x, y);
		CTools.SetElementPosition(this.m_ScriptItem, pt.m_X, pt.m_Y);

		this.m_Parent = wnd;
		Event.observe(document, 'mousedown', this.hide);
		this.m_ScriptItem.style.visibility = 'visible';		
	},

	_hideHandler: function(e)
	{
		if(this.m_ScriptItem)
			this.m_ScriptItem.style.visibility = 'hidden';
		Event.stopObserving(document, 'mousedown', this.hide);
	},
	
	_getItem: function(item, strId)
	{
		var txtNode = document.createTextNode(item);
		var div = document.createElement('div');
		
		div.appendChild(txtNode);
		return div;
	},
		
	_getPopupItem: function(item, strId)
	{
		var txtNode = document.createTextNode(item);
		var tr = document.createElement('tr');
		var td = document.createElement('td');
		td.style.width = '15px';
		td.style.backgroundRepeat = 'no-repeat';
		td.style.backgroundPosition = 'center center';

		tr.onmouseover = function() { this.style.backgroundColor = '#1665CB';this.style.color = '#ffffff';}
		tr.onmouseout = function() { this.style.backgroundColor = '#fcfcfc';this.style.color = '#000000';}
		tr.onmousedown = this.OnMouseClick.bind(this, strId);
		tr.appendChild(td);
		td = document.createElement('td');
		td.appendChild(txtNode);
		tr.appendChild(td);
		return tr;
	},

	OnMouseClick: function(strId)
	{
		if(this.m_Parent)
			this.m_Parent.SendMessage(WM_COMMAND, MNN_SELCHANGE, strId);
	},
	
	InsertMenu: function(nPosition, nFlags, strId, item)
	{
		if(!nFlags)
		{
			if(this.m_ScriptItem)
			{
				var baseObj = this.m_bPopup?this.m_ScriptItem.tBodies[0]:this.m_ScriptItem;
				var obj = this.m_bPopup?this._getPopupItem(item, strId):this._getItem(item, strId);

				if((nPosition >= 0) && (nPosition < baseObj.childNodes.length))
					baseObj.insertBefore(obj, baseObj.childNodes[nPosition]);
				else
					baseObj.appendChild(obj);
			}
			else
			{
				if(!this.m_pendingMenus)
					this.m_pendingMenus = new Array();
				this.m_pendingMenus.splice(nPosition<0?this.m_pendingMenus.length:nPosition, 0, [item, strId, '', '']);
			}
		}
	},
	
	AppendMenu: function(nFlags, strId, item)
	{
		this.InsertMenu(-1, nFlags, strId, item);
	},
	
	CheckMenuItem: function()
	{
	},
	
	EnableMenuItem: function()
	{
	},
	
	GetMenuState: function()
	{
	},
	
	SetMenuItemBitmaps: function(nPosition, nFlags, imgUnchecked, imgChecked)
	{
		if(!nFlags)
		{
			if(this.m_ScriptItem)
			{
				var baseObj = this.m_bPopup?this.m_ScriptItem.tBodies[0]:this.m_ScriptItem;
				if(nPosition < baseObj.childNodes)
				{
					if(this.m_bPopup)
					{
						var td = baseObj.childNodes[nPosition].firstChild;
						td.style.backgroundImage = imgUnchecked.GetBackgroundImage(); 
					}
				}
			}
			else
			{
				if(nPosition < this.m_pendingMenus.length)
				{
					var aTmp = this.m_pendingMenus[nPosition];
					aTmp[2] = imgUnchecked;
					aTmp[3] = imgChecked;
				}
			}
		}
	},
	
	DestroyMenu: function()
	{
		if(this.m_ScriptItem) this.m_ScriptItem.parentNode.removeChild(this.m_ScriptItem);
		for(property in this) { delete this[property]; }
	}
});

/* ------------------------------------------------------------------------------------------- */

var CWaitingBar = Class.create();

Object.extend(Object.extend(CWaitingBar.prototype, CWindow.prototype),
{
	m_strColor: 'red',
	
	m_iMaxWidthRatio: 0.3,
	
	m_oStyle:
	{
		position: 'absolute',
		top: '0px',
		left: '0px',
		height: '100%',
		fontWeight: 'bold',
		textAlign: 'center'
	},
	
	initialize: function(name, ID, Parentwnd, strMsg, iWidth, iHeight)
	{
		CWindow.prototype.initialize.call(this, name, ID, Parentwnd, iWidth||200, iHeight||20);
		this.m_Class = 'CWaitingBar';
		this.m_strMsg = strMsg||'';
	},
	
	SetColor: function(strColor)
	{
		this.m_strColor = strColor;
	},
	
	OnEndCreate: function()
	{
		if(this.m_ScriptItem)
		{
			with(this.m_ScriptItem.style)
			{
				visibility = 'hidden';
				border = '1px solid black';
				backgroundColor = 'white';
				overflow = 'hidden';
			}
			
			var div = document.createElement('div');
			this.m_ScriptItem.appendChild(div);
			with(div.style)
			{
				position = 'absolute';
				top = left = '0px';
				height = '100%';
				width = '0px';
				backgroundColor = this.m_strColor;
			}

			div = document.createElement('div');
			this.m_ScriptItem.appendChild(div);
			Object.extend(div.style, this.m_oStyle);
			div.appendChild(document.createTextNode(this.m_strMsg));
			div.style.width = '100%';
		}
	},
	
	_refresh: function(iStep)
	{
		if(this.m_ScriptItem && this.m_ScriptItem.firstChild)
		{
			var iMaxWidth = Math.round(this.m_iMaxWidthRatio * this.m_Width),
				iRight = (this.m_ScriptItem.firstChild.offsetLeft + this.m_ScriptItem.firstChild.offsetWidth + iStep)%(this.m_Width+iMaxWidth);
				
			var iLeft = (iRight>iMaxWidth)?(this.m_ScriptItem.firstChild.offsetLeft + iStep):0;
			this.m_ScriptItem.firstChild.style.left = iLeft + 'px';
			var iWidth = iLeft?iMaxWidth:iRight;
			this.m_ScriptItem.firstChild.style.width = iWidth + 'px';
		}		
	},
	
	Start: function(iFreq, iStep)
	{
		if(this.m_ScriptItem)
		{
			if(this.m_handler)
				clearInterval(this.m_handler);
			this.m_ScriptItem.style.visibility = 'visible';
			this.m_ScriptItem.firstChild.style.left = '0px';
			this.m_ScriptItem.firstChild.style.width = '0px';
			this.m_handler = setInterval(this._refresh.bind(this, iStep||4), iFreq||30);
		}
	},
	
	Stop: function()
	{
		if(this.m_ScriptItem)
		{
			this.m_ScriptItem.style.visibility = 'hidden';
			clearInterval(this.m_handler);
		}
	}
});

/* ------------------------------------------------------------------------------------------- */

