$(document).ready(function()
{
  collapse(); //controlled by JS instead of CSS, so users without JS still see the modules
  window.loadInterstitial = true;

  if (isArticlePage())
  { 
    cap_on_interstitial()
  }
});


function isArticlePage()
{
    var currentURL = document.location.href;
    if (currentURL.indexOf('article.cfm',0) != -1) 
    {
        return true;
    }
    else 
    {
        return false;
    }
}


function cap_on_interstitial()
{   
   var cap = 4; //1 less so 3
   
   if( (typeof(loadInterstitial)!=="undefined"))  //if we decide to load interstitial
   {
      if (loadInterstitial && document.cookie.indexOf("tifint")< 0 )  //if cookie doesn't exists
      {
	 var x = 1;
      }  
      else
      {
	  var x = getCookie("tifint");	
   	  if((x != undefined)  && (x != ''))
   	  {
     		x = parseInt(x) +1;
   	  }
   	  else
   	  {
      		x = 1;
   	  }      
      }
     
      if(x < cap)
      {   
         //set a cookie to expire tomorrow at 12:00 am
         var tomorrow=new Date();
         tomorrow.setDate(tomorrow.getDate()+1);
         tomorrow.setHours(0); tomorrow.setMinutes(0);
         tomorrow.toGMTString();
	 setCookie('tifint',x, 1);
         showInterstitial();
      }   

   }

}


//collapse the h3 marked elements that aren't overridden with a cookie
function collapse(){
 $(".collapsible").each(function(i){
   switch(getCookie($(this).attr('id')+"opened")) {
   case '0':
     $(this).addClass("collapsed");
     $(this).children("div").hide();
	 break;
   case '1':
   default:
	 $(this).removeClass("collapsed");
     $(this).children("div").slideDown();
   }
 });
 
 $(".collapsible h3").click(function(){//on collapse, hide, change the style and set a cookie
  if ($(this).parent().children("div").is(":hidden")){
   $(this).parent().removeClass("collapsed").children("div").slideDown();
   setCookie($(this).parent().attr('id') + "opened", "1", 365);
  }
  else{
   $(this).parent().addClass("collapsed").children("div").slideUp();
   setCookie($(this).parent().attr('id') + "opened", "0", 365);
  }
 });
 
$('#shareart ul').hide();

$('#shareart').mouseover(function(){

	$(this).addClass("closed");
	$('#shareart').unbind('mouseover');
	});

$('#shareart > a').click(function(){
	if ($(this).parent().children("ul").is(":hidden")){
		$(this).parent().removeClass("closed").addClass("open").children("ul").slideDown();
		
	}
	else {
		$(this).parent().removeClass("open").addClass("closed").children("ul").slideUp();		
	}
});

}//collapse

//standard setcookie/getcookie functions
function setCookie(c_name,value,expiredays)
{ var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/"; }

function getCookie(c_name)
{if (document.cookie.length>0){c_start=document.cookie.indexOf(c_name + "=");if (c_start!=-1){c_start=c_start + c_name.length+1;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 ""; }

//This is used by article template to popup pdfs
function popUpContent(URL)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=800,height=500,left=50,top=50');");
}

//interstitial
function showInterstitial(){
        var $adframe=$('<iframe id="adinterstitial" name="ad3" width="800" height="400" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" bordercolor="#000000" src="http://oascentral.cfo.com/RealMedia/ads/adstream_sx.ads/www.cfo.com/' + OAS_rns + '@x01?"><A HREF="http://oascentral.cfo.com/RealMedia/ads/adstream_sx.ads/www.cfo.com@x01?"></iframe>');	
	$('#inbannerHolder').append($adframe);
	$('#wrap').hide(); 
        $('#wrapper').hide();
	$('#top-ad').hide();
	$('#top').hide();
	$('#interstitial').show();
	$('#interstitial a').text("Skip to CFO.com");
	document.body.style.height="100%";
	inCountdown(13);
}
						
 // recursively countdown from the counttime to 0 
function inCountdown(counttime){
	if(counttime==0) hideInterstitial();
	else{ $('#inadText').text("This is an advertisement. \n You will be redirected to the article in " + counttime  + " seconds."); 
	setTimeout("inCountdown(" + (parseInt(counttime)-1) + ");", 1000); }
}

// hides interstitial; triggered by countdown or user intervention. 
function hideInterstitial(){
	$('#interstitial').hide();
	$('#top-ad').show();
	$('#wrap').show();
	$('#top').show();
        $('#wrapper').show();
        $('#tif_container').height(292);
        $('#tif_container_left').height(282);
        $('#tif_container_right').height(282);
        document.body.style.height="auto";
}
