﻿Type.registerNamespace('ContextPopup');

ContextPopup.ContextPopupBehavior = function(element) {

	ContextPopup.ContextPopupBehavior.initializeBase(this, [element]);

	this._CSSClass = this._ServicePath = this._ServiceMethod = this._image = this._divHelp = this._MouseY = this._MouseX = this._ResourceKey = null;
	this._isOnClick = false;
	this._inImage = false;
	this._TimerInterval = 1000;
}

ContextPopup.ContextPopupBehavior.prototype = {

	initialize: function() {
		ContextPopup.ContextPopupBehavior.callBaseMethod(this, 'initialize');

		if (!this._divHelp) {
			this._divHelp = document.createElement("div");
			this._divHelp.className = this._CSSClass;
			this._divHelp.style.position = "absolute";
			this._divHelp.style.display = "none";
			this.get_element().parentNode.appendChild(this._divHelp);
		}

		if (this.get_IsOnClick()) {
			$addHandler(this.get_element(), "click", Function.createDelegate(this, this.imageIn));
			$addHandler(this._divHelp, "click", Function.createDelegate(this, this.imageOut));
		}
		else {
			$addHandler(this.get_element(), "mouseover", Function.createDelegate(this, this.imageIn));
			$addHandler(this.get_element(), "mouseout", Function.createDelegate(this, this.imageOut));
		}
	},

	dispose: function() {
		ContextPopup.ContextPopupBehavior.callBaseMethod(this, 'dispose');
	},

	get_ServiceMethod: function() { return this._ServiceMethod; },
	set_ServiceMethod: function(value) { this._ServiceMethod = value; },

	get_ResourceKey: function() { return this._ResourceKey; },
	set_ResourceKey: function(value) { this._ResourceKey = value; },

	get_ServicePath: function() { return this._ServicePath; },
	set_ServicePath: function(value) { this._ServicePath = value; },

	get_TimerInterval: function() { return this._TimerInterval; },
	set_TimerInterval: function(value) { this._TimerInterval = value; },

	get_CSSClass: function() { return this._CSSClass; },
	set_CSSClass: function(value) { this._CSSClass = value; },

	get_IsOnClick: function() { return this._isOnClick; },
	set_IsOnClick: function(value) { this._isOnClick = value; },

	imageOut: function(e) {
		if (e) {
			this._inImage = false;
			this._divHelp.style.display = "none";
		}
	},
	imageIn: function(e) {
		if (e) {
			if (this._inImage) {
				this.imageOut(e);
			}
			else {
				this._inImage = true;
				var position = Sys.UI.DomElement.getBounds(e.target);
				var div_position = Sys.UI.DomElement.getBounds(this._divHelp);
				if ((position.x + position.width + div_position.width) >= document.body.clientWidth)
					this._MouseX = document.body.clientWidth - div_position.width;
				else
					this._MouseX = position.x + position.width;

				this._MouseY = position.y;
				if (this._divHelp.innerHTML == "")
					setTimeout(Function.createDelegate(this, this.showTip), this._TimerInterval);
				else
					this.withoutServiceShowing(); //setTimeout(Function.createDelegate(this, this.withoutServiceShowing), this._TimerInterval);
			}
		}
	},
	showTip: function() {
		if (this._inImage) {
			var text = this.get_element().value;
			Sys.Net.WebServiceProxy.invoke(this.get_ServicePath(), this.get_ServiceMethod(), false,
						{ contextKey: this._ResourceKey },
						Function.createDelegate(this, this.cllbck),
						Function.createDelegate(this, this.err),
						text);
		}
	},

	cllbck: function(result) {
		if (this._inImage) {
			this._divHelp.innerHTML = result;
			this.showDiv();
		}
	},

	showDiv: function() {
		this._divHelp.style.left = this._MouseX + "px";
		//Sys.UI.DomElement.setLocation(this._divHelp, this._MouseX, this._MouseY);
		this._divHelp.style.display = "block";
	},

	withoutServiceShowing: function() {
		if (this._inImage) this.showDiv();
	},

	err: function(res) {
		alert(res.get_message());
	}
}

ContextPopup.ContextPopupBehavior.registerClass('ContextPopup.ContextPopupBehavior', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();