/*
 * jQuery doubleSelect Plugin
 * version: 1.0
 * @requires jQuery v1.3.2 or later
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * @version $Id: jquery.doubleSelect.js 3 2009-03-27 09:00:00Z jgeppert $
 * @author  Johannes Geppert <post at jgeppert dot com> http://www.jgeppert.com
 */

/**
 * Converts passed JSON options into <select> elements.
 * 
 * @param String
 *            id of the second select box
 * @param String
 *            option values
 * @param array
 *            options additional options (optional)
 */

( function($) {
	$.fn.doubleSelect = function(doubleid, values, options) {

		options = $.extend( {
			preselectFirst :null,
			preselectSecond :null,
			emptyOption :false,
			emptyKey :-1,
			emptyValue :'Choose ...'
		}, options || {});

		var $first = this;
		var $secondid = "#" + doubleid;
		var $second = $($secondid);

		var setValue = function(value) {
			$second.val(value).change();
		}

		var removeValues = function() {
			$($secondid + " option").remove();
		};

		$(this).change( function() {
			removeValues();
			$current = this.options[this.selectedIndex].value;
			if ($current != '') {
				$.each(values, function(k, v) {
					if ($current == v.key) {
						$.each(v.values, function(k, v2) {
							var o = $("<option>").html(k).attr('value', v2);
							o.appendTo($second);
						});

					}

				});

			} else {
				setValue(options.emptyValue);
			}
		});

		return this.each( function() {
			$first.children().remove();
			$second.children().remove();

			if (options.emptyOption) {
				var o = $("<option>").html(options.emptyValue).attr('value',
						options.emptyKey);
				o.appendTo($first);
			}

			$.each(values, function(k, v) {
				var o = $("<option>").html(k).attr('value', v.key);
				o.appendTo($first);

			});

			if (options.preselectFirst == null) {
				$current = this.options[this.selectedIndex].value;
				if ($current != '') {
					$.each(values, function(k, v) {
						if ($current == v.key) {
							$.each(v.values,
									function(k, v2) {
										var o = $("<option>").html(k).attr(
												'value', v2);
										o.appendTo($second);
									});

						}

					});

				} else {
					setValue(options.emptyValue);
				}
			} else {
				$first.each( function() {
					var o = $first.children();
					;
					var oLength = o.length;
					for ( var i = 0; i < oLength; i++) {
						if (o[i].value.match(options.preselectFirst)) {
							o[i].selected = true;
						}
					}
				});
				$first.change();
				if (options.preselectSecond != null) {
					$second.each( function() {
						var o = $second.children();
						var oLength = o.length;
						for ( var i = 0; i < oLength; i++) {
							if (o[i].value.match(options.preselectSecond)) {
								o[i].selected = true;
							}
						}
					});
				}
			}

		});

	}
})(jQuery);

