/*
 * Linkselect jQuery Plug-in
 *
 * Copyright 2008 Giva, Inc. (http://www.givainc.com/labs/) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Date: 2009-07-30
 * Rev:  1.2.08
 */
(function(A){A.linkselect={version:"1.2.08"};A.fn.linkselect=function(E){var F=typeof arguments[0]=="string"&&arguments[0];var D=F&&Array.prototype.slice.call(arguments,1)||arguments;if(F&&this.length){var C=A.data(this[0],"linkselect");if(F.toLowerCase()=="object"){return C}else{if(C[F]){var B;this.each(function(G){var H=A.data(this,"linkselect")[F].apply(C,D);if(G==0&&H){if(!!H.jquery){B=A([]).add(H)}else{B=H;return false}}else{if(!!H&&!!H.jquery){B=B.add(H)}}});return B||this}else{return this}}}else{return this.each(function(){new A.LinkSelect(this,E)})}};A.LinkSelect=function(k,R){R=A.extend({},A.LinkSelect.defaults,R);var J=this,f=k,l=A(k),W={},Y=false,L=0,X,F=false;this.id=l.attr("id");this.val=function(p,o){if(arguments.length>0){h(p,o);return K}else{return Z.val()}};this.focus=function(){setTimeout(function(){K.focus()},1);return K};this.blur=function(){setTimeout(function(){K.blur()},1);return K};this.open=function(p,o){if(Y){return K}A(document).triggerHandler("click.linkselect");if(o!==false){K.trigger("focus")}setTimeout(function(){g(p)},1);return K};this.disable=function(o){Y=o;K.parent().find("span."+R.classDisabled).remove();K[Y?"hide":"show"]();if(Y){K.after('<span class="'+R.classDisabled+'">'+K.html()+"</span>")}return K};this.replaceOptions=function(o,p){l.children("option").remove();A.each(o,function(q){var r=A("<option/>").attr("value",this.value).html(this.text);if(this.selected==true){r.attr("selected","selected")}if(this.className){r.addClass(this.className)}r.appendTo(l)});O();C();Q().trigger("click.linkselect",[true,p])};var a=m();l.after(a).remove();var Z=a.filter("input");var K=a.filter("a");var H=a.filter("div");var P=a.find(".scrollable");var I=a.find(".title");var S=H.find("ul");var U;Z.addClass(l.attr("className"));A.data(Z[0],"linkselect",this);H.appendTo("body").bind("mousemove.linkselect",function(o){U=o});function C(){S.find("li").bind("mouseover.linkselect",function(o){if(U&&U.type=="keydown"){return }d(A(this));U=o}).bind("click.linkselect",function(t,p,s){t.preventDefault();var o=Q().removeClass(R.classSelected);var q=A(this).addClass(R.classSelected);var r=q.attr("rel")||"";var u=q.find("."+R.classValue).html();G(p);if((s!==false)&&((A.isFunction(R.change)&&(R.change.apply(J,[this,r,u,s])===false))||(A.isFunction(l[0].onchange)&&(l[0].onchange.apply(J,[this,r,u,s])===false)))){o.addClass(R.classSelected);q.removeClass(R.classSelected);return }Z.val(r);K.html(u)[(p!==true)?"trigger":"triggerHandler"]("focus",[p]);if(Y){K.parent().find("span."+R.classDisabled).html(u)}})}C();K.bind("click.linkselect",function(o){o.preventDefault();e();if(A.browser.msie){setTimeout(function(){K.trigger("focus.linkselect")},0)}}).bind("focus.linkselect",function(p,o){if(!H.is(":visible")&&(o!==true)){K.addClass(R.classLinkFocus)}}).bind("blur.linkselect",function(o){if(n(o)){G()}K.removeClass(R.classLinkFocus)}).bind((A.browser.safari?"keydown":"keypress")+".linkselect",function(t,s){if(!!s){var t=s}var p=t.keyCode||t.charCode,r=String.fromCharCode(p).toLowerCase();switch(p){case 38:case 40:t.preventDefault();b((p==38)?-1:1);U=t;break;case 13:t.preventDefault();if(H.is(":visible")){H.find("li."+R.classCurrent).trigger("click.linkselect")}else{K.trigger("click.linkselect")}break;case 9:case 27:G();break;case 35:t.preventDefault();M();U=t;break;case 36:t.preventDefault();V();U=t;break;case 33:case 34:t.preventDefault();var o=H.is(":visible");if(!o){H.show()}var q=parseInt(P.height()/S.find("li:first").outerHeight(),10);if(!o){H.hide()}b((p==33)?q*-1:q);break}if(r!=X){L=0}X=r;if(typeof W[r]!="undefined"){if(L>=W[r].length){L=0}S.find("#"+J.id+"_li_"+W[r][L]).trigger("click.linkselect");t.preventDefault();t.stopPropagation();L++}});if(A.browser.msie){K.bind("keydown.linkselect",function(o){if(",8,9,33,34,35,36,37,38,39,40,".indexOf(","+o.keyCode+",")>-1){return A(this).triggerHandler("keypress.linkselect",[o])}})}A(document).bind("click.linkselect",function(o){if((o.target!==K[0])&&(o.target!==P[0])&&H.is(":visible")){G();K.removeClass(R.classLinkFocus)}});A(window).resize(function(){if(F){N(K,H,true)}});function m(){var t=J.id;var s=l.attr("title");var r=f.selectedIndex==-1?"":f[f.selectedIndex].text;var q=f.selectedIndex==-1?"":f[f.selectedIndex][(A.browser.msie&&A.browser.version<=7&&!(f[f.selectedIndex].attributes.value.specified))?"text":"value"];var p=l.attr("tabindex");var o=['<a href="#'+J.id+'" id="'+J.id+'_link" class="'+R.classLink+'"'+(p?' tabindex="'+p+'"':"")+">"+r+"</a>",'<input type="hidden" name="'+J.id+'" id="'+J.id+'" value="'+q+'" />','<div class="'+R.classContainer+'">',(s)?'<div class="title"><span>'+s+"</span></div>":"",'<div class="scrollable"><ul id="'+J.id+'_list">',i(l.children("option")),"</ul></div>","</div>"];return A(o.join(""))}function i(o){W=[];var p=[];o.each(function(s){var w=A(this);var u=w.is(":selected");var q=A.trim(w.text());var r='<span class="'+R.classValue+'">'+q+"</span>";var v=A.browser.msie&&A.browser.version<=7&&!(this.attributes.value.specified)?this.text:this.value;if(A.isFunction(R.format)){r=R.format.apply(J,[r,v,q,s,w,R])||r}var t=(q.length>1)?q.substring(0,1).toLowerCase():"";if(!W[t]){W[t]=[]}W[t].push(s);var x=A.trim(this.className+" "+(u?R.classSelected:""));p.push('<li id="'+J.id+"_li_"+s+'" rel="'+v+(x.length>0?'" class="'+x:"")+'">'+r+"</li>")});return p.join("")}function O(){E=false;H[0].style.width="";if(I.length){I[0].style.width="";I.css("float","")}S.html(i(l.children("option")))}function h(q,p){var o=S.find("li[rel="+q+"]");if(o.length==0){o=S.find("li:eq(0)")}return o.trigger("click.linkselect",[true,p])}function Q(){var o=S.find("li.selected");if(o.length==0){o=S.find("li:eq(0)")}return o}function T(){var o=S.find("li.current");if(o.length==0){o=Q()}return o}function d(o){H.find(".current").removeClass(R.classCurrent);o.addClass(R.classCurrent);return o}function b(p){var o=T();var q=parseInt(o.attr("id").replace(/(.+)(_(\d+$))/gi,"$3"),10);D(q+p)}function D(r){var q=A("li",H),o;if(!q||q.length==0){return false}var p=T().removeClass(R.classCurrent);if(isNaN(r)||r<0){o=q.eq(0)}else{if(r>q.length-1){o=q.eq(q.length-1)}else{o=q.eq(r)}}if(H.is(":visible")){o.addClass(R.classCurrent);B(o)}else{if(p[0]!==o[0]){o.trigger("click.linkselect")}}}function V(){D(0)}function M(){D(l.children("option").length-1)}function B(p,o){var r=p[0];var t=P[0];var q={pTop:parseInt(P.css("paddingTop"),10)||0,pBottom:parseInt(P.css("paddingBottom"),10)||0,bTop:parseInt(P.css("borderTopWidth"),10)||0,bBottom:parseInt(P.css("borderBottomWidth"),10)||0};if((r.offsetTop+r.offsetHeight)>(t.scrollTop+t.clientHeight)){t.scrollTop=p.offset().top+(t.scrollTop-P.offset().top)-((t.clientHeight/((o==true)?2:1))-(p.outerHeight()+q.pBottom))}else{if(r.offsetTop-q.bTop-q.bBottom<=(t.scrollTop+q.pTop+q.pBottom)){t.scrollTop=p.offset().top+(t.scrollTop-P.offset().top)-q.pTop}}}function e(){if(H.is(":visible")){G()}else{g()}}var E=false;function g(u){var p=Q();K.removeClass(R.classLinkFocus).addClass(R.classLinkOpen);H.show();if(!E){var t=(K.css("display").indexOf("inline")>-1)?K.parent().outerWidth():K.outerWidth();var q=R.fixedWidth?t:H.width();if(q<t){q=t}var s=parseInt(P.css("max-height"),10);if(A.browser.msie&&A.browser.version<=6){if((s>0)&&(P.height()>s)){P.height(s)}}if(S.height()>s){q+=25}var r=parseInt("0"+H.css("max-width"),10);if((r>0)&&(q>r)){q=t=r}H.width(q);if(A.browser.safari){var o=H.width();if(t>o){q=t=o}}I.width(t);if(I.outerWidth()>t){I.width(t-(I.outerWidth()-t))}if(R.titleAlign.toLowerCase()=="right"&&!R.fixedWidth){I.css("float","right")}E=true}N(K,H,true);if(!!A.fn.bgIframe){H.bgIframe()}B(p,true);d(p);if(A.isFunction(R.open)){R.open.apply(this,[H,K,p,I])}if(A.isFunction(u)){u.apply(this,[H,K,p,I])}F=true}function G(o){if(o!==true){K.addClass(R.classLinkFocus).removeClass(R.classLinkOpen)}H.hide();if(A.isFunction(R.close)){R.close.apply(this,[H,K,Q(),I])}F=false}function c(o){var p=false;if(o.is(":hidden")){p=!!o.css("visibility","hidden").show()}var q=A.extend(o.offset(),{width:o.outerWidth(),height:o.outerHeight(),marginLeft:parseInt(A.css(o[0],"marginLeft",true),10)||0,marginRight:parseInt(A.css(o[0],"marginRight",true),10)||0,marginTop:parseInt(A.css(o[0],"marginTop",true),10)||0,marginBottom:parseInt(A.css(o[0],"marginBottom",true),10)||0});if(q.marginTop<0){q.top+=q.marginTop}if(q.marginLeft<0){q.left+=q.marginLeft}q.bottom=q.top+q.height;q.right=q.left+q.width;if(p){o.hide().css("visibility","visible")}return q}function N(s,o){var u=c(s);var t=j();var q=H.outerWidth()+u.left;if(q>t.x){u.left=(u.left-H.outerWidth())+I.outerWidth()}else{var r=H.outerWidth(),p=I.outerWidth();if(r>p){I.width(r-(p-I.width()))}}o.css({position:"absolute",top:u[R.yAxis],left:u.left});return u.bottom}function j(){var o={scrollLeft:A(window).scrollLeft(),scrollTop:A(window).scrollTop(),width:A("body").width(),height:A("body").height()};o.x=o.scrollLeft+o.width;o.y=o.scrollTop+o.height;return o}function n(o){return !("bubbles" in o||"cancelBubble" in o)}if(A.isFunction(R.init)){R.init.apply(this,[l,Z,K,H,P,I,S])}};A.LinkSelect.defaults={classLink:"linkselectLink",classLinkOpen:"linkselectLinkOpen",classLinkFocus:"linkselectLinkFocus",classContainer:"linkselectContainer",classSelected:"selected",classCurrent:"current",classDisabled:"linkselectDisabled",classValue:"linkselectValue",yAxis:"top",titleAlign:"right",fixedWidth:false,init:null,change:null,format:null,open:null,close:null}})(jQuery);