﻿// JScript File
Type.registerNamespace("EteachUI");

// Constructor
EteachUI.HoverPopup = function(element) {
	EteachUI.HoverPopup.initializeBase(this, [element]);

	this._servicePath = null;
	this._serviceMethod = null;
	this._contextKey = null;
	this._targetElem = null;
	this._onFocusDelegate = null;
	this._onBlurDelegate = null;
	this._over = false;
	this._overTarget = false;
	this._showTimer = 0;
	this._hideTimer = 0;

	this._callID = 0;
	this._currentCallID = -1;
}

EteachUI.HoverPopup.prototype = {

	initialize: function() {
		EteachUI.HoverPopup.callBaseMethod(this, 'initialize');

		$addHandlers(this.get_element(), 
			{ 'focus' : this._onFocus,
			'mouseover' : this._onFocus,
			'blur' : this._onBlur,
			'mouseout' : this._onBlur },
			this);
	},

	dispose: function() {
		if (this._targetElem) {
			if (this._onFocusDelegate) {
				Sys.UI.DomEvent.removeHandler(this._targetElem, 'focus', this._onFocusDelegate);
				Sys.UI.DomEvent.removeHandler(this._targetElem, 'mouseover', this._onFocusDelegate);
				delete this._onFocusDelegate;
			}

			if (this._onBlurDelegate) {
				Sys.UI.DomEvent.removeHandler(this._targetElem, 'blur', this._onBlurDelegate);
				Sys.UI.DomEvent.removeHandler(this._targetElem, 'mouseout', this._onBlurDelegate);
				delete this._onBlurDelegate;
			}
		}

		$clearHandlers(this.get_element());

		EteachUI.HoverPopup.callBaseMethod(this, 'dispose');
	},

	show: function() {
		if (this._targetElem === null) {
			this._targetElem = document.createElement("div");
			Sys.UI.DomElement.addCssClass(this._targetElem, 'hoverpanel');
			document.body.appendChild(this._targetElem);

			if (this._onFocusDelegate === null) {
				this._onFocusDelegate = Function.createDelegate(this, this._onFocusTarget);
			}
			Sys.UI.DomEvent.addHandler(this._targetElem, 'mouseover', this._onFocusDelegate);
			Sys.UI.DomEvent.addHandler(this._targetElem, 'focus', this._onFocusDelegate);

			if (this._onBlurDelegate === null) {
				this._onBlurDelegate = Function.createDelegate(this, this._onBlurTarget);
			}
			Sys.UI.DomEvent.addHandler(this._targetElem, 'mouseout', this._onBlurDelegate);
			Sys.UI.DomEvent.addHandler(this._targetElem, 'blur', this._onBlurDelegate);
		}

		this._targetElem.style.display = 'block';
		this._targetElem.style.position = 'absolute';
		this._targetElem.style.zIndex = '1';

		var elemLoc = Sys.UI.DomElement.getLocation(this.get_element());
		var elemBnd = Sys.UI.DomElement.getBounds(this.get_element());

		Sys.UI.DomElement.setLocation(this._targetElem, Math.round(elemLoc.x + (elemBnd.width/2)), Math.round(elemLoc.y - (elemBnd.height/2) - 3));

		if (this._targetElem.innerHTML.length == 0) {
			this._targetElem.innerHTML = '<div class="hoverpanel-head" id="hoverpanel-head"><h2>Loading...</h2></div><div class="hoverpanel-body" align="center" id="hoverpanel-body"><img src="/Images/waiting.gif" /></div>';

			if (this._servicePath && this._serviceMethod) {
				this._currentCallID = ++this._callID;
				Sys.Net.WebServiceProxy.invoke(this._servicePath, this._serviceMethod, false,
					{ contextKey:this._contextKey },
					Function.createDelegate(this, this._onMethodComplete), Function.createDelegate(this, this._onMethodError),
					this._currentCallID);
			}
		}
	},

	hide: function() {
		if (this._targetElem) {
			this._targetElem.style.display = 'none';
			this._targetElem.innerHTML = '';
		}
	},

	//
	// Event delegates
	//

	_onMethodComplete : function (result, userContext, methodName) {
		// ignore if it's not the current call.
		if (userContext != this._currentCallID) return;

		// Time has passed; make sure the element is still accessible
		var e = this._targetElem;
		if (e) {
			e.innerHTML = result.Html;
			if (result.Script.length > 0) {
				eval(result.Script);
			}
		}

		this._currentCallID = -1;
	},

	_onMethodError : function(webServiceError, userContext, methodName) {
		// ignore if it's not the current call.
		if (userContext != this._currentCallID) return;

		var e = this._targetElem;
		if (e) {
			if (webServiceError.get_timedOut()) {
				e.innerHTML = '<div class="hoverpanel-head" id="hoverpanel-head"><h2>Error</h2></div><div class="hoverpanel-body" align="center" id="hoverpanel-body">' + AjaxControlToolkit.Resources.DynamicPopulate_WebServiceTimeout + '</div>';
			} else {
				e.innerHTML = '<div class="hoverpanel-head" id="hoverpanel-head"><h2>Error</h2></div><div class="hoverpanel-body" align="center" id="hoverpanel-body">' + String.format(AjaxControlToolkit.Resources.DynamicPopulate_WebServiceError, webServiceError.get_statusCode()) + '</div>';
			}
		}

		this._currentCallID = -1;
	},

	_onFocus: function(event) {
		this._over = true;

		if (this._hideTimer != 0) {
			window.clearTimeout(this._hideTimer);
			this._hideTimer = 0;
		}
		if (this._showTimer != 0) {
			window.clearTimeout(this._showTimer);
			this._showTimer = 0;
		}

		this._showTimer = window.setTimeout("$find('" + this.get_id() + "')._onTimerShow()", 500);
	},

	_onBlur: function(event) {
		this._over = false;

		if (!this._over && !this._overTarget) {
			if (this._hideTimer != 0) {
				window.clearTimeout(this._hideTimer);
				this._hideTimer = 0;
			}
			if (this._showTimer != 0) {
				window.clearTimeout(this._showTimer);
				this._showTimer = 0;
			}

			this._hideTimer = window.setTimeout("$find('" + this.get_id() + "')._onTimerHide()", 1000);
		}
	},

	_onFocusTarget: function(event) {
		this._overTarget = true;

		if (this._hideTimer != 0) {
			window.clearTimeout(this._hideTimer);
			this._hideTimer = 0;
		}
		if (this._showTimer != 0) {
			window.clearTimeout(this._showTimer);
			this._showTimer = 0;
		}
	},

	_onBlurTarget: function(event) {
		this._overTarget = false;

		if (!this._over && !this._overTarget) {
			if (this._hideTimer != 0) {
				window.clearTimeout(this._hideTimer);
				this._hideTimer = 0;
			}
			if (this._showTimer != 0) {
				window.clearTimeout(this._showTimer);
				this._showTimer = 0;
			}

			this._hideTimer = window.setTimeout("$find('" + this.get_id() + "')._onTimerHide()", 1000);
		}
	},

	_onTimerShow: function() {
		if (this._showTimer != 0) {
			window.clearTimeout(this._showTimer);
			this._showTimer = 0;
		}
		if (this._hideTimer != 0) {
			window.clearTimeout(this._hideTimer);
			this._hideTimer = 0;
		}

		if (this._over || this._overTarget) {
			this.show();
		}
	},

	_onTimerHide: function() {
		if (this._hideTimer != 0) {
			window.clearTimeout(this._hideTimer);
			this._hideTimer = 0;
		}
		if (this._showTimer != 0) {
			window.clearTimeout(this._showTimer);
			this._showTimer = 0;
		}

		if (!this._over && !this._overTarget) {
			this.hide();
		}
	},

	//
	// Control properties
	//

	get_ServicePath : function() {
		return this._servicePath;
	},
	set_ServicePath : function(value) {
		if (this._servicePath != value) {
			this._servicePath = value;
			this.raisePropertyChanged('ServicePath');
		}
	},

	get_ServiceMethod : function() {
		return this._serviceMethod;
	},
	set_ServiceMethod : function(value) {
		if (this._serviceMethod != value) {
			this._serviceMethod = value;
			this.raisePropertyChanged('ServiceMethod');
		}
	},

	get_ContextKey : function() {
		return this._contextKey;
	},
	set_ContextKey : function(value) {
		if (this._contextKey != value) {
			this._contextKey = value;
			this.raisePropertyChanged('ContextKey');
		}
	}
}

EteachUI.HoverPopup.registerClass('EteachUI.HoverPopup', Sys.UI.Control);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
