(function($){'use strict';function findForm(node){if(node.tagName.toLowerCase()!=='form'){return findForm(node.parentNode);}
return node;}
window.SelectFilter={init:function(field_id,field_name,is_stacked){if(field_id.match(/__prefix__/)){return;}
var from_box=document.getElementById(field_id);from_box.id+='_from';from_box.className='filtered';var ps=from_box.parentNode.getElementsByTagName('p');for(var i=0;i<ps.length;i++){if(ps[i].className.indexOf("info")!==-1){from_box.parentNode.removeChild(ps[i]);}else if(ps[i].className.indexOf("help")!==-1){from_box.parentNode.insertBefore(ps[i],from_box.parentNode.firstChild);}}
var selector_div=quickElement('div',from_box.parentNode);selector_div.className=is_stacked?'selector stacked':'selector';var selector_available=quickElement('div',selector_div);selector_available.className='selector-available';var title_available=quickElement('h2',selector_available,interpolate(gettext('Available %s')+' ',[field_name]));quickElement('span',title_available,'','class','help help-tooltip help-icon','title',interpolate(gettext('This is the list of available %s. You may choose some by '+
'selecting them in the box below and then clicking the '+
'"Choose" arrow between the two boxes.'),[field_name]));var filter_p=quickElement('p',selector_available,'','id',field_id+'_filter');filter_p.className='selector-filter';var search_filter_label=quickElement('label',filter_p,'','for',field_id+'_input');quickElement('span',search_filter_label,'','class','help-tooltip search-label-icon','title',interpolate(gettext("Type into this box to filter down the list of available %s."),[field_name]));filter_p.appendChild(document.createTextNode(' '));var filter_input=quickElement('input',filter_p,'','type','text','placeholder',gettext("Filter"));filter_input.id=field_id+'_input';selector_available.appendChild(from_box);var choose_all=quickElement('a',selector_available,gettext('Choose all'),'title',interpolate(gettext('Click to choose all %s at once.'),[field_name]),'href','#','id',field_id+'_add_all_link');choose_all.className='selector-chooseall';var selector_chooser=quickElement('ul',selector_div);selector_chooser.className='selector-chooser';var add_link=quickElement('a',quickElement('li',selector_chooser),gettext('Choose'),'title',gettext('Choose'),'href','#','id',field_id+'_add_link');add_link.className='selector-add';var remove_link=quickElement('a',quickElement('li',selector_chooser),gettext('Remove'),'title',gettext('Remove'),'href','#','id',field_id+'_remove_link');remove_link.className='selector-remove';var selector_chosen=quickElement('div',selector_div);selector_chosen.className='selector-chosen';var title_chosen=quickElement('h2',selector_chosen,interpolate(gettext('Chosen %s')+' ',[field_name]));quickElement('span',title_chosen,'','class','help help-tooltip help-icon','title',interpolate(gettext('This is the list of chosen %s. You may remove some by '+
'selecting them in the box below and then clicking the '+
'"Remove" arrow between the two boxes.'),[field_name]));var to_box=quickElement('select',selector_chosen,'','id',field_id+'_to','multiple','multiple','size',from_box.size,'name',from_box.getAttribute('name'));to_box.className='filtered';var clear_all=quickElement('a',selector_chosen,gettext('Remove all'),'title',interpolate(gettext('Click to remove all chosen %s at once.'),[field_name]),'href','#','id',field_id+'_remove_all_link');clear_all.className='selector-clearall';from_box.setAttribute('name',from_box.getAttribute('name')+'_old');var move_selection=function(e,elem,move_func,from,to){if(elem.className.indexOf('active')!==-1){move_func(from,to);SelectFilter.refresh_icons(field_id);}
e.preventDefault();};addEvent(choose_all,'click',function(e){move_selection(e,this,SelectBox.move_all,field_id+'_from',field_id+'_to');});addEvent(add_link,'click',function(e){move_selection(e,this,SelectBox.move,field_id+'_from',field_id+'_to');});addEvent(remove_link,'click',function(e){move_selection(e,this,SelectBox.move,field_id+'_to',field_id+'_from');});addEvent(clear_all,'click',function(e){move_selection(e,this,SelectBox.move_all,field_id+'_to',field_id+'_from');});addEvent(filter_input,'keypress',function(e){SelectFilter.filter_key_press(e,field_id);});addEvent(filter_input,'keyup',function(e){SelectFilter.filter_key_up(e,field_id);});addEvent(filter_input,'keydown',function(e){SelectFilter.filter_key_down(e,field_id);});addEvent(selector_div,'change',function(e){if(e.target.tagName==='SELECT'){SelectFilter.refresh_icons(field_id);}});addEvent(selector_div,'dblclick',function(e){if(e.target.tagName==='OPTION'){if(e.target.closest('select').id===field_id+'_to'){SelectBox.move(field_id+'_to',field_id+'_from');}else{SelectBox.move(field_id+'_from',field_id+'_to');}
SelectFilter.refresh_icons(field_id);}});addEvent(findForm(from_box),'submit',function(){SelectBox.select_all(field_id+'_to');});SelectBox.init(field_id+'_from');SelectBox.init(field_id+'_to');SelectBox.move(field_id+'_from',field_id+'_to');if(!is_stacked){var j_from_box=$(from_box);var j_to_box=$(to_box);var resize_filters=function(){j_to_box.height($(filter_p).outerHeight()+j_from_box.outerHeight());};if(j_from_box.outerHeight()>0){resize_filters();}else{j_to_box.closest('fieldset').one('show.fieldset',resize_filters);}}
SelectFilter.refresh_icons(field_id);},any_selected:function(field){var any_selected=false;try{field.attr('required','required');any_selected=field.is(':valid');field.removeAttr('required');}catch(e){any_selected=field.find('option:selected').length>0;}
return any_selected;},refresh_icons:function(field_id){var from=$('#'+field_id+'_from');var to=$('#'+field_id+'_to');$('#'+field_id+'_add_link').toggleClass('active',SelectFilter.any_selected(from));$('#'+field_id+'_remove_link').toggleClass('active',SelectFilter.any_selected(to));$('#'+field_id+'_add_all_link').toggleClass('active',from.find('option').length>0);$('#'+field_id+'_remove_all_link').toggleClass('active',to.find('option').length>0);},filter_key_press:function(event,field_id){var from=document.getElementById(field_id+'_from');if((event.which&&event.which===13)||(event.keyCode&&event.keyCode===13)){from.selectedIndex=0;SelectBox.move(field_id+'_from',field_id+'_to');from.selectedIndex=0;event.preventDefault();return false;}},filter_key_up:function(event,field_id){var from=document.getElementById(field_id+'_from');var temp=from.selectedIndex;SelectBox.filter(field_id+'_from',document.getElementById(field_id+'_input').value);from.selectedIndex=temp;return true;},filter_key_down:function(event,field_id){var from=document.getElementById(field_id+'_from');if((event.which&&event.which===39)||(event.keyCode&&event.keyCode===39)){var old_index=from.selectedIndex;SelectBox.move(field_id+'_from',field_id+'_to');from.selectedIndex=(old_index===from.length)?from.length-1:old_index;return false;}
if((event.which&&event.which===40)||(event.keyCode&&event.keyCode===40)){from.selectedIndex=(from.length===from.selectedIndex+1)?0:from.selectedIndex+1;}
if((event.which&&event.which===38)||(event.keyCode&&event.keyCode===38)){from.selectedIndex=(from.selectedIndex===0)?from.length-1:from.selectedIndex-1;}
return true;}};addEvent(window,'load',function(e){$('select.selectfilter, select.selectfilterstacked').each(function(){var $el=$(this),data=$el.data();SelectFilter.init($el.attr('id'),data.fieldName,parseInt(data.isStacked,10));});});})(django.jQuery);