function getAbsolutePos(el)
	{
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.parentNode && el.parentNode.scrollTop)
		r.y -= el.parentNode.scrollTop;
	if (el.offsetParent)
		{
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
		}
	return r;
	}



function mousePageXY(e)
{
  var x = 0, y = 0;

  if (!e) e = window.event;

  if (e.pageX || e.pageY) {
    x = e.pageX;
    y = e.pageY;
  } else if (e.clientX || e.clientY) {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }

  return {"x":x, "y":y};
}


function addHandler(element,event,action,scope, param){
	if (scope)
		action = action.bind(scope);
	
  if(document.addEventListener)element.addEventListener(event,action,param);
  else if(document.attachEvent)element.attachEvent('on'+event,action);
  else element['on'+event]=action;
}


/*Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
  	return __method.apply(object, arguments);
	}
}*/

function addClass(elem, className) {
	if (elem.className == null)
		elem.className = "";
	var classes = elem.className.split(" ");
	for (var i = 0; i < classes.length; i++) {
		if (classes[i] == className)
			return;
	}	
	classes[classes.length] = className;
	elem.className = classes.join(" ");
}

function removeClass (elem, className) {
	var classes = elem.className.split(" ");
	var newClasses = new Array ();
	for (var i = 0; i < classes.length; i++) {
		if (classes[i] == className)
			continue;
		newClasses[newClasses.length] = classes[i];
	}	
	elem.className = newClasses.join(" ");
}



/*function newClass(parent, prop) {
  // Dynamically create class constructor.
  var clazz = function() {
    // Stupid JS need exactly one "operator new" calling for parent
    // constructor just after class definition.
    if (clazz.preparing) return delete(clazz.preparing);
    // Call custom constructor.
    if (clazz.constr) {
      this.constructor = clazz; // we need it!
      clazz.constr.apply(this, arguments);
    }
  }
  clazz.prototype = {}; // no prototype by default
  if (parent) {
    parent.preparing = true;
    clazz.prototype = new parent;
    clazz.prototype.constructor = parent;
    clazz.constr = parent; // BY DEFAULT - parent constructor
  }
  if (prop) {
    var cname = "constructor";
    for (var k in prop) {
      if (k != cname) clazz.prototype[k] = prop[k];
    }
    if (prop[cname] && prop[cname] != Object)
      clazz.constr = prop[cname];
  }
  clazz.prototype.superclass = function() {return this.constructor.prototype};
  return clazz;
}*/


WbsPopwindow = newClass (null, {
	
	constructor: function (config) {
		if (!config)
			config = {};
		this.config = config;
		
		if (!this.config.hideMode)
			this.config.hideMode = "close";
		
		if (!document.wbsPopmenuClickLinked) {
			addHandler(document,'click',wbsPopmenuOnDocumentClick,false);
			document.wbsPopmenuClickLinked = true;
		}
		
		var elem = document.createElement("div");
		elem.style.position = "absolute";
		addClass(elem, this.getWindowCls());
		
		if (config.cls)
			addClass(elem, config.cls);
		
		if (config.width)
			elem.style.width = config.width + "px"
		else if (!config.cls)
			elem.style.width = "auto";
		
		if (config.height)
			elem.style.height = config.height + "px"

		this.elem = elem;
	},
		
	getWindowCls: function() {
		return "wbs-popwindow";
	},
			
	render: function() {
	},
			
		
	show: function (e, targetElem) {
		if(document.wbsPopmenuCurrent != null)
			document.wbsPopmenuCurrent.close();
		this.render();
		//document.wbsPopmenuShowProcess = true;
		
		if(e.stopPropagation) e.stopPropagation();
		else e.cancelBubble = true;
			
		var target=e.target||e.srcElement;
		if (targetElem)
			target = targetElem;
		var cursorPos = mousePageXY(e);
		//var cursorPos = getAbsolutePos(target);
		if (target)
			var targetPos = getAbsolutePos(target);
		if (!targetElem)
			targetPos = cursorPos;
		else
			targetPos.y += target.offsetHeight;
		
		var left = targetPos.x;
		var top = targetPos.y;
		
		//elem.style.top = cursorPos.y;
		var elem = this.elem;
		elem.style.top = top + 0;
		elem.style.left = left + "px";
		
		document.body.appendChild(elem);
		elem.style.visibility = "visible";
		
		document.wbsPopmenuCurrent = this;
		
		if (left + elem.offsetWidth > getDocumentSize().width) {
			elem.style.left = (getDocumentSize().width - elem.offsetWidth-20) + "px";
		}
		if (top + elem.offsetHeight > getDocumentSize().height) {
			var newTop = top - elem.offsetHeight;
			if (newTop > 0)
				elem.style.top = newTop + "px";
		}
		
		
		if (this.onAfterShow) 
			this.onAfterShow(e);
	},
		
	hide: function() {
		var elem = this.elem;
		elem.style.visibility = "hidden";
	},
	
	close: function() {
		if (this.onClose)
			this.onClose();
		
		var elem = this.elem;
		elem.style.visibility = "hidden";
		document.wbsPopmenuCurrent = null;
		elem.parentNode.removeChild(elem);
		delete elem;
		//delete this;
	},
	
	getElem: function () {
		return this.elem;
	},
		
	getInnerElem: function() {
		if (this.innerElem)
			return this.innerElem;
		
		this.innerElem = createDiv("wbs-popwindow-inner");
		this.getElem().appendChild(this.innerElem);
		return this.innerElem;
	}
});


function wbsPopmenuOnDocumentClick(e){
	if (!document.wbsPopmenuCurrent)
		return;
	/*if (document.wbsPopmenuShowProcess) {
		document.wbsPopmenuShowProcess = false;
		return;
	}*/
	e=e||event;
  var target=e.target||e.srcElement;
  var menuElem = document.wbsPopmenuCurrent.getElem();
  if(menuElem){
    var parent=target;
    while(parent.parentNode&&parent!=menuElem)
    	parent=parent.parentNode;
    if(!parent || parent != menuElem)
      document.wbsPopmenuCurrent.close();
  }
}

WbsPopmenu = newClass(WbsPopwindow, {
	constructor: function (config) {
		this.items = new Array ();
		this.superclass().constructor.call(this, config);
	},
		
	render: function() {
		
		var ul = document.createElement("ul");
		for (var i = 0; i < this.config.items.length; i++) {
			var item = this.config.items[i];
			
			var li = this.createItem(item);
			if (item.id) 
				this.items[item.id] = li;
			else
				this.items[this.items.length] = li;
			
			ul.appendChild(li);
		}
		this.elem.appendChild(ul);
	},
		
	getWindowCls: function() {
		if (this.config.withImages)
			return "wbs-popmenu wbs-popmenu-images";
		else
			return "wbs-popmenu";
	},
	
	getItem: function(id) {
		return this.items[id];
	},
	
	setItems: function(items) {
		this.config.items = items;
	},
	
	refreshItem: function (li, item) {
		var newLi = this.createItem(item);
		if (item.id)
			this.items[item.id] = li;
		li.parentNode.replaceChild(newLi, li);
	},
	
	hideItem: function (id) {
		addClass(this.items[id], "hidden");
	},
	
	showItem: function (id) {
		removeClass(this.items[id], "hidden");
	},
	
	createItem: function(item) {
		var li = document.createElement("li");
		if (item.cls)
			addClass(li, item.cls);
		
		if (item.iconCls) {
			addClass(li, "with-image");
			addClass(li,item.iconCls);
		}
		
		if (item == "-") {
			li.className = "separator";
			li.innerHTML = "<div></div>";
		} else if (item.html) {
			li.innerHTML = item.html;
		} else {
			var anchor = document.createElement("a");
			anchor.href = "javascript:void(0)";
			var text = document.createTextNode(item.label);
			anchor.appendChild(text);
			li.appendChild(anchor);
			
			if (!item.hidden)
				addEmptyImg(li);
		}
		
		if (item.onClick && !item.disabled) {
			li.onclick = function(item) { 
				return function() {
					var scope = item.scope ? item.scope : this;
					var val = item.onClick.bind(scope)();
					if (!item.onClickNoHide)
						this.close();
					return val;
				}; 			
			} (item).bind(this);
		}
		
		li.onmouseover = function() {
			if (!this.disabled)
				addClass(this,"highlight");
		}
		li.onmouseout = function() {
			if (!this.disabled)
				removeClass(this, "highlight");
		}
		
		if(item.hidden) {
			addClass(li, "hidden");
		}
		if(item.disabled) {
			addClass(li, "disabled");
			li.disabled = true;
		}
		li.setText = function(text) {this.innerHTML = text;}
		return li;
	}
});

function getDocumentSize()
{
	if (document.clientHeight != null)
		return {height: document.clientHeight, width: document.clientHeight};
	
	if ( typeof(document.documentElement.clientHeight) != 'undefined' && document.documentElement.clientHeight > 0 )
		return {height: document.documentElement.clientHeight, width: document.documentElement.clientWidth};

	if ( typeof(document.body.clientHeight) != 'undefined' )
		return {height: document.body.clientHeight, width: document.body.clientWidth};
	

	return {height: 0, width: 0};
}


/*function $(id) {
	return document.getElementById(id);
}*/

function createDiv(className) {
	return createElem("div", className);
}

function createElem(tag, className, attributes) {
	if (tag == "input" && attributes && (attributes.type == "radio" || attributes.type == "checkbox") && attributes.name) { // IE bug with radio and checkbox elements
			var elem = document.createElement("<input type='" + this.type + "' name='" + this.name + "'>");
	} else
		var elem = document.createElement(tag);
	
	
	elem.show = window.showElem;
	elem.hide = window.hideElem;
	if (className)
		elem.className = className;
	
	if (attributes) {
		for (var attName in attributes)
			elem.setAttribute(attName, attributes[attName]);
	}
	
	return elem;	
}



function addEmptyImg(elem) {
	var img = createElem("img");
	img.src = ("/pages/img/s.gif");
	img.style.width = "1px";
	img.style.height = "1px";	
	elem.appendChild(img);
}



function showMenu (e, link) {
	var menu = new MyMenu();
	if (!e)
		e = window.event;
	menu.show(e);
}

var MyMenu = newClass (WbsPopmenu, {
	constructor: function() {
		var items = [
			{label: "New", onClick: function() {alert("X"); return false;}},
			{label: "Send", onClick: function() {alert("X"); return false;}},
			"-",
			{label: "Exit", onClick: function() {alert("X"); return false;}},
		];
		this.superclass().constructor.call(this, {items: items, withImages: false});
	}		
});

