﻿Type.registerNamespace("Nws.UI");

Nws.UI.WorkingIndicatorBehavior = function(element) {
	Nws.UI.WorkingIndicatorBehavior.initializeBase(this, [element]);
	
	this._background = null;
	this._backgroundColor = "#FFFFFF";
	this._backgroundOpacity = 0.5;
	this._beginRequestDelegate = Function.createDelegate(this, this._beginRequest);
	this._container = null;
	this._endRequestDelegate = Function.createDelegate(this, this._endRequest);
	this._indicatorImage = null;
	this._indicatorImageUrl = null;
	this._windowResizeDelegate = Function.createDelegate(this, this._windowResize);
}
Nws.UI.WorkingIndicatorBehavior.prototype = {
	get_backgroundColor : function() {
		return this._backgroundColor;
	},
	set_backgroundColor : function(value) {
		this._backgroundColor = value;
		$setStyle(this._background, "backgroundColor", value);
	},
	get_backgroundOpacity : function() {
		return this._backgroundOpacity;
	},
	set_backgroundOpacity : function(value) {
		if(value < 0)
			value = 0;
		if(value > 1)
			value = 1;
		this._backgroundOpacity = value;
		$setStyle(this._background, "opacity", value);
	},
	get_indicatorImageUrl : function() {
		return this._indicatorImageUrl;
	},
	set_indicatorImageUrl : function(value) {
		this._indicatorImageUrl = value;
		if(this.get_isInitialized())
			this._indicatorImage.src = value;
	},
	get_visible : function() {
		return ($getStyle(this._container, "visibility") !== "hidden");
	},
	set_visible : function(value) {
		if(this.get_isInitialized()) {
			$setStyle(this._container, "visibility", value ? "visible" : "hidden");
			$setStyle(this._container, "display", value ? "block" : "none");
			if(value)
				this.resetPosition();
		}
	},
	
	_beginRequest : function(sender, args) {
		if(this.get_element() === args.get_postBackElement())
			this.set_visible(true);
	},
	_endRequest : function(sender, args) {
		this.set_visible(false);
	},
	_windowResize : function() {
		if(this.get_visible())
			this.resetPosition();
	},
	dispose : function() {
		var manager = Sys.WebForms.PageRequestManager.getInstance();
		manager.remove_endRequest(this._endRequestDelegate);
		manager.remove_beginRequest(this._beginRequestDelegate);
		$removeHandler(window, "resize", this._windowResizeDelegate);
	
		Nws.UI.WorkingIndicatorBehavior.callBaseMethod(this, "dispose");
	},
	initialize : function() {
		Nws.UI.WorkingIndicatorBehavior.callBaseMethod(this, "initialize");
		
		var element = this.get_element();
		
		this._container = document.createElement("DIV");
		this._background = document.createElement("DIV");
		var indicatorDiv = document.createElement("DIV");
		this._indicatorImage = document.createElement("IMG");
		this._container.appendChild(this._background);
		indicatorDiv.appendChild(this._indicatorImage);
		this._container.appendChild(indicatorDiv);
		element.parentNode.appendChild(this._container, element);
		
		$setStyle(this._container, "display", "none");
		$setStyle(this._container, "position", "absolute");
		
		$setStyle(this._background, "position", "absolute");
		$setStyle(this._background, "left", "0");
		$setStyle(this._background, "top", "0");
		$setStyle(this._background, "width", "100%");
		$setStyle(this._background, "height", "100%");
		$setStyle(this._background, "backgroundColor", this._backgroundColor);
		$setStyle(this._background, "opacity", this._backgroundOpacity);
		
		$setStyle(indicatorDiv, "position", "absolute");
		$setStyle(indicatorDiv, "left", "50%");
		$setStyle(indicatorDiv, "top", "50%");
		
		this._indicatorImage.src = this._indicatorImageUrl;
		$setStyle(this._indicatorImage, "display", "block");
		$setStyle(this._indicatorImage, "position", "relative");
		$setStyle(this._indicatorImage, "left", "-50%");
		$setStyle(this._indicatorImage, "top", "-50%");
		
		$addHandler(window, "resize", this._windowResizeDelegate);
		var manager = Sys.WebForms.PageRequestManager.getInstance();
		manager.add_beginRequest(this._beginRequestDelegate);
		manager.add_endRequest(this._endRequestDelegate);
	},
	resetPosition : function() {
		var element = this.get_element();
		var eBounds = Sys.UI.DomElement.getBounds(element);
		$setStyle(this._container, "width", eBounds.width + "px");
		$setStyle(this._container, "height", eBounds.height + "px");
		Sys.UI.DomElement.setLocation(this._container, eBounds.x, eBounds.y);
	}
}
Nws.UI.WorkingIndicatorBehavior.registerClass("Nws.UI.WorkingIndicatorBehavior", Nws.UI.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();