/*
jQuery common scripts for Loewy Dropdown Menu
- Sukiman, suki@webexcellence.net, http://www.webexcellence.net/
*/
(function($) {
	$.loewy = $.loewy || {};
	
	$.fn.loewyDropDownMenu = function(o) {
		return this.each(function() {
			new $.loewy.dropDownMenu(this,o);
		});
	}
	
	$.loewy.dropDownMenu = function(ele, o) {
		var options = $.extend({ 
			width: 150,
			slideDownSpeed: 1000,
			slideUpSpeed: "fast",
			easeShow:"easeOutQuad",
			easeHide:"easeInQuad"
		},o);
		
		var p = ele;
		var showed = false;
		var self = this;
		
		var height = 0;
		var width = 0;
		var menuitems = undefined;
		var menuresult = undefined;
		var select = undefined;
		var arrow = undefined;
		var selectitems = new Array();
		var defselectitemsidx = 0;
		
		var init = function() {
			w = options.width;
			$(p).css("width", w+"px");
			
			if ($("select",p).length > 0) {
				select = $("select",p)[0];
			}
			
			if (select) {
				t = '';
				ts = '';
				
				var i=0;
				$(select).css("display", "none");
				$("option",select).each(function() {
					if (!$(this).is(".hidden")) {
						if ($(this).html() == "-") {
							t += '<li class="hr"><div></div></li>';
						} else {
							t += '<li rel="'+$(this).val()+'" class="menuitem">'+$(this).html()+'</li>';
						}
					}
					if (this.selected=="selected") {
						ts = $(this).html();
						defselectitemsidx = i;
					}
					
					selectitems[i] = {};
					selectitems[i].v = $(this).val();
					selectitems[i].h = $(this).html();
					
					i++;
				});
				
				if (ts == "") {
					ts = selectitems[defselectitemsidx].h;
				}
				
				r = '<div class="loewy_dropdown_result"><span>'+ts+'</span><a href="#" class="lowey_dropdown_open"><img src="/images/icon_dropdownarrow.gif" /></a></div>';
				r += '<div class="loewy_dropdown_menuitems" style="display:none;"><ul>'+t+'</ul></div>';
				
				$(p).append(r);
				
				menuitems = $($(".loewy_dropdown_menuitems",p)[0]);
				
				menuresult = $($(".loewy_dropdown_result>span",p)[0]);
				arrow = $($(".loewy_dropdown_result>a.lowey_dropdown_open",p)[0]);
				height = 0;
				
				w = 0;
				li = $("li:eq(0)",menuitems);
				while(1) {
					w = w + $.loewy.num(li,"paddingLeft") + $.loewy.num(li,"paddingRight") + $.loewy.num(li,"marginLeft") + $.loewy.num(li,"marginRight");
					
					li = $(li).parent();
					if (li.is(".loewDropDownMenu")) {
						break;
					}
				}
				
				//menu item selection  
				$("li",menuitems).each(function() {
					if ($(this).is(".menuitem")) {
						$(this).width(options.width - w);
						$(this)
							.mouseover(function() {
								$(this).addClass("hover");
							});
						$(this)
							.mouseout(function() {
								$(this).removeClass("hover");
							});
						$(this)
							.mousedown(function(e) {
								e.preventDefault();
								r = $(this).attr("rel");
								menuresult.html($(this).html());
								select.value = r;
								
								hideMenu();
								return false;
							});
					}
				});
				
				//dropdown arrow click
				arrow.bind("click", onArrowClick);
				
				$(p).data("loewydropdown_forcehide",forceHide);
				$(p).data("loewydropdown_forceshow",forceShow);
				$(p).data("loewydropdown_resetvalue",resetValue);
			}
		};
		
		var onArrowClick = function(ev) {
			this.blur();
			showMenu();
			return false;
		};
		
		var onDocClick = function(ev) {
			o = menuresult.offset();
			l = o.left;
			t = o.top;
			h = menuresult.outerHeight();
			
			o = menuitems.offset();
			h += menuitems.outerHeight();
			
			if (ev.clientX < l || ev.clientX > l + width || ev.clientY < t || ev.clientY > (t + h)) {
				hideMenu();
			}
		};
		
		var showMenu = function() {
			if (!showed) {
				$(p).css({position:"absolute"});
				if (height == 0) {
					height = menuitems.outerHeight();
				}
				
				h = menuitems.outerHeight();
				hh = "show";
				if (h < height) {
					hh = "+=" + (height-h);
				}
				
				menuitems.stop();
				menuitems.animate({height:hh},{duration:options.slideDownSpeed,queue:false,easing:options.easeHide});
				showed = true;
				$(document).bind("click", onDocClick);
			}
		};
		
		var hideMenu = function() {
			if (showed) {
				menuitems.stop();
				menuitems.animate({height:"hide"},{duration:options.slideDownSpeed,queue:false,easing:options.easeHide});
				
				showed = false;
				$(document).unbind("click", onDocClick);
			}
		};
		
		var forceHide = function() {
			if (showed) {
				menuitems.stop();
				menuitems.css({display:"none"});
				$(p).css({display:"none"});
				$(document).unbind("click", onDocClick);
				showed = false;
			}
		};
		
		var forceShow = function() {
			$(p).css({display:"block"});
			css = {};
			css.display = "none";
			if (height != 0) {
				css.height=height+"px";
			}
			menuitems.css(css);
		};
		
		var resetValue = function() {
			s = selectitems[defselectitemsidx];
			menuresult.html(s.h);
			select.value = s.v;
		};
		
		init();
	}
})($);