/**
 * Copyright (c) 2006, Sam Foster
 * All rights reserved.
 *
 * This work is licensed under the Creative Commons Attribution 2.5 License. To view a copy 
 * of this license, visit http://creativecommons.org/licenses/by/2.5/ or send a letter to 
 * Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.
 *
 * This work was created by Sam Foster (sam-i-am.com).
 * 
 * The only attribution I require is to keep this notice of copyright & license 
 * in this original source file.
 *
 * Version 0.1.0 - 10/31/2006
 *
 */

/**
 * A Toggler class, that handles switching btween 2 states, 
 * and allows a configuration to fire and handle events in 2 ways
 * the clickEventHandler method expects the actual event arguments as passed by YAHOO.util.Event.addListener
 * the onToggle event can fire to do whatever *after* the toggler has changed state
*/
function ToggleButton(elm, args) {
  this.elm = elm;
  // some defaults..
  this.initialState = "on";
  this.img = (elm.tagName.toLowerCase() == "img") ? elm : elm.getElementsByTagName("IMG")[0];
  if(this.img) {
    // the img is optional
    this.onSrc = "pause.gif";
    this.offSrc = "play.gif";
  }
  this.clickHandler = function() { 
    this.toggle() 
  };
  this.onToggle = function() {
    var msg = "default onToggle action";
  }
  // mixin the args properties
  for(var i in args) {
    this[i] = args[i];
  }
  // preload the img for the other state
  var preloader = new Image(); 
  preloader.src = (this.initialState) ? this.offSrc : this.onSrc;
  this.init();
}
ToggleButton.prototype.init = function() {
  this.currentState = this.initialState;
  var stateName = this.getStateName();
  if(this.img) {
    if(this.img.src !== this[ stateName + "Src" ]) {
      this.img.src = this[ stateName + "Src" ];
    }
  }
  
  YAHOO.util.Event.addListener(this.elm, "click", this.clickHandler, this, true); 
}

ToggleButton.prototype.getBoolState = function() {
  return this.currentState;
}

ToggleButton.prototype.getStateName = function() {
  return (this.currentState) ? "on" : "off";
}

ToggleButton.prototype.toggle = function(doCallback) {
  if(this.getBoolState()) {
    this.toggleOff(doCallback);
  } else {
    this.toggleOn(doCallback);
  }
}

ToggleButton.prototype.toggleOn = function(doCallback) {
  var lastState = this.currentState;
  this.currentState = true;
  // console.log("toggleOn called, current state is: " + this.getStateName());
  if(this.currentState !== lastState && this.img.src !== this.onSrc) {
    // only toggle if the state needs changing
    this.img.src = this.onSrc;

    // allow optional generation of the onToggle "event" (defaults true)
    var doCallback = (typeof doCallback != 'undefined') ? doCallback : true
    if(doCallback) {
      this.onToggle();
    }
  }
}
ToggleButton.prototype.toggleOff = function(doCallback) {
  var lastState = this.currentState;
  this.currentState = false;
  // console.log("toggleOff called, current state is: " + this.getStateName());
  if(this.currentState !== lastState && this.img.src !== this.offSrc) {
    // only toggle if the state needs changing
    if(this.img) {
      this.img.src = this.offSrc;
    }

    // allow generation of the onToggle "event" (defaults true)
    var doCallback = (typeof doCallback != 'undefined') ? doCallback : true
    this.currentState = false;
    if(doCallback) {
      this.onToggle();
    }
  }
}

