$(function (){
	//style selects
	$('select').each(function(index){
		$(this).wrap('<div class="selectWrapper">');
	});
	
	$('.selectWrapper').append('<div class="display"></div>');
	
	$('select').each(function(index){
		var startingOption = $(this).children('option:selected').text();
		$(this).parent().children('.display').html(startingOption);
	});
	$('.selectWrapper').append('<div class="select"></div>');
	$('option').each(function(index){
		var opt = $(this).val();
		var htm = $(this).html();
		$(this).parent().parent().children('.select').append('<div class="option" value="'+opt+'">'+htm+'</div>');
	});
	
	$('.option').each(function(index){
		$(this).click(function(){
			var htm = $(this).html();
			var opt = $(this).attr('value');
			$(this).parent().parent().children('.display').html(htm);
			$(this).parent().parent().children('select').children('option').attr('selected', false);
			$(this).parent().parent().children('select').children("option[value='"+opt+"']").attr('selected', 'selected');
			$(this).parent().hide();
			$(this).parent().parent().children('select').val(opt);
		})
	});
	$('.selectWrapper .display').click(function(){
		$(this).parent().children('.select').show();
	});
	$('.selectWrapper .select').mouseleave(function(){
		$(this).hide();
	});
	$('.selectWrapper .select').each(function(){
		var l = $(this).children().length;
		if(l>15){
			$(this).addClass('long');
		}
	});
	$('.selectWrapper select').each(function(){
		var id = $(this).attr('id');
		$(this).parent().addClass(id);
		$(this).parent().children('.display').addClass(id);
		$(this).parent().children('.select').addClass(id);
	});
	$('textarea').css('border','0px none');
	
	$('.selectWrapper .select').hide();
    
    
    //style checks and radios
	$('label.radio').click(function(){
        var forInput = $(this).attr('forInput');
        var input = $('#'+forInput);
        input.click();
		updateInputs();
	});
	$('label.checkbox').click(function(){
        var forInput = $(this).attr('forInput');
        var input = $('#'+forInput);
        
        if(input.attr('checked') == true){
            input.attr('checked', false);
        }else{
            input.attr('checked',true);
        }
		updateInputs();
	});
    $('label.radio').each(function(){
        var forInput = $(this).attr('for');
        $(this).attr('for', '');
        $(this).attr('forInput', forInput);
    });
    $('label.checkbox').each(function(){
        var forInput = $(this).attr('for');
        $(this).attr('for', '');
        $(this).attr('forInput', forInput);
    });
    
    $('input.styled').hide();
    
	$('label.radio').css('background-position','left');
	$('label.checkbox').css('background-position','left');
});
function updateInputs(){
	$('input.styled').each(function(index){
		var id = $(this).attr('id');
		$('label[forInput="'+id+'"]').removeClass('on');
	});
	$('input.styled:checked').each(function(index){
		var id = $(this).attr('id');
		$('label[forInput="'+id+'"]').addClass('on');
	});
}
$(function(){
	updateInputs();
});
