﻿// Register the namespace for the control.
Type.registerNamespace('Behaviors');

//Define the behavior class which receives a reference to the
//DOM elenet it is associated with.
Behaviors.ImageBehavior = function(element)
{

  //Initialize the base class, passing a reference to the DOM element.  
  Behaviors.ImageBehavior.initializeBase(this, [element]);

  // Private class properties
  this._normalImageUrl = null;
  this._hoverImageUrl = null;
  this._activeImageUrl = null;
  this._disabledImageUrl = null;
}

//Create the prototype definition of the behavior class.
Behaviors.ImageBehavior.prototype = {

  //Initialize the behavior class.
  initialize : function()
  {
  
    //Initialize the base class, passing a reference to the DOM element.  
    Behaviors.ImageBehavior.callBaseMethod(this, 'initialize');
  
    //Create the event delegates and associate them with their handlers.  
    this._onmouseoverHandler = Function.createDelegate(this, this._onMouseOver);
    this._onmouseoutHandler = Function.createDelegate(this, this._onMouseOut);
    this._ommousedownHandler = Function.createDelegate(this, this._onMouseDown);
    this._onmouseupdHandler = Function.createDelegate(this, this._onMouseUp);
    
    //Attach the event handlers to the DOM element associated with the behavior.
    $addHandlers
    (
      this.get_element(),
      { 
        'mouseover' : this._onMouseOver,
        'mouseout' : this._onMouseOut ,
        'mousedown' : this._onMouseDown,
        'mouseup' : this._onMouseUp 
      },
      this
    );
    
    //Set the initial image for the control.
    if(!this.get_element().disabled)
      this.get_element().src = this._normalImageUrl;
    else
      this.get_element().src = this._disabledImageUrl;
  },

  //Clean up the behavior class.
  dispose : function()
  {
    //Clear the behavior event handlers associated with DOM element.
    $clearHandlers(this.get_element());
    //Call the base class dispose() method.
    Behaviors.ImageBehavior.callBaseMethod(this, 'dispose');
  },

  /* Event Delegates*/
  
  //On mouse over set the hover image.
  _onMouseOver : function(e) {
    if (this.get_element() && !this.get_element().disabled) {
      this.get_element().src = this._hoverImageUrl;
    }
  },

  //On mouse out set the normal image.
  _onMouseOut : function(e) {
    if (this.get_element() && !this.get_element().disabled) {
      this.get_element().src = this._normalImageUrl;
    }
  },

  //On mouse down set the active image. 
  _onMouseDown : function(e) {
    if (this.get_element() && !this.get_element().disabled) {
      this.get_element().src = this._activeImageUrl;
    }
  },

  //On mouse up set the normal image.
  _onMouseUp : function(e) {
    if (this.get_element() && !this.get_element().disabled) {
      this.get_element().src = this._normalImageUrl;
    }
  },

  /*Properties*/

  //Property get/set for the normal image.
  get_normalImageUrl : function() {
    return this._normalImageUrl;
  },
  set_normalImageUrl : function(value) {
    if (this._normalImageUrl !== value) {
      this._normalImageUrl = value;
      this.raisePropertyChanged('normalImageUrl');
    }
  },

  //Property get/set for the hover image.
  get_hoverImageUrl : function() {
    return this._hoverImageUrl;
  },
  set_hoverImageUrl : function(value) {
    if (this._hoverImageUrl !== value) {
      this._hoverImageUrl = value;
      this.raisePropertyChanged('hoverImageUrl');
    }
  },

  //Property get/set for the active image.
  get_activeImageUrl : function() {
    return this._activeImageUrl;
  },
  set_activeImageUrl : function(value) {
    if (this._activeImageUrl !== value) {
      this._activeImageUrl = value;
      this.raisePropertyChanged('activeImageUrl');
    }
  },

  //Property get/set for the disabled image.
  get_disabledImageUrl : function() {
    return this._disabledImageUrl;
  },
  set_disabledImageUrl : function(value) {
    if (this._disabledImageUrl !== value) {
      this._disabledImageUrl = value;
      this.raisePropertyChanged('disabledImageUrl');
    }
  }

} //end prototype

// Optional descriptor for JSON serialization.
Behaviors.ImageBehavior.descriptor = 
{
    properties: [
     {name: 'normalImageUrl', type: String},
     {name: 'hoverImageUrl', type: String},
     {name: 'activeImageUrl', type: String}, 
     {name: 'disabledImageUrl', type: String} 
    ]
}

//Register the ImageBehavior class with the client AJAX library and specify
//its base class as Sys.UI.Control.
Behaviors.ImageBehavior.registerClass('Behaviors.ImageBehavior', Sys.UI.Control);

//Notify the Sys.Application class this script has been loaded.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); 