var $p = jQuery.noConflict();
$p(function(){
	custom_fields_remake_fields();
	custom_fields_init_radios();
	custom_fields_init_checkboxes();
	custom_fields_init_select();
});

function custom_fields_remake_fields(){
	$p("input, select, textarea").not(".styled").each(function(){
		init_custom_field(this);
	});
}

/*
** RADIO BUTTONS 
*/

	//Inicia os triggers dos radio buttons customizados
	function custom_fields_init_radios(){
		$p(".fake-radio").live("click", function(){
			if( field_is_checked(this) ){
				return false;
			}
			else{
				var groupName = $p(this).parent('.custom-radio').find("input").attr("name");
				$p(this).parents("form").find('input[name~="'+groupName+'"]:radio').each(function(){
					uncheck_field(this);
				});
				check_field(this);
			}
		});
	}

/*
** CHECKBOXES
*/

	//Inicia os triggers dos checkboxes customizados
	function custom_fields_init_checkboxes(){
		$p(".fake-checkbox").live("mousedown", function(){
			toogle_checkbox(this);
		});
	}

	function toogle_checkbox(checkbox){
		if( field_is_checked(checkbox) ){
			uncheck_field(checkbox);
		}
		else{
			check_field(checkbox);
		}
	}

	/*
	** SELECT
	*/

	function custom_fields_init_select(){
		$p("select").live("change", function(){
			var field_label = $p(this).find("option:selected");
			if($p(field_label).length){
				label_start = $p(field_label).text();
			}
			else{
				field_label = $p(this).find("option:first").text();
			}
			$p(this).parent().find(".label").val(label_start);
		});
	}

/*
** COMMON
*/

	function init_custom_field(field){
		var type = get_field_type(field);
		switch(type){
			case "checkbox":
			case "radio":
				$p(field).wrap('<div class="custom-'+type+'" />');
				$p(field).css("display", "none");
				var classChecked = field_is_checked(field) ? " checked" : "";
				$p(field).before('<span class="fake-'+type+classChecked+'"></span>');
			break;
			case "select":
				$p(field).wrap('<div class="hold-custom-select"></div>');
				$p(field).before('<input type="text" class="label" />');
				var label = $p(field).find("option:selected").text();
				$p(field).parent().find(".label").val(label);
			break;
		}
		$p(field).addClass('styled');
	}

	function get_field_type(field){
		if( $p(field).is("input") ){
			return $p(field).attr("type");
		}
		else if($p(field).is("textarea")){
			return "textarea";
		}
		else if($p(field).is("select")){
			return "select";
		}
		return false;
	}

	function field_is_checked(field){
		var check = field;
		if( 
			$p(field).attr("checked") == "checked" 
			||
			$p(field).attr("checked") == true
			||
			$p(field).is("span.checked")
		){
			return true;
		}
		else{
			return false;
		}
	}
	
	function uncheck_field(field){
		var fieldtype = $p(field).attr("type");
		if( fieldtype == "checkbox" || fieldtype == "radio" ){
			$p(field).attr("checked", false);
			if($p(field).parent().is(".custom-"+fieldtype)){
				$p(field).parent().find('span.fake-'+fieldtype).removeClass("checked");
			}
		}
		else if( $p(field).is('span.fake-checkbox') || $p(field).is('span.fake-radio') ){
			$p(field).removeClass("checked");
			$p(field).parent().find("input").attr("checked", false);
		}
	}

	function check_field(field){
		var fieldtype = $p(field).attr("type");
		if( fieldtype == "checkbox" || fieldtype == "radio" ){
			$p(radio).attr("checked", "checked");
			if($p(field).parent().is(".custom-"+fieldtype)){
				$p(field).parent(".custom-"+fieldtype).find('span.fake-'+fieldtype).addClass("checked");
			}
		}
		else if( $p(field).is('span.fake-checkbox') || $p(field).is('span.fake-radio') ){
			$p(field).addClass("checked");
			$p(field).parent().find("input").attr("checked", "checked");
		}
	}

