/*
 * jQuery Field Plug-in
 *
 * Copyright (c) 2007 Dan G. Switzer, II
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
*/
(function($){

	// set the defaults
	var defaults = {
		// use a comma as the string delimiter
		delimiter: ",",
		// for methods that could return either a string or array, decide default behavior
		useArray: false
	}

	// set default options
	$.Field = {
		version: "0.6",
		setDefaults: function(options){
			$.extend(defaults, options);
		}
	}

	// 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;
	}

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

	// the getValue() method -- break the chain
	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;
	}

	// the setValue() method -- does *not* break the chain
	$.fn.setValue = function(v){
		// f no value, set to empty string
		return setValue(this, (!v ? [""] : v.toString().split(defaults.delimiter)));
	}

	// the setValue() method -- does *not* break the chain
	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].selected = true;
						}
					break;

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

			}
		);

		return jq;
	}

	// the formHash() method -- break the chain
	$.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( !!inHash[n] ){
						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.limitSelection = function(n, _e, _s){
		var self = this;
		// define the callback actions
		var cb_onError = (!!_e) ? _e : function (n){ alert("You can only select a maximum a of " + n + " items."); return false; };
		var cb_onSuccess = (!!_s) ? _s : function (n){ return true; };

		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, n));
			// do callback
			return cb_onError(n, self);
		}

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

	$.fn.createCheckboxRange = function(){
		var iLastSelection = 0, me = this;

		// this finds the position of the current element in the array
		var findArrayPos = function (el){
			var pos = -1;
			$("input[@name='"+me[0].name+"']").each(
				function (i){
					if( this == el ){
						pos = i;
						return false;
					}
				}
			);

			return pos;
		}

		this.each(
			function (lc){
				// only perform this action on checkboxes
				if( this.type != "checkbox" ) return false;
				var self = this;

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

				var checkboxClicked = function (e){
					var bSetChecked = this.checked, current = findArrayPos(e.target), iHigh, iLow;
					// if we don't detect the keypress, exit function
					if( !e.shiftKey ) return;

					// figure out which is the highest and which is the lowest value
					if( iLastSelection > current ){
						iHigh = iLastSelection;
						iLow = current-1;
					} else {
						iHigh = current;
						iLow = iLastSelection-1;
					}

					$("input[@name='"+self.name+"']:gt("+iLow+"):lt("+iHigh+")").attr("checked", bSetChecked ? "checked" : "");
				}

				$(this)
					// unbind the events so we can re-run the createCheckboxRange() plug-in for dynamicall created elements
					.unbind("blur", updateLastCheckbox)
					.unbind("click", checkboxClicked)

					// bind the functions
					.bind("blur", updateLastCheckbox)
					.bind("click", checkboxClicked)
					;

				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);
