var StyledCheckBoxes = new Array();

jQuery(window).load(function() {
    jQuery('input[type="checkbox"]').each(function(i) {
        if (jQuery(this).attr("normality") == null)
            StyledCheckBoxes[i] = new StyledCheckboxField(jQuery(this));
    });
});

//DFH 2009-11-25 Re-styles check boxes when UpdatePanels reload (nuking the styles):
$(document).ready(function() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender, args) {
        for (var i = 0; i < sender._updatePanelClientIDs.length; i++) {
            jQuery('#' + sender._updatePanelClientIDs[i] + ' input[type="checkbox"]').each(function() {
                for (i = 0; i < StyledCheckBoxes.length; i++) {
                    if (StyledCheckBoxes[i].id == jQuery(this).attr("id"))
                        StyledCheckBoxes[i] = new StyledCheckboxField(jQuery(this));
                }
            });
        }
    });
});



function StyledCheckboxField(element) {
    // store real checkbox element and id for access later
    this.checkbox = element;
    this.checkboxID = element.attr("id");
    this.checkboxName = element.attr("name");
	
    // store wrapper id
    this.wrapperID = "#" + this.checkboxName + '_' + this.checkboxID;
	
    // using the name from the old check box create new id and a label id
    this.newCheckboxID = "#" + this.checkboxID + 'Pretty';
    this.newLabelID    = "#" + this.checkboxID + 'Label';
	
    // check for label
    if (!jQuery(this.newLabelID)) {
        alert("No label on checkbox: " + this.checkboxID);
    }
	
    // hide the old ugly checkbox
    this.checkbox.css('display', 'none');
	
    // create and store the new pretty checkbox
    var newCheck = jQuery('<div></div>').attr({
        'id': this.newCheckboxID,
        'className': 'newCheckbox'
    });
    this.newCheckbox = newCheck;
	
    jQuery(this.checkbox).after(this.newCheckbox);
	
    this.setInitialStyles();
	
    // get checkboxHolder and insert pretty checkbox
    jQuery(this.wrapperID).prepend(this.newCheckbox);
	
    this.checked = false;
    if (jQuery(this.checkbox).attr('checked') == true) {
        this.check();
    }
	
    this.addEventHandlers();
}

StyledCheckboxField.method('toggle', function() {
    if (this.checked) {
        this.unCheck();
    } else {
        this.check();
    }
});

StyledCheckboxField.method('check', function() {
    if (!this.checked) {
        this.newCheckbox.css('background-position', 'left -24px');
        //jQuery('#' + this.checkboxID).attr('checked', true);
        this.checked = true;
    }
});

StyledCheckboxField.method('unCheck', function() {
    if (this.checked) {
        this.newCheckbox.css('background-position', 'left 0px');
        //jQuery('#' + this.checkboxID).removeAttr('checked');
        this.checked = false;
    }
});

StyledCheckboxField.method('addEventHandlers', function() {
    var _this = this;
    this.newCheckbox.click(function() {
        _this.toggle();
        jQuery('#' + _this.checkboxID).trigger('click');
    });
    jQuery(this.newLabelID).click(function() {
        _this.toggle();
        jQuery('#' + _this.checkboxID).trigger('click');
    });
});

StyledCheckboxField.method('setInitialStyles', function() {
    this.newCheckbox.css({
        background: 'transparent url(/images/checkbox.png) left top no-repeat',
        cursor: 'pointer',
        height: '24px',
        width: '21px'
    });
	
    jQuery(this.newLabelID).css({
        cursor: 'pointer'
    });
});
