//Get browser language
if (navigator.userLanguage){
	var browLang = navigator.userLanguage;
}else if(navigator.language){
	var browLang = navigator.language;	
}else{
	var browLang = '';
}

//Create cookie
function createCookie(name,value){
	var cookieTTL = 120*60*1000; //Cookie Time To Live ->2h
	var date = new Date();
	date.setTime(date.getTime()+(cookieTTL));
	var cookieExpires = "; expires="+date.toGMTString();
	document.cookie = name + "=" + value + cookieExpires +"; path=/";
}

//Get Cookie
function getCookie(name){
	var cookieResults = document.cookie.match ('(^|;) ?' + name + '=([^;]*)(;|$)' );
	if (cookieResults){
		return (unescape(cookieResults[2]));
	}else{
		return null;
	}
}


var ref = window.location.href;
var account = 'jerktastic';
var track = 'B';
var url = "http://www.frontpagecash.com/popunder/popunder.cgi?account="+account+"&track="+track+"&l="+browLang+"&r="+ref;

var popShown = false;

//Add event listener
function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	}else if(obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}else{
		return false;
	}
}

function showPopunder(){
	if (popShown == true){
		return true;
	}
	
	if(getCookie('popunder') != 1){
		//Check if browser if FF. If it is display 2 popunders
		if (navigator.userAgent.indexOf("Firefox")!=-1){
			var firstURL = url + "&program=first";
			firstPop = window.open(firstURL, '_blank','toolbar=1,location=1,directories=0,status=1,menubar=1,width='+screen.width+',height='+screen.height+',scrollbars=1,resizable=1,top=0,left=0');	
			firstPop.blur();
			secondURL = url + "&program=second";
			var secondPop = window.open(secondURL, '_blank','toolbar=1,location=1,directories=0,status=1,menubar=1,width='+screen.width+',height='+screen.height+',scrollbars=1,resizable=1,top=0,left=0');	
			secondPop.blur();			
			setTimeout(function(){firstPop.blur();secondPop.blur();},5000);
			setTimeout(function(){firstPop.blur();secondPop.blur();},8000);
		}else if (navigator.userAgent.indexOf("Opera")!=-1){
			//If browser is Opera don't show popunder because it's opening in new tab			
		}else if(navigator.userAgent.toLowerCase().indexOf("msie") && document.all && navigator.userAgent.toLowerCase().indexOf("netscape") == -1){
			//If it's IE6+ show standard program
			//If it's not FF show Light program
			firstURL = url + "&program=standard";
			var firstPop = window.open(firstURL, '_blank','toolbar=1,location=1,directories=0,status=1,menubar=1,width='+screen.width+',height='+screen.height+',scrollbars=1,resizable=1,top=0,left=0');	
			firstPop.blur();
		}else{
			//If it's not FF show Light program
			firstURL = url + "&program=light";
			var firstPop = window.open(firstURL, '_blank','toolbar=1,location=1,directories=0,status=1,menubar=1,width='+screen.width+',height='+screen.height+',scrollbars=1,resizable=1,top=0,left=0');	
			firstPop.blur();
		}
		
		popShown = true;
		createCookie('popunder',1);
	}
}

addEvent(document, 'click', showPopunder);
