var dropmenu = {
	mouseOutDetectTimeForDown : 1000,
	mouseOutDetectTimeForOver : 100,
	init: function() {
		$(".dropdown").each(function(index, obj){
			var _menu = $($(this).attr("rel"));
			var _btn = $(this).parent();
			
			$(this).click(function(evt){
				evt.preventDefault();
				
				_menu.attr("dropoveron", "1");
				var _t = _btn.position().top + _btn.height();
				
				var _l = (_menu.attr("align") == "right") ? 
							_btn.position().left - (_menu.width() - _btn.width()) : 
							_btn.position().left - ((_menu.width() - _btn.width()) / 2);
				_menu.css({
					'top' : _t,
					'left' : _l
				});
				
				if(_menu.filter(":visible").length > 0) {
					//_menu.removeClass("on");
					_menu.slideUp();
				} else {
					var _link = $(this);
					
					if(typeof(jQuery.easing.easeOutBounce) != "function") {
						jQuery.easing.easeOutBounce = function (x, t, b, c, d) {
							if ((t/=d) < (1/2.75)) {
								return c*(7.5625*t*t) + b;
							} else if (t < (2/2.75)) {
								return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
							} else if (t < (2.5/2.75)) {
								return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
							} else {
								return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
							}
						};
					} 
					
					_menu.slideDown(700, 'easeOutBounce',function(){
						//_menu.addClass("on");
						var _iField = $(this).find("input[type=text]:eq(0)");
						if(_iField.length > 0)
							_iField.focus().select();
					});
				
				}
				
			}).mouseover(function(){
				_menu.attr("dropoveron", "1");			
			});
			
			var _hide = function() {
				if(_menu.attr("dropoveron") == "0" && _menu.find(".focused").length == 0) {
					_menu.slideUp(300, function() {
						$(this).hide();
						_btn.children(".dropdown").removeClass("on");
					});
				}
			};
			
			_btn.mouseout(function() {
				_menu.attr("dropoveron", "0");
							
				setTimeout(_hide, dropmenu.mouseOutDetectTimeForDown);
			});
			
			_menu.mouseover(function(){
				_menu.attr("dropoveron", "1");			
			}).mouseout(function() {
				_menu.attr("dropoveron", "0");
							
				setTimeout(_hide, dropmenu.mouseOutDetectTimeForDown);
			});
			
		});
			
		$(".dropover").each(function(index, obj){
			var _menu = $($(this).attr("rel"));
			var _btn = $(this).parent();
			var _align = $(this).attr("align");
			var _this = $(this);
			
			if($(this).attr("block") == "1") {
				$(this).click(function(evt){
					evt.preventDefault();
					$(this).attr("href", "");
				});
			}
			
			_btn.mouseover(function(){
				
				var _t = _btn.position().top + _btn.outerHeight();
				if($(this).attr("offset"))
					var _offset = $(this).attr("offset")
				else {
					var _offset = (_menu.offset().left <= 0) ? 0 : 1;
					$(this).attr("offset", _offset);
				}
				var _l = (_align == "center") ? 
							((_offset == 1) ? _btn.position().left : 0 )- ((_menu.outerWidth() - _btn.outerWidth()) / 2) :
							_btn.position().left;
				
				_menu.css({
					'top' : _t,
					'left' : _l
				});
				
				_btn.attr("dropoveron", "1");
				if(!_btn.hasClass("on"))
					_btn.addClass("on");
				_menu.show();
			}).mouseout(function() {
				_btn.attr("dropoveron", "0");
				var _hide = function() {
					if(_btn.attr("dropoveron") == "0") {
						_menu.hide();
						_btn.removeClass("on");
					}
				};
							
				setTimeout(_hide, dropmenu.mouseOutDetectTimeForOver);
			});
		});
	}
};
