(function($){
    $.fn.daveselect = function(o) {
	
	var base = $(this);

	// Defaults
	var options = jQuery.extend({
		backimage: {
			url: ""
		},
		backbutton: {
			url: "",
			topOffset: 0,
			rightOffset: 0,
			height: 10,
			width: 10
		},
		backdrop: '#FFFFFF',
		border: '#000000',
		backhover: '#000000',
		text: {
			paddingTop: "0",
			paddingRight: "0",
			paddingBottom: "0",
			paddingLeft: "0"
		}
	},o);

	base.each(function(){
		var current = $(this);
		var width = current.outerWidth();
		var height = current.outerHeight();
		var new_width = (width - options.text.paddingRight - options.text.paddingLeft);
		var new_height = (height - options.text.paddingTop - options.text.paddingBottom);
		var font_size = current.css("font-size");
		var font_color = current.css("color");
		var font_align = current.css("text-align");
		var selectoptions = current.children("option");
		var selectoptionshtml = '<ul name="'+current.attr("name")+'" style="list-style: none; padding: 0; margin: 0;">';
		var selectoptionfirst = current.children("option:selected").text();	
		var nutitle = current.attr("title");	

		selectoptions.each(function(){
			selectoptionshtml += '<li rel="'+$(this).val()+'" style="font-size: 13px; padding: 3px 0;">'+$(this).text()+'</li>';
		});
		selectoptionshtml + "</ul>";
		current.before("<div><div></div></div>").prev().css({ position: "relative",
			display: "inline-block",
			"vertical-align": "middle",
			width: new_width+"px",
			"font-size" : font_size,
			color: font_color, 
			height: new_height+"px",
			margin: "4px 0px",
			"text-align": font_align,
			padding: options.text.paddingTop+"px "+options.text.paddingRight+"px "+options.text.paddingBottom+"px "+options.text.paddingLeft+"px",
			background: "url("+options.backimage.url+")"
		}).attr("title",nutitle).addClass("jq-tiptip").children().before(selectoptionfirst).css({ top: options.backbutton.topOffset+"px", 
			right: options.backbutton.rightOffset+"px", 
			position: "absolute",
			background: "url("+options.backbutton.url+")", 
			width: options.backbutton.width+"px", 
			cursor: "pointer",
			height: options.backbutton.height+"px" 
		}).after('<div class="jq-daveselect-drop"></div>').siblings().css({ "z-index":"3000", position: "absolute", 
			"background-color": options.backdrop, border: "1px solid "+options.border, "border-radius": "1px", display: "none",
			"-moz-border-radius": "1px","-webkit-border-radius": "1px", "text-align": "center", "font-size":"11px","cursor":"pointer","max-height": "150px","overflow-y":"auto", width: new_width+"px", top: height+"px", right: "0px"
		}).prev().click(function(){
			if($(this).siblings(":visible").length)
			{
				$(this).siblings().slideUp();
			}
			else
			{
				$(".jq-daveselect-drop:visible").hide();
				$(this).siblings().slideDown();
			}
		}).next().html(selectoptionshtml).find("ul").css("z-index","1000").find("li").css("z-index","140").hover(function(){
			$(this).css("background-color",options.backhover);
		}, function(){
			$(this).css("background-color",options.backdrop);
		}).click(function(){
			var li = $(this);
			var value = li.attr("rel");
			var litext = li.text();
			li.parent().parent().hide().parent().contents().filter(function(){
				return this.nodeType == 3;
			}).replaceWith(litext).end().parent().next("select").val(value).attr("selected","true");
			current.val(value);
		});
		
		current.hide();
		
		
	});

};

})(jQuery);

