﻿// Register the namespace
Type.registerNamespace('Samples');

// Constructor function
Samples.HighlightBehavior = function(element)
{
    // Initialize the base
    Samples.HighlightBehavior.initializeBase(this, [element]);
    
    // Initialize instance level fields
    this._originalImage = null;
    this._highlightImage = null;
}

// Prototype
Samples.HighlightBehavior.prototype = {
    // Initialize
    initialize: function() {
        Samples.HighlightBehavior.callBaseMethod(this, 'initialize');

        // Get a reference to the associated element
        var img = this.get_element();
        
        // Wire up handler for onmouseover 
        var delegate = Function.createDelegate(this, this._onMouseOver);
        $addHandler(img, 'mouseover', delegate);
        
        // Wire up handler for onmouseout
        delegate = Function.createDelegate(this, this._onMouseOut);
        $addHandler(img, 'mouseout', delegate);
    },
    
    // Dispose
    dispose: function() {
        $clearHandlers(this.get_element());

        Samples.HighlightBehavior.callBaseMethod(this, 'dispose');
    },
    
    // Properties
    get_originalImage: function() {
        return this._originalImage;
    },
    
    get_highlightImage: function() {
        return this._highlightImage;
    },
    set_highlightImage: function(value) {
        if (value != this._highlightImage)
        {
            this._highlightImage = value;
            this.raisePropertyChanged('highlightImage');
        }
    },
    
    // Event Handlers
    _onMouseOver: function(e) {
        // Get a ref to the image element
        var img = this.get_element();
        
        // Set the image
        if (img && !img.disabled && img.src != this.get_highlightImage())
        {
            // Store off the original image
            this._originalImage = img.src;
            
            // Set the source to the highlight image
            img.src = this.get_highlightImage();
        }
    },
    
    _onMouseOut: function(e) {
        // Get a ref to the image element
        var img = this.get_element();
        
        // Set the image
        if (img && !img.disabled)
        {
            // Revert to the original image
            img.src = this.get_originalImage();
        }
    }
    
}

// Register the class, implementing Behavior
Samples.HighlightBehavior.registerClass('Samples.HighlightBehavior', Sys.UI.Behavior);

// Tell the infrastructure that the script is done loading
Sys.Application.notifyScriptLoaded();
