
  /////////////////////////////////////////////////////
  //                                                 //
  //          C-NOOFS Forecast Viewer v0.6           //
  //               Web User Interface                //
  //                                                 //
  /////////////////////////////////////////////////////


/**
 * Default Constructor for the Forecast Viewer UI
 * NOTE:  ViewerInterface is a static class...instantiating it is useless
 */
ViewerInterface = function() { }

ViewerInterface.Render = function()
{
	ViewerInterface.RenderLoading();
	
	// Check to see if the forecast viewer web service is online
	YAHOO.util.Connect.asyncRequest(CFG.OnlineCheck.Method, CFG.OnlineCheck.URI, 
	{
		success:function(o)
		{
			if ( o.responseText != null )
			{
				var parts = o.responseText.split("\n");
				
				if ( parts[0] == "OFFLINE" )
				{
					document.body.removeChild(document.getElementById("Loading"));
					ViewerInterface.RenderOffline(parts[1]);
				}
				else
				{
					ViewerInterface.RenderMain();
					document.body.removeChild(document.getElementById("Loading"));
				}
			}
			else
			{
				alert("FAILURE: " + o.statusText);
			}
		},
		failure:function(o)
		{
			alert("FAILURE: " + o.statusText);
		},
		timeout:5000
	});
}


ViewerInterface.RenderOffline = function(msg)
{
	var body = document.body;

		// Create the container for the loading box	
	 	var container = document.createElement("div");
		container.id = "Loading";
		container.className = "loadingBox";
		container.style.backgroundImage = "none";
		container.style.paddingLeft = "20px";
		container.style.width = "420px";
		container.style.textAlign = "center";
		
		container.style.marginTop = ((YAHOO.util.Dom.getViewportHeight() - container.offsetHeight) / 4) + "px";

			var header = document.createElement("div");
			header.innerHTML = "Forecast Viewer Temporarily Unavailable";
			header.className = "title";
			
		container.appendChild(header);

			var subtext = document.createElement("div");
			subtext.innerHTML = msg;
			subtext.className = "subtitle";
			subtext.style.paddingTop = "10px";
			subtext.style.paddingBottom = "10px";
			
		container.appendChild(subtext);

		
	body.appendChild(container);
}


ViewerInterface.RenderLoading = function()
{
	var body = document.body;

		// Create the container for the loading box	
	 	var container = document.createElement("div");
		container.id = "Loading";
		container.className = "loadingBox";
		container.style.marginTop = ((YAHOO.util.Dom.getViewportHeight() - container.offsetHeight) / 4) + "px";

			var header = document.createElement("div");
			header.innerHTML = "Please Wait";
			header.className = "title";
			
		container.appendChild(header);

			var subtext = document.createElement("div");
			subtext.innerHTML = "Loading forecast viewer...";
			subtext.className = "subtitle";
			
		container.appendChild(subtext);

		
	body.appendChild(container);
}




ViewerInterface.RenderProcessingBox = function()
{
	var body = document.body;
	
		var overlay = document.createElement("div");
		overlay.id = "ProcessingRequest";
		overlay.className = "opaqueOverlay";
		overlay.style.height = (YAHOO.util.Dom.getViewportHeight() + 75) + "px";

			// Create the container for the loading box	
		 	var container = document.createElement("div");
			container.className = "loadingBox";
			container.style.width = "245px";
			container.style.marginTop = ((YAHOO.util.Dom.getViewportHeight() - container.offsetHeight) / 4) + "px";
	
				var header = document.createElement("div");
				header.innerHTML = "Please Wait";
				header.className = "title";
				
			container.appendChild(header);
	
				var subtext = document.createElement("div");
				subtext.innerHTML = "Relaying your request to the Server...";
				subtext.className = "subtitle";
				
			container.appendChild(subtext);
		overlay.appendChild(container);
	body.appendChild(overlay);
}


/**
 * Static function to render the Forecast Viewer UI
 * This function has CrAzYiNdEnTiNG!
 */
ViewerInterface.RenderMain = function()
{
	var UIG = new UserInputGroup("Search");

 	var body = document.body;
 	
	 	// Create the YUI container object
	 	var container = document.createElement("div");
	 	container.id = "doc";
	 	
		 	// Create the header container
		 	var cHead = document.createElement("div");
		 	cHead.id = "hd";
		 	
			 	// Add the page title
			 	var pgTitle = document.createElement("p");
			 	pgTitle.className="pageTitle";
			 	pgTitle.innerHTML = Lang.PageTitle;
		 	
		 	cHead.appendChild(pgTitle);

			 	// Add the subtitle
			 	var sbTitle = document.createElement("p");
			 	sbTitle.className="pageSubTitle";
			 	sbTitle.innerHTML = Lang.PageSubTitle;

		 	cHead.appendChild(sbTitle);

			 	// Add the horizontal rule
			 	var horizrule = document.createElement("p");
			 	horizrule.className="horizRule";

		 	cHead.appendChild(horizrule);
	 	
 		container.appendChild(cHead);

		 	// Create the body container
		 	var cBody = document.createElement("div");
		 	cBody.id = "bd";

				// Build the Forecast Search box		 	
				var VB = new CollapseBox("SearchPanel", Lang.SearchBoxTitle);
				for ( key in CFG.Fields )
				{
					 	var rowContainer = document.createElement("div");
					 	
							var rowLabel = document.createElement("div");
							rowLabel.className = "label";
							rowLabel.innerHTML = Lang.Fields[key] + ":";
							
						rowContainer.appendChild(rowLabel);
						
							var UserInput = UserInputFactory.createManager(key,CFG.Fields[key]);
							UIG.add(key,UserInput);
							
							var rowInput = UserInput.getContainer();
							rowInput.classname = "input";
							
						rowContainer.appendChild(rowInput);						
					VB.addRow(key,rowContainer);
					
				}				

			cBody.appendChild(VB.getContainer());

				var TBL = new ResultsTable("Forecast", CFG.ResultsTable);
				var TBLUP = new ResultsTableUpdater(TBL,UIG);

				// Set the handler to call when a user changes an input field
				UIG.setChangeHandler(function()
				{
					TBLUP.currentPage = 1;
					ViewerInterface.RenderProcessingBox();
					TBLUP.triggerUpdate();
				});
				
			cBody.appendChild(TBL.getContainer());
			 	
		container.appendChild(cBody);
	
	// Append the container to the body  	
	body.appendChild(container);
	
	TBLUP.triggerUpdate();
}

