/* ------------------------------------------------------------------------
	prettyCheckboxes
	
	Developped By: Stephane Caron (http://www.no-margin-for-errors.com)
	Inspired By: All the non user friendly custom checkboxes solutions ;)
	Version: 1.0.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: 26,
        checkboxHeight: 25,
        className: 'prettyCheckbox',
        display: 'list'
    }, settings);

    $(this).each(function() {
        // Find the label
        $label = $('label[for="' + $(this).attr('id') + '"]');

        //        if  ($(this).attr('disabled')==true) {
        //            alert($(this));
        //        }
        ////        alert($(this).attr('disabled'));
        //BNY        disablePrettyCheckboxes($(this));

        // MB: bail out if this checkbox already have been prettyfied
        if ($label.hasClass(settings.className)) {
            //Om själva input checkboxen (som ligger bakom) är disablad,
            //ska en utgråad checkbox-bild synas    $(this).attr('disabled')
            //if ($('input#' + $(this).attr('for')).attr('disabled') == true) {
            if ($(this).attr('disabled') == true) {
                $label.find('span.holder').addClass('holderDisabled');
            }
            else {
                $label.find('span.holderDisabled').removeClass('holderDisabled');
            }        
        
            return;
        }

        // Add the checkbox holder to the label
        //        if ($(this).attr('disabled') == false) {

        $label.wrapInner("<span class='labelWrap' />");

        $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');

        //Om själva input checkboxen (som ligger bakom) är disablad,
        //ska en utgråad checkbox-bild synas    $(this).attr('disabled')
        //if ($('input#' + $(this).attr('for')).attr('disabled') == true) {
        if ($(this).attr('disabled') == true) {
            $label.find('span.holder').addClass('holderDisabled');
        }
        else {
            $label.find('span.holderDisabled').removeClass('holderDisabled');
        }


        // Associate the click event
        $label.bind('click', function() {
            //Om själva input checkboxen (som ligger bakom) är disablad, 
            //ska en utgråad checkbox-bild synas OCH det skall inte gå att klicka på checkboxen
            if ($('input#' + $(this).attr('for')).attr('disabled') == true) {
//B                $(this).find('span.holder').addClass('holderDisabled');
//                $(this).find('span.holder').removeClass('holder');
//                alert("jajaja1 " + $(this));
                return;
            }
            else {
//                $label.find('span.holderDisabled').addClass('holder');
//B                $label.find('span.holderDisabled').removeClass('holderDisabled');
            }

            $('input#' + $(this).attr('for')).triggerHandler('click');

            if ($('input#' + $(this).attr('for')).is(':checkbox')) {
                $(this).toggleClass('checked');
                $('input#' + $(this).attr('for')).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;
            };
        });

        $('input#' + $label.attr('for')).bind('keypress', function(e) {

            //Om själva input checkboxen (som ligger bakom) är disablad, 
            //ska en utgråad checkbox-bild synas OCH det skall inte gå att klicka på checkboxen
            if ($('input#' + $(this).attr('for')).attr('disabled') == true) {
                return;
            }
            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');
				};
			});
		};
};

function disablePrettyCheckboxes(ctrl) {
    if (ctrl.attr('disabled') == true) {
        $label = $('label[for="' + $(ctrl).attr('id') + '"]');
        $label.visible = false;
        alert(ctrl);
        alert($label);
//        $label.find('span.holderWrap').width(2).height(4);
    }
}