﻿if (!window.BIG) {
	throw "BIG.Frames.js: BIG.js does not appear to have been loaded"
}

if (!BIG.Window) {
	throw "BIG.Frames.js: BIG.Window.js does not appear to have been loaded"
}

BIG.Frames = {};

BIG.Frames.PopupFrame = function () {
	// Used during a call to Open to determine
	// if the content has already been loaded.
	var visible = false;	
	
	var layer = null;
	var frame = null;
	var content = null;
	
	var currentSource = "";
	
	var request;
	
	function GetElements(LayerId, FrameId, ContentId) {
		layer = document.getElementById(LayerId);
		frame = document.getElementById(FrameId);
		content = document.getElementById(ContentId);
		
		if (content == null) {
			throw "BIG.Frame.PopupFrame: Unable to find content element";
		}
		
		if (frame == null) {
			frame = content;
		}
	}
	
	function OnReadyStateChange() {
		if (request.readyState == 4 && request.status == 200) {
			if (request.responseText){				
				content.innerHTML = request.responseText;
			}
        }
	}
	
	return {
		LayerId: null,
		FrameId: null,
		ContentId: null,
		
		EnablePositioning: false,
		
		Source: "",
		UseAJAX: false,
		
		HorizontalAlignment: BIG.Alignment.None,
		VerticalAlignment: BIG.Alignment.None,
		
		Left: 0,
		Top: 0,
		
		Visible: false,
		
		Show: function () {
			if (arguments.length == 1) {
				this.Source =  arguments[0];
			}
			
			GetElements(this.LayerId, this.FrameId, this.ContentId);			
			
			if ((this.Source) && (this.Source != currentSource)) {
				
				if (this.UseAJAX) {
					content.innerHTML = "";
					request = BIG.GetXmlHttpRequest();
					request.open("GET", this.Source, true);
					request.onreadystatechange = OnReadyStateChange;
					request.send(null);
				} else {
					var s = new String();
					if (content.tagName.toLowerCase() == "iframe") {
						content.src = this.Source;
					} else {
						throw "BIG.Frame.PopupFrame: Currently only supports the IFRAME element unless AJAX is enabled";
					}
				}
				
				contentSource = this.Source;				
			}
			
			if (this.EnablePositioning) {
				this.Update(true);
			}
			
			layer.style.visibility = "visible";
			frame.style.visibility = "visible";
			
			visible = true;
			this.Visible = visible;
		},

		Hide: function () {
			GetElements(this.LayerId, this.FrameId, this.ContentId);
			
			layer.style.visibility = "hidden";
			frame.style.visibility = "hidden";
			
			if (this.UseAJAX) {
				content.innerHTML = "";
			} else {
				var s = new String();
				if (content.tagName.toLowerCase() == "iframe") {
					content.src = "about:blank";
				} else {
					throw "BIG.Frame.PopupFrame: Currently only supports the IFRAME element unless AJAX is enabled";
				}
			}
			
			visible = false;
			this.Visible = visible;
		},
		
		Update: function () {
			var force = arguments.length == 1 ? arguments[0] : false;
		
			if (visible || force) {
				GetElements(this.LayerId, this.FrameId, this.ContentId);
				
				var size = BIG.Window.GetWindowSize();
				
				if (layer != null) {
					layer.style.left = "0px";
					layer.style.top = "0px";
					layer.style.width = size.Width + "px";
					layer.style.height = size.Height + "px";
				}
				
				BIG.Window.PositionElement(frame, this.Left, this.Top, this.HorizontalAlignment, this.VerticalAlignment);
			}	
		}
	}
}