/*global jQuery*/
String.prototype.htmlentitize = function() {
	return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
};

(function($) {
	$.fn.dropdown = function(options) {
		var params = $.extend(true, null, $.fn.dropdown.defaults, options);
		
		return this.each(function() {
			var select = $(this);
			if(!select.is("select")) { return false; }
			var choices = select.find("option");
			for(var i = 0; i < choices.length; i++) {
				params.entries.push({ text: choices.eq(i).text(), value: choices.eq(i).val() || choices.eq(i).text() });
			}
			
			var selected_text = choices.filter("[selected]").text() || "";
			var input = $('<input type="hidden" class="hidden" id="' + select.attr("id") + '" name="' + select.attr("name") + '" value="' + select.val().htmlentitize() + '"/>').insertBefore(select);
			var control = $('<div class="httpool-dropdown' + (params.className != null ? ' ' + params.className : '') + '" id="' + select.attr("id") + '-div">' + selected_text.htmlentitize() + '</div>').css("cursor", "pointer");
			
			var removeBox = function() {
				var box = $("ul[name='dropdown_" + select.attr('name') + "']");
				box.slideUp(80).queue(function() { box.remove(); });
			};
			
			var createBox = function() {
				//control = $(control.get(0));
				removeBox();
				var box = '<ul class="httpool-dropdown-box" name="dropdown_' + select.attr('name') + '" style="position:absolute;left:0;top:0;">';
				if(params.includeBlank) {
					box += '<li xvalue="">&nbsp;</li>';
				}
				for(var i = 0; i < params.entries.length; i++) {
					box += '<li xvalue="' + params.entries[i].value + '">' + params.entries[i].text + '</li>';
				}
				box += '</ul>';
				var choices = $(box).appendTo("body").css({
					top: control.offset().top + control.verticalSize() + "px",
					left: control.offset().left + "px",
					width: control.horizontalSize() + "px",
					cursor: "default",
					zIndex: 1e4
				}).hide().slideDown(80);
				
				choices.find("li").click(function() {
					control.text($(this).text());
					input.val($(this).attr("xvalue"));
					input.trigger("change");
					removeBox();
					return false;
				}).hover(
					function() {
						$(this).addClass("active");
						return false;
					},
					function() {
						$(this).removeClass("active");
						return false;
					}
				).css({ width: (control.width() - 4) + "px"});
				
			};
			
			control.click(function() {
				if($("ul[name='dropdown_" + select.attr('name') + "']").length) {
					removeBox();
					return false;
				} else {
					createBox();
					$("body").one("click", function() {
						removeBox();
					});
					return false;
				}
			});
			
			select.replaceWith(control);
		});
	};

	$.fn.dropdown.defaults = {
		className: null,
		includeBlank: false,
		entries: []
	};
	
	$.fn.verticalSize = function() {
		var t = this.eq(0);
		if(!t) { return null; }
		return Number(t.css("paddingTop").replace("px", "")) + t.height() + Number(t.css("paddingBottom").replace("px", ""));
	};
	
	$.fn.horizontalSize = function() {
		var t = this.eq(0);
		if(!t) { return null; }
		return Number(t.css("paddingLeft").replace("px", "")) + t.width() + Number(t.css("paddingRight").replace("px", ""));
	};
})(jQuery);