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 > ul")').slideUp('fast');
				
				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 > ul').attr('class', 'jScrollPaneContainer_on').hide().slideDown('fast');
				jQuery('#' + id ).css('z-index', '1000');
				
				/*
				trans.css('width', $(document).width());  
         		trans.css('height', $(document).height());
          		trans.show();
				*/
				
			}
			
			var hideMenu = function(obj)
			{
				
				jQuery('#' + id + ' > ul > li').eq(0).attr('class', 'drop_select_li_first');
				
				jQuery('#' + id + ' > ul > li > ul').slideUp('fast');
				
				//trans.hide();
				
			}
			
			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(':select').each(function(i){
					
					if(i > 0 && jQuery(this).is(':selected'))
					{
						var json = jQuery('#' + id).find('.drop_select_sub_ul:eq(0)').eq(i-1).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);
			
			//------------------------------------------------------
			// Add click actions
			//------------------------------------------------------
			
			jQuery('#' + id + ' > ul > li a').eq(0).bind("click", function(){
				showMenu();
				return false;
			});
			
			jQuery('#' + id + ' > ul > li > ul > li > a').bind("click", function(){
				selectItem(jQuery(this).attr('rel'));
				hideMenu();
				return false;
			});
			
			$('.drop_select_a_first').parent().css('position', 'relative');
			
			var top = -(parseInt($('.drop_select_a_first:eq(0)').height()) * 0.5);
			
			var css = {
				position: 'absolute',
				top: '50%',
				marginTop: top
			}
			
			$('.drop_select_a_first').css(css);
			
			//------------------------------------------------------
			// Auto select
			//------------------------------------------------------
			
			autoSelect();
			
		}
		
	);
	
}
