$.fn.selectbox = function() { 
	
	var select = this;
	
	// hide current Object
	$(select).hide();
	
	if($(select).parent('.custom-selectbox').length > 0) {
	    // reset form if alreadt customized
	    $(select).parent('.custom-selectbox').find('.selectbox-value').remove();
	    $(select).parent('.custom-selectbox').find('.selectbox-options').remove();
	    $(select).unwrap();
	}
	
	$(select).wrap('<div class="custom-selectbox"></div>');
	
	// attach event
	$(select).parent().click(function(){
		$(this).find('.selectbox-options').toggle();
	});
	
	// create custom options
	var customOptions = '';
	var defaultVal = '';
	var defaultValFlag = 0;
	$(select).find('option').each(function(){
		
		var value = $(this).attr('value');
		if(!value) {
			value = $(this).text();
		}
		
		if(defaultValFlag == 0) {
			customOptions = customOptions + '<li style="display:none" val="' + value + '">' + $(this).text() + '</li>';
			defaultVal = $(this).text();
			defaultValFlag = 1;
		} else {
			customOptions = customOptions + '<li val="' + value + '">' + $(this).text() + '</li>';
		}
	});
	customOptions = '<ul class="selectbox-options">' + customOptions + '</ul>';
	
	// create new value div
	var valueContainer = '<div class="selectbox-value">' + defaultVal + '</div>';
	
	// create custom select box
	var customSelectBox = valueContainer + customOptions;
	$(select).parent().append(customSelectBox);	
	
	$(select).parent().find('.selectbox-options li').each(function(){
		$(this).click(function(){
			var optvalue = $(this).attr('val'); 	
			if(!optvalue) {
				optvalue = $(this).text();
			}
			var selectedVal = $(this).text();
			$(select).parent().find('.selectbox-value').text(selectedVal);
			
			$(select).val(optvalue);
			
			$(select).parent().find('.selectbox-options li').each(function(){
				if($(this).text() == selectedVal) {
					$(this).hide();
				} else {
					$(this).show();
				}
			});
			
		});
	});
}


$(document).ready( function() {
	$('.selectbox').each(function(){
		//$(this).selectbox();
	});
});
