function SmartEncode(value)
{
	var retVal = "";
	try
	{
		retVal = encodeURIComponent(value);
	}
	catch(e)
	{
		retVal = escape(value);
	}
	return retVal;
}

function IsValidObject(value) 
{ 
	var ret = false;
	if("undefined" != typeof(value) && null != value)
		ret = true; 
	return ret; 
} 

function SightMaxMonitor(propertyName)
{
	this.PropertyName			= propertyName									;

	this.SiteID					= "2"									;
	this.QueueID				= "1"									;
	this.BrowserID				= "64a81314-cb16-4030-aac3-eb6eabb60e3b"								;
	this.SessionID				= "e42b40b2-f505-49c1-9c8c-b8110343ad64"								;
	this.Path					= "http://www.americasforestryauthority.com/SightMaxAgentInterface/"									;
	
	this.ChatWindowOptions		= "width=490,height=404,resizable=no,scrollbars=no,menubar=no,status=no"						;
	this.InviteDivTopOffset		= 25						;
	this.InviteDivLeftOffset	= 200						;
	
	document.writeln("<DIV STYLE=\"position: absolute; top: 0px; left: 0px; visibility: hidden\"><IMG SRC=\"" + this.Path + "Images/Spacer.gif\" ID=CommandImage NAME=CommandImage></DIV>");
	// Coords are: L, T, R, B
	document.writeln("<DIV ID=SightMaxChatRequest STYLE=\"position: absolute; z-index:9999; top: 0px; left: 0px; visibility: hidden\"><map name=\"InviteMap\"><area href=\"javascript: SightMaxCancel();\" shape=\"rect\" coords=\"0,145,255,166\"><area href=\"javascript: SightMaxAccept();\" shape=\"rect\" coords=\"0,0,255,145\"></map><img border=\"0\" SRC=\"" + this.Path + "Images/fullInvite2Chat.gif\" usemap=\"#InviteMap\"></DIV>");
	
	this.PendingImageDownload	= false											;
	this.CommandImage			= document.CommandImage							;
	this.CommandTimerID			= null											;
	this.InviteDiv				= document.getElementById("SightMaxChatRequest");
	this.Attempts				= 0												;
	this.ImageCheckFrequency	= 1000											;
	this.ImageCheckTimeSpan		= 15000											;
	this.LastHeight				= -1											;
	this.UniqueID				= 0												;
	this.bConnected				= true											;
	
	this.lastTop				= -1											;
	this.bTopChanged			= true											;
	this.scrollTimeout			= -1											;
	this.inviteResponse			= null											;
	
	
	var pd				= parent.document;
	
	var screenRes		= "";
	if(IsValidObject(screen) && IsValidObject(screen.width) && IsValidObject(screen.height) && IsValidObject(screen.colorDepth))
		screenRes = "&screenRes=" + SmartEncode(screen.width + 'x' + screen.height + 'x' + screen.colorDepth);
	var currentPage		= "";
	if(IsValidObject(pd.location.protocol) && IsValidObject(pd.location.hostname) && IsValidObject(pd.location.pathname))
		currentPage = "&currentPage=" + SmartEncode(pd.location.protocol + '//' + pd.location.hostname + pd.location.pathname);

	var referrer		= pd.referrer;
	if(!IsValidObject(referrer) || referrer == "")
		referrer = "";
	else
		referrer = "&referrer=" + SmartEncode(referrer);
	var pageTitle		= pd.title;
	if(!IsValidObject(pageTitle) || pageTitle == "")
		pageTitle = "";
	else
		pageTitle = "&pageTitle=" + SmartEncode(pageTitle);
	var queryString		= pd.location.search;
	if(!IsValidObject(queryString) || queryString == "")
		queryString = "";
	else
		queryString = "&queryString=" + SmartEncode(queryString);
	var cookies			= pd.cookie;
	if(!IsValidObject(cookies) || cookies == "")
		cookies = "";
	else
		cookies = "&cookies=" + SmartEncode(cookies);
		
	var maxLength			= 2000;
	
	var requestString = "commandInit.smgif?siteID=" + this.SiteID + "&sessionID=" + this.SessionID;
	
	if(requestString.length + screenRes.length < maxLength)
		requestString += screenRes;
	
	if(requestString.length + currentPage.length < maxLength)
		requestString += currentPage;
		
	if(requestString.length + referrer.length < maxLength)
		requestString += referrer;
		
	if(requestString.length + pageTitle.length < maxLength)
		requestString += pageTitle;
		
	if(requestString.length + queryString.length + cookies.length < maxLength)
	{
		requestString += queryString + cookies;	
	}
	
	this.SendRequest(requestString);
		
	this.SetCommandTimer(this.ImageCheckFrequency);
}

SightMaxMonitor.prototype.GetUniqueID = function()
{
	return this.UniqueID++;
}

SightMaxMonitor.prototype.SetCommandTimer = function(milliseconds)
{
	if(null == milliseconds)
		milliseconds = this.ImageCheckTimeSpan;
		
	this.CommandTimerID = window.setTimeout(this.PropertyName + ".Process();", milliseconds);
}

SightMaxMonitor.prototype.ClearCommandTimer = function()
{
	window.clearTimeout(this.CommandTimerID);
}

SightMaxMonitor.prototype.Process = function()
{
	var gotImage = false;
	if(!this.PendingImageDownload)
	{
		this.SendPollRequest();
	}
	else
	{
		this.Attempts++;
		if(this.Attempts > 6)
		{
			this.PendingImageDownload = false;
			this.SendPollRequest();
		}
		else
		{
			gotImage = true;
			this.GetCommand();
		}
	}
	
	if(this.bConnected)
	{
		if(gotImage)
			this.SetCommandTimer(this.ImageCheckFrequency);
		else
			this.SetCommandTimer(this.ImageCheckTimeSpan);
	}			
}

SightMaxMonitor.prototype.SendPollRequest = function()
{
	this.SendRequest("commandmonitor.smgif?u=" + this.GetUniqueID() + "&sessionID=" + this.SessionID + "&siteID=" + this.SiteID + "&BrowserID=" + this.BrowserID);
}

SightMaxMonitor.prototype.SendRequest = function(newSrc)
{
	this.Attempts				= 0;
	this.PendingImageDownload	= true;
	this.CommandImage.src		= this.Path + newSrc;
}

SightMaxMonitor.prototype.SendCoBrowseData = function()
{
	var allCookies	= escape(document.cookie);
	var queryString = escape(window.location.search);
	var src = "commandcobrowse.smgif?=" + this.GetUniqueID() + "&sessionID=" + this.SessionID + "&siteID=" + this.SiteID + "&cookies=" + allCookies + "&query=" + queryString;
	this.SendRequest(src);
}

SightMaxMonitor.prototype.GetCommand = function()
{
	if(this.PendingImageDownload)
	{
		var w = this.CommandImage.height;
		if(0 != w)
		{
			this.PendingImageDownload = false;
			this.HandleCommand(w);
		}
	} 
}

SightMaxMonitor.prototype.HandleCommand = function(height)
{
	if(height == this.LastHeight)
		return;
	this.LastHeight = height;
	switch(height)
	{
		case 10:
			break;
		case 20:
			this.InviteChat();
			break;
		case 30:
			//this.InvokeChat();
			break;
		case 40:
			this.bConnected = false;
			break;
		case 50:
			this.SendCoBrowseData();
			break;
	}
}








///Operator invoked/invited chat section.

SightMaxMonitor.prototype.OpenChat = function(startType)
{
	if("undefined" == typeof(startType))
		startType = "";
	var WinStr = this.Path + "PreChatSurvey.aspx?siteID=" + this.SiteID + "&queueID=" + this.QueueID + "&startType=" + startType;
	window.open(WinStr, 'sightMaxChatWindow', this.ChatWindowOptions);
}

SightMaxMonitor.prototype.InvokeChat = function()
{
	this.OpenChat("OpInvoke");
}

SightMaxMonitor.prototype.InviteChat = function()
{
	if(null == this.InviteDiv) 
		return;
	try
	{
		//this.CheckScroll();//place <div> before making visible.
		this.InviteDiv.style.visibility = "visible";
		this.SetScrollTimer();
	}
	catch(e)
	{
	}
}

function SightMaxAccept()
{
	var instance = document.sightMaxMonitor;
	instance.ClientAcceptInvite();
	instance.inviteResponse = new Image();
	instance.inviteResponse.src = instance.Path + "commandmonitor.smgif?u=" + instance.GetUniqueID() 
		+ "&sessionID=" + instance.SessionID + "&siteID=" + instance.SiteID 
		+ "&BrowserID=" + instance.BrowserID + "&inviteResponse=accepted";
}

function SightMaxCancel()
{
	var instance = document.sightMaxMonitor;
	instance.ClientCancelInvite();
	instance.inviteResponse = new Image();
	instance.inviteResponse.src = instance.Path + "commandmonitor.smgif?u=" + instance.GetUniqueID() 
		+ "&sessionID=" + instance.SessionID + "&siteID=" + instance.SiteID 
		+ "&BrowserID=" + instance.BrowserID + "&inviteResponse=declined";
}


SightMaxMonitor.prototype.ClientAcceptInvite = function()
{
	this.HideInvite();
	this.OpenChat("OpInvite");
}

SightMaxMonitor.prototype.OpCancelInvite = function()
{
	var img = new Image();
	img.src = 
	this.HideInvite();
}

SightMaxMonitor.prototype.ClientCancelInvite = function()
{
	this.HideInvite();
}

SightMaxMonitor.prototype.HideInvite = function()
{
	if(null == this.InviteDiv)
		return;
	try
	{
		this.InviteDiv.style.visibility = "hidden";
		this.ClearScrollTimer();
	}
	catch(e)
	{
	}	
}

SightMaxMonitor.prototype.PlaceInvite = function(thLeft, theTop)
{
	try
	{
		this.InviteDiv.style.top	= (theTop + this.InviteDivTopOffset)		+ "px";
		this.InviteDiv.style.left	= (theLeft + this.InviteDivLeftOffset)		+ "px";
	}
	catch(e)
	{
	}
}

SightMaxMonitor.prototype.SetScrollTimer = function()
{
	this.scrollTimeout = window.setTimeout(this.PropertyName +  ".CheckScroll();", 250);
}

SightMaxMonitor.prototype.ClearScrollTimer = function()
{
	window.clearTimeout(this.scrollTimeout);
}

SightMaxMonitor.prototype.CheckScroll = function()
{
	var thisTop = -1;
	var thisLeft = -1;
	if("undefined" == typeof(document.body.scrollTop))
	{
		thisTop = window.pageYOffset;
		thisLeft = window.pageXOffset;
	}
	else
	{
		thisTop = document.body.scrollTop;
		thisLeft = document.body.scrollLeft;
	}
	if(thisTop != this.lastTop)
	{
		this.bTopChanged = true;
	}
	else if(this.bTopChanged)
	{
		this.PlaceInvite(thisLeft, thisTop);
		this.bTopChanged = false;
	}
	this.lastTop = thisTop;
	this.SetScrollTimer();
}

document.sightMaxMonitor	=	new SightMaxMonitor("document.sightMaxMonitor");