﻿Type.registerNamespace("Nws.UI");

Nws.UI.PositioningBehavior = function(element) {
	Nws.UI.PositioningBehavior.initializeBase(this, [element]);
	
	this._endRequestDelegate = Function.createDelegate(this, this._endRequest);
	this._margin = {"top":0, "right":0, "bottom":0, "left":0};
	this._windowResizeDelegate = Function.createDelegate(this, this._windowResize);
	this._windowScrollDelegate = Function.createDelegate(this, this._windowScroll);
}
Nws.UI.PositioningBehavior.prototype = {
	get_margin : function() {
		return this._margin;
	},
	set_margin : function(value) {
		this._margin = value;
		this.resetPosition();
	},

	_endRequest : function(sender, args) {
		this.resetPosition();
	},
	_windowResize : function() {
		this.resetPosition();
	},
	_windowScroll : function() {
		this.resetPosition();
	},
	dispose : function() {
		Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(this._endRequestDelegate);
		$removeHandler(window, "resize", this._windowResizeDelegate);
		$removeHandler(window, "scroll", this._windowScrollDelegate);
		Sys.WebForms.PageRequestManager
		
		Nws.UI.PositioningBehavior.callBaseMethod(this, "dispose");
	},
	initialize : function() {
		Nws.UI.PositioningBehavior.callBaseMethod(this, "initialize");
		
		Sys.UI.DomElement.setLocation(this.get_element(), this._margin.left, this._margin.top);
		this.resetPosition();
		
		$addHandler(window, "resize", this._windowResizeDelegate);
		$addHandler(window, "scroll", this._windowScrollDelegate);
		Sys.WebForms.PageRequestManager.getInstance().add_endRequest(this._endRequestDelegate);
	},
	resetPosition : function() {
		var e = this.get_element();
		var margin = this.get_margin();
		var eBounds = Sys.UI.DomElement.getBounds(e);
		var vBounds = Nws.UI.Utility.getViewportBounds();
		var eBR = new Sys.UI.Point(eBounds.x + eBounds.width, eBounds.y + eBounds.height);
		var vBR = new Sys.UI.Point(vBounds.x + vBounds.width, vBounds.y + vBounds.height);
		var location = new Sys.UI.Point(eBounds.x, eBounds.y);
		
		if((margin.left + eBounds.width + margin.right) <= vBounds.width)
			location.x = Math.round((vBounds.width / 2) - (eBounds.width / 2)) + vBounds.x;
		else if(((eBounds.x - margin.left) > vBounds.x) && ((eBR.x + margin.right) > vBR.x))
			location.x = vBounds.x + margin.left;
		else if(((eBR.x + margin.right) < vBR.x) && ((eBounds.x - margin.left) < vBR.x))
			location.x = vBR.x - margin.right - eBounds.width;
			
		if((margin.top + eBounds.height + margin.bottom) <= vBounds.height)
			location.y = Math.round((vBounds.height / 2) - (eBounds.height / 2)) + vBounds.y;
		else if(((eBounds.y - margin.top) > vBounds.y) && ((eBR.y + margin.bottom) > vBR.y))
			location.y = vBounds.y + margin.top;
		else if(((eBR.y + margin.bottom) < vBR.y) && ((eBounds.y - margin.top) < vBR.y))
			location.y = vBR.y - margin.bottom - eBounds.height;
			
		Sys.UI.DomElement.setLocation(e, location.x, location.y);
	}
}
Nws.UI.PositioningBehavior.registerClass("Nws.UI.PositioningBehavior", Nws.UI.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();