/*
 * jQuery Multiple Select Box Plugin 0.5.3
 * 
 * http://plugins.jquery.com/project/jquerymultipleselectbox
 * http://code.google.com/p/jquerymultipleselectbox/
 * 
 * Apache License 2.0
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * @author Dreamltf
 * @date 2011/06/12
 * 
 * Depends: jquery.js
 */
(function(h){var e="MultipleSelectBox";var d="disabled";var c="selected";var g="selecting";var b={maxLimit:-1,scrollSpeed:20,valueRendererArray:null,isMouseEventEnabled:true,isKeyEventEnabled:true,onSelectStart:null,onSelectEnd:null,onSelectChange:null};h.extend(h.fn,{multipleSelectBox:function(i){i=h.extend({},b,i);return this.each(function(){var j=h(this);j.addClass(e).data("options",i);j.css("MozUserSelect","none").bind("selectstart",function(){return false;});j.destroyMultipleSelectBox().recalculateMultipleSelectBox();if(i.onSelectStart){j.bind("onSelectStart",i.onSelectStart);}if(i.onSelectEnd){j.bind("onSelectEnd",i.onSelectEnd);}if(i.onSelectChange){j.bind("onSelectChange",i.onSelectChange);}f(j,i);});},getMultipleSelectBoxCachedRows:function(j,i){return this.pushStack(h.map(this,function(k){var m=h(k);var l=m.data("rows");if(j||!l){l=m.children("li");m.data("rows",l);}if(i){l=l.filter(i);}return l.get();}));},getMultipleSelectBoxRowIndex:function(){return this.data("index");},getMultipleSelectBoxOptions:function(){return this.data("options");},drawMultipleSelectBox:function(k,i,j){j=h.extend({isGetPositionByCache:false,isSelectionOpposite:false,isSelectionRetained:false,isScrollBarFrozen:false,scrollIndex:-1},j);return this.each(function(){var u=h(this);var t=u.getMultipleSelectBoxCachedRows();var v=u.getMultipleSelectBoxOptions();if(!j.isGetPositionByCache){u.recalculateMultipleSelectBox(true,true);}var l=u.data("info");if(k<0||i<0||v.maxLimit==0||t.eq(k).hasClass(d)){return this;}var p=Math.min(k,i);var n=Math.max(k,i);var r=[];var o=[];var m=0;t.each(function(w){var y=h(this);y.removeClass(g);if(!y.hasClass(d)){var x=y.hasClass(c);if(p<=w&&w<=n){if(x){if(j.isSelectionOpposite){r.push(y);}else{m++;}}else{o.push(y);}}else{if(x){if(j.isSelectionRetained){m++;}else{r.push(y);}}}}});var q=o.length;if(v.maxLimit>0&&(q+m)>v.maxLimit){return this;}t.eq(i).addClass(g);for(var s in r){r[s].removeClass(c);}for(var s in o){o[s].addClass(c);}if(!j.isScrollBarFrozen){if(j.scrollIndex>=0){u.scrollTop(l.rowInfoArray[j.scrollIndex].topPos);}else{if(k>i){u.scrollTop(l.rowInfoArray[i].topPos);}else{if(k<i){u.scrollTop(l.rowInfoArray[i].bottomPos-l.height);}}}}l.lastStartIndex=k;l.lastCurrentIndex=i;return this;});},serializeMultipleSelectBoxArray:function(){var i=this.getMultipleSelectBoxOptions();return h.map(this.getMultipleSelectBoxCachedRows(),function(m,j){var l=h(m);var k=null;if(!l.hasClass(d)&&l.hasClass(c)&&(i.valueRendererArray==null||(k=i.valueRendererArray[j])==null)){k=l.text();}return k;});},yieldMultipleSelectBox:function(){h(document).unbind("mouseleave."+e+" mousemove."+e);return this.unbind("mouseenter mouseleave mouseover");},destroyMultipleSelectBox:function(){return this.yieldMultipleSelectBox().each(function(){var j=h(this);j.unbind("mousedown onSelectStart onSelectEnd onSelectChange");var i=j.data("rows");if(i){i.removeData("index");}j.removeData("info").removeData("rows");});},recalculateMultipleSelectBox:function(k,i,l,j){return this.each(function(){var q=h(this);var n=q.getMultipleSelectBoxCachedRows(j);var m=q.data("info");if(!m){k=true;i=true;l=true;m={};q.data("info",m);}if(i){var p=[];var o=-1;n.each(function(r){var t=h(this);var s=t.offset().top;if(r==0){o=s;}s-=o;t.data("index",r);p.push({topPos:s,bottomPos:s+t.outerHeight()});});m.rowInfoArray=p;}if(k){m.topPos=q.offset().top;m.bottomPos=m.topPos+q.outerHeight();m.height=q.innerHeight();m.scrollHeight=this.scrollHeight;}if(l){m.lastStartIndex=-1;m.lastCurrentIndex=-1;m.prevStartIndex=-1;m.prevCurrentIndex=-1;}});}});function a(i){return h("ul."+e).yieldMultipleSelectBox().each(function(){var l=h(this);var k=l.getMultipleSelectBoxOptions();var j=l.data("info");if(l.hasClass(g)&&(k.onSelectEnd||k.onSelectChange)){var m=[i,l.serializeMultipleSelectBoxArray(),j.lastStartIndex,j.lastCurrentIndex,j.prevStartIndex,j.prevCurrentIndex];if(k.onSelectEnd){k.onSelectEnd.apply(l[0],m);}if(k.onSelectChange&&(m[2]!=m[4]||m[3]!=m[5])){k.onSelectChange.apply(l[0],m);}}l.removeClass(g);j.prevStartIndex=j.lastStartIndex;j.prevCurrentIndex=j.lastCurrentIndex;});}function f(m,j){var i=m.getMultipleSelectBoxCachedRows();var k=i.length;var l=h(document);if(j.isMouseEventEnabled){m.bind("mousedown",function(t){var o=h(t.target);if(this==o[0]){return true;}else{if(this!=o.parent()[0]){o=o.parents("ul."+e+">li").eq(0);}}var u=o.getMultipleSelectBoxRowIndex();if(j.onSelectStart){var s=j.onSelectStart.apply(m[0],[t,u]);if(typeof(s)=="boolean"&&!s){return false;}}m.recalculateMultipleSelectBox(true,true);var q=m.data("info");var p=u;var n=false;var r=false;if(j.isKeyEventEnabled){if(t.shiftKey){u=q.lastStartIndex;}else{if(t.ctrlKey){n=true;r=true;}}}m.addClass(g);m.drawMultipleSelectBox(u,p,{isGetPositionByCache:true,isSelectionOpposite:n,isSelectionRetained:r,isScrollBarFrozen:true});m.yieldMultipleSelectBox().bind("mouseenter",function(){l.unbind("mousemove."+e);}).bind("mouseleave",function(){var v=-1;var w=0;l.bind("mousemove."+e,function(C){var y=C.pageY;var x=-1;if(j.scrollSpeed<=0){}else{if(y<q.topPos){if(p>0&&(v<0||y<v)){w+=(q.topPos-y)/5;var A=q.rowInfoArray[p].topPos-(j.scrollSpeed/20*w);if(A>0){for(var z=p-1;z>=0;z--){if(A>=q.rowInfoArray[z].topPos){break;}p=z;w=0;}}else{p=0;}if(u<=p){x=p;}m.drawMultipleSelectBox(u,p,{isGetPositionByCache:true,isSelectionRetained:r,scrollIndex:x});}}else{if(y>q.bottomPos){if(p<k-1&&(v<0||y>v)){w+=(y-q.bottomPos)/5;var A=q.rowInfoArray[p].bottomPos+(j.scrollSpeed/20*w);if(A<q.scrollHeight){for(var z=p+1;z<k;z++){if(A<q.rowInfoArray[z].bottomPos){break;}p=z;w=0;}}else{p=k-1;}if(u>=p){var B=q.rowInfoArray[p].bottomPos-q.height;for(var z=p-1;z>=0;z--){if(B>=q.rowInfoArray[z].topPos){x=z;break;}}}m.drawMultipleSelectBox(u,p,{isGetPositionByCache:true,isSelectionRetained:r,scrollIndex:x});}}}}v=y;});}).bind("mouseover",function(w){var v=h(w.target);if(this==v.parent()[0]){p=v.getMultipleSelectBoxRowIndex();m.drawMultipleSelectBox(u,p,{isGetPositionByCache:true,isSelectionRetained:r,isScrollBarFrozen:true});}});if(h.browser.msie){l.bind("mouseleave."+e,function(){l.one("mousemove."+e,function(v){if(!v.button){a(v);}});});}return false;});}return m;}h(document).bind("mouseup."+e,function(i){a(i);});})(jQuery);
