//global.js - all functions called from more than one page on CFO

// promoname should change for every alert promo; showalertbar is true or false to display
// change the $alerttext variable inside of the alertbar function with new text when a new promo comes on
var promoname="bostonsurvey"
var showalertbar=false;

// document.ready - generally better than onload. do not add any functions unless you are checking for the presence of the variable you are calling or modifying (if (typeof(x)!=="undefined"){if(x){}}). (jQuery selectors do this automatically)
$(document).ready(function(){
	popups();
	if(showalertbar && !getCookie(promoname)) {alertbar(promoname)};
});

//pops up non-cfo links and pdfs in a new window, tracks hostname or filename in GA
function popups(){
 $("a[href^='http:']").bind('click', function(){
	if(this.host.indexOf("cfo.com")<0){	 
	 day = new Date(); id = day.getTime();
	 window.open(this.href, id);
	 pageTracker._trackPageview('/outgoing/' + this.host);
	 return false;
	}
 });
 $("a[href$='pdf'], a[href$='xls']").bind('click', function(){
	 day = new Date(); id = day.getTime();
	 window.open(this.href, id);
	 pageTracker._trackPageview('/downloads/' + this.pathname);	
	 return false;
 });
}


//alertbar  - use to show an alert bar at the top of every page. be sure to change $alerttext's first chain link to new text/link
function alertbar(promoname){
	var $alerttext=$('<a href="http://www.surveymonkey.com/s.aspx?sm=s8oqNmF2wJzGxSyokzLB5w_3d_3d"><strong>Boston Readers:</strong> Participate in a 2-hour CFO.com focus group and earn $150 &nbsp;&raquo;</a>').css('float', 'left').css('overflow', 'auto');
	var $closelink=$('<a href="javascript:void(0);" style="font-size:0.8em; float:right; margin-right:20px; padding-top:2px;" id="alertboxclose">no thanks</a>').click(function(){$alertbar.slideUp(); setCookie(promoname, 'true', 365); return false;});
    var $alertbar=$('<div id="alertbar"></div>').append($alerttext).append($closelink).css('background', 'transparent url(http://www.cfo.com/images/alertbarbg.png) repeat-x scroll left top').css('font-family', 'arial').css('text-align', 'center').css('padding', '5px 0 10px 0').css('height', '28px').prependTo($('body'));
    $alerttext.width($('body').width()-$closelink.width()-30);
	
}

//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 ""; }

function about_nav(opennav){
	  $("#aboutnav").children("li").each( function(){
		if($(this)[0].id !== opennav){ $(this).children("ul").hide(); }
	  });
};