/* Copyright (C) MOR YAZILIM. http://www.moryazilim.com
 * All rights reserved. This software is published under the terms of the
 * GNU Lesser General Public License. http://www.gnu.org/licenses/lgpl.txt
 */
function AnimatedButton(node) {
  this.props = null;
  this.base = MoreMotionObject;
  this.base(node);
  this.isAnimatedButton = true;
  this.img = node;
  this.setDisabled = function(value) {
    if (value == true) {
      this.props.isDisabled = true;
      this.img.src = this.props.disabledSource;
      this.img.style.cursor = "default";
      if (this.img.onclick) {
        this.props.savedOnclick = this.img.onclick + "";
        this.img.onclick = "";
      }
    } else {
      this.props.isDisabled = false;
      this.img.src = this.props.normalSource;
      this.img.style.cursor = "pointer";
      if (this.props.savedOnclick) {
        eval("this.img.onclick = " + this.props.savedOnclick);
      }
    }
    this.saveProps();
  };
  this.setEvent = function(eventName, jsCode) {
    var s = "";
    eval("var event = this.node." + eventName);
    if (event) {
      s = event + "";
      s = s.after("{",s);
      s = s.beforeLast("}",s);
      if (s.indexOf(jsCode) > -1) return;
    }
    eval ("this.node." + eventName + " = function() { " + jsCode + s + " }");
  };
  this.setEvents = function() {
    this.setEvent("onmouseover","ABMgr.mouseOver(this);");
    this.setEvent("onmouseout","ABMgr.mouseOut(this);");
    this.setEvent("onmousedown","ABMgr.mouseDown(this);");
    this.setEvent("onmouseup","ABMgr.mouseUp(this);");
  };
  this.setSources = function() {
    this.mouseOverImage = new Image();
    this.mouseOverImage.src = this.props.mouseOverSource;
    this.mouseDownImage = new Image();
    this.mouseDownImage.src = this.props.mouseDownSource;
    this.disabledImage = new Image();
    this.disabledImage.src = this.props.disabledSource;
  };
  this.select = function() {
    this.isSelected = true;
    this.img.src = this.props.selectedSource;
  };
  this.init = function() {
    this.setEvents();
    this.setSources();
    this.setDisabled(this.boolProp("isDisabled"));
  };
};
function AnimatedButtonManager() {
  this.current = null;
  this.getButton = function(node) {
    if (!this.current || this.current != node) {
      this.current = OMgr.getObject(node);
    }
    return this.current;
  };
  this.mouseOver = function(node) {
    var b = this.getButton(node);
    if (!b.boolProp("isDisabled"))
      b.img.src = b.props.mouseOverSource;
  };
  this.mouseDown = function(node) {
    var b = this.getButton(node);
    if (!b.boolProp("isDisabled"))
      b.img.src = b.props.mouseDownSource;
  };
  this.mouseOut = function(node) {
    var b = this.getButton(node);
    if (!b.boolProp("isDisabled"))
      b.img.src = b.props.normalSource;
  };
  this.mouseUp = function(node) {
    var b = this.getButton(node);
    if (!b.boolProp("isDisabled"))
      b.img.src = b.props.normalSource;
  };
};
var ABMgr = new AnimatedButtonManager();
