
/*************************************************************************************************************
	
	MENU
	- add_item
	- show
	- hide

*************************************************************************************************************/

MENU = function(id, parent_id) {
	MENU.superclass.constructor.call(this, id, parent_id);
}

Class(
	MENU, OBJECT,

	"init", function(id, parent_id) {
		MENU.superclass.init.call(this, id, parent_id);
		
		this.items = new Array();
	},

	"clear", function() {
		delete this.items;
		delete this.menu;

		MENU.superclass.clear.call(this);
	},

	"addItem", function(item, callback, scope) {
		var width = 120;
		var height = 19;
		var count = this.items.length;
		var el = DW_UTIL.addDiv(this.get_uid(count), this.id);

		if( count ) {
			var sep = DW_UTIL.addDiv("", this.id);
			DW_UTIL.setXYWH(sep, 5, 5 + (count - 1)* (height + 5) + 21, width, 1);
			DW_UTIL.setBackground(sep, "#ccc");
		}

		DW_UTIL.setXYWH(el, 5, 5 + count * (height + 5), width, height);
		DW_UTIL.setBackground(el, "#fff");
		el.innerHTML = item;
		YAHOO.util.Dom.setStyle(el, "font", "8pt verdana");
		YAHOO.util.Dom.setStyle(el, "padding", "2px");
		YAHOO.util.Dom.setStyle(el, "cursor", "pointer");
		YAHOO.util.Event.addListener(el, "click", callback, scope, true);
		YAHOO.util.Event.addListener(el, "click", this.hide, this, true);
		this.items.push(el);

		DW_UTIL.setWH(this.menu, width + 10, (count + 1) * (height + 5) + 10 - 5);
	},

	"create", function() {
		this.menu = DW_UTIL.addDiv(this.id);
		DW_UTIL.setBackground(this.menu, "#fff");
		DW_UTIL.setBorder(this.menu, 1, "#999");
		YAHOO.util.Dom.setStyle(this.menu, "display", "none");
		YAHOO.util.Dom.setStyle(this.menu, "zIndex", "950");

		YAHOO.util.Event.addListener(this.parent_id, "mousedown", this.show, this, true);

		YAHOO.util.Event.addListener(document, "click", function(e) {
			var r = YAHOO.util.Dom.getRegion(this.menu);
			if( e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom ) {
				this.hide();
			}
		}, this, true);
	},

	"destroy", function() {
		DW_UTIL.safeDestroy(this.id);

		MENU.superclass.destroy.call(this);
	},

	"show", function(e) {
		if( !this.menu || !this.items.length ) {
			return;
		}
		YAHOO.util.Dom.setStyle(this.menu, "display", "block");
		DW_UTIL.setXY(this.menu, e.clientX - 2, e.clientY - 2);

		e.cancelBubble = true;
	},

	"hide", function() {
		if( !this.menu || !this.items.length ) {
			return;
		}
		YAHOO.util.Dom.setStyle(this.menu, "display", "none");
	}
);

RIGHT_MENU = function(id, parent_id) {
	RIGHT_MENU.superclass.constructor.call(this, id, parent_id);
}

Class(
	RIGHT_MENU, MENU,

	"show", function(e) {
		if( e.button != 2 ) {
			return;
		}

		RIGHT_MENU.superclass.show.call(this, e);
	}
);
