$(document).ready(function(){
 collapse(); //controlled by JS instead of CSS, so users without JS still see the modules
 //popups();
});


//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(){
	//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();
	document.cookie="tifint=1; expires="+tomorrow+"; path=/";
	
	var $adframe=$('<iframe frameborder="0" height="480" width="800" src="http://ad.doubleclick.net/adi/cfo2.dart/takeover;dcopt=ist;kw=;sz=800x480;tile=1;ord=' + ord + '"  scrolling="no"></iframe>');
	$('#inbannerHolder').append($adframe);
	$('#wrap').hide(); 
	$('#top-ad').hide();
	$('#interstitial').show();
	$('#interstitial a').text("Skip to Article");
	document.body.style.height="100%";
	//pageTracker._trackPageview('/interstitial/');
	pageTracker._trackEvent('interstitial','view');
	inCountdown(14);
}
						
 // 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();
	document.body.style.height="auto";
}