$(document).ready(function(){

   $(".tablesorter").tablesorter();

   if($(".story-mini").length){
    $(".story-full").hide();
   }

   $(".story-readmore").click(function(e){
             e.preventDefault();
             
             if($(this).hasClass("ShowHideLink_Closed"))
                $(this).removeClass("ShowHideLink_Closed").addClass("ShowHideLink_Open");

             else if($(this).hasClass("ShowHideLink_Open"))
                 $(this).removeClass("ShowHideLink_Open").addClass("ShowHideLink_Closed");
            
             $(".story-full").toggle();

             });
   
   var currDiv;
   var newDiv;

   $(".swapDiv").hide();
   $(".swapStart").show();

   $("a.swapDivSelector").click(function(e){
         e.preventDefault();
         
         // get the href thats been clicked
         currDiv = $(this).attr('href');
         
         // Show this div
         // get the div that it references         
         // get the divs siblings
         $(currDiv).show().siblings(".swapDiv").each(function(){
         	// hide the div
         	$(this).hide();
         	
         	// for each sibling swapDiv div, figure out the href of the the link to it (the href matches its id)
         	var thisid = "#" + $(this).attr('id');
         	// remove the active class on links to this div
         	$("a[href='" + thisid + "']").removeClass('activeSwapDivSelector');
         });
         
         // add the active class to the link that was clicked.
         $(this).addClass('activeSwapDivSelector');       
     });
     
     // if theres a filter list
     $("ul.filterList").each(function() { 
     	// build an unordered list before it for the filter controls
     		var listItemString = "";
     		var filterListString = "";
     		// build an array to figure out each of the filter controls
     		var optionTexts = [];
     		$(this).children("li").each(function() { 
     			optionTexts.push($(this).attr("class"));
     			$(this).hide();
     		});
     		optionTexts.sort();
     		optionTexts.reverse();
     		
     		var lastYear = 0;
     		var selectedYear = "";
	     	for ( var i=0, len=optionTexts.length; i<len; ++i ){
	     	 	if (lastYear != optionTexts[i])
	     	 	{
	     	 	  var selectedClass = "";
	     	 	  if (i == 0){ 
	     	 	  	selectedYear = optionTexts[i];
	     	 	  	selectedClass = "current first";
	     	 	  };
	     	 	 
	     	 	 // need to figure out if this is the last one so that the class of -last- can be applied to the li 
	     	 	 	     	 	  
	     	 	  listItemString += "<li class='" + selectedClass  + "'><a href='#'>" + optionTexts[i] + "</a></li>"
	     	 	}
	     	 	lastYear = optionTexts[i];	     	 
	         }
     		
     		filterListString  = "<ul class=filterListSwitcher>" + listItemString + "</ul>";
     		
     		$(this).before(filterListString);
     		
     		$(this).children("li").filter("." + selectedYear).show();     	
     	
     });
     
     $("ul.filterListSwitcher > li:last").addClass("last");
     
     $("ul.filterListSwitcher > li a").click(function(e) { 
     	e.preventDefault();
     	$("ul.filterListSwitcher > li").removeClass("current");
     	$(this).parent().addClass("current");
     	$("ul.filterList").children("li").hide().filter("." + $(this).text()).show();  
     });
    
});

