﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

Type.registerNamespace("ZikBay.Web.UI.Controls");

// --------------------------------------------------------------
// HoverImageExtender class, derived from Sys.Sys.UI.Control
// --------------------------------------------------------------

ZikBay.Web.UI.Controls.HoverImageExtender = function(element) {
    //#region Constructor
    ZikBay.Web.UI.Controls.HoverImageExtender.initializeBase(this, [element]);
    
    this._tooltipBehaviorID = null;
    this._hoverImageUrl = "";
    this._hoverImageTitle = "";
    this._progressImageUrl = "";
    
    this._hoverImage = null;
    this._progressImage = null;
        
    this._tooltip = null;
    
    this._imageLoaded = false;
    this._hoverCancelled = false;
    
    this.hoverDelegate = Function.createDelegate(this, this.hover);
    this.outDelegate = Function.createDelegate(this, this.out);
    //#endregion
}

ZikBay.Web.UI.Controls.HoverImageExtender.prototype = {
    // Overrides
    //#region
    initialize: function() {
        ZikBay.Web.UI.Controls.HoverImageExtender.callBaseMethod(this, 'initialize');
        var attr = document.createAttribute("tooltiptitle");
        attr.value = this._hoverImageTitle;
        this.get_element().attributes.setNamedItem(attr);
        // Preload progress image
        if(this._progressImageUrl && this._progressImageUrl != "") {
            this._progressImage = document.createElement("img");
            this._progressImage.src = this._progressImageUrl;
        }
        if(this._progressImage) {
            this.get_element().title = this._progressImage.src;
        }
        $addHandler(this.get_element(), "mouseover", this.hoverDelegate);
        $addHandler(this.get_element(), "mouseout", this.outDelegate);
        this.attachTooltip();
    },

    dispose: function() {
        this.detachTooltip();
        this._progressImage = null;
        this._hoverImage = null;
        ZikBay.Web.UI.Controls.HoverImageExtender.callBaseMethod(this, 'dispose');
    },
    //#endregion
    
    //#region Properties
    get_tooltipBehaviorID : function() {
        return this._tooltipBehaviorID;
    },
    set_tooltipBehaviorID : function(value) {
        this._tooltipBehaviorID = value;
    },
    get_hoverImageUrl : function() {
        return this._hoverImageUrl;
    },
    set_hoverImageUrl : function(value) {
        this._hoverImageUrl = value;
    },
    get_hoverImageTitle : function() {
        return this._hoverImageTitle;
    },
    set_hoverImageTitle : function(value) {
        this._hoverImageTitle = value;
    },
    get_progressImageUrl : function() {
        return this._progressImageUrl;
    },
    set_progressImageUrl : function(value) {
        this._progressImageUrl = value;
    },
    //#endregion
    
    //#region Private functions
    attachTooltip : function() {
        if(this._tooltipBehaviorID && this._tooltipBehaviorID!="") {
            this._tooltip = $find(this._tooltipBehaviorID);
            if(this._tooltip) {
                this._tooltip.addTooltipControl(this.get_element());
            }
        }
    },
    detachTooltip : function() {
        if(this._tooltip) {
            this._tooltip.removeTooltipControl(this.get_element());
        }
    },
    //#endregion
    
    //#region Public functions
    //#endregion
    
    //#region Event handlers
    hover : function(e) {
        if(!this._imageLoaded) {
            this._hoverCancelled = false;
            this.preload(this._hoverImageUrl);
        }
    },
    out : function(e) {
        this._hoverCancelled = true;
    },
    preload : function(url) {
        if(url && url != "") {
            this._hoverImage = document.createElement("img");
		    this._hoverImage.onload = Function.createDelegate(this, this.preloadComplete);
		    this._hoverImage.onerror = function() {};
		    this._hoverImage.src = url;
		}
    },
    preloadComplete : function(e) {
        if(!this._hoverCancelled && this._hoverImage && !this._imageLoaded) {
            var element = this.get_element();
            $removeHandler(element, "mouseover", this.hoverDelegate);
            $removeHandler(element, "mouseout", this.outDelegate);
            var tooltipTitle = "";
            element.title = this._hoverImage.src;
            this.attachTooltip();
            if(this._tooltip) {
                if(element && element.attributes && element.attributes.getNamedItem("tooltiptitle")) {
                    tooltipTitle = element.attributes.getNamedItem("tooltiptitle").value;
                }
                this._tooltip.show(element, tooltipTitle, this._hoverImage.src);
                this._tooltip._move(null);
            }
            this._imageLoaded = true;
        }
    }
    //#endregion
}
ZikBay.Web.UI.Controls.HoverImageExtender.registerClass('ZikBay.Web.UI.Controls.HoverImageExtender', Sys.UI.Behavior);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();