if(jQuery) (function($){
	
	$.extend($.fn, {
			 
		dropdown: function(o, callback) {
			// options
			if (!o) var o = {};
			if (o.layerStackIndex == undefined) o.layerStackIndex = 0;
			if (o.noneSelected == undefined) o.noneSelected = 'Select';
			var hasfocus = 0;
			var nosubs = !$(this).next('.ddMenu').find('div').hasClass('ddSub');
			
			// input
			$(this).css("cursor", "default");
			$(this).val(o.noneSelected);
			
			// z-indexes
			$(this).parent().css("z-index",(100 + o.layerStackIndex));
			//if ($(this).parent().parent().hasClass('formline')) $(this).parent().parent().css("z-index",(100 + o.layerStackIndex));
			$(this).next('.ddMenu').css("z-index",(120 + o.layerStackIndex)).hide();
			
			// initialize
			if (!nosubs) {
				$(this).next('.ddMenu').find('.ddMain').find('ul').find('li').find('a').removeClass('optSel');
				$(this).next('.ddMenu').find('.ddSub').hide();
				$(this).next('.ddMenu').find('.ddMain').find('ul').find('li:first').find('a').addClass('optSel');
				$(this).next('.ddMenu').find('.ddSub:first').show();
			}
			
			// input events
			$(this).mouseover( function() {
				$(this).addClass('hover');
			}).mouseout( function() {
				$(this).removeClass('hover');
			}).click( function() {
				// Show/hide list
				if ($(this).hasClass('active')) {
					$(this).dropdownOptionsHide();
				} else {
					$(this).dropdownOptionsShow();
				}
				return false;
			}).focus( function() {
				$(this).addClass('focus');
			}).blur( function() {
				$(this).removeClass('focus');
				if ($(this).hasClass('active') && hasfocus > 0 ) {
					//alert('container visible and has focus')
				} else {
					$(this).dropdownOptionsHide();
				}
			});
			
			// cat events
			if (!nosubs) {
				$(this).next('.ddMenu').find('.ddMain').find('ul').find('li').each( function() {
					$(this).find('a').mouseover( function() {
						if ($(this).hasClass('optSel')) {
							
						} else {
							hasfocus = 1;
							var nam = $(this).attr('class');
							// de-select all
							$(this).parent().parent().parent().parent().find('.ddMain').find('a').removeClass('optSel');
							$(this).parent().parent().parent().parent().find('.ddSub').hide();
							// select
							$(this).addClass('optSel');
							$(this).parent().parent().parent().parent().find('#' + nam).show();
						}
					}).mouseout( function() {
						//hasfocus = -1;
					});
				});
			}
			
			// sub events
			if (!nosubs) {
				$(this).next('.ddMenu').find('.ddSub').find('.ddSubCol').find('label').mouseover( function() {
					hasfocus = 1;
					$(this).parent().find('label').removeClass('hover');
					$(this).addClass('hover');
				}).mouseout( function() {
					hasfocus = -1;
					$(this).parent().find('label').removeClass('hover');
				});
			} else {
				$(this).next('.ddMenu').find('.ddMain').find('label').mouseover( function() {
					hasfocus = 1;
					$(this).parent().find('label').removeClass('hover');
					$(this).addClass('hover');
				}).mouseout( function() {
					hasfocus = -1;
					$(this).parent().find('label').removeClass('hover');
				});
			}
			
		},
		
		// Hide the dropdown
		dropdownOptionsHide: function() {
			$(this).hasfocus = 0;
			$(this).removeClass('active').next('.ddMenu').hide();
		},
		
		// Show the dropdown
		dropdownOptionsShow: function() {
			// Show it
			$(this).addClass('active').next('.ddMenu').show();
			
			// Position it
			var offset = $(this).position();
			$(this).next('.ddMenu').css({ top: '30px' });
			$(this).next('.ddMenu').css({ left: '2px' });
			
			// Disappear on hover out
			dropdownCurrent = $(this);
			var timer = '';
			$(this).next('.ddMenu').hover( function() {
				clearTimeout(timer);
			}, function() {
				timer = setTimeout('$(dropdownCurrent).dropdownOptionsHide(); $(dropdownCurrent).unbind("hover");', 250);
			});
			
		}
		
	});
	
})(jQuery);