/**
 * Checkbox management.
 *
 * @author Christian Hansen <christian@resource-it.dk>
 * @version 1.0
 * @package Resource it Modules
 * @copyright Resource it ApS
 *
 **/


/*===================================
  CLASS Checkbox
  ===================================*/

    /**
     * Checkbox Constructor.
     * @param string label      - the initial Checkbox label
     * @param string checked    - [optional] true if checked else false - default it's unchecked
     **/
    function Checkbox ( label, checked ) {
        this.iLabel = label;
        this.node = document.createElement("div");
        this.node.style.padding = "0px";
        this.node.style.margin = "0px";
        this.node.style.width = "auto";
        this.node.style.overflow = "visible";

        this.checkbox = document.createElement("input");
        this.checkbox.type = "checkbox";

        this.label = document.createElement("span");
        this.label.style.whiteSpace = "nowrap";
        this.label.innerHTML = this.iLabel;
        this.label.style.cursor = "pointer";
        this.label.style.fontFamily = resExt.theme.widgets.globals.fontFamily;
        this.label.style.fontSize = resExt.theme.widgets.globals.fontSize + "px";

        var tbl = document.createElement("table");
            tbl.cellspacing = "0px";
            tbl.cellpadding = "0px";
            tbl.style.border = "none";
            tbl.style.borderSpacing = "0px";
            tbl.style.marginLeft = tbl.style.marginRight = "4px";

        var tbody = document.createElement("tbody");
        var tr = document.createElement("tr");
        var tdcheckbox = document.createElement("td");
            tdcheckbox.appendChild(this.checkbox);
        var tdlabel = document.createElement("td");
            tdlabel.appendChild(this.label);
        
        tr.appendChild(tdcheckbox);
        tr.appendChild(tdlabel);
        tbody.appendChild(tr);
        tbl.appendChild(tbody);
        this.node.appendChild(tbl);

        resExt.addEventHandler(this.checkbox,"click",function() {
            if ( this.checkbox.checked ) {
                if ( this.onchecked !== undefined ) this.onchecked();
            } else {
                if ( this.onchecked !== undefined ) this.onunchecked();
            }//else
        }.bind(this));

        this.checkbox.checked = checked;

    }//Checkbox


    /**
     * Sets the Checkboxs label. If no argument is supplied the label is restored to its initial value.
     * @param string label  - the desired label.
     **/
    Checkbox.prototype.setLabel = function( label ) {
        if ( label !== undefined ) {
            this.label.innerHTML = label;
        } else {
            this.label.innerHTML = this.iLabel;
        }//else
    }//setLabel


    Checkbox.prototype.setMode = function( mode ) {
        mode = mode !== undefined ? mode : "active";
        switch ( mode ) {
            case "load":
                this.disable();
                this.loader();
                break;
            default:
                clearTimeout(this.loaderTimer);
                this.enable();
                this.icon.innerHTML = "";
                this.icon.appendChild(this.iIcon);
        }//switch 
    }//setMode


    Checkbox.prototype.loader = function ( number ) {

        //preload loader images
        if ( this.loaderImages === undefined ) {
            this.loaderImages = new Array();

            for ( var c = 0; c < resExt.theme.widgets.Checkbox.loaderImages.length; c++ ) {
                this.loaderImages[c] = resExt.image({src:resExt.theme.paths.gfx + resExt.theme.widgets.Checkbox.loaderPath + resExt.theme.widgets.Checkbox.loaderImages[c],width:16,height:16});
            }//for

        }//if 

        if ( this.node.disabled  ) {

            if ( number !== undefined ) number = ++number >= resExt.theme.widgets.Checkbox.loaderImages.length ? 0 : number;
            else number = 0;
            this.icon.innerHTML = "";
            this.icon.appendChild(this.loaderImages[number]);
            if ( this.node.disabled ) this.loaderTimer = setTimeout(function() {
                this.loader(number);
            }.bind(this,number),60);

        }//if

    }//loader


    /**
     * Sets the Checkbox style
     * @param string style - [optional] the prefered style of this Checkbox.
     **/
    Checkbox.prototype.setStyle = function(style) {

        switch(style) {
            case "link":
                for ( var style in resExt.theme.widgets.Checkbox.link.Checkbox ) {
                    this.node.style[style] = resExt.theme.widgets.Checkbox.link.Checkbox[style];
                }
                for ( var style in resExt.theme.widgets.Checkbox.link.label ) {
                    this.label.style[style] = resExt.theme.widgets.Checkbox.link.label[style];
                }
                break;
            case "Checkbox":
            default:
        }//switch

    }//setStyle


    Checkbox.prototype.setId = function(id) {
        this.node.id = id;
    }//setId
    
    Checkbox.prototype.enable = function() {
        this.node.disabled = false;
    }//enable

    Checkbox.prototype.disable = function() {
        this.node.disabled = "disabled";
    }//disable

/*===================================
  Events
  ===================================*/

    Checkbox.prototype.checked = function(action) {
        this.onchecked = action;
    }//checked

    Checkbox.prototype.unchecked = function(action) {
        this.onunchecked = action;
    }//checked

