    /*
 * @author: Thomas Pink - Netural Communication
 * @version: 0.8
 */
(function($){
    var hidden = false;
    var methods = {
        init: function(options){
            return this.each(function(index){
                methods.buildDom($(this),index);
                $(this).next(".customselectbox").find(".activeSelection").bind("click",methods.activeClicked);
                $(this).next(".customselectbox").find(".activeSelection").bind("focusout",methods.looseFocus);
            });
            
        },
        destroy: function(){
            return this.each(function(index){
                $(this).unbind('.customselectbox');
                $(this).next(".customselectbox").remove();
            })       
        },
        
        buildDom: function(me,index) {
            me.attr("class");
            var outer = $("<div>").addClass(me.attr("class") + " customselectbox").attr("rev","customselectbox-"+index); 
            outer.append( $("<a>").addClass("activeSelection").text(me.children("option").eq(0).text()) );
            
            if(me.children("option").length > 0) {
                var list = $("<ul>").addClass("customselectboxlist").attr("rel","customselectbox-"+index);
                
                $.each(me.attr("class").split(" "),function(index, value){
                    list.addClass("customselectboxlist-"+value);
                });
                
                me.children("option").each(function(index){
                    var listitem = $("<li>");
                    listitem.addClass("itemcount-"+index);
                    listitem.append($("<a>").text($(this).text()));
                    if(index == ((me.children("option").length)-1))
                        listitem.addClass("last");
                    else if(index == 0)
                        listitem.addClass("first");
                    else if(index == 1)
                        listitem.addClass("second");
                    else
                        listitem.addClass("middle");
                    list.append(listitem);
                });
                list.hide();
                $(outer).append(list);
            }
            
            me.after(outer);
            me.attr("style","top:-9999px; left:-9999px; position:absolute;");
            if(hidden == false) {
                me.parents().each(function(){
                    if($(this).css("overflow") == "hidden") {
                        hidden = true;
                        return false;
                    }
                });
            }
        },
        
        openList: function(list) {            
			$('.customselectboxlist').each(function(){
				methods.closeList($(this));});
            list.slideDown(function () {
				$(".customselectboxlist").jScrollPane();
				$(".customselectboxlist").find('.jspTrack').height("113");
			});
        },
        
        closeList: function(list) {
            list.slideUp(function() {
                var parent = list.parent();
                if(parent[0].nodeName.toLowerCase() == "body")
                    list.remove();
            });
        },
        
        listItemClicked: function() {
            var id = $(this).parents(".customselectboxlist").eq(0).attr("rel");
            var list = $('div[rev="'+id+'"]');
            var a = $(this).parent().attr("class").split("-");
			list.prev("select").eq(0).children("option").attr("selected", false);
            list.prev("select").eq(0).children("option").eq(parseInt(a[1])).attr('selected', 'selected');
			list.prev("select").change();
            list.children(".activeSelection").text($(this).text());
            
            list.toggleClass("open");
            
            methods.closeList($(this).parents(".customselectboxlist").eq(0));
			
			list.find("a").unbind("click",methods.listItemClicked );
        },
        
        activeClicked: function() {
            var list = null;
            if(!hidden) {
                list = $(this).next(".customselectboxlist");
            } else {
                list = $('body > ul[rel="'+$(this).parent().attr("rev")+'"]');
                if(list.length == 0) {
                    var width = $(this).next(".customselectboxlist").width();
                    var top = $(this).parent().offset().top + parseInt($(this).next(".customselectboxlist").css("top").substr(0,$(this).next(".customselectboxlist").css("top").length-2));
                    var left = $(this).parent().offset().left + parseInt($(this).next(".customselectboxlist").css("left").substr(0,$(this).next(".customselectboxlist").css("left").length-2));

                    var copy = $(this).next(".customselectboxlist").clone();
                    
                    $("body").append(copy);
                    list = copy;
                    list.width(width);
                    list.css("top",top+"px");
                    list.css("left",left+"px");
                }
            }
            list.find("a").bind("click",methods.listItemClicked );
            
            if(list.is(":hidden"))
                methods.openList(list);
            else
                methods.closeList(list);
                
            $(this).parent().toggleClass("open");
			
        },
        looseFocus: function() {
                var list = $(this).next(".customselectboxlist").hide(); 
                alert("test");
        },
        keyboardControl: function() {
            
        }
    };
    
    $.fn.customselectbox = function(method){
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else 
            if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            }
            else {
                $.error('Method ' + method + ' does not exist on jQuery.customselectbox');
            }   
    }; 
})(jQuery);

