
(function($){

	// set the defaults
	var defaults = {
		// use a comma as the string delimiter
		delimiter: ",",
		// the default key binding to check for when using the createCheckboxRange()
		checkboxRangeKeyBinding: "shiftKey",
		// for methods that could return either a string or array, decide default behavior
		useArray: false
	};

	// set default options
	$.Field = {
		version: "0.9.2",
		setDefaults: function(options){
			$.extend(defaults, options);
		},
		setProperty: function(prop, value){
			defaults[prop] = value;
		},
		getProperty: function(prop){
			return defaults[prop];
		}
	};


	// this will set/get the values for a field based upon and array
	$.fn.fieldArray = function(v){
		var t = $type(v);

		// if no value supplied, return an array of values
		if( t == "undefined" ) return getValue(this);

		// convert the number/string into an array
		if( t == "string" ||  t == "number" ){
			v = v.toString().split(defaults.delimiter);
			t = "array";
		}

		// set the value -- doesn't break the chaing
		if( t == "array" ) return setValue(this, v);

		// if we don't know what do to, don't break the chain
		return this;
	};

	$.fn.getValue = function(){
		// return the values as a comma-delimited string
		return getValue(this).join(defaults.delimiter);
	};

	var getValue = function(jq){
		var v = [];

		jq.each(
			function (lc){
				// get the current type
				var t = getType(this);

				switch( t ){
					case "checkbox": case "radio":
						// if the checkbox or radio element is checked
						if( this.checked ) v.push(this.value);
					break;

					case "select":
						if( this.type == "select-one" ){
							v.push( (this.selectedIndex == -1) ? "" : getOptionVal(this[this.selectedIndex]) );
						} else {
							// loop through all element in the array for this field
							for( var i=0; i < this.length; i++ ){
								// if the element is selected, get the selected values
								if( this[i].selected ){
									// append the selected value, if the value property doesn't exist, use the text
									v.push(getOptionVal(this[i]));
								}
							}
						}
					break;

					case "text":
						v.push(this.value);
					break;
				}
			}
		);

		// return the values as an array
		return v;
	};

	$.fn.setValue = function(v){
		// f no value, set to empty string
		return setValue(this, ((!v && (v !== 0)) ? [""] : v.toString().split(defaults.delimiter)));
	};

	var setValue = function(jq, v){
		jq.each(
			function (lc){
				var t = getType(this), x;

				switch( t ){
					case "checkbox": case "radio":
						if( valueExists(v, this.value) ) this.checked = true;
						else this.checked = false;
					break;

					case "select":
						var bSelectOne = (this.type == "select-one");
						var bKeepLooking = true; // if select-one type, then only select the first value found
						// loop through all element in the array for this field
						for( var i=0; i < this.length; i++ ){
							x = getOptionVal(this[i]);
							bSelectItem = valueExists(v, x);
							if( bSelectItem ){
								this[i].selected = true;
								// if a select-one element
								if( bSelectOne ){
									// no need to look farther
									bKeepLooking = false;
									// stop the loop
									break;
								}
							} else if( !bSelectOne ) this[i].selected = false;
						}
						// if a select-one box and nothing selected, then try to select the default value
						if( bSelectOne && bKeepLooking && !!this[0] ){
							this[0].selected = true;
						}
					break;

					case "text":
						this.value = v.join(defaults.delimiter);
					break;
				}

			}
		);

		return jq;
	};

	$.fn.formHash = function(inHash){
		var bGetHash = (arguments.length == 0);
		// create a hash to return
		var stHash = {};

		// run the code for each form
		this.filter("form").each(
			function (){
				// get all the form elements
				var els = this.elements, el, n, stProcessed = {}, jel;

				// loop through the elements and process
				for( var i=0, elsMax = els.length; i < elsMax; i++ ){
					el = els[i]; 
					n = el.name;

					// if the element doesn't have a name, then skip it
					if( !n || stProcessed[n] ) continue;

					// create a jquery object to the current named form elements
					var jel = $(el.tagName.toLowerCase() + "[name='"+n+"']", this);

					// if we're getting the values, get them now
					if( bGetHash ){
						stHash[n] = jel[defaults.useArray ? "fieldArray" : "getValue"]();
					// if we're setting values, set them now
					} else if( typeof inHash[n] != "undefined" ){
						jel[defaults.useArray ? "fieldArray" : "setValue"](inHash[n]);
					}

					stProcessed[n] = true;
				}
			}
		);

		// if getting a hash map return it, otherwise return the jQuery object
		return (bGetHash) ? stHash : this;
	};

	$.fn.autoAdvance = function(callback){
		return this.find(":text,:password,textarea").bind(
			"keyup.autoAdvance",
			function (e){
				var
					// get the field
					$field = $(this),
					// get the maxlength for the field
					iMaxLength = parseInt($field.attr("maxlength"), 10);

				// if the user tabs to the field, exit event handler
				// this will prevent movement if the field is already
				// field in with the max number of characters
				if( isNaN(iMaxLength) || ("|9|16|37|38|39|40|".indexOf("|" + e.keyCode + "|") > -1) ) return true;

				// if the value of the field is greater than maxlength attribute,
				// then move the focus to the next field
				if( $field.getValue().length >= $field.attr("maxlength") ){
					// move to the next field and select the existing value
					var $next = $field.moveNext().select();
					if( $.isFunction(callback) ) callback.apply($field, [$next]);
				}
			}
		);
	};
	$.fn.moveNext = function(){
		return this.moveIndex("next");
	};

	$.fn.movePrev = function(){
		return this.moveIndex("prev");
	};

	$.fn.moveIndex = function(i){
		// get the current position and elements
		var aPos = getFieldPosition(this);

		// if a string option has been specified, calculate the position
		if( i == "next" ) i = aPos[0] + 1; // get the next item
		else if( i == "prev" ) i = aPos[0] - 1; // get the previous item

		// make sure the index position is within the bounds of the elements array
		if( i < 0 ) i = aPos[1].length - 1;
		else if( i >= aPos[1].length ) i = 0;

		return $(aPos[1][i]).trigger("focus");
	};

	$.fn.getTabIndex = function(){
		// return the position of the form field
		return getFieldPosition(this)[0];
	};

	var getFieldPosition = function (jq){
		var
			// get the first matching field
			$field = jq.filter("input, select, textarea").get(0),
			// store items with a tabindex
			aTabIndex = [],
			// store items with no tabindex
			aPosIndex = [];

		// if there is no match, return 0
		if( !$field ) return [-1, []];

		// make a single pass thru all form elements
		$.each(
			$field.form.elements,
			function (i, o){
				if( o.tagName != "FIELDSET" && !o.disabled ){
					if( o.tabIndex > 0 ){
						aTabIndex.push(o);
					} else {
						aPosIndex.push(o);
					}
				}
			}
		);

		// sort the fields that had tab indexes
		aTabIndex.sort(
			function (a, b){
				return a.tabIndex - b.tabIndex;
			}
		);

		// merge the elements to create the correct tab position
		aTabIndex = $.merge(aTabIndex, aPosIndex);

		for( var i=0; i < aTabIndex.length; i++ ){
			if( aTabIndex[i] == $field ) return [i, aTabIndex];
		}

		return [-1, aTabIndex];
	};

	$.fn.limitSelection = function(limit, options){
		// get the options to use
		var opt = jQuery.extend(
			(limit && limit.constructor == Object ? limit : {
					limit: limit
				, onsuccess: function (limit){ return true; }
				, onfailure: function (limit){ alert("You can only select a maximum a of " + limit + " items."); return false; }
			})
			, options);
		
		var self = this;

		var getCount = function (el){
			if( el.type == "select-multiple" ) return $("option:selected", self).length;
			else if( el.type == "checkbox" ) return self.filter(":checked").length;
			return 0;
		};

		var undoSelect = function (){
			// reduce selection to n items
			setValue(self, getValue(self).slice(0, opt.limit));
			// do callback
			return opt.onfailure.apply(self, [opt.limit]);
		};

		return this.bind(
			(!!self[0] && self[0].type == "select-multiple") ? "change.limitSelection" : "click.limitSelection",
			function (){
				if( getCount(this) > opt.limit ){
					// run callback, it must return false to prevent action
					return (this.type == "select-multiple") ? undoSelect() : opt.onfailure.apply(self, [opt.limit]);
				}
				opt.onsuccess.apply(self, [opt.limit]);
				return true;
			}
		);
	};


	$.fn.createCheckboxRange = function(callback){
		// get the options to use
		var opt = jQuery.extend(
			(callback && callback.constructor == Object ? callback : {
					bind: defaults.checkboxRangeKeyBinding
				, onclick: callback
			})
			, callback);

		var iLastSelection = 0, self = this, bCallback = $.isFunction(opt.onclick);

		// if there's a call back, bind it now and run it
		if( bCallback ) 
			this.each(function (){opt.onclick.apply(this, [$(this).is(":checked")])});
		
		// loop through each checkbox and return the jQuery object
		return this.each(
			function (){
				// only perform this action on checkboxes
				if( this.type != "checkbox" ) return false;
				var el = this;

				var updateLastCheckbox = function (e){
					iLastSelection = self.index(e.target);
				};

				var checkboxClicked = function (e){
					var bSetChecked = this.checked, current = self.index(e.target), low = Math.min(iLastSelection, current), high = Math.max(iLastSelection+1, current);
					// run the callback for the clicked item
					if( bCallback ) $(this).each(function (){opt.onclick.apply(this, [bSetChecked])});
					// if we don't detect the keypress, exit function
					if( !e[opt.bind] ) return;
					
					// loop through the items in the selected range
					for( var i=low; i < high; i++ ){
						// make sure to correctly set the checked status
						var item = self.eq(i).attr("checked", bSetChecked ? "checked" : "");
						// run the callback
						if( bCallback ) opt.onclick.apply(item[0], [bSetChecked]);
					}
					
					return true;
				};
				
				$(this)
					// unbind the events so we can re-run the createCheckboxRange() plug-in for dynamically created elements
					.unbind("click.createCheckboxRange")

					// bind the functions, we bind on blur for keyboard selected items
					.bind("click.createCheckboxRange", checkboxClicked)
					.bind("click.createCheckboxRange", updateLastCheckbox)
					;

				return true;
			}
		);
	};

	// determines how to process a field
	var getType = function (el){
		var t = el.type;

		switch( t ){
			case "select": case "select-one": case "select-multiple":
				t = "select";
				break;
			case "text": case "hidden": case "textarea": case "password": case "button": case "submit": case "submit":
				t = "text";
				break;
			case "checkbox": case "radio":
				t = t;
				break;
		}
		return t;
	};

	// gets the value of a select element
	var getOptionVal = function (el){
		 return jQuery.browser.msie && !(el.attributes['value'].specified) ? el.text : el.value;
	};

	// checks to see if a value exists in an array
	var valueExists = function (a, v){
		return ($.inArray(v, a) > -1);
	};

	// correctly gets the type of an object (including array/dates)
	var $type = function (o){
		var t = (typeof o).toLowerCase();

		if( t == "object" ){
			if( o instanceof Array ) t = "array";
	 		else if( o instanceof Date ) t = "date";
	 	}
	 	return t;
	};

	// checks to see if an object is the specified type
	var $isType = function (o, v){
		return ($type(o) == String(v).toLowerCase());
	};

})(jQuery);
