/**
 *  $Id: selectbox.js 138 2006-12-05 18:35:36Z wingedfox $
 *  $HeadURL: https://svn.debugger.ru/repos/jslibs/BrowserExtensions/tags/BrowserExtensions.001/dom/selectbox.js $
 *
 *  Extends the selectbox interface
 *
 *  @author Ilya Lebedev <ilya@lebedev.net>
 *  @modified $Date: 2006-12-05 21:35:36 +0300 (Втр, 05 Дек 2006) $
 *  @version $Rev: 138 $
 *  @title Selectbox
 *  @license LGPL 2.1 or later
 */
Selectbox=function(I){var l=this;var o;var O=[];var Q={'all':[[]],'added':[[]],'deleted':[[]]};var _=function(){if((o=document.getElementById(I))&&o.tagName.toLowerCase()!='select')throw new Error('Node with supplied id="'+I+'" is not the <select> box.');if(!o){o=document.createElement('select');o.id='selectbox'+(new Date).valueOf();}else{l.addOptionsList(o.options,false,false);}o.onblur=e};var c=function(){return String(this.text);};var C=function(i,x,X,z,Z){var w=0;if(!isRegExp(i)){i=RegExp.escape(i);if(!isString(Z))Z='start';switch(Z.toLowerCase()){case"any":i=new RegExp(i,"i");break;case"end":i=new RegExp(i+"$","i");break;case"exact":i=new RegExp("^"+i+"$","i");break;case"start":case"default":i=new RegExp("^"+i,"i");break}}for(var W=0,s=O.length;W<s;W++){if(z&&O[W][x]==X)continue;if(i.test(O[W].text)){w++;O[W][x]=X}else{if(O[W][x]!=X||!z){O[W][x]=!X}}}if(w)l.updateOptions();return w};var e=function(){for(var i=0,x=this.options.length;i<x;i++){if(!isUndefined(this.options[i].__idx)&&O[this.options[i].__idx]){O[this.options[i].__idx].selected=this.options[i].selected;O[this.options[i].__idx].defaultSelected=this.options[i].defaultSelected}}};var v=function(i,x,X){if(!Q[i])return false;var z=Q[i][Q[i].length-1];z[z.length]={'text':x,'value':X};return true};var V=function(){for(var i in Q){if(!Q.hasOwnProperty(i)||0==Q[i][Q[i].length-1].length)continue;Q[i][Q[i].length]=[]}};this.hasOptions=function(){return O.length};this.updateOptions=function(){var i=0;for(var x=0,X=O.length;x<X;x++){v('all',O[x].text,O[x].value);if(O[x].visible){o.options[i].text=O[x].text;o.options[i].value=O[x].value;o.options[i].defaultSelected=O[x].defaultSelected;o.options[i].selected=O[x].selected;o.options[i].__idx=x;i++}}for(var x=o.options.length;x>=i;x--)o.options[x]=null;V();};this.selectAllOptions=function(){for(var i=0;i<O.length;i++){if(O[i].visible){O[i].selected=true}}this.updateOptions();};this.selectMatchingOptions=function(i,x){return C(i,'selected',true,true,x);};this.selectOnlyMatchingOptions=function(i,x){return C(i,'selected',true,false,x);};this.unselectAllOptions=function(){for(var i=0;i<O.length;i++){if(O[i].visible){O[i].selected=false}}o.selectedIndex=-1;this.updateOptions();};this.unselectMatchingOptions=function(i,x){return C(i,'selected',false,true,x);};this.unselectOnlyMatchingOptions=function(i,x){return C(i,'selected',false,false,x);};this.fixMatchingOptions=function(i,x){return C(i,'fixed',true,true,x);};this.fixOnlyMatchingOptions=function(i,x){return C(i,'fixed',true,false,x);};this.unfixMatchingOptions=function(i,x){return C(i,'fixed',false,true,x);};this.unfixOnlyMatchingOptions=function(i,x){return C(i,'fixed',false,false,x);};this.showMatchingOptions=function(i,x){return C(i,'visible',true,true,x);};this.showOnlyMatchingOptions=function(i,x){return C(i,'visible',true,false,x);};this.hideMatchingOptions=function(i,x){return C(i,'visible',false,true,x);};this.hideOnlyMatchingOptions=function(i,x){return C(i,'visible',false,false,x);};this.sort=function(){O=O.sort();this.updateOptions();};this.getValue=function(){return o.value};this.getOption=function(I,i){if(!isNumeric(I)||I<0||!o.options[I])return"";return isString(i)?o.options[I][i]:[text.value,o.options[I].value]};this.getSelectedOptions=function(){var i=[];for(var x=0,X=o.options.length;x<X;x++){if(o.options[x].selected){i[i.length]=[o.options[x].text,o.options[x].value]}}return i};this.getOptionsLength=function(){return o.options.length};this.getSelectedIndex=function(){return o.selectedIndex};this.getId=function(){return I};this.getEl=function(){return o};this.getOptionsNames=function(i){if(i)i=new RegExp("^("+RegExp.escape(i)+")$","i");var x=[];for(var X=0,z=O.length;X<z;X++){if(isUndefined(i)||(isRegExp(i)&&i.test(O[X].value))){x[x.length]=O[X].text}}return x};this.getOptionsTrack=function(i,x,X,z){if(!Q[i])return[];if(!z||!Q[i][z])z=Q[i].length-2;if(!Q[i][z])z=Q[i].length-1;if(!Q[i][z])z=0;var Z=[];for(var w=0,W=Q[i][z].length;w<W;w++){Z[Z.length]=x?Q[i][z][w][x]:Q[i][z][w]}return X?Z.join(X):Z};this.removeMatchingOptions=function(i,x){var X=l.getSelectedOptions().flatten([0]);l.selectOnlyMatchingOptions(i,x);l.removeSelectedOptions();if(X.length>0)l.selectOnlyMatchingOptions(X,'exact');};this.removeSelectedOptions=function(){var i=[],x;for(var X=(O.length-1);X>=0;X--){if(O[X].selected&&(x=l.removeOption(X,false)))i[i.length]=x}if(i)this.updateOptions();o.selectedIndex=-1;return i};this.removeAllOptions=function(){var i=[],x;for(var X=(o.options.length-1);X>=0;X--){if(x=l.removeOption(X,false))i[i.length]=x}o.selectedIndex=-1;if(i)l.updateOptions();return i};this.removeOption=function(i,x){if(!O[i])return false;v('deleted',O[i].text,O[i].value);var X=O[i];O.splice(i,1);if(false!==x)this.updateOptions();return X};this.addOption=function(i,x,X,z,Z,w){if(!isScalar(i)||!isScalar(x))return false;if(isUndefined(w))w=true;O[O.length]={'text':i,'value':x,'defaultSelected':X,'selected':z,'visible':true,'fixed':false};if(w)o.options[o.options.length]=new Option(i,x,X,z);O[O.length-1].toString=c;v('added',O[O.length-1].text,O[O.length-1].value);if(Z)this.sort();return true};this.addOptionsList=function(i,x,X){if(isUndefined(i)||!isNumeric(i.length)||i.length<1)return false;if(isUndefined(X))X=true;for(var z=0,Z=i.length;z<Z;z++){var w=i[z];if(w.text)l.addOption(w.text,w.value?w.value:w.text,Boolean(w.selected),Boolean(w.defaultSelected),false,X);else if(isString(w))l.addOption(w,w,false,false,false,X);else if(w[0])l.addOption(w[0],w[1]?w[1]:w[0],Boolean(w[2]),Boolean(w[3]),false,X);}if(x)this.sort();return true};this.copySelectedOptions=function(i,x,X){if(!i||!i.addOption)return false;if(x){l.unselectMatchingOptions(x);}for(var z=0,Z=O.length;z<Z;z++){if(O[z].selected&&O[z].visible){i.addOption(O[z].text,O[z].value,O[z].selected,O[z].defaultSelected);}}if(X){i.sort();}else{i.updateOptions();}return true};this.copyAllOptions=function(i,x,X){if(!i||!i.addOption)return false;l.selectAllOptions();l.copySelectedOptions(i,x,X);};this.moveSelectedOptions=function(i,x,X){if(!i||!i.addOption)return false;if(x){l.unselectMatchingOptions(x);}i.unselectAllOptions();var z=l.removeSelectedOptions(false);for(var Z=z.length-1;Z>=0;Z--){i.addOption(z[Z].text,z[Z].value,z[Z].defaultSelected,z[Z].selected);}if(X){i.sort();l.sort();}else{i.updateOptions();l.updateOptions();}return true};this.moveAllOptions=function(i,x,X){if(!i||!i.addOption)return false;l.selectAllOptions();l.moveSelectedOptions(i,x,X);};this.swapOptions=function(i,x){if(!O[i]||O[x])return false;var X=O[i];O[i]=O[x];O[x]=X;this.updateOptions();};this.moveSelectedOptionsUp=function(){var x=false;for(i=0,oL=O.length;i<oL;i++){if(O[i].selected&&O[i].visible){if(i!=0&&!O[i-1].selected&&!O[i-1].fixed){this.swapOptions(i,i-1);obj.options[i-1].selected=true}}}};this.moveSelectedOptionsUp=function(){var x=false;for(oL=i=O.length-1;i>=0;i--){if(O[i].selected&&O[i].visible){if(i!=oL&&!O[i+1].selected&&!O[i+1].fixed){this.swapOptions(i,i+1);obj.options[i+1].selected=true}}}};this.selectOption=function(I,i){if(!isNumber(I)||(!i&&(I<0||!o.options[Math.abs(I)])))return false;l.unselectAllOptions();if(I>=o.options.length){I=o.options.length-1}else if(I<0){I=0}o.options[I].selected=true;o.options[I].fireEvent('onmouseup');O[o.options[I].__idx].selected=true;o.selectedIndex=I;return true};this.selectPrev=function(x){var X=false,z=false;for(i=o.options.length-1;i>=0;i--){if(z||o.options[i].selected){var Z=o.options[i].selected;o.options[i].selected=z;O[o.options[i].__idx].selected=z;z=Z}}if(z){if(x){for(i=o.options.length-1;i>=0;i--){o.options[0].selected=false;o.options[i].selected=true;O[o.options[0].__idx].selected=false;O[o.options[i].__idx].selected=true;break}}else{o.options[0].selected=true;O[o.options[0].__idx].selected=true}}};this.selectNext=function(x){var X=false,z=false;for(i=0,oL=o.options.length;i<oL;i++){if(z||o.options[i].selected){var Z=o.options[i].selected;o.options[i].selected=z;O[o.options[i].__idx].selected=z;z=Z}}if(z){if(x){for(i=0,oL=o.options.length;i<oL;i++){O[o.options[oL-1].__idx].selected=false;O[o.options[i].__idx].selected=true;o.options[oL-1].selected=false;o.options[i].selected=true;break}}else{o.options[oL-1].selected=true;O[o.options[oL-1].__idx].selected=true}}};_();};
