jQuery.fn.makeSelector = function(options) {

	var settings = {initialValue: '', maxChars: -1, height: 400, doReload: true};

	settings = jQuery.extend(settings, options);

	if (settings.initialValue == '') {
		settings.initialValue = $(this).find('li:first').html();
		$(this).find('li:first').hide();
	}
	
	var targetId = "."+ $(this).attr('class');
	
    $(this)
		.find('.selectorText')
			.html(getText(settings.initialValue))
			.data('url', $(this).find('li:first').find('a').attr('href'))
		.hover(function() {
				$(this).addClass('active');
			}, function() {
				$(this).removeClass('active');
			})
		.click(function() {
		        var ul = $(this).parent().next('ul');
		        if (ul.is(':hidden')) {
		            ul.show();
		            ul.find('li:first').addClass('active');

		        } else {
		            ul.hide();
		        }
			});
	
	$(this).find('ul').each(function() {
		if ($(this).height() > settings.height) {
			$(this).css('overflow', 'auto');
			$(this).height(settings.height);
		}
	});
	
    $(this).find('li')
		.hover(function() {
		        $(this).addClass('active');
			}, function() {
		        $(this).removeClass('active');
		    })
		.click(function() {
		        var val = $(this)
							.parents(targetId)
							.find('.selectorText')
							.data('url', $(this).find('a').attr('href'))
							.text(getText($(this).text()));
				
				var ul = $(this).parent();
				showListElements(ul);
				ul.hide();
//AS - copied the line below from the .selectorButton to here to enable changing countries by just selecting a country. (don't need to press blue arrow)
				if (settings.doReload) {
					window.location.href = $(this).parents(targetId).find('.selectorText').data('url');
				}
//AS

				$(this).removeClass('active');
				if (ul.find('li').size() > 1) {
					$(this).hide();
				}
			});

    $(this).find('.selectorButton').click(function() {
        window.location.href = $(this).parents(targetId).find('.selectorText').data('url');
    });

    $(document).click(function(e) {	
        if ($(e.target).hasClass('selectorText') || $(e.target).hasClass('selectorButtonUsual')) {
			//$(targetId).find('ul').hide();
		} else {
			$(targetId).find('ul').hide();
		}
    });

	function getText(text) {
		if (settings.maxChars != -1 && settings.maxChars < text.length) {
			var ret = '';
			for (var i = 0; i < settings.maxChars; i++) {
				ret += text.charAt(i);
			}
			return ret;
		}
		return text;
	}
	
	function showListElements(ul) {
		$(ul).find('li').each(function() {
			$(this).show();
		});
	}
	
    return this;
}