
	//=======================================================================
	//	Global variables/functions
	//=======================================================================
    var BRWOSER_APP_NAME_FF	= "FireFox";
    var BRWOSER_APP_NAME_IE	= "Internet Explorer";
    var BRWOSER_APP_NAME_OTHER = "Other";
    var DISPLAY_TYPE_WIDGET_ALWAYS_ON = 0;
	var DISPLAY_TYPE_WIDGET_LIB_ONLINE = 1;
	var ACTION_ONLINE_HOST_CHECK = 0;
	var STATUS_HOSTS_NA = -1;
	var STATUS_HOSTS_OFFLINE = 0;
	var STATUS_HOSTS_ONLINE = 1;
    //=======================================================================
    function detectBrowser() {
		var oBrowserData = {
			'appName'		: '',
			'appVersion'	: ''
		};
		if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
			var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			if (ffversion>=3) {
				//document.write("You're using FF 3.x or above");
				oBrowserData.appName = BRWOSER_APP_NAME_FF;
				oBrowserData.appVersion	= '3.x+';
				
				return oBrowserData;
			}
			else if (ffversion>=2) {
				//document.write("You're using FF 2.x");
				oBrowserData.appName = BRWOSER_APP_NAME_FF;
				oBrowserData.appVersion	= '2.x';
				
				return oBrowserData;
			}
			else if (ffversion>=1) {
				//document.write("You're using FF 1.x");
				oBrowserData.appName = BRWOSER_APP_NAME_FF;
				oBrowserData.appVersion	= '1.x';
				
				return oBrowserData;
			} else {
				//document.write("n/a");
				oBrowserData.appName = BRWOSER_APP_NAME_FF;
				oBrowserData.appVersion	= 'n/a';
				
				return oBrowserData;
			}
		}
		else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
			var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			if (ieversion>=8) {
				//document.write("You're using IE8 or above");
				oBrowserData.appName = BRWOSER_APP_NAME_IE;
				oBrowserData.appVersion	= '8+';
				
				return oBrowserData;
			}
			else if (ieversion>=7) {
				//document.write("You're using IE7.x");
				oBrowserData.appName = BRWOSER_APP_NAME_IE;
				oBrowserData.appVersion	= '7.x';
				
				return oBrowserData;
			}
			else if (ieversion>=6) {
				//document.write("You're using IE6.x");
				oBrowserData.appName = BRWOSER_APP_NAME_IE;
				oBrowserData.appVersion	= '6.x';
				
				return oBrowserData;
			}
			else if (ieversion>=5) {
				//document.write("You're using IE5.x");
				oBrowserData.appName = BRWOSER_APP_NAME_IE;
				oBrowserData.appVersion	= '5.x';
				
				return oBrowserData;
			}
			else {
				//document.write("n/a");
				oBrowserData.appName = BRWOSER_APP_NAME_IE;
				oBrowserData.appVersion	= 'n/a';
				
				return oBrowserData;
			}
		} else {
			//document.write("other");
			oBrowserData.appName = BRWOSER_APP_NAME_OTHER;
			oBrowserData.appVersion	= 'n/a';
			
			return oBrowserData;
		}

	}
	//alert(detectBrowser().appName);
	//=======================================================================
	//	The Manipulator object provides the communication between server and
	//	client (right now just polling for online status).
	//=======================================================================
    function Manipulator() {
		this.vrlSiteName = '/psu-de';
        this.externalURLQuery = "";
        this.ENUM_TARGET_SERVER = 'server';
        this.ENUM_TARGET_CLIENT = 'client';
        this.pollingInterval = 6000;
        this.pollingLastUpdate = new Date().getMilliseconds();
    }
	//=======================================================================
    Manipulator.prototype.externalURL = function() {
        return "http://vrlplus.cb.docutek.com/" + this.vrlSiteName + "/widget/server-comm.asp";
    }
	//=======================================================================
    Manipulator.prototype.virtualDeskFormURL = function(virtual_desk_id, q_data) {
		//alert(typeof(virtual_desk_id));
		var strQSVDeskID = ( virtual_desk_id.length > 0 ? "&virtual_desk_id=" + virtual_desk_id : "" );
        return "http://vrlplus.cb.docutek.com/" + this.vrlSiteName + "/vrl_talk_redir.asp?q_data=" + escape(q_data) + strQSVDeskID;
    }
	//=======================================================================
    Manipulator.prototype.addScript = function(obj_msg) {
        var scriptTag = document.createElement("script");
        //debugger;
        scriptTag.type  = "text/javascript";
        scriptTag.src   = this.externalURL() + ( obj_msg['name'] ? "?" + "sid=" + obj_msg['id'] + "&" + obj_msg['name'] + "=" + escape(obj_msg['value']) : "" );
        
        //alert(scriptTag.src);
        //debugger;
        try {
            // Append script tag to header of client page.
            this.getScriptArea().appendChild(scriptTag);
            
            return true;
        }
        catch (err) {
            return false;
        }
    }
    //=======================================================================
    Manipulator.prototype.openWindow = function(url, name, parms) {
        window.open(url, name, parms);
    }
    //=======================================================================
    Manipulator.prototype.getScriptArea = function() {
        var parentElement = document.getElementsByTagName("head");
        
        if (!parentElement || parentElement.length < 1) {
            parentElement = document.body;
        }
        else {
            parentElement = parentElement[0];
        }
        return parentElement;
    }
    //=======================================================================
    Manipulator.prototype.sendMessage = function(data_element, target) {
        var oElt = data_element;
        //debugger;
        var oMsg = new Message();
        
        oMsg.name   = 'msg';
        oMsg.id     = '';
        oMsg.value  = oElt.value;
        
        if ( target == this.ENUM_TARGET_SERVER ) {
            this.addScript(oMsg)
            oElt.value = "";
        }
        else if ( target == this.ENUM_TARGET_CLIENT ) {
			this.GUIDo.updateUI(parseInt(oElt.value));
            //alert(oElt.value);
        }
        //var oMessagesDiv = document.getElementById("divSTatus");
        //oMessagesDiv.innerHTML += "<div>" + oMsg['value'] + "</div>";
        //oMessagesDiv.scrollTop = oMessagesDiv.scrollHeight;
    }
    //=======================================================================
    Manipulator.prototype.poll = function() {
        var oMsg = new Message();
        
        oMsg.id     = '';
        oMsg.name   = 'polling';
        oMsg.value  = '';
        
        this.addScript(oMsg);
        //alert(Manipulator.pollingLastUpdate);
    }
    //=======================================================================
    function Message() {
        this.name   = '';
        this.id     = '';
        this.value  = '';
    }
    //=======================================================================
    var message_queue = [];
    //=======================================================================
    
    //=======================================================================
    //	GUI object that builds widget html.
    //=======================================================================
    function GUIDo(obj_config) {
        this.config = obj_config;
        this.setConfigProperties();
        this.browserData = detectBrowser();
    }
    //=======================================================================
    GUIDo.prototype.setConfigProperties = function() {
		this.displayType			= this.getConfigPropVal('display-type', DISPLAY_TYPE_WIDGET_ALWAYS_ON);
		this.divAltContentName		= this.getConfigPropVal('div-alt-content-name', '');
		this.virtualDeskId			= this.getConfigPropVal('virtual-desk-id', '');
		this.vrlSiteName			= this.getConfigPropVal('vrl-site-name', '/psu-de');
        this.textColor              = this.getConfigPropVal('text-color', '#000');
        this.textFont	            = this.getConfigPropVal('text-font', 'Trebuchet MS');
        this.labelTitle             = this.getConfigPropVal('label-title', 'Send a Question!');
        this.labelButton            = this.getConfigPropVal('label-button', 'Ask Question');
        this.labelWidgetLibsOnline	= this.getConfigPropVal('label-widget-libs-online', 'Librarians are online');
        this.labelWidgetLibsOffline = this.getConfigPropVal('label-widget-libs-offline', 'No librarians online');
        this.divContainerName       = this.getConfigPropVal('div-container-name', '');
        this.divContainerWidth      = this.getConfigPropVal('width', 175);
        this.divContainerHeight     = this.getConfigPropVal('height', 150);
        this.divBGColor             = this.getConfigPropVal('bg-color', '#050608');
        this.divBGImage             = this.getConfigPropVal('bg-image', '');
        this.divBorderStyle         = this.getConfigPropVal('border-style', '1px solid #999');
        this.inputButtonSendStyle   = this.getConfigPropVal('button-style', ' background-color: #dcdcdc; color: #000; font-family: Trebuchet MS; font-size: 9pt; height: 23px;');
		
		// 
		this.widgetLoaded = false;
		this.hostOnlineStatus = STATUS_HOSTS_NA;
		
		Manipulator.vrlSiteName = this.vrlSiteName;
		Manipulator.GUIDo = this;
    }
    //=======================================================================
    GUIDo.prototype.getConfigPropVal = function(prop_name, default_val) {
        return ( this.config[prop_name] ? this.config[prop_name] : default_val );
    }
    //=======================================================================
    GUIDo.prototype.openQuestionFormWin = function() {
		strQData = document.getElementById('txtQuestion').value.replace(/[\r\n]+/g, ' ');
		//alert(strQData);
		strVDeskFormURL = Manipulator.virtualDeskFormURL(this.virtualDeskId, strQData);
		
        window.open(strVDeskFormURL);
    }
    //=======================================================================
    GUIDo.prototype.updateUI = function(hosts_online_status) {
		//alert('Updating UI: ' + hosts_online_status);
		//debugger;
		
		oContainer = document.getElementById(this.divContainerName);
		
		if ( this.displayType == DISPLAY_TYPE_WIDGET_ALWAYS_ON || hosts_online_status == STATUS_HOSTS_ONLINE ) {
			if ( this.widgetLoaded ) {
				var oOnlineStatusDiv = document.getElementById("divOnlineStatus");
				var oOnlineStatusImage = document.getElementById("divOnlineStatusImage");
				var oOnlineStatusMessage = document.getElementById("divOnlineStatusMessage");

				// TODO: Set status line.
				if ( hosts_online_status == STATUS_HOSTS_ONLINE ) {
					oOnlineStatusImage.innerHTML	= "<img src='http://vrlplus.cb.docutek.com/psu-de/images/vrl_ready_to_browse.gif'>";
					oOnlineStatusMessage.innerHTML	= this.labelWidgetLibsOnline;
				} else {
					oOnlineStatusImage.innerHTML	= "<img src='http://vrlplus.cb.docutek.com/psu-de/images/vrl_not_ready_to_browse.gif'>";
					oOnlineStatusMessage.innerHTML	= this.labelWidgetLibsOffline;
				}
			} else {
				try {
					this.loadWidget(hosts_online_status);
					this.updateUI(hosts_online_status);
				} catch(err) {
					oContainer.innerHTML = "There was an error generating the UI: " + err.message;
				}
			}
		} else if ( this.displayType == DISPLAY_TYPE_WIDGET_LIB_ONLINE && hosts_online_status == STATUS_HOSTS_OFFLINE ) {
			oContainer.innerHTML = document.getElementById(this.divAltContentName).innerHTML;
			
			this.widgetLoaded = false;
		}
		
		this.hostOnlineStatus = hosts_online_status;
    }
    //=======================================================================
    GUIDo.prototype.generateUI = function() {
		// Pass
    }
    //=======================================================================
    GUIDo.prototype.loadWidget = function() {
		var oContainer;
		
		oContainer = document.getElementById(this.divContainerName);
	    
		//MB: append PX to the end of the height and width so it will work in FF and IE both
		oContainer.style.height = this.divContainerHeight + "px";
		oContainer.style.width = this.divContainerWidth + "px";

		//oContainer.style.border = "solid 1px";
	    
		var oTitleDiv = document.createElement("div");
	    
		//oTitleDiv.style.backgroundImage = "url()";
		
		//MB: changed the padding here to 10px because the title div and content divs were coming out different sizes
		//oTitleDiv.style.paddingTop      = "3px";
		//oTitleDiv.style.paddingLeft     = "5px";
		//oTitleDiv.style.paddingBottom   = "5px";
		oTitleDiv.style.padding   		= "5px";
		
		oTitleDiv.style.fontWeight      = "bold";
		oTitleDiv.style.color           = this.textColor;
		oTitleDiv.style.fontSize        = "8pt";
		oTitleDiv.style.fontFamily      = this.textFont
		
		//MB: comment out this width value since it doenst appear to be necessary
		//oTitleDiv.style.width           = ( this.browserData.appName == BRWOSER_APP_NAME_FF ? this.divContainerWidth - 7 : this.divContainerWidth );
	    
		//oTitleDiv.style.height          = "24px";
		oTitleDiv.style.backgroundColor = this.divBGColor;
		oTitleDiv.style.borderLeft      = this.divBorderStyle;
		oTitleDiv.style.borderRight     = this.divBorderStyle;
		oTitleDiv.style.borderTop       = this.divBorderStyle;
		oTitleDiv.style.borderBottom    = this.divBorderStyle;
	    
		oTitleDiv.innerHTML             = this.labelTitle; //"<div style=' ;'>" + this.labelTitle + "</div>";
	    
		var oUIDiv = document.createElement("div");
	    
		var intFormEltWidth     = this.divContainerWidth - 10;
		var strFormEltSpacing   = "5px";
		var strFormEltFontStyle = "font-family: Trebuchet MS; font-size: 9pt;";
	    
		//MB: comment out this width value since it doenst appear to be necessary
		//oUIDiv.style.width              = ( this.browserData.appName == BRWOSER_APP_NAME_FF ? this.divContainerWidth - 22 : this.divContainerWidth );
	    
		//MB: append PX to the end of this height property so it will work in firefox
		oUIDiv.style.height             = ( this.browserData.appName == BRWOSER_APP_NAME_FF ? this.divContainerHeight - 21 : this.divContainerHeight - 12 ) + "px";
		
		oUIDiv.style.padding            = "10px";
		oUIDiv.style.borderLeft         = this.divBorderStyle;
		oUIDiv.style.borderRight        = this.divBorderStyle;
		oUIDiv.style.borderBottom       = this.divBorderStyle;
		oUIDiv.style.backgroundColor    = this.divBGColor;
		oUIDiv.style.backgroundImage    = this.divBGImage;
		oUIDiv.style.backgroundRepeat   = "repeat-x";
	    
		/*oUIDiv.innerHTML                = "<form id='frmMessage' style='margin: 0px; padding: 0px;' onsubmit='Manipulator.sendMessage(document.forms[0].txtMessage, Manipulator.ENUM_TARGET_SERVER); return false;'>" +
										"<div id='divMessages' class='margin-std' style='padding: 3px; margin-bottom: " + strFormEltSpacing + "; " + strFormEltFontStyle + "overflow: auto; height: " + (this.divContainerHeight - 50) + "px; border: solid 1px #999; background-color: #fff; width: " + intFormEltWidth + ";'></div>" + 
										"<input type='text' id='txtMessage' value='' style='margin-bottom: " + strFormEltSpacing + "; width: " + intFormEltWidth + "; " + strFormEltFontStyle + "' \>" +
										"<br/>" + 
										"<input type='button' value='Ask' style='" + this.inputButtonSendStyle + "' onclick='Manipulator.sendMessage(document.forms[0].txtMessage, Manipulator.ENUM_TARGET_SERVER)'>" +
										"<div id='divOnlineStatus'></div>" +
										"</form>"; */
		var oUISubDiv = document.createElement("div");
		var oQForm = document.createElement("form");
		var oQInput = document.createElement("textarea");
		var oQButton = document.createElement("input");
		var oOnlineStatusDiv = document.createElement("div");
	    
		oQForm.setAttribute('id', 'frmQuestion');
		oQForm.style.margin	= '0px';
	    
		oQInput.setAttribute('id', 'txtQuestion');
		//oQInput.setAttribute('cols', 21);
		//oQInput.setAttribute('rows', 5);
		oQInput.style.fontSize			= oTitleDiv.style.fontSize;
		oQInput.style.border			= this.divBorderStyle;
		oQInput.style.padding			= "2px;"
		oQInput.style.backgroundColor	= "#fff";
		oQInput.style.width				= this.divContainerWidth - 22;
		oQInput.style.height			= 70;
		
		//MB: these values help firefox render the textarea box properly
		oQInput.cols					= (this.divContainerWidth - 22)/7.5;
		oQInput.rows					= 4;
	    
		oQButton.setAttribute('id', 'btnAsk');
		oQButton.setAttribute('type', 'button');
		oQButton.setAttribute('value', this.labelButton);
		oQButton.setAttribute('onclick', 'oGUIObj.openQuestionFormWin();');
	    
		oQButton.style.marginTop	= '5px';
		oQButton.style.marginBottom	= '3px';
		oQButton.style.fontFamily	= this.textFont;
		oQButton.style.fontSize		= '12px';
		
		//MB: set this as display=block so that it would display the button on its own line, since it was wrapping up to the previous line in FF
		oQButton.style.display		= 'block';
	    
		strOnlineStatusCode = "<table><tr><td><div id='divOnlineStatusImage' style='display: inline;'><img src='http://vrlplus.cb.docutek.com/psu-de/images/vrl_not_ready_to_browse.gif'></div></td><td><div id='divOnlineStatusMessage' style='font-family: " + this.textFont + "; font-size: 11px'></div></td></table>";
	    
		oOnlineStatusDiv.setAttribute('id', 'divOnlineStatus');
		oOnlineStatusDiv.setAttribute('style', 'font-size: ' + oTitleDiv.style.fontSize + '; font-family: ' + oTitleDiv.style.fontFamily);
		oOnlineStatusDiv.innerHTML	= strOnlineStatusCode;
	    
		if ( this.browserData.appName == BRWOSER_APP_NAME_IE ){
			oUISubDiv.innerHTML = "<textarea id='txtQuestion' rows='5' cols='18' style='width: " + (this.divContainerWidth - 22) + ";border: " + this.divBorderStyle + "'></textarea><input type='button' id='btnAsk' value='" + this.labelButton + "' style='font-family: " + this.textFont + "; font-size: 11px; margin-top: 5px; margin-bottom: 3px; padding: 0px;' onclick=\"oGUIObj.openQuestionFormWin();\"><div id='divOnlineStatus' style='font-size: " + oTitleDiv.style.fontSize + "; font-family: " + oTitleDiv.style.fontFamily + "'>" + strOnlineStatusCode + "</div>";
		}
		else if ( this.browserData.appName == BRWOSER_APP_NAME_FF ){
			oUISubDiv.appendChild(oQInput);
			oUISubDiv.appendChild(oQButton);
			oUISubDiv.appendChild(oOnlineStatusDiv);
		} else {
			oUISubDiv.appendChild(oQInput);
			oUISubDiv.appendChild(oQButton);
			oUISubDiv.appendChild(oOnlineStatusDiv);
		}
	    
		oUIDiv.appendChild(oUISubDiv);
		/*
		Possible future dev - chat.
		oUIDiv.innerHTML                = "<form id='frmMessage' style='margin: 0px; padding: 0px;'>" +
										"<div>Enter a question</div>" + 
										"<input type='text' id='txtQuestion' value='' style='margin-bottom: " + strFormEltSpacing + "; width: " + intFormEltWidth + "; " + strFormEltFontStyle + "' \>" +
										"<br/>" + 
										"<input type='button' id='btnAsk' value='Ask' style='" + this.inputButtonSendStyle + "' onclick=\"oGUIObj.openQuestionFormWin();\">" +
										"<div id='divOnlineStatus'></div>" +
										"</form>";
	    
		var oOverlayDiv = document.createElement("div");
	    
		oOverlayDiv.style.position           = "absolute";
		oOverlayDiv.style.top                = "0px";
		oOverlayDiv.style.left               = "0px";
		oOverlayDiv.style.width              = "100%";
		oOverlayDiv.style.height             = "100%";
		oOverlayDiv.style.padding            = "0px";
		oOverlayDiv.style.margin             = "0px";
		oOverlayDiv.style.visibility         = "hidden";
		//oOverlayDiv.style.backgroundColor    = "#f1f1f1";
		oOverlayDiv.style.backgroundImage    = "url()";
		oOverlayDiv.style.backgroundRepeat   = "no-repeat";
		//oOverlayDiv.innerHTML                = "<img src='' />";
		*/
		oContainer.innerHTML = "" //"<style>.margin-std { margin: 3px; } .margin-std-sides {margin-top: 0px; margin-left: 3px; margin-right: 3px; margin-bottom: 0px;} </style>";
		oContainer.appendChild(oTitleDiv);
		oContainer.appendChild(oUIDiv);
		//oContainer.appendChild(oOverlayDiv);
		this.widgetLoaded = true;
    }
	//=======================================================================
	//=======================================================================
    var Manipulator = new Manipulator();
    
    setInterval("Manipulator.poll()", Manipulator.pollingInterval);
    //=======================================================================
