var selectbox = {
	init: function() {		
		$(document).ready(function() {			
			$("select.selectbox").each(function() {
				var sboxId = $(this).attr('id');
				
				var sboxDropdown = document.createElement('div');
				$(sboxDropdown).attr('id', sboxId + '_container').addClass('selectbox_container');
				sboxDropdown.selectboxSelected = undefined;
				
				var sboxList = document.createElement('ul');
				$(sboxList).attr('id', sboxId + '_list');
				sboxDropdown.selectboxList = sboxList;
				
				var sboxInput = document.createElement('input');
				$(sboxInput).attr('type', 'hidden').val(0).addClass('hidden').attr('name', $(this).attr('name'));
				
				var sboxValue = document.createElement('div');
				$(sboxValue).addClass('selectbox_value');
				
				var alerted = false;
				$(this).children('option').each(function() {
						var li = document.createElement('li');
						li.selectboxDropdown = sboxDropdown;
						li.selectboxItemPrefix = $(this).attr('value');
						li.selectboxItemCountry = $(this).text();
						li.selectboxValue = sboxValue;
						li.selectboxInput = sboxInput;
						$(li).attr('rel', $(this).attr('value'));
						
						$(li).html(li.selectboxItemCountry + '<br/>+' + li.selectboxItemPrefix);
						$(li)
							.css('background-image', 'url(images/prefixes/'+li.selectboxItemPrefix+'.gif)')
							.css('background-repeat', 'no-repeat')
							.css('background-position', '5px center')
							.click(function() { selectbox.select(this); selectbox.toggle(this.selectboxDropdown); })
							.mouseover(function() { $(this).addClass('hover'); })
							.mouseout(function() {  $(this).removeClass('hover'); });
								
						if($(this).is(':selected')) { sboxDropdown.selectboxSelected = li; }
						
						if($(this).attr('value')!=48 && sboxId == 'cpa_prefix' && $('#facb:checked').val()==undefined) {
							$(li).css('display','none');
						}
						$(sboxList).append(li);
				});
				
				
				$(sboxList).addClass('selectbox_list');
				$(sboxDropdown).append(sboxInput).append(sboxValue);
				$('body').append(sboxList);
				$(this).replaceWith(sboxDropdown);
				
				$(sboxDropdown).click(function() { selectbox.toggle(this) });
				if(sboxDropdown.selectboxSelected != undefined) selectbox.select(sboxDropdown.selectboxSelected);
			});
		});
	},
	
	select: function(item) {
		$(item.selectboxValue)
			.text('+' + item.selectboxItemPrefix)
			.css('background-image', $(item).css('background-image'))
			.css('background-position', 'left center')
			.css('background-repeat', 'no-repeat');
		$(item.selectboxInput).val(item.selectboxItemPrefix);
		$(item.selectboxDropdown.selectboxSelected).removeClass('selected');
		$(item).addClass('selected');
		item.selectboxDropdown.selectboxSelected = item;
	},
	
	toggle: function(item) {
		var offset = $(item).offset();
		offset.top += $(item).height() + 3;
		
		$(item.selectboxList).css("left", offset.left+"px").css("top", offset.top+"px").toggle();
		
		if($(item.selectboxList).is(":visible")) {
			if(item.selectboxSelected != item) {
				var offset = $(item.selectboxSelected).position().top;
				$(this).scrollTop(offset - 30);
			}
		} else {
			item.selectboxList.selectboxFocus = false;
		}
	}
};

selectbox.init();

