/* ------------------------------------------------------------------------
	prettyCheckboxes

	Developped By: Stephane Caron (http://www.no-margin-for-errors.com)
	Inspired By: All the non user friendly custom checkboxes solutions ;)
	Version: 1.1

	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */

	jQuery.fn.prettyCheckboxes = function(settings) {
		settings = jQuery.extend({
					checkboxWidth: 14,
					checkboxHeight: 14,
					className : 'prettyCheckbox',
					display: 'list',
					cnt:'5'
				}, settings);

        //var cnt=$(':checked',this)[0];      alert(cnt);
		$(this).each(function(){

			// Find the label
			$label = $('label[for="'+$(this).attr('id')+'"]');
			// Add the checkbox holder to the label
			$label.prepend("<span class='holderWrap'><span class='holder'></span></span>");

			// If the checkbox is checked, display it as checked
			if($(this).is(':checked')) { $label.addClass('checked'); };

			// Assign the class on the label
			$label.addClass(settings.className).addClass($(this).attr('type')).addClass(settings.display);

			// Assign the dimensions to the checkbox display
			$label.find('span.holderWrap').width(settings.checkboxWidth).height(settings.checkboxHeight);
			$label.find('span.holder').width(settings.checkboxWidth);

			// Hide the checkbox
			$(this).addClass('hiddenCheckbox');

			// Associate the click event
			if($(this).is(':disabled')) { $label.addClass('disabled'); }
			else $label.bind('click',function(){
              cnt=$(this).parent().parent().parent().parent().find('input[type=checkbox]:checked').length+1;
                if(settings.cnt=='0')cnt=0;

				$('input#' + $(this).attr('for')).triggerHandler('click');

				if($('input#' + $(this).attr('for')).is(':checkbox')){
					if(cnt<=settings.cnt){
						$(this).toggleClass('checked');
						$('input#' + $(this).attr('for')).checked = true;
						$(this).find('span.holder').css('top',0);
					}else{
						$(this).removeClass('checked');
						$(this).find('span.holder').css('top',0);
						$('input#' + $(this).attr('for'))[0].checked=true;
					}
				}else{
					$toCheck = $('input#' + $(this).attr('for'));

					// Uncheck all radio
					$('input[name="'+$toCheck.attr('name')+'"]').each(function(){
						$('label[for="' + $(this).attr('id')+'"]').removeClass('checked');
					});

					$(this).addClass('checked');
					$toCheck.checked = true;
				};
			  //}
			});


		});
	};


