jQuery.fn.dropSelect = function(settings)
{
	
	settings = jQuery.extend({
	
	}, settings);
	
	return this.each(
		
		function()
		{
			
			var $this = jQuery(this);
			var id = jQuery(this).attr('id');
			var newID = 'drop_select_' + id;
			var html = '<div class="drop_select" id="' + id + '">';
			var className = jQuery(this).attr('class');
			var newClassName = "drop_select_select";
			var currentMenu;
			
			//------------------------------------------------------
			// Helper Functions
			//------------------------------------------------------
			
			var addTopLevelItem = function(value, label, classname, index)
			{
				var html = '<li class="drop_select_li_first">' + addAnchorTag(value, label, classname, index);
				return html;
			}
			
			var addListItem = function(value, label, classname, index)
			{
				var html = '<li class="drop_select_li">' + addAnchorTag(value, label, classname, index) + '</li>';
				return html;	
			}
		
			var addAnchorTag = function(value, label, classname, index)
			{
				var html = '<a href="" class="' + classname + '" rel="{' + "'value':" + "'" + value + "'" + "," + "'label':" + "'" + label + "'" + "," +  "'index':" + "'" + index + "'" + '}">' + label + '</a>';
				return html;
			}
			
			var showMenu = function()
			{
				jQuery('.drop_select_li_first_on:not("#' + id + ' > ul > li")').attr('class', 'drop_select_li_first');
				jQuery('.jScrollPaneContainer_on:not("#' + id + ' > ul > li > div")').slideUp('fast').parent().parent().parent().css('z-index', '999');
				if(jQuery('#' + id + ' > ul > li').eq(0).attr('class') == 'drop_select_li_first_on')
				{
					hideMenu();
					return;
				}
				jQuery('#' + id + ' > ul > li').eq(0).attr('class', 'drop_select_li_first_on');
				jQuery('#' + id + ' > ul > li > div').attr('class', 'jScrollPaneContainer_on').hide().slideDown('fast');
				jQuery('#' + id ).css('z-index', '1000');
			}
			
			var hideMenu = function(obj)
			{
				jQuery('#' + id + ' > ul > li').eq(0).attr('class', 'drop_select_li_first');
				jQuery('#' + id + ' > ul > li > div').slideUp('fast');
			}
			
			var selectItem = function(json)
			{
				var json = eval('(' + json + ')');
				jQuery('#' + id + ' > ul > li > a').attr('class', 'drop_select_a_first_on').html(json.label);
				$this.find('option').eq(json.index).attr("selected", "selected");
			}
			
			var autoSelect = function()
			{
				$this.find('option[class="selected"]').each(function(i){
					var json = jQuery('#' + id + ' > ul > li > div > ul > li').eq(this.index).find('a').attr('rel');
					selectItem(json);
				});	
			}
			
			//------------------------------------------------------
			// Parse the options and build the menu
			//------------------------------------------------------
			
			$this.find(':select').each(function(i){
				
				// Get the first option and set it as the top level item
				// Set all remaining options as the drop menu items
				
				var value = jQuery(this).attr('value');
				var label = jQuery(this).html();
				
				if(i == 0)
				{
					html += '<ul class="drop_select_ul">';
					html += addTopLevelItem(value, label, 'drop_select_a_first', i);
					html += '<ul class="drop_select_sub_ul">';
					html += addListItem(value, label, '', i);
				}
				else
				{
					html += addListItem(value, label, '', i);
				}
				
			});
			
			html += '</ul></li></ul></div>'; // End Parse
			
			//------------------------------------------------------
			// Add the menu to the select elements parent
			// Then hide the select menu
			//------------------------------------------------------
			
			jQuery(this).parent().append(html);
			jQuery(this).css('position', 'absolute').css('left', '-10000px');
			jQuery(this).attr('id', newID).attr('class', newClassName);
			
			$('.drop_select_sub_ul').jScrollPane({showArrows:false, scrollbarWidth: 10});
			
			//------------------------------------------------------
			// Add click actions
			//------------------------------------------------------
			
			jQuery('#' + id + ' > ul > li a').eq(0).bind("click", function(){
				showMenu();
				return false;
			});
			
			jQuery('#' + id + ' > ul > li > div > ul > li > a').bind("click", function(){
				selectItem(jQuery(this).attr('rel'));
				hideMenu();
				return false;
			});
			
			//------------------------------------------------------
			// Auto select
			//------------------------------------------------------
			
			autoSelect();
			
		}
		
	);
	
}