// Create nice Radio
this.NiceRadio = function() {

	if ( $("input[type=radio]").length != 0 ) {

		$("label input[type=radio]").addClass("RadioClass").css({opacity: 0}).parent().addClass("RadioLabelClass");
		$(".RadioLabelClass").contents().not("input[type=radio]").wrap("<span></span>");

		if ( $(".RadioClass:checked").length != 0 ) {
			$(".RadioClass").each(function() {
				if ( $(this).is(":checked") ) {
					$(this).parent("label").addClass("RadioSelected");
				}
			});
		}

		$(".RadioLabelClass").click(function() {
			var rName = $(this).find(".RadioClass").attr("name");

			if ( $.browser.msie && parseInt($.browser.version) == 6 ) {
				$(this).find(".RadioClass").attr("checked", true);

				$("input[name="+rName+"]").each(function() {
					if ( $(this).is(":checked") ) {
						$(this).parent().addClass("RadioSelected");
					} else {
						$(this).parent().removeClass("RadioSelected");
					}
				});
			}
				else {
			  	if ( $(this).find(".RadioClass").is(":checked") ) {
			   		$(this).addClass("RadioSelected");
					} else {
			   		$("input[name="+rName+"]").parent().removeClass("RadioSelected");
					}
				}
		});

	}

};


// Create nice Checkbox
this.NiceCheckbox = function() {

	if ( $("input[type=checkbox]").length != 0 ) {

		$("label input[type=checkbox]").addClass("CheckBoxClass").css({opacity: 0}).parent().addClass("CheckBoxLabelClass");
		$(".CheckBoxLabelClass").contents().not("input[type=checkbox]").wrap("<span></span>");

		if ( $(".CheckBoxClass:checked").length != 0 ) {
			$(".CheckBoxClass").each(function() {
				if ( $(this).is(":checked") ) {
					$(this).parent("label").addClass("LabelSelected");
				}
			});
		}

		$(".CheckBoxLabelClass").click(function() {
			if ( $.browser.msie && parseInt($.browser.version) == 6 ) {
	  	  if ( $(this).find(".CheckBoxClass").is(":not(:checked)") ) {
					$(this).find(".CheckBoxClass").attr("checked", true);
	  			$(this).addClass("LabelSelected");
	  		} else {
	  		  $(this).find(".CheckBoxClass").attr("checked", false);
	  			$(this).removeClass("LabelSelected");
	  		}
			}
			  else {
		  	  if ( $(this).find(".CheckBoxClass").is(":checked") ) {
		  			$(this).addClass("LabelSelected");
		  		} else {
		  			$(this).removeClass("LabelSelected");
		  		}
	  		}
		});

	}

};


// Select all Checkbox - uwaga zeby dzialalo calosc musi byc w "class=BoxCheckAll"
this.SelectAllCheckbox = function() {

	if ( $(".BoxCheckAll").length != 0 ) {

		$(".LabelCheckAll").each(function() {
			var AllItem = $(this).parents(".BoxCheckAll").find("input[type=checkbox]").length - 1;
			var Checked = $(this).parents(".BoxCheckAll").find("input[type=checkbox]:checked").length;

			if ( AllItem == Checked ) {
				$(this).attr("checked", true);
				if ( $(".CheckBoxClass").length != 0 ) {  //gdy -> NiceCheckbox()
					$(this).parent("label").addClass("LabelSelected");
				}
			}

			$(this).parents(".BoxCheckAll").find("label").not(".LabelCheckAll").bind("click", function() {
			  var Selected = $(this).parents(".BoxCheckAll").find("input[type=checkbox]:checked").not("input[name=CheckAll]").length;

				if ( AllItem == Selected ) {
					$(this).parents(".BoxCheckAll").find("input[name=CheckAll]").attr("checked", true);
					if ( $(".CheckBoxClass").length != 0 ) {  //gdy -> NiceCheckbox()
						$(this).parents(".BoxCheckAll").find("input[name=CheckAll]").parent("label").addClass("LabelSelected");
					}
			  } else {
					$(this).parents(".BoxCheckAll").find("input[name=CheckAll]").attr("checked", false);
					if ( $(".CheckBoxClass").length != 0 ) {  //gdy -> NiceCheckbox()
						$(this).parents(".BoxCheckAll").find("input[name=CheckAll]").parent("label").removeClass("LabelSelected");
					}
				}
			});
		});

		$(".LabelCheckAll").click( function() {
			if ( $(this).find("input[name=CheckAll]").is(":checked") ) {
				$(this).parents(".BoxCheckAll").find("input[type=checkbox]").attr("checked", true);
				if ( $(".CheckBoxClass").length != 0 ) {  //gdy -> NiceCheckbox()
					$(this).parents(".BoxCheckAll").find("label").addClass("LabelSelected");
				}
			} else {
					$(this).parents(".BoxCheckAll").find("input[type=checkbox]").attr("checked", false);
					if ( $(".CheckBoxClass").length != 0 ) {  //gdy -> NiceCheckbox()
						$(this).parents(".BoxCheckAll").find("label").removeClass("LabelSelected");
					}
				}
		});

	}

};


$(document).ready(function() {
 NiceRadio();
 NiceCheckbox();
 SelectAllCheckbox();
});
