var relpConnected;
var relpLanguage = 'ns';

var relpViewerHook;

if (relpConnected != true)
{
	//create the divs
	document.write('<script language="JavaScript" type="text/javascript" src="http://www.realelivepeople.com/relp_swfobject.js"></script>');
	//document.write('<script language="JavaScript" type="text/javascript" src="http://www.realelivepeople.com/swfobject/swfobject.js"></script>');
	if (typeof jQuery == 'undefined')
	{
		document.write('<script language="JavaScript" type="text/javascript" src="http://www.realelivepeople.com/tracking/jquery-1.4.2.min.js"></script>');	
	}	
	document.write('<script language="JavaScript" type="text/javascript" src="http://www.realelivepeople.com/tracking/webBox.js"></script>');
	document.write('<script language="JavaScript" type="text/javascript" src="http://www.realelivepeople.com/tracking/adomatic_beta.js"></script>');

	document.write('<div id=\'ReLPTrafficHook\' name=\'ReLPTrafficHook\' ></div>');
	//document.write('<div id=\'chatInvitation\' name=\'chatInvitation\' style="z-index: 500">&nbsp</div>');
}

//ReLP 2.x
function ShowReLPInvite(guestID, chatRequestID)
{	
	//check for a preexisting invite
	var existingInvite = document.getElementById("ReLPChatRequestContainer");
	if (existingInvite != null)
	{
		document.body.removeChild(existingInvite);
	}
	
	//create new chat request container div
	var containingDiv = document.createElement('div');	
	containingDiv.id = "ReLPChatRequestContainer";
	document.body.appendChild(containingDiv);
	
	ReLP_HideAllAds();
	
	//create new box
	var newBox = new WebBox("ReLPChatRequest_" + chatRequestID, 400, 380, true);		
	newBox.setAnchor("center", "center");	
	newBox.write("ReLPChatRequestContainer");
	
	
	var ReLPChatInvitation = new relp_SWFObject("http://realelivepeople.com/chatInvitation.swf?guestID=" + guestID + "&invitationID=" + chatRequestID + "&ISO639=" + GetISO639() , "ReLPChatInvitation", "400", "380", "7", "#FFFFFF");
	ReLPChatInvitation.addParam("allowScriptAccess", "always");
	ReLPChatInvitation.addParam("quality", "high");
	ReLPChatInvitation.write("ReLPChatRequest_" + chatRequestID);	
	
	
	/*
	var params = 
	{
  		allowScriptAccess: "always",
  		quality: "high"
	};
	var attributes = 
	{
  		id: "ReLPChatInvitation",
  		name: "ReLPChatInvitation"
	};

	swfobject.embedSWF("http://realelivepeople.com/chatInvitation.swf?guestID=" + guestID + "&invitationID=" + chatRequestID + "&ISO639=" + GetISO639() , "ReLPChatRequest_" + chatRequestID, "400", "380", "7", "expressInstall.swf", null, params, attributes);
	*/
}

//Relp 2.x
function HideChatInvite()
{
	var existingInvite = document.getElementById("ReLPChatRequestContainer");
	if (existingInvite != null)
	{
		existingInvite.innerHTML = "";
		document.body.removeChild(existingInvite);
	}	
}

//ReLP 2.x
function AcceptReLPChat(guestID, chatID, failsafeHTML, openChatMessage, closeChatMessage)
{	
	var mywindow = window.open ("http://realelivepeople.com/guest.php?chatID=" + chatID + "&guestID=" + guestID + "&ISO639=" + GetISO639(), 
							"ReLPGuestChat",
							"location=0,status=0,scrollbars=0,width=580,height=505");
							
	if (mywindow == null || mywindow == undefined) //if window was intercepted
	{
		var chatCode = 'window.open("http://realelivepeople.com/guest.php?chatID=' + chatID + '&guestID=' + guestID + '&ISO639=' + GetISO639() + '", "ReLPGuestChat", "location=0,status=0,scrollbars=0,width=580,height=505")';
		ShowRelPFailsafe(failsafeHTML, openChatMessage, closeChatMessage, chatCode);
	}	
	else //if window was allowed
	{	
		HideChatInvite();
	}
}

//Added 18/Feb/2008 to avoid issue with firefox popup blocker
function OpenNewReLPChat(guestID, requestedHost, failsafeHTML, openChatMessage, closeChatMessage)
{		
	var mywindow = window.open("http://realelivepeople.com/guest.php?requestedHost=" + requestedHost + "&guestID=" + guestID + "&ISO639=" + GetISO639() + "&auto=true", 
							"ReLPGuestChat",
							"location=0,status=0,scrollbars=0,width=580,height=505");
	
	if (mywindow == null || mywindow == undefined) //if window was intercepted
	{
		var chatCode = 'window.open("http://realelivepeople.com/guest.php?requestedHost=' + requestedHost + '&guestID=' + guestID + '&ISO639=' + GetISO639() + '&auto=true", "ReLPGuestChat", "location=0,status=0,scrollbars=0,width=580,height=505")';
		ShowRelPFailsafe(failsafeHTML, openChatMessage, closeChatMessage, chatCode);
	}	
	else //if window was allowed
	{	
		HideChatInvite();
	}
}

//Added 8/Apr/2008 to add an HTML failsafe chat link when popup blocker intercepts chat window
function ShowRelPFailsafe(failsafeHTML, openChatMessage, closeChatMessage, chatCode)
{
	//alert('Showing ReLP Failsafe: \n' + failsafeHTML + '\n' + chatCode);
	var chatInvitation = document.getElementById("chatInvitation");
	var failsafeContent;

	
	if (failsafeHTML == null || failsafeHTML == undefined || failsafeHTML == 'undefined' || failsafeHTML == "")
	{
		//use default failsafe content when none is specified
		failsafeContent = '<div style=\'background-color: #cccccc;\'>';		
		failsafeContent += '<h2>Your popup blocker has prevented the chat window from opening automatically. Click the "Open Chat" button below to open the window directly.</h2>';							
	}
	else
	{
		failsafeContent = failsafeHTML;
	}
	
			failsafeContent += '<div>';
				failsafeContent += '<input type=\'button\' value=\'' + openChatMessage + '\' onClick=\'' + chatCode + ';HideChatInvite();\' />';
				failsafeContent += '<input type=\'button\' value=\'' + closeChatMessage + '\' onClick=\'HideChatInvite()\' />';
			failsafeContent += '</div><br />';
		failsafeContent += '</div>';
	
	chatInvitation.innerHTML = failsafeContent;	
	//alert("showed failsafe \n\n" + chatInvitation.innerHTML)
}

//ReLP 2.x
function ConnectReLP(RELSiteName, language)
{
	
	
	relpConnected = true;
	
	if (typeof(language) != 'undefined')
	{
		relpLanguage = language;
		//alert(language);
	}
	
	
	var theBody = document.getElementsByTagName('body')[0];
	
	//***Create ReLP 2.x viewerhook***
	
	var ReLPTrafficHook = document.getElementById('ReLPTrafficHook');
	ReLPTrafficHook.style.position = "absolute";
	ReLPTrafficHook.style.float = "none";
	ReLPTrafficHook.style.top = "-500px";	
	
	
	relpViewerHook = new relp_SWFObject("http://www.realelivepeople.com/tracking/ViewerHook.swf?siteName=" + RELSiteName + "&ISO639=" + GetISO639() + "&referrer=" + escape(document.referrer) + "&browserInfo=" + GetReLPBrowserInfo() + "&pageTitle=" + document.title, "ReLPTrafficHookFlash", "1", "1", "8", "#FFFFFF");
	relpViewerHook.addParam("allowScriptAccess", "always");
	relpViewerHook.addParam("quality", "high");
	relpViewerHook.write("ReLPTrafficHook");	
	
	
	/*
	var params = 
	{
  		allowScriptAccess: "always"
	};
	var attributes = 
	{
  		id: "ReLPTrafficHookFlash",
  		name: "ReLPTrafficHookFlash"
	};

	swfobject.embedSWF("http://www.realelivepeople.com/tracking/ViewerHook.swf?siteName=" + RELSiteName + "&ISO639=" + GetISO639() + "&referrer=" + escape(document.referrer) + "&browserInfo=" + GetReLPBrowserInfo() + "&pageTitle=" + document.title, "ReLPTrafficHook", "1", "1", "8", "expressInstall.swf", null, params, attributes);
	*/
}

function ReLP_GetViewerHook()
{
	
	var movieName = "ReLPTrafficHookFlash";
	
	//return relpViewerHook;
	
	
	
	if (window.document[movieName]) 
	{
		return window.document[movieName];
	}
	
	if (navigator.appName.indexOf("Microsoft Internet")==-1)
	{
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName]; 
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		return document.getElementById(movieName);
	}
	
}



function GetISO639()
{
	if (relpLanguage != 'ns') //specified to relp explicitly
	{
		return relpLanguage;
	}
	else if (typeof(_ISO639) != 'undefined') //defined in the page implicitly
	{
		return _ISO639;
	}
	else //default to english
	{
		return 'eng';
	}
}

function GetReLPBrowserInfo()
{
	var nVer = navigator.appVersion;
	var nAgt = navigator.userAgent;
	var browserName  = '';
	var fullVersion  = 0; 

	
	if ((verOffset=nAgt.indexOf("MSIE"))!=-1) { // In Internet Explorer, the true version is after "MSIE" in userAgent
		browserName  = "Internet Explorer";
		fullVersion  = parseFloat(nAgt.substring(verOffset+5));	
	}
	else if ((verOffset=nAgt.indexOf("Opera"))!=-1) 	// In Opera, the true version is after "Opera" 
	{
		browserName  = "Opera";
		fullVersion  = parseFloat(nAgt.substring(verOffset+6));
	}
	else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) 	// In Firefox, the true version is after "Firefox" 
	{
		browserName  = "Firefox";
		fullVersion  = parseFloat(nAgt.substring(verOffset+8));		
	}
	else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) 	// In most other browsers, "name/version" is at the end of userAgent 
	{
		browserName  = nAgt.substring(nameOffset,verOffset);
		fullVersion  = parseFloat(nAgt.substring(verOffset+1));
		if (!isNaN(fullVersion)) 
		{
			fullVersion = parseInt(''+fullVersion);
		}
		else 
		{
			fullVersion  = 0; majorVersion = 0;
		}
	}
	
	// Finally, if no name and/or no version detected from userAgent...
	if (browserName.toLowerCase() == browserName.toUpperCase() || fullVersion==0)
	{
		browserName  = navigator.appName;
		fullVersion  = parseFloat(nVer);		
	}
	
	return browserName + ' ' + fullVersion;
}