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


/*===================================
  CLASS Button
  ===================================*/

    /**
     * Button Constructor.
     * @param string label  - the initial button label
     * @param string icon   - [optional] the default icon for this button
     **/
    function Button ( label, icon ) {
        this.iLabel = label;
        this.path = resExt.theme.paths.gfx + "button/16x16/";
        this.node = document.createElement("button");
        this.node.style.padding = "0px";
        this.node.style.margin = "0px";
        this.node.style.width = "auto";
        this.node.style.overflow = "visible";

        //this.node.type = "button";

        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.button.button.fontFamily;
        this.label.style.fontSize = resExt.theme.widgets.button.button.fontSize + "px";

        if ( icon !== undefined ) {
            this.iIcon = resExt.image({src:this.path + icon,width:16,height:16});
            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";
        } else {
            this.iIcon = resExt.image({src:this.path + resExt.theme.widgets.button.defaultIcon,width:1,height:16});
            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";
        }//else

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

    }//Button


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


    /**
     * Sets the button icon - if no argument is supplied the icon is restored to its initial value
     * @param string icon   - the desired icon
     **/
    Button.prototype.setIcon = function ( icon ) {
        if ( !this.node.disabled ) {
            clearTimeout(this.loaderTimer);
            var newicon = resExt.image({src:this.path + icon,width:16,height:16});
            this.icon.innerHTML = "";
            this.icon.appendChild(newicon);
        }//if
    }//setIcon


    Button.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


    Button.prototype.loader = function ( number ) {

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

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

        }//if 

        if ( this.node.disabled  ) {

            if ( number !== undefined ) number = ++number >= resExt.theme.widgets.button.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 button style
     * @param string style - [optional] the prefered style of this button.
     **/
    Button.prototype.setStyle = function(style) {

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

    }//setStyle

    Button.prototype.setWidth = function(width, mode) {
        this.node.style.width = width + "px";
        if ( mode !== undefined ) this.node.style.overflow = "hidden";
    }//setWidth

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

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

    Button.prototype.isenabled = function() {
        return !this.node.disabled;
    }//isenabled
