var popUnder=function(){
    
  function init(){
  	firstVisit();
    attachPopUnders();
  }
  
  function firstVisit() {
		if (popUnder.readCookie('_popunder_start_date') == null) {
			var date = new Date();
			popUnder.createCookie('_popunder_start_date',parseInt(date.getTime()/1000),2160);
		}
	}
  
  // Add click events to links.
  function attachPopUnders() {
    var i, a;
    for(i=0; (a = document.getElementsByTagName("a")[i]); i++) {
      addClickEvent(document.getElementsByTagName("a")[i],function(){ launchPopUnder(); });
    }
    return null;
  }
  
  // Add a click event to an element, preserving existing events.
  function addClickEvent(e,f){var o=e.onclick; if(typeof e.onclick!='function'){e.onclick=f;}else{e.onclick=function(){o();f();};}}
  
  // Popunder launch code.
  function launchPopUnder () {
  	if(popUnder.readCookie('_popunder_open_state') != 1) {
	    newWindow = window.open('/littleones/popunder/popunder-slo.html', '_APAPopUnder', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=680,height=480');
	    newWindow.blur();
	    window.blur(); // Webkit fix.
	    window.focus();
	  }
    return false;
  }
  
  // Set up chalking up of a content viewed hit after number of seconds has passed.
  function timeToGoal(seconds) {var goalTrack = setTimeout("popUnder.trackPage();",1000*parseInt(seconds));}
  
  // Increment the tracking cookie.
  function trackPage() {
    var cookieVal = readCookie('_APA_PopUnder_Count');
    var trackedPages = readCookie('_APA_PopUnder_Pages');
    var process = false;
    
    // Do we have a value?
    if (cookieVal) {
      // Check _APA_PopUnder_Pages for a match. If matched, we don't increment the tally and drop out.
      if (trackedPages) {
        var pos = trackedPages.indexOf(window.location + '[]');
        if (pos>=0) {} else process = true;
      }
      // This is a new page. Increment our tally.
      var newVal = 1 + parseInt(cookieVal);
      
    } else {
      // Start the tally at 1 (no value).
      var newVal = 1;
      var trackedPages = '';
      process = true; 
    }
    
    // Save cookies.
    if (process) {
      createCookie('_APA_PopUnder_Count',newVal,2160); // 90 days          
      createCookie('_APA_PopUnder_Pages',trackedPages + window.location + '[]',2160); // 90 days
    }
  }
  
  function getGoalTotal () {
    return popUnder.readCookie('_APA_PopUnder_Count');
  }
  
  // Create a cookie.
  function createCookie(name,value,hours) {
    if (hours) {
      var date = new Date();
      date.setTime(date.getTime()+(hours*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
    else 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;
  }
  
  // Drop cookie to mark popunder as open.
  function popunderIsOpen() {
		popUnder.createCookie('_popunder_open_state','1',0);
	}
	
	// Remove popunder open cookie when it is closed.
  function popunderIsClosed() {
		popUnder.createCookie('_popunder_open_state','0',-1);
	}
	
	function allowedGroup1(theTime) {
		var today = new Date();
		var currentTime = parseInt(today.getTime()/1000);
		var diff = currentTime-popUnder.readCookie('_popunder_start_date');
		if (diff > parseInt(theTime)) return false;
		else return true;
	}

  return {
    init:init,
    firstVisit:firstVisit,
    attachPopUnders:attachPopUnders,
    addClickEvent:addClickEvent,
    launchPopUnder:launchPopUnder,
    timeToGoal:timeToGoal,
    trackPage:trackPage,
    getGoalTotal:getGoalTotal,
    createCookie:createCookie,
    readCookie:readCookie,
    popunderIsOpen:popunderIsOpen,
    popunderIsClosed:popunderIsClosed,
    allowedGroup1:allowedGroup1
  };

}();

// Run on page load (comment from this point to disable the popunder appearing).
var o=window.onload;
if(typeof window.onload!='function'){
  window.onload = function(){popUnder.init()};
}else{
  window.onload = function(){o(); popUnder.init();};
}