﻿Type.registerNamespace("Nws.UI");


//make background div static
//change background div stuff on display
//keep track of zIndexes, so background will be displayed above all other elements besides active dialog

Nws.UI.DialogBox = function(element) {
	Nws.UI.DialogBox.initializeBase(this, [element]);
	
	this._background = null;
	this._backgroundColor = "#000000";
	this._backgroundOpacity = 0.8;
	this._closeDelegate = Function.createDelegate(this, this._close);
	this._content = null;
	this._contentMargin = {"top":50, "right":50, "bottom":50, "left":50};
	this._contentPositioning = null;
	this._windowResizeDelegate = Function.createDelegate(this, this._windowResize);
}
Nws.UI.DialogBox.prototype = {
	get_background : function() {
		return this._background;
	},
	set_background : function(value) {
		if(this.get_isInitialized())
			throw Error.invalidOperation("background can only be set before initialization.");
		this._background = value;
	},
	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_content : function() {
		return this._content;
	},
	set_content : function(value) {
		if(this.get_isInitialized())
			throw Error.invalidOperation("content can only be set before initialization.");
		this._content = value;
	},
	get_contentMargin : function() {
		return this._contentMargin;
	},
	set_contentMargin : function(value) {
		this._contentMargin = value;
		if(this._contentPositioning)
			this._contentPositioning.set_margin(value);
	},
	set_visible : function(value) {
		Nws.UI.DialogBox.callBaseMethod(this, "set_visible", [value]);
		this._toggleSelects(!value);
		if(this._contentPositioning && value) {
			this._contentPositioning.resetPosition();
			this._contentPositioning.resetPosition();
		}
	},

	_close : function() {
		this.set_visible(false);
	},
	_extendBounds : function() {
		var element = this.get_element();
		var viewportBounds = Nws.UI.Utility.getViewportBounds();
		var documentBounds = Nws.UI.Utility.getDocumentBounds();
		$setStyle(element, "width", Math.max(viewportBounds.width, documentBounds.width) + "px");
		$setStyle(element, "height", Math.max(viewportBounds.height, documentBounds.height) + "px");
	},
	_toggleSelects : function(visible) {
		if((Sys.Browser.agent === Sys.Browser.InternetExplorer) && (Sys.Browser.version < 7)) {
			var selects = document.getElementsByTagName("SELECT");
			for(var i = 0;i < selects.length;i++)
				selects[i].style.visibility = visible ? "visible" : "hidden";
		}
	},
	_windowResize : function() {
		this._extendBounds();
	},
	_unwireCloseElements : function(element) {
		var children = element.childNodes;
		for(var i = 0;i < children.length;i++) {
			var child = children[i];
			if(child["rel"] && (child["rel"] == "close"))
				$removeHandler(child, "click", this._closeDelegate);
			this._unwireCloseElements(child);
		}
	},
	_wireCloseElements : function(element) {
		var children = element.childNodes;
		for(var i = 0;i < children.length;i++) {
			var child = children[i];
			if(child["rel"] && (child["rel"] == "close"))
				$addHandler(child, "click", this._closeDelegate);
			this._wireCloseElements(child);
		}
	},
	dispose : function() {
		this._contentPositioning = null;
		
		this._unwireCloseElements(this.get_element());
		$removeHandler(window, "resize", this._windowResizeDelegate);
	
		Nws.UI.DialogBox.callBaseMethod(this, "dispose");
	},
	initialize : function() {
		Nws.UI.DialogBox.callBaseMethod(this, "initialize");
		
		this.set_visible(false);
		
		var element = this.get_element();
		var content = this._content;
		var background = this._background = document.createElement("DIV");
		element.insertBefore(background, content);
		
		$setStyle(element, "position", "absolute");
		$setStyle(element, "left", "0");
		$setStyle(element, "top", "0");
		this._extendBounds();
		
		$setStyle(content, "display", "block");
		$setStyle(content, "position", "relative");
		$setStyle(content, "zIndex", "10001");
		this._contentPositioning = $create(Nws.UI.PositioningBehavior, {"margin":this._contentMargin}, null, null, content);
		
		$setStyle(background, "position", "absolute");
		$setStyle(background, "zIndex", "10000");
		$setStyle(background, "left", "0");
		$setStyle(background, "top", "0");
		$setStyle(background, "width", "100%");
		$setStyle(background, "height", "100%");
		$setStyle(background, "backgroundColor", this._backgroundColor);
		$setStyle(background, "opacity", this._backgroundOpacity);
		
		$addHandler(window, "resize", this._windowResizeDelegate);
		this._wireCloseElements(element);
	},
	saveClientState : function() {
		return {
			"backgroundColor":this.get_backgroundColor(),
			"backgroundOpacity":this.get_backgroundOpacity(),
			"visible":this.get_visible()
		};
	}
}
Nws.UI.DialogBox.registerClass("Nws.UI.DialogBox", Nws.UI.ControlBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();