(function($){
	$.fn.dropDown = function(settings){
		var $this=$(this), index=0, $visibleUL = null;
		var defaults = {
			arrowCls: "default"
		}
		if (settings) {
            $.extend(defaults, settings);
        };
		
		jQuery.fn.extend({
			switchContent: function($option){
				$option.parent().find("option[selected='selected']").removeAttr("selected");
				$option.parent().val($option.val());
				var $dynContent = $(this).find("span.dynamic-content");
				$dynContent.html($option.text());
				if($visibleUL){
					$visibleUL.find("li").removeClass("selected");
					$visibleUL.find("li[data-value='"+$option.val()+"']").addClass("selected");
					$visibleUL.css("width", $dynContent.parent().outerWidth());
				}
				return $dynContent;
			},
			upDown: function(){
				return $(this).each(function(){
					$(this).keydown(function(e){
						if($("ul.dropDownList").is(":visible")){
							var upDown = true;
							switch(e.keyCode){
								case 38:
									index = (index <= 0 ? $visibleUL.find("li").length-1 : index-1);
								break;
								case 40:
									index = (index >= $visibleUL.find("li").length-1 ? 0 : index+1);
								break;
								case 13:
									//$('.dropDownList').hide();
									upDown = false;
								break;
								default:
									upDown = false;
							}
							//Up & Down
							if(upDown){
								$visibleUL.find("li").removeClass("hover");
								var $currentLI = $($visibleUL.find("li").get(index));
								$($currentLI).addClass("hover");
								$visibleUL.prev("a.dropDownSelected").switchContent($visibleUL.parent().find("select:first option[value='"+$currentLI.attr('data-value')+"']"));
								return false;
							}
						}
					})
				})
			}
		})
		$(document).upDown().click(function(e){
		    if($(e.target).is('a.dropDownSelected, a.dropDownSelected *'))return;
		    $('.dropDownList').hide();
		});;

		return $this.each(function(){
			var $options = $(this).find("option"),
				random = Math.floor(Math.random()*101), 
				$ul = $("<ul>").attr("id", "dropDown"+(new Date()).getTime()+random).addClass("dropDownList").css("position", "absolute"), 
				$selectedElement = $("<a>").addClass("dropDownSelected greenBtn dropDown").attr("href", "javascript:void(0)"),
				$arrow = $("<span>").addClass("arrow " + defaults.arrowCls),
				$content = $("<span>").addClass("fs15 dynamic-content");
				$selectedElement.append($content).append($arrow);
			$options.each(function(index, option){
				var $option = $(option);
				if ($option.is(":selected"))
					$selectedElement.switchContent($option);
				if($option.val()){
					var $li = $("<li>")
									.addClass("dropDownElement" + ($option.is(":selected") ? " selected" : ""))
									.html($option.text())
									.attr("data-value", $option.val())
									.click(function(){
										$selectedElement.switchContent($option);
									})
					$ul.append($li);
				}
			})
			$selectedElement.click(function(){
				$ul.css("z-index", 1000).slideToggle("fast", function(){
					var $ul = $(this);
					$visibleUL = $ul;
					index = $($visibleUL.find("li")).index($($visibleUL.find("li.selected")));
					if(!$ul.is(":visible")){
						$visibleUL = null;
						$ul.css("z-index", 0);
						index = 0;
					}
				});	
			}).show();
			$ul;
			$(this).after($ul).after($selectedElement).hide();
		});
	}
})(jQuery);
