$(document).ready(function() {  

// If you don't sanitize first, the biggerlink() function breaks when its targets are parsed
  if (top.location != location) {top.location.href = document.location.href;} // Get your own website, Digg and Facebook

  $('#moreoptions a').click(function () {
    $('#footer').fadeTo("slow", .2).fadeTo("slow", 1);
  });
    
$('ul.kwicks').kwicks({max: 250, spacing: 5});	//Kwicks functionality for Primary Nav 
$("a[rel='external']").append('<span class="external-link">&nbsp;&rarr;</span>');

var orgLinks = $('#cfimark')
orgLinks.fadeTo(1, .15); 	//fade out the CFI and CSH buttons

var activeLi = $('#sidebar li.active')
var inactiveLi = $('#sidebar ul.subnav li.inactive')

$('#csi, .bigger, div.announcement, div.article-single, #cfimark').biggerlink({otherstriggermaster:false}); 	//make entire elements clickable

$("#sidebar input.nuke").click(function () { 
  $(this).attr({value: ""});
});

activeLi.biggerlink()
inactiveLi.biggerlink()
orgLinks.biggerlink()

var unavailable = $('.unavailable');
if (unavailable.size() != 0) {
    $('div.showall').show();

$('input#showallarticles:checkbox').click(function() { //Hide articles on archive pages
  if ($(this).attr("checked") == false){
    unavailable.slideUp();
    $(this).attr("checked", false);
  }
  else {
    unavailable.slideDown();
    $(this).attr("checked", true);
  };
});

}; // if unavailable...


orgLinks.hover( 		//fade them in and out on hover
	function() {
      $(this).fadeTo('fast', .9);
		},
	function() {
      $(this).fadeTo('slow', .15);
		});

inactiveLi.fadeTo(1, .6);	//Fade out sidebar subnav links
inactiveLi.hover(
	function() {
		$(this).fadeTo(400, 1);
		activeLi.fadeTo(400, .6);
		},
	function() {
		$(this).fadeTo(400, .6);
		activeLi.fadeTo(400, 1);
		});

$("table.hash tr td:even").css("background", "#7fc5d4");
$("table.hash tr td:odd").css("background", "#cbf6ff");
$("table.zebra tr:odd").css("background", "#7fc5d4");

//if (navigator.appName == "Microsoft Internet Explorer"){ // Hate
//	activeLi.hide().show(); //Forces li.active elements to redraw in IE
//	activeLi.fadeTo(1, 1); //Otherwise they render a little wonky
		if (navigator.appVersion == '4.0 (compatible; MSIE 6.0; Windows NT 5.1)') {
			$('.kwicks li').css({backgroundImage: 'url(/img/ie6sprite1.png)'}).hide().show(); //added with js for accessibility
		}; // end ie6 test
//	} // end ie test
//else {
//  }; //end ie else

var socialMedia = $('div#socialMedia')
socialMedia.fadeTo(1, .4);
socialMedia.hover(
	function() {
		$(this).fadeTo(400, 1);
		},
	function() {
		$(this).fadeTo(400, .4);
		});


$(window).load(function() { // stuff that goes wrong if executed while elements are still loading 

  var imageDivs = $("div.image")
  var imageDivCount = imageDivs.length;

  imageDivs.each(function(){
    var imgWidth = $("img", this).width();
    $(this).width(imgWidth);
    if (imgWidth > 399) {
      $(this).css("float", "none").css("clear", "both").css("margin", "2em auto");
      };
  });

  var content       = $("#content") 
  var sidebar       = $('#sidebar')       // Cache a bunch of selectors and bludgeon all the heights into compliance
  var wrapper       = $('#wrapper')
  var sidebarHeight	= sidebar.height(); 	// Equalize #sidebar, #content, and #content_wrapper
  var bodyHeight	= content.height(); 	// get #body's height
  var bio           = $("#bio");
  var bioHeight     = bio.height();
  var fudgeFactor   = 0;


  if ( bioHeight ){
    fudgeFactor   = 30;
    if (navigator.appName == "Microsoft Internet Explorer"){ wrapper.css("paddingBottom", 20); }; // ( IE vs. CSS ) == Fail
    };

  if ( sidebarHeight > (bodyHeight + bioHeight) ) { // Balance Columns
    content.height(sidebarHeight - bioHeight - fudgeFactor);
    wrapper.hide().show()
    };

}); //End window.load

}); //End document.ready
