/*
    jQuery-SelectBox
    
    Traditional select elements are very difficult to style by themselves, 
    but they are also very usable and feature rich. This plugin attempts to 
    recreate all selectbox functionality and appearance while adding 
    animation and stylability.
    
    This product includes software developed 
    by RevSystems, Inc (http://www.revsystems.com/) and its contributors
    
    Please see the accompanying LICENSE.txt for licensing information.

    acTimeout: 800, // time between each keyup for the user to create a search string
    animDuration: 200, // time to open/close dropdown in ms
    ddCtx: 'body', // body | self | any selector | a function that returns a selector (the original select is the context)
    dropupThreshold: 150, // the minimum amount of extra space required above the selectbox for it to display a dropup
    fixedWidth: false, // if false, dropdown expands to widest and display conforms to whatever is selected
    maxHeight: false, // if an integer, show scrollbars if the dropdown is too tall
    maxWidth: false, // if an integer, prevent the display/dropdown from growing past this width; longer items will be clipped
    selectboxClass: 'selectbox', // class to apply our markup
    useTie: false, // if jquery.tie is included and this is true, the selectbox will update dynamically                    
    arrowMarkup: "<div class='arrow_btn'><span class='arrow'></span></div>",                // markup appended to the display, typically for styling an arrow
    displayFormat: undefined,                // use optionFormat by default
    // formatting for the display; note that it will be wrapped with <a href='#'><span class='text'></span></a>
    optionFormat: function( ogIndex, optIndex ) {
        if($(this).size() > 0) {
            var label = $(this).attr("label");
            if(label && label.length > 0) {
                return label;
            }
            return $(this).text();
        } else {
            return "";
        }
    },                
    // the function to produce optgroup markup
    optgroupFormat: function( ogIndex ) {
        return "<span class='label'>" + $(this).attr("label") + "</span>";
    }
*/
(function(e,d,g){e.fn.borderWidth=function(){return e(this).outerWidth()-e(this).innerWidth()};e.fn.paddingWidth=function(){return e(this).innerWidth()-e(this).width()};e.fn.extraWidth=function(){return e(this).outerWidth(true)-e(this).width()};e.fn.offsetFrom=function(i){var h=e(i);return{left:e(this).offset().left-h.offset().left,top:e(this).offset().top-h.offset().top}};e.fn.maxWidth=function(){var h=0;e(this).each(function(){if(e(this).width()>h){h=e(this).width()}});return h};e.fn.triggerAll=function(h,i){return e(this).each(function(){e(this).triggerHandler(h,i)})};var c=Array.prototype.slice,a=function(){return Math.floor(Math.random()*999999999)};e.proto=function(){var i=arguments[0],h=arguments[1],j=h,l={},k;opts=e.extend({elem:"elem",access:"access",init:"init",instantAccess:false},arguments[2]);if(h._super){l[opts.init]=function(){};j=h.extend(l)}e.fn[i]=function(){var m,n=arguments;e(this).each(function(){var p=e(this),q=p.data(i),o=!q;if(o){q=new j();if(h._super){q[opts.init]=h.prototype.init}q[opts.elem]=p[0];if(q[opts.init]){q[opts.init].apply(q,opts.instantAccess?[]:c.call(n,0))}p.data(i,q)}if(!o||opts.instantAccess){if(q[opts.access]){q[opts.access].apply(q,c.call(n,0))}if(n.length>0){if(e.isFunction(q[n[0]])){m=q[n[0]].apply(q,c.call(n,1))}else{if(n.length===1){if(e.getObject){m=e.getObject(n[0],q)}else{m=q[n[0]]}}else{if(e.setObject){e.setObject(n[0],n[1],q)}else{q[n[0]]=n[1]}}}}else{if(m===k){m=p.data(i)}}}});if(m===k){return e(this)}return m}};var b=function(){return false},f=function(){var q=this,U={},m=null,C=null,u=null,v=null,l=null,ac=null,W="",G=null,V=null,i=null,T,Z,n,af,s,ad,Y,M,aa,w,ae,h,ab,S,Q,L,y,z,j,X,R,J,I,P,H,O,E,B,k,N,p,A,F,x,t,r,K,D;T=function(){u=e("<div class='sb "+U.selectboxClass+" "+m.attr("class")+"' id='sb"+a()+"'></div>").attr("role","listbox").attr("aria-has-popup","true").attr("aria-labelledby",C.attr("id")?C.attr("id"):"");e("body").append(u);var ag=m.children().size()>0?U.displayFormat.call(m.find("option:selected")[0],0,0):"&nbsp;";v=e("<div class='display "+m.attr("class")+"' id='sbd"+a()+"'></div>").append("<div class='text'>"+ag+"</div>").append(U.arrowMarkup);u.append(v);l=e("<ul class='"+U.selectboxClass+" items "+m.attr("class")+"' role='menu' id='sbdd"+a()+"'></ul>").attr("aria-hidden","true");u.append(l).attr("aria-owns",l.attr("id"));if(m.children().size()===0){l.append(Z())}else{m.children().each(function(ah){var ai,aj,ak,al;if(e(this).is("optgroup")){aj=e(this);ak=e("<li class='optgroup'>"+U.optgroupFormat.call(aj[0],ah+1)+"</li>").addClass(aj.is(":disabled")?"disabled":"").attr("aria-disabled",aj.is(":disabled")?"true":"");al=e("<ul class='items'></ul>");ak.append(al);l.append(ak);aj.children("option").each(function(){ai=Z(e(this),ah).addClass(aj.is(":disabled")?"disabled":"").attr("aria-disabled",aj.is(":disabled")?"true":"");al.append(ai)})}else{l.append(Z(e(this),ah))}})}ac=l.find("li").not(".optgroup");u.attr("aria-active-descendant",ac.filter(".selected").attr("id"));l.children(":first").addClass("first");l.children(":last").addClass("last");if(!U.fixedWidth){var o=l.find(".text, .optgroup").maxWidth()+v.extraWidth()+1;u.width(U.maxWidth?Math.min(U.maxWidth,o):o)}else{if(U.maxWidth&&u.width()>U.maxWidth){u.width(U.maxWidth)}}m.before(u).addClass("has_sb").hide().show();ae();K();l.hide();if(!m.is(":disabled")){m.bind("blur.sb",af).bind("focus.sb",n);v.mouseup(J).mouseup(S).click(b).focus(z).blur(j).hover(X,R);O().click(Q).hover(X,R);l.find(".optgroup").hover(X,R).click(b);ac.filter(".disabled").click(b);if(!e.browser.msie||e.browser.version>=9){e(d).resize(e.throttle?e.throttle(100,h):ab)}}else{u.addClass("disabled").attr("aria-disabled");v.click(function(ah){ah.preventDefault()})}u.bind("close.sb",w).bind("destroy.sb",s);m.bind("reload.sb",ad);if(e.fn.tie&&U.useTie){m.bind("domupdate.sb",Y)}};ab=function(){clearTimeout(i);i=setTimeout(h,50)};h=function(){if(u.is(".open")){ae();M(true)}};Z=function(ah,o){if(!ah){ah=e("<option value=''>&nbsp;</option>");o=0}var aj=e("<li id='sbo"+a()+"'></li>").attr("role","option").data("orig",ah[0]).data("value",ah?ah.attr("value"):"").addClass(ah.is(":selected")?"selected":"").addClass(ah.is(":disabled")?"disabled":"").attr("aria-disabled",ah.is(":disabled")?"true":""),ai=e("<div class='item'></div>"),ag=e("<div class='text'></div>").html(U.optionFormat.call(ah[0],0,o+1));return aj.append(ai.append(ag))};n=function(){t();v.triggerHandler("focus")};af=function(){if(!u.is(".open")){v.triggerHandler("blur")}};s=function(o){u.remove();m.unbind(".sb").removeClass("has_sb");e(d).unbind("resize",ab);if(!o){m.removeData("sb")}};ad=function(){var ag=u.is(".open"),o=v.is(".focused");w(true);s(true);q.init(U);if(ag){m.focus();M(true)}else{if(o){m.focus()}}};Y=function(){clearTimeout(V);V=setTimeout(ad,30)};x=function(){u.removeClass("focused");w();D()};D=function(){e(document).unbind("click",x).unbind("keyup",L).unbind("keypress",r).unbind("keydown",r).unbind("keydown",y)};A=function(){e(".sb.open."+U.selectboxClass).triggerAll("close")};t=function(){e(".sb.focused."+U.selectboxClass).not(u[0]).find(".display").blur()};F=function(){e(".sb.open."+U.selectboxClass).not(u[0]).triggerAll("close")};w=function(o){if(u.is(".open")){v.blur();ac.removeClass("hover");D();l.attr("aria-hidden","true");if(o===true){l.hide();u.removeClass("open");u.append(l)}else{l.fadeOut(U.animDuration,function(){u.removeClass("open");u.append(l)})}}};P=function(){var o=null;if(U.ddCtx==="self"){o=u}else{if(e.isFunction(U.ddCtx)){o=e(U.ddCtx.call(m[0]))}else{o=e(U.ddCtx)}}return o};H=function(){return ac.filter(".selected")};O=function(){return ac.not(".disabled")};aa=function(){l.scrollTop(l.scrollTop()+H().offsetFrom(l).top-l.height()/2+H().outerHeight(true)/2)};K=function(){if(e.browser.msie&&e.browser.version<8){e("."+U.selectboxClass+" .display").hide().show()}};M=function(ag){var o,ah=P();t();u.addClass("open");ah.append(l);o=ae();l.attr("aria-hidden","false");if(ag===true){l.show();aa()}else{if(o==="down"){l.slideDown(U.animDuration,aa)}else{l.fadeIn(U.animDuration,aa)}}m.focus()};ae=function(){var aj=P(),aq=0,ah=v.offsetFrom(aj).left,ag=0,ak="",an,o,ap,ao,ai,ar,am,al;l.removeClass("above");l.show().css({maxHeight:"none",position:"relative",visibility:"hidden"});if(!U.fixedWidth){l.width(v.outerWidth()-l.extraWidth()+1)}ap=e(d).scrollTop()+e(d).height()-v.offset().top-v.outerHeight();ao=v.offset().top-e(d).scrollTop();ai=v.offsetFrom(aj).top+v.outerHeight();ar=ap-ao+U.dropupThreshold;if(l.outerHeight()<ap){aq=U.maxHeight?U.maxHeight:ap;ag=ai;ak="down"}else{if(l.outerHeight()<ao){aq=U.maxHeight?U.maxHeight:ao;ag=v.offsetFrom(aj).top-Math.min(aq,l.outerHeight());ak="up"}else{if(ar>=0){aq=U.maxHeight?U.maxHeight:ap;ag=ai;ak="down"}else{if(ar<0){aq=U.maxHeight?U.maxHeight:ao;ag=v.offsetFrom(aj).top-Math.min(aq,l.outerHeight());ak="up"}else{aq=U.maxHeight?U.maxHeight:"none";ag=ai;ak="down"}}}}an=(""+e("body").css("margin-left")).match(/^\d+/)?e("body").css("margin-left"):0;o=(""+e("body").css("margin-top")).match(/^\d+/)?e("body").css("margin-top"):0;am=e().jquery>="1.4.2"?parseInt(an):e("body").offset().left;al=e().jquery>="1.4.2"?parseInt(o):e("body").offset().top;l.hide().css({left:ah+(aj.is("body")?am:0),maxHeight:aq,position:"absolute",top:ag+(aj.is("body")?al:0),visibility:"visible"});if(ak==="up"){l.addClass("above")}return ak};S=function(o){if(u.is(".open")){w()}else{M()}return false};E=function(){var ag=e(this),o=m.val(),ah=ag.data("value");if(U.useTie&&e.fn.tie){m.find("option").old_removeAttr("selected");e(ag.data("orig")).old_attr("selected","selected")}else{m.find("option").removeAttr("selected");e(ag.data("orig")).attr("selected","selected")}O().removeClass("selected");ag.addClass("selected");u.attr("aria-active-descendant",ag.attr("id"));v.find(".text").attr("title",ag.find(".text").html());v.find(".text").html(U.displayFormat.call(ag.data("orig")));if(o!==ah){m.change()}};Q=function(o){x();m.focus();E.call(this);return false};B=function(){W=""};k=function(aj){var ai,ah,o,ag=O();for(ai=0;ai<ag.size();ai++){o=ag.eq(ai).find(".text");ah=o.children().size()==0?o.text():o.find("*").text();if(aj.length>0&&ah.toLowerCase().match("^"+aj.toLowerCase())){return ag.eq(ai)}}return null};N=function(ag){var o=k(ag);if(o!==null){E.call(o[0]);return true}return false};r=function(o){if(o.ctrlKey||o.altKey){return}if(o.which===38||o.which===40||o.which===8||o.which===32){o.preventDefault()}};p=function(aj){var ai,ah,o=H(),ag=O();for(ai=ag.index(o)+1;ai<ag.size();ai++){ah=ag.eq(ai).find(".text").text();if(ah!==""&&ah.substring(0,1).toLowerCase()===aj.toLowerCase()){E.call(ag.eq(ai)[0]);return true}}return false};y=function(ah){if(ah.altKey||ah.ctrlKey){return false}var ag=H(),o=O();switch(ah.which){case 9:w();j();break;case 35:if(ag.size()>0){ah.preventDefault();E.call(o.filter(":last")[0]);aa()}break;case 36:if(ag.size()>0){ah.preventDefault();E.call(o.filter(":first")[0]);aa()}break;case 38:if(ag.size()>0){if(o.filter(":first")[0]!==ag[0]){ah.preventDefault();E.call(o.eq(o.index(ag)-1)[0])}aa()}break;case 40:if(ag.size()>0){if(o.filter(":last")[0]!==ag[0]){ah.preventDefault();E.call(o.eq(o.index(ag)+1)[0]);aa()}}else{if(ac.size()>1){ah.preventDefault();E.call(ac.eq(0)[0])}}break;default:break}};L=function(o){if(o.altKey||o.ctrlKey){return false}if(o.which!==38&&o.which!==40){W+=String.fromCharCode(o.keyCode);if(N(W)){clearTimeout(G);G=setTimeout(B,U.acTimeout)}else{if(p(String.fromCharCode(o.keyCode))){aa();clearTimeout(G);G=setTimeout(B,U.acTimeout)}else{B();clearTimeout(G)}}}};z=function(){F();u.addClass("focused");e(document).click(x).keyup(L).keypress(r).keydown(r).keydown(y)};j=function(){u.removeClass("focused");v.removeClass("active");e(document).unbind("keyup",L).unbind("keydown",r).unbind("keypress",r).unbind("keydown",y)};X=function(){e(this).addClass("hover")};R=function(){e(this).removeClass("hover")};J=function(){v.addClass("active");e(document).bind("mouseup",I)};I=function(){v.removeClass("active");e(document).unbind("mouseup",I)};this.init=function(o){if(e.browser.msie&&e.browser.version<7){return}m=e(this.elem);if(m.attr("id")){C=e("label[for='"+m.attr("id")+"']:first")}if(!C||C.size()===0){C=m.closest("label")}if(m.hasClass("has_sb")){return}U=e.extend({acTimeout:800,animDuration:200,ddCtx:"body",dropupThreshold:150,fixedWidth:false,maxHeight:false,maxWidth:false,selectboxClass:"selectbox",useTie:false,arrowMarkup:"<div class='arrow_btn'><span class='arrow'></span></div>",displayFormat:g,optionFormat:function(ag,ai){if(e(this).size()>0){var ah=e(this).attr("label");if(ah&&ah.length>0){return ah}return e(this).text()}else{return""}},optgroupFormat:function(ag){return"<span class='label'>"+e(this).attr("label")+"</span>"}},o);U.displayFormat=U.displayFormat||U.optionFormat;T()};this.open=M;this.close=w;this.refresh=ad;this.destroy=s;this.options=function(o){U=e.extend(U,o);ad()}};e.proto("sb",f)}(jQuery,window));
