(function(b){function f(a,c,d){c=h(a,c,d);i(d,a,c)}function h(a,c,d){var e=[];e.push(c);if(a[c].belongs_to!==c&&a[c].belongs_to){e.push(a[c].belongs_to);f(a,a[c].belongs_to,d)}return e}function i(a,c,d){d.reverse();for(var e=0;e<d.length;e++)if(a=="link"||a=="radio")b("#"+d[e]).click();else if(a=="dropdown")if(d[e]!="ROOT"){b("#"+d[e]).attr("selected","selected");b.fn.treebuilder.next_category(c,b("#"+d[e]).parent())}}b.fn.treebuilder=function(a){var c=b.extend({},b.fn.treebuilder.defaults,a);for(var d in c)b.fn.treebuilder[d]=
c[d];return this.each(function(){$this=b(this);var e=b.meta?b.extend({},c,$this.data()):c,g=e.start_param||b.findParam(e.url_param);b(this).treebuilder.options=e;b(this).treebuilder.tree=e.tree;b(this).treebuilder.elementID=this.id;b(this).html("");b(this).treebuilder.display_nodes(e);g&&f(e.tree,g,e.element_type)})};b.fn.treebuilder.display_nodes=function(a){a=a.key;var c=this.tree;if((this.parent=a)&&c[a].children){b.fn.treebuilder.display_start(c,c[a]);for(var d=0;d<c[a].children.length;d++)b.fn.treebuilder.display_content(c[c[a].children[d]]);
b.fn.treebuilder.display_end(c,c[a])}else a&&b.fn.treebuilder.final_output(c[a]);this.element_type=="dropdown"&&c[a].children&&b.fn.treebuilder.bind_event(this.tree)};b.fn.treebuilder.defaults={tree:"",key:"ROOT",key_callback:function(a){return a.region_key},display_callback:function(a){return a.display_name},url_callback:function(a){return a.url_part+"?"+a.category_key},url_param:"",start_param:"",node_class:"tree_node",child_node_class:"child",select_text:"Select",prevent_link_condition:function(){return 0}};
b.fn.treebuilder.display_start=function(a,c){if(this.element_type=="link")b("#"+this.elementID).append('<div class="'+this.options.node_class+'"></div>');else if(this.element_type=="dropdown"){a=b("#"+this.elementID).append('<div class="'+this.options.node_class+'"><select name="select_child"><option value="">'+this.options.select_text+"</option></select></div>");b.fn.treebuilder.display_element_title(a,c)}else this.element_type=="radio"&&b("#"+this.elementID).append('<div class="'+this.options.node_class+
'"></div>');this.parent!="ROOT"&&b("#"+this.elementID).children("."+this.options.node_class+":last").addClass(this.options.child_node_class)};b.fn.treebuilder.display_content=function(a){if(this.element_type=="link"){b.fn.treebuilder.display_link(a);b.fn.treebuilder.bind_event(this.tree,a)}else if(this.element_type=="dropdown")b.fn.treebuilder.display_dropdown(a);else if(this.element_type=="radio"){b.fn.treebuilder.display_radio(a);b.fn.treebuilder.bind_event(this.tree,a)}};b.fn.treebuilder.display_link=
function(a){b("#"+this.elementID).children("."+this.options.node_class+":last").append('<a id="'+this.options.key_callback(a)+'" href="'+this.options.url_callback(a)+'">'+this.options.display_callback(a)+"</a>")};b.fn.treebuilder.display_dropdown=function(a){b("#"+this.elementID).children("."+this.options.node_class+":last").children("select:last").append('<option id="'+this.options.key_callback(a)+'" value="'+this.options.key_callback(a)+'">'+this.options.display_callback(a)+"</option>")};b.fn.treebuilder.display_radio=
function(a){b("#"+this.elementID).children("."+this.options.node_class).append('<input type="radio" name="'+this.parent+'" value="'+this.options.key_callback(a)+'">'+this.options.display_callback(a))};b.fn.treebuilder.display_element_title=function(){};b.fn.treebuilder.display_end=function(a){this.element_type=="dropdown"&&b("#"+this.elementID).children("."+this.options.node_class+":last").children("select").bind("change",{tree:a},function(c){b.fn.treebuilder.next_category(c.data.tree,this)})};b.fn.treebuilder.bind_event=
function(a,c){if(this.element_type=="link")b("#"+this.elementID).children("."+this.options.node_class+":last").children("a:last").bind("click",{tree:a,options:this.options},function(d){if(d.data.tree[this.id].children||d.data.options.prevent_link_condition(c)){d.preventDefault();b.fn.treebuilder.next_category(d.data.tree,this);"function"==typeof d.data.options.after_non_leaf_click&&d.data.options.after_non_leaf_click(d,c)}});else if(this.element_type=="radio")b("#"+this.elementID).children("."+this.options.node_class+
":last").children("input:last").bind("click",{tree:a},function(d){b.fn.treebuilder.next_category(d.data.tree,this)});else this.element_type=="dropdown"&&b("#"+this.elementID).children("."+this.options.node_class+":last").children("select:last").bind("change",{tree:a},function(d){b.fn.treebuilder.next_category(d.data.tree,this);return false})};b.fn.treebuilder.next_category=function(a,c){b(c).siblings().removeClass("current");b(c).addClass("current");b(c).parent().nextAll().remove();if(this.element_type==
"link")b("#"+this.elementID).children("."+this.options.node_class+":last").after(b.fn.treebuilder.display_nodes({tree:a,key:c.id}));else if(this.element_type=="dropdown")b("#"+this.elementID).children("."+this.options.node_class+":last").after(b.fn.treebuilder.display_nodes({tree:a,key:b(c).val()}));else this.element_type=="radio"&&b("#"+this.elementID).children("."+this.options.node_class+":last").after()};b.fn.treebuilder.final_output=function(a){if(this.element_type=="dropdown")b("#"+this.elementID).append('<input type="hidden" name="'+
this.options.url_param+'" value="'+this.options.key_callback(a)+'" /> <p> you have selected '+this.options.display_callback(a)+"</p>");else this.element_type=="radio"&&b("#"+this.elementID).append('<input type="hidden" name="'+this.options.url_param+'" value="'+this.options.key_callback(a)+'" /> <p> you have selected '+this.options.display_callback(a)+"</p>")};b.findParam=function(a){var c=(new RegExp("[\\?&]"+a+"=([^&#]*)")).exec(window.location.href);if(c)return c[1]||0;c=b("input[name='"+a+"']").val();
return a?c:0}})(jQuery);
