(function($) {
	var me = {
		'origSelect' : false
	};
	var newMethods = {

		// Init Method
		fancySelect : function() {
			if( !$(this).is('select') ) { return false; }
			me.origSelect = this;
			var w = $(this).width();
			var c = new Array();
			$(this).find('option').each(function(i) {
				c[i] = {
					v : $(this).val(),
					c : $(this).text(),
					s : $(this).attr('selected')
				};
			});
			
			var id = 'fs_' + 'th';
			$(this).parent().append('<div id="' + id + '"><div class="fs_content"><div class="fs_wrapper"><ul></ul></div></div></div>');
			var fs = $('#' + id);
			$(fs).addClass('fancy_select');
			
			// Build the options (li:s)
			$.each(c, function(index, value) {
				$(fs).find('ul:first').append('<li></li>');
				$(fs).find('li:last').attr('rel',c[index].v);
				$(fs).find('li:last').text(c[index].c);
				if( c[index].s ) {
					$(fs).find('li:last').addClass('active');
				}
			});
			$(fs).prepend('<strong>' + c[0].c + '</strong>');
			$(fs).find('.fs_content').append('<div class="fs_arr_up">Upp</div><div class="fs_arr_down">Ner</div>');
			$(fs).find('li:first').hide();
			$(fs).find('li:eq(1)').addClass('first');
			$(fs).find('li:last').addClass('last');			
			
			// Bind events
			$(fs).find('strong:first').bind('click', $(this).fancySelectToggle );
			$(fs).find('li').bind('click', $(this).fancySelectToggleActive);
			$(fs).find('.fs_arr_up').bind('click', $(this).fancySelectScroll);
			$(fs).find('.fs_arr_down').bind('click', $(this).fancySelectScroll);			
			
			// Set active
			if( $(fs).find('li.active').is('li') ) {
				a = $(fs).find('li.active');
				$(fs).find('strong').text( $(a).text() );
			}
			
			// Hide the old SELECT
			$(this).hide();
		},
		
		// Click on SELECT		
		fancySelectToggle: function() {
			if( $(this).parents('.fancy_select:first').hasClass('active') ) {
				$(this).parents('.fancy_select:first').removeClass('active');
			} else {
				$(this).parents('.fancy_select:first').addClass('active');
				var s = $(this).parents().find('li.active:first').is('li');
				if( s ) {
					s = $(this).parents().find('li.active:first').position();
					$(this).parents().find('.fs_wrapper:first').scrollTop(s.top);
				}
			}
		},

		// Click on OPTION
		fancySelectToggleActive : function() {
			if( !$(this).hasClass('active') ) {
				$(me.origSelect).find('option[value="'+ $(this).attr('rel') +'"]').attr('selected', 'selected');
				$(this).parent().find('li.active').removeClass('active');
				$(this).addClass('active');
				$(this).parents('div.fancy_select:first').find('strong').text( $(this).text() );
				$(me.origSelect).removeClass('active');
				me['justClosed'] = true;	
			}
			$(this).fancySelectToggle();
		},
		
		fancySelectScroll : function() {
			var u = 	$(this).hasClass('fs_arr_up'); // Which direction
			var w = 	$(this).parent().find('.fs_wrapper'); // The wrapper surrounding the content to be scrolled
			var c = 	$(w).find('ul:first'); // The content to be scrolled
			var cs = 	$(w).scrollTop();
			var ss = 	$(c).find('li:first').height(); // Pixels to scroll each time
			var np = 0; // New position
			if( u ) { np = cs - ss; }
			else { np = cs + ss; }
			$(w).scrollTop(np);
		}
	}
		
	jQuery.each(newMethods,function(i){
		jQuery.fn[i] = this;
	});
	
})(jQuery);
