var angellist=angellist||{};angellist.company_selector={version:"1.2",labels:{remove:"Delete",no_results:"No results found.",search:"Add a company:",search_placeholder:"Start typing..."},company_ids:[],create_company_list:function(){var a=jQuery("#angellist-company-selector-results");if(a.length===0){return}a.html("");angellist.company_selector.company_list=jQuery("
").attr("id","angellist-company-selector-companies");a.append(angellist.company_selector.company_list);angellist.company_selector.enable_editable_company_list()},add_company:function(c){if(!jQuery.isPlainObject(c)||c.value===undefined||c.value===""||c.label===undefined||jQuery.inArray(c.value,angellist.company_selector.company_ids)>-1){return}if(angellist.company_selector.company_list===undefined||angellist.company_selector.company_list.length===0){angellist.company_selector.create_company_list()}var b=angellist.company_selector.company_ids.length;var a=jQuery(" ").text(c.label);a.append(jQuery(" ").attr({type:"hidden",name:"angellist-company["+b+"][id]"}).addClass("angellist-company-id").val(c.value));a.append(jQuery(" ").attr({type:"hidden",name:"angellist-company["+b+"][name]"}).val(c.label));a.append(angellist.company_selector.company_delete_button());a.mouseenter(angellist.company_selector.company_mouseenter).mouseleave(angellist.company_selector.company_mouseleave);angellist.company_selector.company_list.append(a);angellist.company_selector.company_ids.push(c.value);if(angellist.company_selector.company_ids.length==2){angellist.company_selector.enable_editable_company_list()}},company_delete_button:function(){return jQuery(" ").attr({type:"button",title:angellist.company_selector.labels.remove}).addClass("angellist-delete-company").text("X").click(angellist.company_selector.delete_company_handler).hide()},delete_company_handler:function(){var b=jQuery(this).closest("li");if(b.length===0){return}var c=parseInt(b.find(".angellist-company-id").val(),10);if(typeof c!=="number"){return}var a=jQuery.inArray(c,angellist.company_selector.company_ids);if(a!==-1){angellist.company_selector.company_ids.splice(a,1)}if(angellist.company_selector.company_ids.length===0){angellist.company_selector.company_list.remove();delete angellist.company_selector.company_list}else{b.remove();angellist.company_selector.company_list_onchange()}},company_mouseenter:function(){jQuery(this).find("button").show()},company_mouseleave:function(){jQuery(this).find("button").hide()},company_list_onchange:function(){angellist.company_selector.company_list.find("li").each(function(a){jQuery(this).find("input").each(function(){var c=jQuery(this);var b=c.attr("name");c.attr("name",b.substring(0,18)+a+b.substring(b.indexOf("]",18)))})})},create_autocomplete_search:function(){var a=jQuery("
").attr("id","angellist-company-selector-search");var c="angellist-company-selector-searchbox";if(typeof angellist.company_selector.labels.search==="string"){a.append(jQuery("
").append(jQuery(" ").attr("for",c).text(angellist.company_selector.labels.search)))}var b=jQuery(" ").attr({id:c,type:"search",size:30,autocomplete:"on"});if(typeof angellist.company_selector.labels.search_placeholder==="string"){b.attr("placeholder",angellist.company_selector.labels.search_placeholder)}a.append(b);angellist.company_selector.post_box.find("div.inside").append(a)},enable_autocomplete_search:function(){var a=jQuery("#angellist-company-selector-search");if(a.length===0){angellist.company_selector.create_autocomplete_search();a=jQuery("#angellist-company-selector-search")}a.show();jQuery("#angellist-company-selector-searchbox").autocomplete({appendTo:a,disabled:false,focus:function(){return false},minLength:3,select:function(b,c){jQuery("#angellist-company-selector-searchbox").val("");angellist.company_selector.add_company(c.item);return false},source:function(d,b){var c=jQuery.trim(d.term);if(c===""){return}jQuery.ajax({url:angellist.company_selector.search_url,data:{action:"angellist-search",q:c},dataType:"json",success:function(e){b(jQuery.map(e,function(f){return{label:f.name,value:parseInt(f.id,10)}}))},statusCode:{404:function(){b([{label:angellist.company_selector.labels.no_results,value:""}])}}})}})},enable_company_delete:function(){jQuery("#angellist-company-selector-companies li").each(function(){var a=jQuery(this);a.append(angellist.company_selector.company_delete_button());a.mouseenter(angellist.company_selector.company_mouseenter).mouseleave(angellist.company_selector.company_mouseleave)})},enable_editable_company_list:function(){if(angellist.company_selector.company_list===undefined){angellist.company_selector.company_list=jQuery("#angellist-company-selector-companies")}if(angellist.company_selector.company_list.length===0){return}angellist.company_selector.company_list.sortable({axis:"y",containment:"parent",cursor:"move",dropOnEmpty:false,items:"li",update:angellist.company_selector.company_list_onchange})},enable:function(){if(angellist.company_selector.company_ids.length>0){angellist.company_selector.company_list=jQuery("#angellist-company-selector-companies");angellist.company_selector.enable_company_delete();if(angellist.company_selector.company_ids.length>1){angellist.company_selector.enable_editable_company_list()}}angellist.company_selector.enable_autocomplete_search()}};jQuery(function(){angellist.company_selector.post_box=jQuery("#angellist-company-selector");if(angellist.company_selector.post_box.length===0){return}else{angellist.company_selector.post_box.trigger("angellist-company-selector-onload")}angellist.company_selector.enable()});