if(typeof(Sonata)=='undefined') Sonata = {};

Sonata.DropLinkOnly = function(idMenuName) {
	/*
		use this for navbar items without dropmenus, this makes sure a dropped menu is closed when mouseover the item
	*/
	this.idMenuName = idMenuName;
	this.jqMenuName = $("#"+this.idMenuName);
	//mprint("droplink link="+this.jqMenuName.size());
	
	this.jqMenuName.mouseenter(Sonata.DropMenu.hideOthers);
}
Sonata.DropMenu = function(idMenuName,idMenuItems,options) {
	var thisptr = this;
	this._setOptions(options);
	this.idMenuName = idMenuName;
	this.idMenuItems = idMenuItems;
	this.isAttached = false;
	this.attach();
	this.jqMenuName = $("#"+this.idMenuName);
	this.jqMenuItems = $("#"+this.idMenuItems);
	this.timers = {show:{},hide:{}};
	this.isVisible = false;
	if(this.options.attach) this.attach();
	//mprint("DropMenu idMenuName="+idMenuName+" idMenuItems="+idMenuItems);
	//mprint("DropMenu jqMenuName="+this.jqMenuName.size()+" items="+this.jqMenuItems.size());
	Sonata.DropMenu._allMenus.push(this);
}

Sonata.DropMenu._defOptions = {
	showDelay:200
	, hideDelay:500
	, attach:true
	, zIndex:100
	, dx:10
	, dy:0
	, blockItem: true	//make all items in the menu the same size
	, minWidth: 0		//set the minimum width of the menu (fix for IE), see setMinWidth() below
	, onshow: null		//called when menu is dropped
	, onhide: null		//called when menu is hidden
}
Sonata.DropMenu._allMenus = [];

Sonata.DropMenu.prototype._setOptions = function(options) {
	var d = Sonata.DropMenu._defOptions;

	if(typeof(options) == 'undefined') {
		options = {};
	}
	var name;
	var opt = {};
	for(name in d) {
		opt[name] = d[name];
	}
	for(name in options) {
		opt[name] = options[name];
	}
	this.options = opt;
}
Sonata.DropMenu.prototype.attach = function() {
	var thisptr = this;
	if(this.isAttached) return;
	//mprint("attached");
	this.isAttached = true;
	this.jqMenuName = $("#"+this.idMenuName);
	this.jqMenuItems = $("#"+this.idMenuItems);
	//mprint("jqMenuName="+this.jqMenuName.size());
	this.jqMenuName.mouseenter(over);
	this.jqMenuName.mouseleave(out);
	
	this.jqMenuItems.mouseenter(overItem);
	this.jqMenuItems.mouseleave(outItem);
	this.jqMenuItems.click(clickItem);
	//make sure the menu displays over other items
	this.jqMenuItems.css("z-index",this.options.zIndex);
	//mprint("after attached");
	
	if(this.options.blockItem) {
		var hrefs = $("#"+this.idMenuItems+" a");
		hrefs.css("display","block");
		//hrefs.each( function() { mprint("hrefs each w="+$(this).width());} );
	}
	function over() {
		thisptr.hideOthers();
		thisptr.timers.show = jQuery.timer(thisptr.options.showDelay,showOver);
	}
	function showOver() {
		thisptr.cancelHide();
		thisptr.timers.show = {};
		thisptr.showItems();
	}
	function out() {
		thisptr.cancelShow();
		thisptr.timers.hide = jQuery.timer(thisptr.options.hideDelay,hideOut);
	}
	function hideOut() {
		thisptr.timers.hide = {};
		thisptr.hideItems();
	}
	
	function overItem() {
		thisptr.cancelHide();
	}
	function outItem() {
		out();
	}
	function clickItem(event) {
		//after clicking the <a>, we drop through here
		
		if(typeof event.originalTarget != 'undefined' && typeof event.originalTarget.blur != 'undefined')
			event.originalTarget.blur();	//take away keyboard focus (the little box around the item)
		thisptr.hideItems();
	}
	function clickHref(event) {
		//mprint("clickHref this="+this);
		this.blur();
		thisptr.hideItems();
		event.preventDefault();
		
	}
}
Sonata.DropMenu.prototype.setMinWidth = function() {
	var hrefs = $("#"+this.idMenuItems+" a");
	//var mstyles = $("#"+this.idMenuItems+ " .jgMenuItemStyle");
	var hW = hrefs.width();
	//var sW = mstyles.width();
	//mprint("hw="+hW+" sW="+sW);
	if(this.options.minWidth > 0) {
		var w = this.options.minWidth;
		if(w> hW) {
			hrefs.width(w); //w-(sW-hW));
			//mstyles.width(w);
		}
	}
	/*
	if(false) {
		var mWidHref = 0, mWidStyle = 0;
		hrefs.each( function() { 
			var w = $(this).width();
			if(w>mWidHref) mWidHref = w;
		} );
		mprint("href w="+mWidHref);
		mstyles.each( function() { 
			var w = $(this).width();
			if(w>mWidStyle) mWidStyle = w;
		} );
		mprint("mstyles w="+mWidStyle);
	}
	*/

}
Sonata.DropMenu.prototype.cancelHide = function() {
	if(typeof this.timers.hide.timer != 'undefined')
		jQuery.clearTimer(this.timers.hide);
	this.timers.hide= {};
}
Sonata.DropMenu.prototype.cancelShow = function() {
	if(typeof this.timers.show.timer != 'undefined')
		jQuery.clearTimer(this.timers.show);
	this.timers.show= {};
}
Sonata.DropMenu.prototype.showItems = function() {
	if(this.isVisible) return;
	this.cancelHide();
	this.isVisible = true;
	var menu = this.jqMenuName;
	var pos = menu.position();
	var h = menu.height();
	var w = menu.width();
	var dx = this.options.dx;
	var dy = this.options.dy;
	this.jqMenuItems.css({"left":pos.left+dx,"top":pos.top+h+dy});
	//mprint("jqMenuItems="+this.jqMenuItems.size());
	this.jqMenuItems.show();
	if(typeof this.options.onshow == 'function')
		this.options.onshow(this);
	if(this.options.minWidth > 0)
		this.setMinWidth();
}

Sonata.DropMenu.prototype.hideItems = function() {
	if(this.isVisible==false) return;
	this.isVisible = false;
	this.cancelShow();
	this.jqMenuItems.hide();
	if(typeof this.options.onhide == 'function')
		this.options.onhide(this);
}
Sonata.DropMenu.prototype.hideOthers = function() {
	Sonata.DropMenu.hideOthers();
}
Sonata.DropMenu.hideOthers = function() {
	var a = Sonata.DropMenu._allMenus;
	var i,n = a.length;
	//mprint("hide others n="+n);
	var m;
	for(i=0;i<n;i++) {
		m = a[i];
		m.hideItems();
	}
}