jQuery.fn.defaultButton = function(settings) {
	if(!(settings.button || settings.triggers || settings.onTrigger))
		settings = {button:$(settings)};
	
	settings = jQuery.extend({
		triggers : [13],
		onTrigger : function(ev, btn) { $(btn).trigger("click"); },
		button : $(":submit", this).get(0),
		autoExclude : $("textarea")
	}, settings);	
	
	this.not(settings.autoExclude).each(function() {
		$(this).bind("keypress", function(ev) {	
			var r = true;
			var kc = ev.keyCode;
			if(settings.onTrigger && settings.button) {
				var tcnt = settings.triggers.length; 
				for(var i = 0; i < tcnt; i++) {
					if(settings.triggers[i] == kc) {
						settings.onTrigger(ev, settings.button); 
						r = false;
					}
				}
			}
			return r;
		});
	});
	return this;
}

// Sets the "disabled" attribute of each item to "disabled", @returns jQuery chain
jQuery.fn.disable = function() { return this.each(function() { this.setAttribute("disabled", "disabled"); });}

// Removes the "disabled" attribute of each item, @returns jQuery chain
jQuery.fn.enable = function() { return this.each(function() { this.removeAttribute("disabled"); }); }

// Removes the "checked" attribute of each item, @returns jQuery chain
jQuery.fn.uncheck = function() { return this.each(function() { this.removeAttribute("checked"); }); }

// Sets the "checked" attribute of each item to "checked", @returns jQuery chain
jQuery.fn.check = function() { return this.each(function() { this.setAttribute("checked", "checked") }); }

// Limits the length of an input field to a maximum length.
jQuery.fn.limitInput = function(settings) {	
	settings = jQuery.extend({
		maxLength:0,
		restrictedKeys:[],
		allowedKeys:[8,9,13,16,17,18,19,20,27,33,34,35,36,37,38,39,40,45,46,112,113,114,115,116,117,118,119,120,121,122,123,144,145],
		onKeyAccepted:function(value, key) { },
		onKeyDenied:function(value, key) { },
		truncateOnChange:true
	}, settings);	
	
	var arrayContains = function(array, value) {
		if(!array) return false;
		var len = array.length;
		for(var i = 0; i < len; i++)
			if(array[i] == value) return true;			
		return false;
	};
	
	var allow = function(value, key) {
		if(settings.onKeyAccepted) settings.onKeyAccepted(value, key);
	}
	
	var deny = function(value, key) {
		if(settings.onKeyDenied) settings.onKeyDenied(value, key);
	}
	
	this.each(function(i) {
		$(this).bind("keypress", function(ev) {
			var key = ev.keyCode;
			if (arrayContains(settings.allowedKeys, key)) {
				allow(this.value, key);	
				return true;
			} else if (this.value.length >= settings.maxLength || arrayContains(settings.restrictedKeys, key)) {
				deny(this.value, key);
				return false;
			} else {
				allow(this.value, key);
				return true;
			}
		});
	});
	
	if(settings.truncateOnChange) {
		this.change(function() {
			if(this.value.length >= settings.maxLength) this.value = this.value.slice(0, settings.maxLength);
		});
	}
	return this;
}



function getElem(id){	
	var dropId = "";
	switch(id){
		case "specialtyFoods":
			dropId = "specialtyDropDown";
		break;
		case "Recipes":
			dropId = "recipesDropDown";
		break;
		case "contactUs":
			dropId = "contactDropDown";
		break
		case "aboutUs":
			dropId = "aboutDropDown";
		break;
		case "orderInformation":
			dropId = "orderDropDown";
		break;
		case "Philanthropy":
			dropId = "PhilDropDown";
		break;		
		default:
			dropId="";
	}
	return dropId;
}
function DropDowns(id){
	var masterElem = (document.getElementById)?document.getElementById(id): null;
	var dropId = getElem(id);
	if(masterElem){
		makeDropDown(dropId, masterElem);
		endDropDown(dropId, masterElem);
	}
	return false;
}
function makeDropDown(i, master){
	var drop = (document.getElementById)?document.getElementById(i): null;	
	master.onmouseover = function(){
		drop.style.visibility = "visible";
		master.style.display = "block";
		drop.style.display = "block";
	}
	drop.onmouseover = function(){
		drop.style.visibility = "visible";
		master.style.display = "block";
		drop.style.display = "block";		
	}
	return false;
}
function endDropDown(i, master){
	var drop = (document.getElementById)?document.getElementById(i): null;
	drop.onmouseout = function(){
		drop.style.visibility = "hidden";
		drop.style.display = "none";		
	}
	master.onmouseout = function(){
		drop.style.visibility = "hidden";
		drop.style.display = "none";		
	}	
	return false;
}
function doDropDown(){
	DropDowns("specialtyFoods");
	DropDowns("Recipes");
	DropDowns("contactUs");
	DropDowns("aboutUs");
	DropDowns("orderInformation");
	DropDowns("Philanthropy");	
	return false;
}
$(document).ready(function() {
	doDropDown();
	
	var acct = $(".accountList");
	acct.defaultButton($(":image",acct));
	
	$(".disabled").click(function() { return false; });
	$(".hasDefaultButton").each(function(i) {
		$(this).defaultButton($(".defaultButton", this));
	});
});