/*
 * jQuery selectbox plugin
 *
 * Copyright (c) 2007 Sadri Sahraoui (brainfault.com)
 * Licensed under the GPL license and MIT:
 *   http://www.opensource.org/licenses/GPL-license.php
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * The code is inspired from Autocomplete plugin (http://www.dyve.net/jquery/?autocomplete)
 *
 * Revision: $Id$
 * Version: 0.5
 * 
 * Changelog :
 *  Version 0.5 
 *  - separate css style for current selected element and hover element which solve the highlight issue 
 *  Version 0.4
 *  - Fix width when the select is in a hidden div   @Pawel Maziarz
 *  - Add a unique id for generated li to avoid conflict with other selects and empty values @Pawel Maziarz
 */
jQuery.fn.extend({selectbox:function(A){return this.each(function(){new jQuery.SelectBox(this,A)
})
}});
if(!window.console){var console={log:function(A){}}
}jQuery.SelectBox=function(T,E){var B=E||{};
B.inputClass=B.inputClass||"selectbox";
B.containerClass=B.containerClass||"selectbox-wrapper";
B.hoverClass=B.hoverClass||"current";
B.currentClass=B.selectedClass||"selected";
B.debug=B.debug||false;
B.direction=B.direction||"auto";
var K=T.id;
var F=-1;
var D=false;
var S=0;
var R=$(T);
var P=H(B);
var C=M(B);
R.hide().before(C).before(P);
Q();
C.click(function(){if(!D){A()
}}).focus(function(){if(P.not(":visible")){D=true;
A()
}}).blur(function(){if(P.is(":visible")&&S>0){if(B.debug){console.log("container visible and has focus")
}}else{if($.browser.msie){if(document.activeElement.getAttribute("id").indexOf("_container")==-1){I()
}else{C.focus()
}}else{I()
}}}).keydown(function(U){switch(U.keyCode){case 38:U.preventDefault();
L(-1);
break;
case 40:U.preventDefault();
L(1);
break;
case 13:U.preventDefault();
$("li."+B.hoverClass).trigger("click");
break;
case 27:I();
break
}});
function I(){S=0;
P.hide()
}function Q(){P.append(N(C.attr("id"))).hide();
var U=C.outerWidth()-2;
P.width(U+"px")
}function H(V){var U=document.createElement("div");
P=$(U);
P.attr("id",K+"_container");
P.addClass(V.containerClass);
return P
}function M(V){var U=document.createElement("input");
var W=$(U);
W.attr("id",K+"_input");
W.attr("type","text");
W.addClass(V.inputClass);
W.attr("autocomplete","off");
W.attr("readonly","readonly");
W.attr("tabIndex",R.attr("tabindex"));
return W
}function L(V){var U=$("li",P);
if(!U){return 
}F+=V;
if(F<0){F=0
}else{if(F>=U.size()){F=U.size()-1
}}U.removeClass(B.hoverClass);
$(U[F]).addClass(B.hoverClass)
}function G(){var U=$("li."+B.currentClass,P).get(0);
var V=(""+U.id).split("_");
var W=V[V.length-1];
R.val(W).attr("selectedIndex",P.find("li").index(U));
C.val($(U).html());
return true
}function A(){if(P.not(":visible")){var V=C.position();
var U=$(document).height();
P.css("top",V.top+C.outerHeight()+"px");
P.css("left",V.left+"px");
P.show();
if(B.direction=="up"){P.css("margin-top","-"+(P.outerHeight()+C.outerHeight()-2)+"px")
}else{if(B.direction=="down"){}else{if(U<$(document).height()){P.css("margin-top","-"+(P.outerHeight()+C.outerHeight()-2)+"px")
}}}}else{P.hide()
}}function O(){return R.val()
}function J(){return C.val()
}function N(V){var W=new Array();
var U=document.createElement("ul");
R.children("option").each(function(){var X=document.createElement("li");
X.setAttribute("id",V+"_"+$(this).val());
X.innerHTML=$(this).html();
if($(this).is(":selected")){C.val($(this).html());
$(X).addClass(B.currentClass)
}U.appendChild(X);
$(X).mouseover(function(Y){S=1;
if(B.debug){console.log("over on : "+this.id)
}jQuery(Y.target,P).addClass(B.hoverClass)
}).mouseout(function(Y){S=-1;
if(B.debug){console.log("out on : "+this.id)
}jQuery(Y.target,P).removeClass(B.hoverClass)
}).click(function(Y){var Z=$("li."+B.hoverClass,P).get(0);
if(B.debug){console.log("click on :"+this.id)
}$("li."+B.currentClass).removeClass(B.currentClass);
$(this).addClass(B.currentClass);
G();
I();
R.change()
})
});
return U
}};
