﻿// Common.js : common elements not specific to any one TNRIS website

/***********************************************************************************************
* Toggle Button Class - create a toggle button out of any clickable element, executes callbacks
*   onClass/offClass: css classes to assign to the element depending on the current state
*   toggleState: function called after click - returns new state of toggle (may return existing state to disable toggle)
***********************************************************************************************/
var ToggleButton = Class.create();
ToggleButton.prototype={
    initialize: function(element, _options) {
        this.element = $(element);
        var options = $H({onClass: "toggleOn", offClass: "toggleOff"}).merge(_options);
        this.onClass = options.get('onClass');
        this.offClass = options.get('offClass');
        this.toggleState = options.get('toggleState');
        this.element.observe("click", this.onClick.bind(this));

        this.applyStyles(options.get('value'));        
    },
    
    applyStyles: function(state) {
        if (state) {
            this.element.removeClassName(this.offClass);
            this.element.addClassName(this.onClass);
        } else {
            this.element.removeClassName(this.onClass);
            this.element.addClassName(this.offClass);
        }
    },
    
    onClick: function() {
        var state = this.toggleState();
        this.applyStyles(state);
    }
};

if (typeof(Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }

