/* ------------------------------------------------------------------------
	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: 15,
					checkboxHeight: 15,
					className : 'prettyCheckbox',
					display: 'list'
				}, settings);

		$(this).each(function(){
			// Find the label
			$label = $('label[for="'+$(this).attr('id')+'"]');

			// Add the checkbox holder to the label
			$label.wrapInner("<span class='labelTextHolder'><span class='labelTextHolder-inner'><span></span></span></span>");
			$label.prepend("<span class='holderWrapHelper'><span class='holderWrap'><span class='holder'></span></span></span>");
			

			// If the checkbox is checked, display it as checked
			if($(this).is(':checked')) {
				$label.addClass('checked');
				if($(this).parents("ul").attr("class") == "listFldsA") {
					$(".listFldsA li").removeClass("selectedFld");
					$(this).parents("li").addClass("selectedFld");
				}
			};

			// 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);
			var h1 = $label.find('span.holderWrap').height();
			var h2 = $label.find('span.labelTextHolder').height();
		
			if ((h2 > h1) && ($(this).parents("ul").attr("class") == "listFldsA")) {
				$label.find('span.holderWrapHelper').css({
					paddingTop: (h2 / 2) - (settings.checkboxHeight / 2)
				});
				$label.parents("li").find('span.price').css({
					paddingTop: (h2 / 2) - (settings.checkboxHeight / 2)
				});
			}
			

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

			// Associate the click event
			$label.bind('click',function(){
				$('input#' + $(this).attr('for')).triggerHandler('click');
				
				if($('input#' + $(this).attr('for')).is(':checkbox')){
					$(this).toggleClass('checked');
					$('input#' + $(this).attr('for')).attr("checked", true);
					
					$(this).find('span.holder').css('top',0);
					
				}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.attr("checked", true);
					
					//
					
					if($(this).parents("ul").attr("class") == "listFldsA") {
						$(".listFldsA li").removeClass("selectedFld");
						$(this).parents("li").addClass("selectedFld");
					}
				};
				return false;
			});
			
			$label.parents("li").bind('click',function(){
				$('input#' + $(this).find("label").attr('for')).triggerHandler('change');
				
				if($('input#' + $(this).find("label").attr('for')).is(':checkbox')){
					$(this).find("label").toggleClass('checked');
					$('input#' + $(this).find("label").attr('for')).attr("checked", true);
					
					$(this).find("label").find('span.holder').css('top',0);
					
				}else{
					$toCheck = $('input#' + $(this).find("label").attr('for'));

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

					$(this).find("label").addClass('checked');
					$toCheck.attr("checked", true);
					
					//
					
					if($(this).find("label").parents("ul").attr("class") == "listFldsA") {
						$(".listFldsA li").removeClass("selectedFld");
						$(this).find("label").parents("li").addClass("selectedFld");
					}
				};
				return false;
			});
			
			$('input#' + $label.attr('for')).bind('keypress',function(e){
				if(e.keyCode == 32){
					if($.browser.msie){
						$('label[for="'+$(this).attr('id')+'"]').toggleClass("checked");
					}else{
						$(this).trigger('click');
					}
					return false;
				};
			});
		});
	};
	
	checkAllPrettyCheckboxes = function(caller, container){
		if($(caller).is(':checked')){
			// Find the label corresponding to each checkbox and click it
			$(container).find('input[type=checkbox]:not(:checked)').each(function(){
				$('label[for="'+$(this).attr('id')+'"]').trigger('click');
				if($.browser.msie){
					$(this).attr('checked','checked');
				}else{
					$(this).trigger('click');
				};
			});
		}else{
			$(container).find('input[type=checkbox]:checked').each(function(){
				$('label[for="'+$(this).attr('id')+'"]').trigger('click');
				if($.browser.msie){
					$(this).attr('checked','');
				}else{
					$(this).trigger('click');
				};
			});
		};
	};
