var timer;

function startAp() {
  if (getCookie("SkewsMeOccupyThePlanetAd"))
  {
    return;
  }
	setLeft();
  if (document.layers) document.layers.pa.display = 'block';
	else if (document.all) document.all.pa.style.display = 'block';
	else if (document.getElementById) document.getElementById("pa").style.display = 'block';

  if (document.layers) document.layers.livestream.display = 'inline';
	else if (document.all) document.all.livestream.style.display = 'inline';
	else if (document.getElementById) document.getElementById("livestream").style.display = 'inline';

	document.getElementById("livestream").src = "http://cdn.livestream.com/embed/globalrevolution?layout=4&color=0xe7e7e7&autoPlay=false&mute=false&iconColorOver=0x888888&iconColor=0x777777&allowchat=true&height=193&width=300";

	showAp();
}

function hideAp() { 
	if (document.layers) document.layers.pa.display = 'none';
	else if (document.all) document.all.pa.style.display = 'none';
	else if (document.getElementById) document.getElementById("pa").style.display = 'none';

	if (document.layers) document.layers.livestream.display = 'none';
	else if (document.all) document.all.livestream.style.display = 'none';
	else if (document.getElementById) document.getElementById("livestream").style.display = 'none';

  document.getElementById("livestream").src="";
	setCookie("SkewsMeOccupyThePlanetAd", "hide:1");
}

function forceAp()
{
  if (getCookie("SkewsMeOccupyThePlanetAd"))
  {
    document.cookie = "SkewsMeOccupyThePlanetAd='';expires=Thu, 01-Jan-1970 00:00:01 GMT";
  }
	setLeft();
  if (document.layers) document.layers.pa.display = 'block';
	else if (document.all) document.all.pa.style.display = 'block';
	else if (document.getElementById) document.getElementById("pa").style.display = 'block';

  if (document.layers) document.layers.livestream.display = 'inline';
	else if (document.all) document.all.livestream.style.display = 'inline';
	else if (document.getElementById) document.getElementById("livestream").style.display = 'inline';

	document.getElementById("livestream").src = "http://cdn.livestream.com/embed/globalrevolution?layout=4&color=0xe7e7e7&autoPlay=true&mute=false&iconColorOver=0x888888&iconColor=0x777777&allowchat=true&height=193&width=300";

  showAp();
}

function showAp() { 
  var tAp = 300;	// set this to the actual pixel distance from the top where you want the pa div to be at the end of the descent

  if (document.layers) document.layers.livestream.display = 'inline';
	else if (document.all) document.all.livestream.style.display = 'inline';
	else if (document.getElementById) document.getElementById("livestream").style.display = 'inline';

	state=typeof tPos;
	if(state=='undefined') tPos = -750; // set this to the same value of the top property for the pa div in the style sheet;
	if(tPos < tAp) { 
		tPos+=25;
		if (document.layers){
		  document.layers.pa.top = tPos+"px";
		}
		else if (document.all){
		  document.all.pa.style.top = tPos+"px";
  	}
		else if (document.getElementById){
		  document.getElementById("pa").style.top = tPos+"px"; 
		}
	}

	if(timer!=null) clearInterval(timer);
	timer = setTimeout("showAp()",10);
}

function getoPos() {
	if (document.layers) alert(document.layers.pa.top);
	else if (document.all) alert(document.all.pa.style.top);
	else if (document.getElementById) alert(document.getElementById("pa").style.top);
}

function setLeft() {
  var wAp = 850; // set this to the same value of the width property for the pa div in the style sheet, smaller to move it to the right, larger to move it to the left

	if (document.layers) document.layers.pa.left = ((window.innerWidth / 2) - (wAp / 2))+"px";
	else if (document.all) document.all.pa.style.left = ((document.body.offsetWidth / 2) - (wAp / 2))+"px";
	else if (document.getElementById) document.getElementById("pa").style.left = ((window.innerWidth / 2) - (wAp / 2))+"px";
}


// Source: http://www.w3schools.com/JS/js_cookies.asp
function setCookie(c_name,value)
{
  if (value.length > 3900)
    alert('Cookie data may be too long: ' + value.length + ' bytes');

  var exdate=new Date();
  exdate.setMinutes(exdate.getMinutes() + 5);
  document.cookie = c_name + "=" + escape(value) + ";expires=" + exdate.toGMTString() + ";path=/";
}

// Source: http://www.w3schools.com/JS/js_cookies.asp
function getCookie(c_name)
{
  if (document.cookie.length>0)
  {
    var c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    { 
      c_start=c_start + c_name.length+1; 
      var c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1)
        c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}


