/*********************************************
 * Helper functions for the display of a 
 * roadblock advertisement.
 *********************************************
*/
var RBContainer, RBBackground, RBAd;
var RBIntID = 0;

ff = false; v3 = false; op = false; ie4  = false; ie5 = false; nn4 = false; nn6 = false; isMac = false; aol = false;
if(document.images){
	if(navigator.userAgent.indexOf("Opera") != -1)op = true;
	else if(navigator.userAgent.indexOf("AOL") != -1) aol = true;
	else if(navigator.userAgent.indexOf("Firefox") != -1) ff = true;
	else {
		if (document.all && !document.getElementById) ie4 = true;
		if (document.layers) nn4 = true;
		if (document.all && document.getElementById) ie5 = true;
		if (document.addEventListener) nn6 = true;
	}
}
else v3 = true;	

if(navigator.userAgent.indexOf("Mac") != -1) { isMac = true; }


// Browser compatible element getter
function GetDocumentElementById(id)
{
	if (document.all)
		return document.all[id];
	if (document.getElementById(id))
		return document.getElementById(id);
	return null;
}

function HandleRBContainer(adTime)
{
	var dsoctop = 0;
	var dsoclft = 0;
	if (document.documentElement) {
		dsoctop = document.documentElement.scrollTop;
		dsoclft = document.documentElement.scrollLeft;
	}
	else {
		dsoctop = document.all ? document.body.scrollTop 
			+ (ff || (ie5 && !isMac) ? document.body.parentElement.scrollTop : 0): pageYOffset;
		dsoclft = document.all ? document.body.scrollLeft 
			+ (ff || (ie5 && !isMac) ? document.body.parentElement.scrollLeft : 0): pageXOffset;
	}

	var cliWidth = 0;
	var cliHeight = 0;
	if (op || aol) {
		cliHeight = window.innerHeight;
		cliWidth = window.innerWidth;
	}
	else {
		cliHeight = document.documentElement.clientHeight;
		cliWidth = document.documentElement.clientWidth;
	}
	
	//alert(cliWidth + " x " + cliHeight);
		
	var flt = RBContainer.style;

	flt.height = cliHeight + 'px';
	flt.width = cliWidth + 'px';
	flt.top = dsoctop + 'px';
	flt.left = dsoclft + 'px';
	//alert("End");
	//flt.display = 'block';
	
	if (ie5 && !isMac && RBIntID == null){
		var DivRef = RBContainer;
		var IfrRef = document.getElementById('DivShim');
		IfrRef.style.top = flt.top;
		IfrRef.style.left = flt.left;
		
		IfrRef.style.width = flt.width;
		IfrRef.style.height = flt.height;
//			IfrRef.style.top = DivRef.style.top;
		//IfrRef.style.left = DivRef.style.left;
		IfrRef.style.display = "block";
		//IfrRef.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
	}
	
	//set the close
	setTimeout("reveal()",adTime);
}

function clickAndHide(url)
{
	//alert(url);
	var i = new Image();
	i.src = url;
	reveal();
}

//clear the ad
function reveal()
{
    if (RBIntID != 0) 
    {
        clearInterval(RBIntID);
    }
    RBIntID = 0;
    RBContainer.style.display = 'none';
    if (ie5 && !isMac)
	    GetDocumentElementById('DivShim').style.display = "none";
    toggleAds();
}

function ShowRoadblock(cookieExpireHours, adTimeSeconds, cookiename)
{
	//alert('v3:' + v3 + ' op:' + op + ' ie5:' + ie5 + ' isMac:' + isMac);

	RBContainer = GetDocumentElementById('roadblockcontainer');
	RBBackground = GetDocumentElementById('roadblockbackground');
	RBAd = GetDocumentElementById('roadblock');
	toggleAds();

	if (readCookie(cookiename) != 'served')
	{
		RBBackground.style.display = 'block';
		RBAd.style.display = 'block';
		//convert ad time to milliseconds
		//RBIntID = setInterval("HandleRBContainer(" + adTimeSeconds * 1000 + ")", 10);
		RBIntID = setTimeout("HandleRBContainer(" + adTimeSeconds * 1000 + ")", 10);
		//alert("showing ad: " + readCookie('roadblock'));
		//24 hours coookie: 
		//writeCookie("traansitional", "served", 24);
		//session cookie:
		writeCookie(cookiename, "served", cookieExpireHours);
	}
	else
	{
		//alert("cookie is present, no ad to show: " + readCookie('roadblock'));
		reveal();
	}
	
}

// Returns value of the cookie 
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}


// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}

function toggleAds()
{
    var adDiv;
    var i;
    for (i = 1; i <= 3; i++)
    {
    adDiv = document.getElementById('ad_' + i);
    if (adDiv != null)
        {
            if (adDiv.style.visibility == "" || adDiv.style.visibility == null)
            {
                adDiv.style.visibility = "hidden";
            }
            else
            {
                adDiv.style.visibility = "visible";
            }
        }
    }
}

