/**
 * Dropdown Menu - jQuery plugin for accessible, unobtrusive Dropdown Menus based on Selectfields
 * Inspired by http://www.themaninblue.com/experiment/AccessibleSelect/ which is nice but not working without Jsavascript
 * @requires jQuery v1.0.3
 * Usage:
 * $("form").dropdownmenu();
 *
 * http://blog.ginader.de
 *
 * Copyright (c) 2007 Dirk Ginader (ginader.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * @maker Dirk Ginader
 * @contributor Michael Haschke, http:/eye48.com/
 *
 * Version: 1.1
 *
 * Differences to Version 1.0:
 *	o it removes button[type=submit] and input[type=submit] elements
 *	o it works with multiple select elements in one form
 */

(function($) {
	$.fn.dropdownmenu = function(settings) {
		$(this).each(function() {
			var f = $(this);
			var selects = f.find("select");
			selects.each(function(i) {
				var s = $(this);
				if(!s.activeIndex) {
					s[0].activeIndex = s[0].selectedIndex;
					s[0].changed = false;
				}
				s.keydown(function(e) {
					var keyCodeTab = "9";
					var keyCodeEnter = "13";
					var keyCodeEsc = "27";
					if(e.keyCode == keyCodeTab || e.keyCode == keyCodeEnter) {
						dropdownmenuonchange(this,f);
					}
					if(e.keyCode == keyCodeEsc) {
						this.selectedIndex = this.activeIndex;
					}
				})
				s.blur(function(e) {
					dropdownmenuonchange(this,f);
				})
				s.click(function() {
					dropdownmenuonchange(this,f);
				});
			});
			$(this).find("button[@type=submit]").remove();
		});
		return this;
	};
	dropdownmenuonchange = function(s,f) {
		if(s.selectedIndex != s.activeIndex && !s.changed) {
			s.changed = true;
			f.submit();
		}
	}
})(jQuery);