/* class PopupWindow */
function dx_PopupWindow(parent) 
{
	this.parent = parent;
	this.id = "undefined";
	this.element = null;
	this.frame = null;
	
	this.Contains = function(pt) {
		var el = this.GetElement();
		if( Exists(el) && this.IsElemVisible(el) ) {
			var s = dxPopup.Pos.GetElementSize(el);
			var p = dxPopup.Pos.GetAbsolutePos(el);
			var r = new dx_Rect(p.x, p.y, s.x, s.y);
			return r.Contains(pt);
		}
		return false;
	}
	this.GetElementById = function(id) {
		return dxPopup.Obj.GetElementById(id);
	}
	this.GetElement = function() {
		if(Exists(this.element) == false) {
			this.element = this.GetElementById(this.id);
		}
		return this.element;
	}
	this.Popup = function(x, y) {
		this.SetPos(x, y);
		var frame = this.GetFrame();
		this.SetElemPos(frame, x, y);
		this.ShowElem(frame);
		this.Show();
	}
	this.GetFrame = function(id) {
		if(Exists(this.frame) == false) {
			this.frame = this.GetElementById(this.id + "Frame");
			if( Exists(this.frame) ) {
				var el = this.GetElement();
				var size = dxPopup.Pos.GetElementSize(el);
				this.frame.style.width = size.x;
				this.frame.style.height = size.y;
			}
		}
		return this.frame;
	}
	this.SetPos = function(x, y) {
		var el = this.GetElement();
		this.SetElemPos(el, x, y);
	}
	this.SetElemPos = function(el, x, y) {
		if( Exists(el) ) dxPopup.Pos.SetAbsolutePos(el, x, y);
	}
	this.Hide = function() {
		var el = this.GetElement();
		if( Exists(el) ) this.SetElementVisibility(el, false);
		if( Exists(this.frame) ) this.SetElementVisibility(this.frame, false);
	}
	this.Show = function() {
		var el = this.GetElement();
		this.ShowElem(el);
	}
	this.ShowElem = function(el) {
		if( Exists(el) ) this.SetElementVisibility(el, true);
	}
	this.SetElementVisibility = function(el, visible) {
		dxPopup.Obj.SetElementVisibility(el, visible);
	}
	this.IsElemVisible = function(el) {
		return dxPopup.Obj.IsElementVisible(el);
	}
}
/* class PopupControl */
function dx_PopupControl() 
{
	this.id = "undefined";
	this.hideDelay = 3000;
	this.left = 0;
	this.top = 0;
	this.items = [];
	
	this.HideAll = function() {
		for(var i = 0; i < this.items.length; i++) {
			this.items[i].Hide();
		}
	}
	this.AddItem = function() {
		var item = new dx_PopupWindow(this);
		this.items.Add(item);
		return item;
	}
	this.GetItemById = function(id) {
		for(var i = 0; i < this.items.length; i++)
			if(id == this.items[i].id) return this.items[i];
		return null;
	}
}
/* class PopupManager */
function dx_PopupManager() 
{
	this.timer = null;
	this.wind = null;
	this.items = [];
	
	this.Initialize = function() {
	}
	this.OnResize = function() {
		this.HideAll();
	}
	this.HideAll = function() {
		for(var i = 0; i < this.items.length; i++) {
			this.items[i].HideAll();
		}
		this.ClearTopWindow();
	}
	this.AddItem = function() {
		var item = new dx_PopupControl();
		this.items.Add(item);
		return item;
	}
	this.GetItemById = function(id) {
		for(var i = 0; i < this.items.length; i++)
			if(id.indexOf(this.items[i].id) == 0) return this.items[i];
		return null;
	}
	this.GetWindowById = function(id) {
		var item = this.GetItemById(id);
		if( Exists(item) ) 
			return item.GetItemById(id);
		return null;
	}
	this.PopupWindow = function(id, x, y) {
		var wind = this.GetWindowById(id);
		if( Exists(wind) ) {	
			this.HideAll();
			wind.Popup(x,y);
			this.SetTimeout(wind.parent.hideDelay);
			this.wind = wind;
		}
	}
	this.HideWindow = function(id) {
		if(Exists(this.wind) && this.wind.id == id) {
			this.HideTopWindow();
		} else {
			var wind = this.GetWindowById(id);
			if( Exists(wind) ) wind.Hide();
		}
	}
	this.OnMouseDown = function(e) {
		if( Exists(this.wind) ) {
			var pt = dxPopup.Pos.EventPoint(e, window);
			if(Exists(pt) && this.wind.Contains(pt) == false)
				this.HideTopWindow();
		}
	}
	this.OnMouseMove = function(e) {
		if(this.wind != null) {
			var pt = dxPopup.Pos.EventPoint(e, window);
			if( Exists(pt) ) {
				if( this.wind.Contains(pt) ) this.ClearTimeout();
				else this.SetTimeout(wind.parent.hideDelay);
			}
		}
	}
	this.ClearTimeout = function() {
		if( Exists(this.timer) ) {
			clearTimeout(this.timer);
			this.timer = null;
		}
	}
	this.SetTimeout = function(delay) {
		if(Exists(this.timer) == false && delay > 0) {
			this.timer = setTimeout("dxPopup.Mng.HideTopWindow()",delay);
		}
	}
	this.HideTopWindow = function() {
		if( Exists(this.wind) ) {
			this.wind.Hide();
			this.ClearTopWindow();
		}
	}
	this.ClearTopWindow = function() {
		this.wind = null;
		this.ClearTimeout();
	}
}

// globals
var dxPopup = [];
dxPopup["Inf"] = new dx_Info();
dxPopup["Utl"] = new dx_Utils();
dxPopup["Mng"] = new dx_PopupManager();
dxPopup["Pos"] = dxPopup.Utl.CreatePosHelper();
dxPopup["Obj"] = dxPopup.Utl.CreateObjHelper();
dxPopup["Evt"] = new dx_EventManager();

function CreatePopupControl() {
	return dxPopup.Mng.AddItem();
}
function PopupWindow(id, elem, x, y) {
	var _elem = GetArgValue(arguments, 1, null);
	var _x = GetArgValue(arguments, 2, 0);
	var _y = GetArgValue(arguments, 3, 0);
	
	if(null != _elem) { 
		var pt = dxPopup.Pos.GetAbsolutePos(_elem);
		_x += pt.x; _y += pt.y;
	}
	dxPopup.Mng.PopupWindow(id, _x, _y);
}
function ShowWindow(id, e, x, y) {
	var _e = GetArgValue(arguments, 1, null);
	var _x = GetArgValue(arguments, 2, 0);
	var _y = GetArgValue(arguments, 3, 0);
	if(null != _e) {
		_x += dxPopup.Pos.GetEventX(_e, window);
		_y += dxPopup.Pos.GetEventY(_e, window);
	}
	
/*	if(arguments.length == 1) {
		var wind = dxPopup.Mng.GetWindowById(id);
		if( Exists(wind) ) {	
			x = wind.parent.left;
			y = wind.parent.top;
		}
	} */
	dxPopup.Mng.PopupWindow(id, _x, _y);
}
function HideWindow(id) {
	dxPopup.Mng.HideWindow(id);
}

dxPopup.Evt.Attach("window.onresize", function() { dxPopup.Mng.OnResize(); } );
window.onresize = function() { dxPopup.Evt.Exec("window.onresize"); }

dxPopup.Evt.Attach("window.onload", function() { dxPopup.Mng.Initialize(); } );
window.onload = function() { dxPopup.Evt.Exec("window.onload"); }

dxPopup.Evt.Attach("window.document.onmousedown", function(e) { dxPopup.Mng.OnMouseDown(e); } );
window.document.onmousedown = function(e) { dxPopup.Evt.Exec("window.document.onmousedown",e); }

if(dxPopup.Inf.ns4) window.document.captureEvents(Event.MOUSEMOVE);
dxPopup.Evt.Attach("window.document.onmousemove", function(e) { dxPopup.Mng.OnMouseMove(e); } );
window.document.onmousemove = function(e) { dxPopup.Evt.Exec("window.document.onmousemove",e); }

