/*
 * jQuery doubleSelect Plugin
 * version: 1.3
 * @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-04-24 12:00:00Z $
 * @author  Johannes Geppert <post at jgeppert dot com> http://www.jgeppert.com
 *  7/23/2009 - J. Hinds change to two pass method to locate element for 'selected' attribute
 */
/**
 * 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(a){a.fn.doubleSelect=function(i,c,d){d=a.extend({preselectFirst:null,preselectSecond:null,emptyOption:false,emptyKey:-1,emptyValue:"Choose ..."},d||{});var g=this;var b="#"+i;var f=a(b);var h=function(j){f.val(j).change()};var e=function(){a(b+" option").remove()};a(this).change(function(){e();$current=this.options[this.selectedIndex].value;if($current!=""){a.each(c,function(l,j){var m;if($current==j.key){a.each(j.values,function(n,o){if(!m&&(j.defaultvalue!==null&&o==j.defaultvalue)){m=n}if(d.preselectSecond!==null&&o==d.preselectSecond){m=n}});a.each(j.values,function(n,q){var p=a("<option>").html(n).attr("value",q);if(d.preselectSecond){a.each(d.preselectSecond,function(k,o){if(q==o){p.html(n).attr("selected","selected")}})}p.appendTo(f)})}})}else{h(d.emptyValue)}});return this.each(function(){g.children().remove();f.children().remove();if(d.emptyOption){var j=a("<option>").html(d.emptyValue).attr("value",d.emptyKey);j.appendTo(g)}a.each(c,function(m,l){var n=a("<option>").html(m).attr("value",l.key);if(d.preselectFirst!==null&&l.key==d.preselectFirst){n.html(m).attr("selected","selected")}n.appendTo(g)});if(d.preselectFirst===null){$current=this.options[this.selectedIndex].value;if($current!=""){a.each(c,function(m,l){var n;if($current==l.key){a.each(l.values,function(o,p){if(!n&&(l.defaultvalue!==null&&p==l.defaultvalue)){n=o}if(d.preselectSecond!==null&&p==d.preselectSecond){n=o}});a.each(l.values,function(p,r){var q=a("<option>").html(p).attr("value",r);if(p===n){q.html(p).attr("selected","selected")}q.appendTo(f)})}})}else{h(d.emptyValue)}}else{g.change()}})}})(jQuery);
