
//Detect the user's browser.
var IE = /*@cc_on!@*/false;

//Make the base url and the current page global.
var baseUrl = '';
var page = '';

//let's add an addloadevent function so we don't need external libraries like jquery
function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') {
		window.onload = func; 
	} else { 
		window.onload = function() { 
			if (oldonload) { 
				oldonload(); 
			} 
			func(); 
		} 
	}
} 

//Add Array.indexOf if it doesn't exist.
//The client needs js >= 1.6 for built-in indexOf function.
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(elt /*, from*/) {
	var len = this.length >>> 0;

	var from = Number(arguments[1]) || 0;
	from = (from < 0) ? Math.ceil(from) : Math.floor(from);
	if (from < 0) {
		from += len;
	}

	for (; from < len; from++) {
		if (from in this && this[from] === elt) {
			return from;
		}
    }
	return -1;
	};
}

//Set the base url.
function setBaseUrl() {
	
	//Determine the base url. The environment could be dev, stage, or live.
	baseUrl = location.href.replace('%2D', '-');

	if (baseUrl.match('/metozolv2010-dev')) {
		baseUrl = 'http://medthinkinteractive.com/metozolv2010-dev';
	} else if (baseUrl.match('/metozolv2010')) {
		baseUrl = 'http://medthinkinteractive.com/metozolv2010';
	} else if (baseUrl.match('/localhost')) {
		baseUrl = 'http://localhost/Metozolv';
	} else if (baseUrl.match('metozolv-stage')) {
		baseUrl = 'http://www.metozolv-stage.com';
	} else if (baseUrl.match('salixtest.salix.com')) {
		baseUrl = 'http://salixtest.salix.com';
	} else {
		baseUrl = 'http://www.metozolvodt.com';
	}
}

//Set the page the user is currently visiting.
function setPage() {
			
	//Get the page the user is currently on.
	page = location.href.replace('%2D', '-').replace(baseUrl, '');
	page = page.replace('/', '').replace('/', '');
	if (!page) {
		page = 'home';
	}
	page = page.replace('%2D', '-');
}

//Create a cookie.
function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

//Read a cookie.      
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//Place a lightbox on the page with the provided content.
function createLightbox(agreementHTML) {
			
	//Define the ligthbox.
	var lightbox = document.createElement('div');
	lightbox.id = 'lightbox-wrapper';
	lightbox.innerHTML = '<div id="overlay"></div>' + 
		'<div id="lightbox-container"><img src="'+baseUrl+'/assets/images/lightbox/letter-top.png"><div id="lightbox">' + 
		agreementHTML + 
		'<button name="agreementBtn" id="agreementBtn">Close</button>' + 
		'</div><img src="'+baseUrl+'/assets/images/lightbox/letter-bottom.png"></div>';
	
	//Display the lightbox.
	document.body.appendChild(lightbox);
	
	//When the user clicks to agree to the agreement, set a cookie and show the page content.
	document.getElementById('agreementBtn').onclick = function() {
		createCookie("readAgreement", "yes", 365);
		document.body.removeChild(lightbox);
	}
}

/**
 * On page load, check to see if this user has already accepted the agreement (by looking for a cookie).
 * If the cookie exists, do nothing. If the cookie doesn't exist, show the agreement. This ensures that the user has agreed to the
 * agreement before seeing any page content.
 */
addLoadEvent(function() { 
	
	//Set the bae url and current page. 
	//These will be available globally.
	setBaseUrl();
	setPage();
	
	//Look for the cookie that shows the user read the agreement.
	var readAgreement = readCookie("readAgreement");
	
	//Only execute the following code if the user has not agreed to the agreement.
	if (readAgreement == null) {
		
		//Only add the lightbox to certain pages.
		switch(page) {
		
		    //these are the pages without index.aspx
			case 'home':
			case 'what-is-metozolv-odt':
			case 'right-for-you':
			case 'diabetic-gastroparesis':
			case 'refractory-gerd':	
			
			//these are the same pages with index.aspx
			case 'index.aspx':
			case 'what-is-metozolv-odtindex.aspx':
			case 'right-for-youindex.aspx':
			case 'diabetic-gastroparesisindex.aspx':
			case 'refractory-gerdindex.aspx':	
			
			//Get the html for the agreement.
			var iframe = document.createElement('iframe');
			iframe.setAttribute("src", baseUrl + "/assets/copy/letter.html"); 
			iframe.setAttribute("id", "lightbox-iframe");
			document.body.appendChild(iframe);
			
			//Check each 1/2 second until the iframe contents are available.
			var interval = null;
			var agreementHTML = null;		
			interval = setInterval(function() {
				
				var iframe = document.getElementById('lightbox-iframe');			
				var agreementHTML = iframe.contentWindow.document.body.innerHTML;			
				if (agreementHTML) {
					
					//Format today's date.
					var d = new Date();
					var month= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];				
					var thisMonth = month[d.getMonth()];
					var thisDay = d.getDate();
					var thisYear = d.getFullYear();
					
					//Update the date to today's date.
					agreementHTML = agreementHTML.replace('[date]', thisMonth + ' ' + thisDay + ', ' + thisYear);
					
					//Remove the iframe. It was only for storage purposes.
					document.body.removeChild(iframe);
					
					//Stop the interval.
					clearInterval(interval);
					
					//Place the lightbox on the page.
					createLightbox(agreementHTML);
				}
			}, 500);	
			
			break;
		}
	}
}); 
