/* 20090420-1600 */
/******************************************************************************
 * Checkbox Radiobutton 2.0 - Terra Networks - Equipe Webdev
 * 2008
 * Modulo independente
 *
 * COMO UTILIZAR
 * 
 * Use a classe chk-rdo-replace nos checkboxes e radiobuttons que deseja fazer o replace
 *
 *
 * PARAMETROS
 *
 * --
 *
 ******************************************************************************/

(function($) {

	$.extend({

		// Checkbox radiobutton replace
		checkboxRadiobutton: function(e) {

			/* Radio replace */
			var rdoReplace = typeof(e) == "undefined" ? $("input:radio.chk-rdo-replace:not(.replaced)") : $(e).find("input:radio.chk-rdo-replace:not(.replaced)");

			rdoReplace.each(function() {
				$(this).addClass("replaced");
				$(this).css("opacity","0");
				$(this).css("display","none");
				$(this).before($(this).attr("checked") ? "<span class='rdo-replace rdo-checked'>Radio</span>" : "<span class='rdo-replace'>Radio</span>");
				$(this).prev().click(function() {
					$(this).next().attr("checked","checked");
					$(this).next().change();
				});
				
				$(this).change(function() {
					rdoClick($(this).attr("name"));
				});

				// IE Fix
				$(this).click(function() {
					rdoReplace.change();
				});
			});

			function rdoClick(name) {
				$("input:radio.chk-rdo-replace[name='"+name+"']").each(function() {
					if ($(this).attr("checked")) {
						$(this).prev().addClass("rdo-checked");
					} else {
						$(this).prev().removeClass("rdo-checked");
					}
				});
			}
			/* // Radio replace */


			/* Checkbox replace */
			var chkReplace = typeof(e) == "undefined" ? $("input:checkbox.chk-rdo-replace:not(.replaced)") : $(e).find("input:checkbox.chk-rdo-replace:not(.replaced)");

			chkReplace.css("opacity","0");
			chkReplace.css("display","none");
			chkReplace.each(function() {
				$(this).addClass("replaced");

				$("label[for='"+$(this).attr("id")+"']").removeAttr("for");
				if ($(this).attr("checked")) {
					$(this).before("<span class='chk-replace chk-checked'>Checkbox</span>");
				} else {
					$(this).before("<span class='chk-replace'>Checkbox</span>");
				}
			});

			$(".chk-replace, input:checkbox + label").click(function() {
				// objCheckbox
				var objCheckbox = null;
				if ($(this).is("label")) {
					objCheckbox = $(this).prev();
				} else {
					objCheckbox = $(this).next();
				}

				if (objCheckbox.attr("checked")) {
					objCheckbox.removeAttr("checked");
					objCheckbox.prev().removeClass("chk-checked");
				} else {
					objCheckbox.attr("checked","checked");
					objCheckbox.prev().addClass("chk-checked");
				}
			});
			/* // Checkbox replace */
		}
	});
})(jQuery);

// $.checkboxRadiobutton();