function HideContent(d) {
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
//document.getElementById(d).style.display = "table-row";
document.getElementById(d).style.display = '';
}
function ReverseDisplay(d) {
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

$(document).ready(function(){


	// text size cookie
 	var sizeCookie = readCookie("size");
 	// sizeCookie ? setTextSize(sizeCookie, false) : setTextSize('size2', true);
 	if (sizeCookie) setTextSize(sizeCookie, false);

	$("#size4").click(function(){setTextSize('size4', true); return false;});
	$("#size3").click(function(){setTextSize('size3', true); return false;});
	$("#size2").click(function(){setTextSize('size2', true); return false;});
	$("#size1").click(function(){setTextSize('size1', true); return false;});
	$('a#share_link').click(function() {
		if ($('#share_list').css('display') == 'none') {$('#share_list').fadeIn('fast');}
		else {$('#share_list').fadeOut('fast');}
	});

	//make all article body text to outside sites open in a new window
	$('.section1 p a').not("a[href*='truthdig'], a[href*='addthis'], a[href*='#below']").attr("target", '_blank');



	//outgoing link tracking
	$('a:not(.popupwindow)').filter(function() {
		var thehref = this;
		if (thehref.hostname && thehref.hostname !== location.hostname) {
			$(thehref).not(".noautolink").bind('click keypress', function(event) {
				var code=event.charCode || event.keyCode;
				if (!code || (code && code == 13)) {
					if(pageTracker){
						var fixedLink = this.href;
						fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
						fixedLink = '/outgoing/' + fixedLink;
						pageTracker._trackPageview(fixedLink);
					};
				};
			});
		};
	});





	// make the right column ad float
	/*
		if( /Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {$('.floating_box').removeClass('floating_box_absolute');}

		var win_top = $(window);
		if( $('.floating_box_absolute').length ){

			var top_pos = $('.floating_box_absolute').offset().top;

			$(window).scroll(function(){
				// console.log(win_top.scrollTop() , '  ' , top_pos);
				if( win_top.scrollTop() >= top_pos - 0 ){$('.floating_box_absolute').addClass('floating_box_fixed');} else {$('.floating_box_absolute').removeClass('floating_box_fixed');}
			});
		}
	*/

});





function setTextSize(new_size, ie_fix) {
	var elements = "body, body div, body p, body th, body td, body li, body dd";
	var smaller_elements = ".article_tools, p.mailinglist, div.continue";

	if ($.browser.msie && ie_fix == true) {
		switch (new_size) {
			case 'size4' : 
				new_size = 'size3';
			break;
			case 'size3' : 
				new_size = 'size2';
			break;
			case 'size2' : 
				new_size = 'size1';
			break;
			case 'size1' : 
				new_size = 'size0';
			break;
		}	
	}

	
	switch (new_size) {
		case 'size4' : 
			$(elements).css({fontSize:"large"});
		break;
		case 'size3' : 
			$(elements).css({fontSize:"medium"});
		break;
		case 'size1' : 
			$(elements).css({fontSize:"x-small"});
		break;
		case 'size0' : 
			$(elements).css({fontSize:"xx-small"});
		break;
		default : // this is equivalent to a size2
			$(elements).css({fontSize:"small"});
		break;
	}

	$(smaller_elements).css({fontSize:"smaller"});

 	createCookie("size", new_size, 365);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// function to highlight input fields
function highlightColor(theForm,theElement,theColor){document.forms[theForm].elements[theElement].style.backgroundColor=theColor;}

// function to blank out text fields with focused
var blanked = new Array();
function blank(textfield) {
  if (blanked[textfield.name] != true)
  {
    blanked[textfield.name] = true;
    textfield.value = "";
  }
}


// function to swap the newsletter box arrow
intImage = 2;
function swapImage() {
switch (intImage) {
 case 1:
   IMG1.src = "/images/submit_arrow.gif"
   intImage = 2
   return(false);
case 2:
   IMG1.src = "/images/submit_arrow_over.gif"
   intImage = 1
   return(false);
 }
}


;(function($){
	
	$(document).ready(function(){
		if ($('.td_related_entries_container').length > 0) {
			new SocialFly(document.getElementById('socialfly'), {
				visibleClass : 'socialfly-visible',
				closingClass : 'socialfly-closing',
				autoHide     : true
			});
		}
	});

	function SocialFly(element, config) {
		this.element = element;
		this.settings = config;
		this.visible = false;
		this.init();
	};

	SocialFly.prototype.init = function() {
		this.bind();
		this.checkPosition();
	};

	SocialFly.prototype.bind = function() {
		var that = this;

		$(window).on('scroll.socialfly resize.socialfly', function() {that.checkPosition();});

		$(that.element)
			.on('click', '.socialfly-close', function(event){
				event.preventDefault();
				createCookie('facebook_socialfly', 'hide', 1);  // 1 == keep the cookie for 1 day  
				that.hide();
			});
	};



	$.fn.isOnScreen = function(){
	
		var win = $(window);
	
		var viewport = {
			top : win.scrollTop(),
			left : win.scrollLeft()
		};

		viewport.right = viewport.left + win.width();
		viewport.bottom = viewport.top + win.height();
	
		var bounds = this.offset();
		bounds.right = bounds.left + this.outerWidth();
		bounds.bottom = bounds.top + this.outerHeight();

		return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
	
	};


	SocialFly.prototype.checkPosition = function() {

		if ($('.td_related_entries_container').length > 0) {

			if(window.pageYOffset >= $('div.entry_body_wrapper .entry_body_wrapper').height() / 2 + 200 && (readCookie("facebook_socialfly") != "hide") && window.pageYOffset <= $('div.entry_body_wrapper .entry_body_wrapper').height() + 200)
			{this.show();} else {this.hide();}
		}

	};

	SocialFly.prototype.unbind = function($target, event) {
		$target.off(event + '.socialfly')
	};

	SocialFly.prototype.show = function() {
		if(this.visible){return;};

		this.visible = true;

		if(!this.settings.autoHide){
			this.unbind($(window), 'scroll');
			this.unbind($(window), 'resize');
		};

		$(this.element).addClass(this.settings.visibleClass);

	};

	SocialFly.prototype.hide = function() {
		if(!this.visible){return;};

		this.visible = false;

		$(this.element).addClass(this.settings.closingClass).removeClass(this.settings.visibleClass);
	};

})(jQuery);