/* ------------------------------------------------------------------------
	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.
------------------------------------------------------------------------- */
	var lastCheckboxGroupId;	
	jQuery.fn.prettyCheckboxes = function(options) {
		defaults = jQuery.extend({
					checkboxWidth: 14,
					checkboxHeight: 14,
					checkboxHolderWidth: 16,
					checkboxHolderHeight: 16,
					className : 'prettyCheckbox',
					display: 'list',
					id:'selection'
				}, settings);
		var settings = jQuery.extend(defaults, options); //replaces defaults with any parameters passed on				
		if(lastCheckboxGroupId == 'paymentMethodSelection' && settings.id == 'paymentMethodSelection') {
			lastCheckboxGroupId	= null;
			return;
		}
		lastCheckboxGroupId = settings.id;
		
		var $j = jQuery.noConflict(); 
		$j(this).each(function(){
			// Find the label
			$label = $j('label[for="'+$j(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($j(this).is(':checked')) { $label.addClass('checked'); };

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

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

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

			// Associate the click event
			$label.bind('click',function(){
				$j('input#' + $j(this).attr('for')).triggerHandler('click');
				
				if($j('input#' + $j(this).attr('for')).is(':checkbox')){
					$j(this).toggleClass('checked');
					$j('input#' + $j(this).attr('for')).checked = true;
					
					$j(this).find('span.holder').css('top',0);
				}else{
					$toCheck = $j('input#' + $j(this).attr('for'));

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

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