(function($) {
	
    $.fn.fakeselect = function(options) {
		
        var defaults = {
			
			defaultValue		: 'Select ...',
			pathToCountryFlags	: '/images/website/flags/',
			countryFlags		: false,
			selectHandler		: null,
			rows				: null
			
		};
		
		var settings = $.extend(defaults, options);
		
		var realselect = $(this);
		
		function toggleHandler(e) {
			
			e.preventDefault();
			
			$(fakeselect).parent().removeClass('error');
			
			$(fakeselect).find('.dropdown').toggle();
			
		}
		
		function selectHandler(e) {
			
			e.preventDefault();
			
			$(fakeselect).find('.dropdown li').removeClass('active');
			
			$(fakeselect).find('.text').addClass('text-w-icon');
			
			$(fakeselect).find('.text').text($(this).text());
			
			$(fakeselect).find('.text').css('background-image', $(this).css('background-image'))
			
			$(this).parent().addClass('active');
			
			$(fakeselect).val($(this).attr('href'));
			
			if ($.isFunction(settings.selectHandler)) {
				
				settings.selectHandler(this);
			
			}
			
			$(realselect).val($(this).text());
			
		}
		
		var fakeselect = $('<div/>').addClass('fakeselect');
		
		if (settings.countryFlags) {
			
			fakeselect.addClass('fakeselect-w-icons');
			
		}
		
		var text = $('<span/>', {
			
			text	: settings.defaultValue
			
		}).addClass('text');
		
		var toggler = $('<a/>', {
			
			href	: '#',
			title	: settings.defaultValue,
			text	: settings.defaultValue
			
		}).addClass('toggler');
		
		toggler.bind('click', toggleHandler);
		
		var dropdown = $('<ul/>').addClass('dropdown');
		
		if (settings.rows) {
			
			if ($(this).find('option').length > settings.rows) {
				
				dropdown.height(settings.rows * 30).css('overflow-x', 'hidden');
				
			}
			
		}
		
		if ($(this).find('optgroup').length > 0) {
			
			$(this).find('optgroup').each(function() {
				
				//alert('Found: ' + $(this).attr('label'));
				
				var listitem = $('<li/>', {
					
					text	: $(this).attr('label')
					
				}).addClass('group');
				
				dropdown.append(listitem);
				
				$(this).find('option').each(function() {
					
					var listitem = $('<li/>');
					
					var anchor = $('<a/>', {
						
						href	: $(this).val(),
						title	: $(this).text(),
						text	: $(this).text()
						
					});
					
					if (settings.countryFlags) {
						
						anchor.css('background-image', 'url(' + settings.pathToCountryFlags + $(this).attr('class') + '.gif)')
						
					}
					
					anchor.bind('click', selectHandler);
					anchor.bind('click', toggleHandler);
					
					listitem.append(anchor);
					dropdown.append(listitem);
					
				});
				
			});
			
		} else {
		
			$(this).find('option').each(function() {
				
				var listitem = $('<li/>');
				
				var anchor = $('<a/>', {
					
					href	: $(this).val(),
					title	: $(this).text(),
					text	: $(this).text()
					
				});
				
				if (settings.countryFlags) {
					
					anchor.css('background-image', 'url(' + settings.pathToCountryFlags + $(this).attr('class') + '.gif)')
					
				}
				
				anchor.bind('click', selectHandler);
				anchor.bind('click', toggleHandler);
				
				listitem.append(anchor);
				dropdown.append(listitem);
				
			});
			
		}
			
		$('<option/>', {
			
			value	: -1,
			text	: settings.defaultValue
			
		}).prependTo($(this));
		
		$(this).val(-1);
		
		fakeselect.append(text);
		fakeselect.append(toggler);
		fakeselect.append(dropdown);
		
		$(this).after(fakeselect);
		
	}
	
})(jQuery);
