﻿(function($) {
	$.widget("ui.moremenu", {
		hideDelay : 100,
		items: null,
		container : null,
		bindToElement : null,
		OnItemClick:null,
		OnBeforeShow:null,
		OnBeforeShowValue : "",
		OnHide : null,
		hideSpeed : 50,
		showSpeed : 50,
		header : '',
		_init: function() {
			var o = this.options;
			this.id = $(this.element).attr("id");
			if (this.id == "") this.id='more_menu_'+Math.ceil(Math.random()*10000);
			this.items = new Array();
			if (typeof (o.items) != "undefined") {
				for (var i = 0; i < o.items.length; i++) {
					this.items.push(new CDropDownItem(o.items[i].text, o.items[i].command, o.items[i].value, o.items[i].href));
				}
			}
			if (typeof(o.header)!="undefined"){
				this.header = o.header;
			}
			if (typeof(o.itemclick)!="undefined"){
				this.OnItemClick = o.itemclick;
			}
			if (typeof(o.onbeforeshow)!="undefined"){
				this.OnBeforeShow = o.onbeforeshow;
			}
			if (typeof(o.onaftershow)!="undefined"){
				this.OnAfterShow = o.onaftershow;
			}
			if (typeof(o.onhide)!="undefined"){
				this.OnHide = o.onhide;
			}
			if (typeof(o.beforeshowvalue)!="undefined"){
				this.OnBeforeShowValue = o.beforeshowvalue;
			}
			
			if (typeof(o.bindTo)!="undefined"){
				if(typeof(o.bindTo) == "string" ){
					this.bindToElement = $(o.bindTo);
				}else{
					this.bindToElement = o.bindTo;
				}
				if (this.bindToElement.length != 1) this.bindToElement = null;
			}
			var self = this;
			this.element.click(function(jsEvent){
				jsEvent.stopPropagation();
				self.Show(jsEvent);
			});
			this.element.bind("mouseout", function(){
				self.CloseBox();
			});
			return this;
		},
		
		Show : function(jsEvent){
			if ( typeof(this.OnBeforeShow) == "function" ) {
				this.OnBeforeShow(this, jsEvent);
			}else if ( typeof(window[this.OnBeforeShow]) == "function"){
				window[this.OnBeforeShow](this, jsEvent);
			}
			if (!this.rendered) this._render();
			this.container.show(self.showSpeed);
			if (typeof(this.OnAfterShow) == "function" && this.OnAfterShow != null){
				this.OnAfterShow(this, jsEvent);
			}
		},
		SetPosition : function(_left, _top){
			this.container.css({"left":_left, "top":_top});
		},
		_render: function() {
			if (this.container == null){this.buildContainer();}
			this.insideBox.text("");
			var self = this;
			for (var i=0;i<this.items.length;i++){
				if (this.items[i].href != ""){
					var ht = "<a id='"+this.id+"_"+i+"' href='"+this.items[i].href+"'>"+this.items[i].text+"</a>";
				}else{
					var ht = "<a id='"+this.id+"_"+i+"' index='"+i+"' v='"+this.items[i].value+"' c='"+this.items[i].command+"' href='javascript:'>"+this.items[i].text+"</a>";
				}
				
				this.insideBox.append(ht);	
				var index = i;
				if (this.items[i].href == "")
				{
					$J("#"+this.id+"_"+i).click(function(jsEvent){
						if(typeof(self.OnItemClick) == "function"){
							var ind = parseInt($(this).attr("index"));
							self.container.hide(self.hideSpeed, function(){ if (self.OnHide != null && typeof(window[self.OnHide]) == "function") window[self.OnHide](self); });
							self.OnItemClick(self.items[ind],jsEvent);
						}
					});
				}
			}
			this.rendered = true;
		},
		buildContainer : function(){
			var self = this;
			
			var ht = "<span class='r-box more-menu' style='display:none' id='" + this.id + "_moreMenuContainer'>";
			ht += "<span class='rtop'></span>";
			ht += "<span class='content-wrap'><span class='content'>";
			if (this.header != ""){
				ht+="<span class='header'>"+this.header+"</span>"
			}
			ht +="<span class='inside'>&nbsp;</span></span></span>";
			ht += "<span class='rbottom'></span>";
			ht += "</span>";
			if (this.bindToElement != null){
				this.bindToElement.append(ht);
			}else{
				this.element.append(ht);
			}
			this.container = $("#" + this.id + "_moreMenuContainer");
			this.insideBox = $("#" + this.id + "_moreMenuContainer .content .inside");
			this.container.bind("mouseout", function(){
				self.CloseBox();
			});
			this.container.bind("mouseover", function(){
				if (self.timer != null) clearTimeout(self.timer);
			});
		},
		
		CloseBox : function(){
			var self = this;
			if (self.timer != null) clearTimeout(self.timer);
			self.timer = setTimeout(
				function(){
					if (typeof(self.container)!="undefined" && self.container != null){
						self.container.hide(self.hideSpeed, function(){ if (self.OnHide != null && typeof(window[self.OnHide]) == "function") window[self.OnHide](self); });
					}
				}
			, self.hideDelay);
		},
		
		AddItem:function(_ddItem){
			this.items.push(_ddItem);
		},
		
		RemoveItem:function(_ddItem){
			
		},
		
		FindItemByValue:function(_value){
			var l = this.items.length;
			for (var i;i<l;i++){
				if (this.items[i].value ==_value) return this.items[i];
			}
			return null;
		},
		ClearItemsByCommandName : function(_cname){
			var l = this.items.length;
			for (var i=0;i<l;i++){
				if (this.items[i].command == _cname)  this.items[i] = null;
			}
			this.items.removeEmptyElements();
		},
		
		GetMenuBox : function(){
			return this.container;
		},
		
		GetMenuBoxSize : function(){
			return {width:this.container.width(), height:this.container.height()};
		}
	})
})(jQuery);

var CDropDownItem = function(_text, _command, _value, _href) {
	this.text = _text;
	this.command = _command;
	this.value = _value;
	if (typeof(_href)!="undefined")
		this.href = _href;
	else 
		this.href = "";
}