// set global variables
var surveyWindow;
var stillinternal = false;
var cookieIsSetToDisableSurvey = false;

// initialize Shadowbox overlay
Shadowbox.init({
	skipSetup: true,
	modal: true,
	players: ["html"]
});

// attach events to site window
AttachEvent(window,'load',launchSurveyOptions);
AttachEvent(window,'beforeunload',setFocus);

/* browser detection */
var IE = (navigator.appVersion.indexOf("MSIE")==-1) ? false : true;
var chrome = (navigator.userAgent.indexOf('Chrome/') > -1) ? true : false;

// define settings for survey overlay
function launchSurveyOptions()
{
	// do not show survey options if user has already seen them
	if (surveyDisabled()) return;

	// attach click events required for survey overlay and popup window functionality
	attachClickEventsForSurvey();

	// open survey overlay
	Shadowbox.open({
		content: document.getElementById('survey-overlay').innerHTML,
		player: "html",
		height: 392,
		width: 715
	});

	// create cookie that prevents survey overlay from appearing again
	setCookie('surveyDisabled', 1, 365);
	; 
}

// check for existence of cookie that disables survey overlay; survey should also be disabled on site home page
function surveyDisabled()
{
	var isSurveyDisabled = getCookie('surveyDisabled'); // check for existence of cookie
	var isHomePage = (window.location.href.indexOf('/aprepitant/emend/hcp/index.jsp') > -1) ? true : false; // check to see if URL is for home page

	if ((isSurveyDisabled != null && isSurveyDisabled != "") || isHomePage) return true;
	else return false;
}

// attach a click event to all page links to indicate that the user has not left the site
function attachClickEventsForSurvey()
{
	var domainTest = new RegExp(self.location.hostname);
	var anchors = document.getElementsByTagName?document.getElementsByTagName('a'):document.anchors;

	for (var i=0,len=anchors.length;i<len;i++)
	{
		var a = anchors[i];
		if (domainTest.test(a.href)) AttachEvent(a,'click',function(){stillinternal = true;});
	}
}

// change window focus to the survey if the user has left the site
function setFocus()
{
	if (!stillinternal && surveyWindow)
	{
		if (!IE) {window.blur();} // bug fix for Firefox - remove focus from parent window
		if (chrome) {surveyWindow.blur();} // bug fix for Chrome - window must be blurred before focus will work
		surveyWindow.focus();
	};
}

// popup the survey window and set focus based on the user's response
function launchSurvey(response)
{
	var url = "/aprepitant/emend/hcp/survey.jsp";
	var windowName = "survey";
	var attribs = "address=no,status=yes,scrollbars=yes,resizable=yes,menubar=yes";

	surveyWindow = window.open(url, windowName, attribs);
	surveyWindow.resizeTo(980, 600); // resize survey window - doing this independently allows focus to be given to window in multiple browsers
	if (response == 1)
	{
		surveyWindow.focus();
	}
	else
	{
		surveyWindow.blur();
		if (chrome) // bug fix for Chrome - window must be blurred before focus will work
		{
			window.blur();
			window.focus();
		}
	}
}
