var numOfStories; //number of stories in currently selected year of news
var numOfCols = 10; //set this to the number of columns to display on a page
var currNewsArray = []; //currently selected year's worth of news
var newsArr = []; //associative array; stores all news array variables in memory

var storyList;
$(document).ready(function(){

    $(".finNewsLink").each(function(i){
       $(this).click(function(){
          
          var selYr = $(this).attr("yr");
          loadYear(selYr);
       }).removeAttr("href");
    });

    //default to the most recent year
    var mostRecentYr = $(".finNewsLink:first").attr("yr");
    loadYear(mostRecentYr); 
    
});


function loadYear(yr){ 
    $("#finNewsTable").fadeOut("slow");
    $("#finNewsTablePaging").fadeOut("slow");

    var yrStr= yr+'';   
    if(newsArr[yrStr] != undefined){//check if the year's worth of stories are currently in memory
         currNewsArray = newsArr[yrStr];
         numOfStories = currNewsArray.length;
         //alert("newsArray is defined, stories in memory");
    }
    else{
        //check if we already have current year's worth of stories stored as a cookie. If not, get them   
        var cookieYrName = "financialNews" + yr;
        
        if($.cookie(cookieYrName)){
           //alert("newsArray not defined, but we have data in a cookie");  
           var tmp = $.cookie(cookieYrName); 
           storyList= eval("[" + tmp + "]");
           currNewsArray = storyList;
	       numOfStories = storyList.length; 
           newsArr[yrStr] = storyList;
           
           $("#finNewsTable").fadeIn("slow");
           $("#finNewsTablePaging").fadeIn("slow");
        }
        else{//no cookie for this year, get data and save it to a cookie
           //alert("no cookie or local var, make the ajax trip");
           $.get("/GetPressRoomNewsStories.aspx?year="+yr, 
	         function(data){//function called on response
	            var fData = data.replace(/[\n\r\t]/g,''); //strip out newlines, which will break the json
			    storyList = eval("["+ fData + "]");
			    var storyListForCookie = fData = newsArr[yrStr];//keep a copy in local memory for quick retrieval
			    $("#loadingDiv").hide("slow");
             
			    //save stories from this year as a cookie
			    $.cookie(cookieYrName, storyListForCookie, { expires:7}); //expires one day from now
			    currNewsArray = storyList;
	            numOfStories = storyList.length; 
 
	         	generateTable(1, numOfCols);   
	            generateIndex();    
	            
	            $("#finNewsTable").fadeIn("slow");	
	            $("#finNewsTablePaging").fadeIn("slow");	 		 
		     });
		     
		    //show loading div while waiting for ajax to load
	        $("#loadingDiv").show("slow"); 
		    return;     
	    }

    } 	
    
	generateTable(1, numOfCols);   
	generateIndex();   
}

function generateIndex(){
    var objToAdd = "";
    var numOfPgs = Math.ceil(currNewsArray.length / numOfCols);
    
    var i;
    for(i=1; i<= numOfPgs; i++){
        objToAdd = objToAdd + "<span class=\"finNewsPager\">" + i + "</span> | ";
    }
    $("#finNewsTablePaging").html(objToAdd);
    $(".finNewsPager").click(function(){
        var pg= parseInt($(this).text());
		generateTable(pg, numOfCols);
    });
}

function generateTable(page, numCols){
	var startCol = ((page-1)*numCols);
	var endCol;
	
	if(startCol >= currNewsArray.length){//don't load past last page
	    startCol = currNewsArray.length - 1 - numCols;
	}
	
	//make sure we don't loop after reaching the end of data
	if (currNewsArray.length - 1  < startCol + numCols){
		endCol = currNewsArray.length;
	}
	else{
		endCol = startCol + numCols;
	}
    	
	//alert("startCol: "+ startCol);
	//alert("endCol: "+ endCol);
	//generate the table
	var objToAdd = "<table class=\"finNewsTbl\"><th class=\"left\">Date</th><th class=\"left\">Title</th>";
	var i;
	for(i=startCol; i<endCol; i++){
        //objToAdd=objToAdd + "<tr><td class=\"left\">" + formatDate(currNewsArray[i].date) + "</td><td class=\"right\"><a href=\"/our-company/press-room/press-releases/press-release.aspx?StoryID=" + currNewsArray[i].id + "\">" + stripCredits(currNewsArray[i].title) + "</a></td></tr>";
		//should be the above by Im having a permalink issue
		objToAdd=objToAdd + "<tr><td class=\"left\">" + formatDate(currNewsArray[i].date) + "</td><td class=\"right\"><a href=\"" + currNewsArray[i].id + "\" target=\"_self\">" + currNewsArray[i].title + "</a></td></tr>";
	}
	objToAdd = objToAdd + "</table>";
    	
	//add table to page
	$("#finNewsTable").html(objToAdd);
	
}

function formatDate(ud){
   var yr = ud.substring(0,4);
   var mo = ud.substring(4,6);
   var day = ud.substring(6,8);
   var fd = mo + "/" + day + "/" + yr;
   return fd;
}