var TYPE = 1; //1 is minutesToLive and 2 is count
var TIMELIMIT = 1440; // happen every TIMELIMIT minutes when TYPE = 1
var FREQUENCY = 2; // happen every FREQUENCY times when TYPE = 2

function Cookie(name) 
{
    this.$name = name;

    var listCookies = document.cookie;
    if (listCookies == "") return;

    var cookies = listCookies.split(';');
    var cookie = null;
    for(var i = 0; i < cookies.length; i++) 
    {
		cookies[i] = cookies[i].replace(/^\s+/,"");
        if (cookies[i].substring(0, name.length+1) == (name + "=")) 
		{
            cookie = cookies[i];
            break;
        }
    }

    if (cookie == null) return;

    var cookieValue = cookie.substring(name.length+1);

    var a = cookieValue.split('&');
    for(var i=0; i < a.length; i++)
        a[i] = a[i].split(':');

    for(var i = 0; i < a.length; i++) {
        this[a[i][0]] = decodeURIComponent(a[i][1]);
    }
}


Cookie.prototype.store = function(minutesToLive, path, domain, secure) 
{
    var cookieValue = "";
    for (var prop in this) 
    {
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
            continue;
            
        if (cookieValue != "") 
        	cookieValue += '&';
        	
        cookieValue += prop + ':' + encodeURIComponent(this[prop]);
    }

    var cookie = this.$name + '=' + cookieValue;
    
    if (minutesToLive || minutesToLive == 0) 
    {
		var date = new Date();
		date.setTime(date.getTime() + Math.ceil(minutesToLive * 60 * 1000));
		cookie += ";expires=" + date.toGMTString();
    }
    document.cookie = cookie;
}

Cookie.prototype.remove = function(path, domain, secure) 
{
    for (var prop in this) 
    {
        if (prop.charAt(0) != '$' && typeof this[prop] != 'function')
            delete this[prop];
    }

    this.store(0, path, domain, secure);
}


Cookie.enabled = function() 
{
    if (navigator.cookieEnabled != undefined) 
    	return navigator.cookieEnabled;
    	
    if (Cookie.enabled.cache != undefined) 
    	return Cookie.enabled.cache;
    	
    document.cookie = "testcookie=test; max-age=10000";

    var cookies = document.cookie;

    if (cookies.indexOf("testcookie=test") == -1) 
    {
	    return Cookie.enabled.cache = false;
	} else {
	    document.cookie = "testcookie=test; max-age=0";
        return Cookie.enabled.cache = true;
	}
}

function skiprequest()
{
	//Auto skip if query param cmpid= (campaign id) is set
	
	var ref = document.referrer;
	if (ref.match(/(reddit|digg)\.com/i) != null)
		return true;
	
	 var url = window.location.toString();
	  return (url.match(/cmpid=/i) != null);
	 
	 url.match(/\?(.+)$/);
	 var params = RegExp.$1;

	 var params = params.split("&");
	 var queryStringList = {};
	 
	 for(var i=0;i<params.length;i++)
	 {
		 if (params[i].match(/cmpid=/i))
		 	return true;
	 }

	 return false;
}

var cookie = new Cookie("UGOwelcome");
var shouldskip = skiprequest();

if (TYPE == 2)
{
	if (!cookie.pagehit)
	{
		if (!shouldskip)
			cookie.pagehit = 1;
		else
			cookie.pagehit = 0;
		
		cookie.store(TIMELIMIT);
	}
	else
	{
		if (!shouldskip)
		{
			cookie.pagehit++;
			cookie.store(TIMELIMIT);
		}
		else
		{
			cookie.pagehit = 0;
			cookie.store(TIMELIMIT);
		}
	}
}

var pit = 0;
if (cookie.pagehit)
	pit = (cookie.pagehit % FREQUENCY) == 1; //if pit is true, we should show the takeover

if ( (TYPE == 1 && !cookie.welcomeMat && !shouldskip) || ((TYPE == 2 && pit) && !shouldskip)) 
{
  if (TYPE == 1)
  {
	cookie.welcomeMat = 1;
	cookie.store(TIMELIMIT);
  }

function HideContent()
{
	//document.body.style.height = "1px";
	document.body.style.overflow = "hidden";
	document.body.scroll = "no";
}

function ShowContent()
{
	clearInterval(descroll);

	document.body.style.height = "auto";
	document.body.style.overflow = "auto";
	document.body.scroll = "auto";
	document.getElementById("welcomeMat").style.display = "none";
}

function printAdCode_iframe()
{
	printWelcomeMatBegin();

	if (typeof(cachebuster) == 'undefined')
		var cachebuster = Math.floor(Math.random()*10000000000);
	
	if (typeof(tile) == 'undefined')
		var tile = 1;
	else
		tile++;

	var urlLoc = document.URL;
	var items = urlLoc.split('/');
	var source = '';
	source += items[0] + '//';
	source += items[2] + '/';
	source += 'do/prestitial';
	
   var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	   
	if (false)
	{
		//fullscreen transparency
     
        document.write('\n<iframe src="' + source + '" width="100%" height="'+windowHeight+'px" frameborder="0" scrolling="no" allowtransparency="true" style="margin: 0 auto;"></iframe>\n');
	}
	else
	{
		//standard takeover
		document.write('\n<iframe src="' + source + '" width="984" height="900" frameborder="0" scrolling="no" bgcolor="d2d2d2" style="margin: 0 auto;"></iframe>\n');
	}
	
	printWelcomeMatEnd();

	HideContent();
}

function printWelcomeMatBegin()
{
    var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
      document.write('<div id="welcomeMat" style="background-color: #d2d2d2; position: absolute; top: 0px; left: 0px; z-index: 111111151; height: 9999px; width: 100%; text-align: center; color: white;">\n');
}

function printWelcomeMatEnd()
{
	document.write('</div>');	

	descroll = setTimeout('undoPageScroll()', 100);
}

function undoPageScroll()
{
	if (document.getElementById('welcomeMat').style.display != 'none')
	{
		window.scrollBy(0, -6000);
		descroll = setTimeout('undoPageScroll();', 1000);
	}
	else
		clearInterval(descroll);
}


function returnToRequestedPage()
{
	ShowContent();
}

printAdCode_iframe();

setTimeout("ShowContent()", "15000");

}
