var listPageSize = 6;

$(document).ready(function(){
	initList("/news/", "#news");
});

function initList(directory, id)
{
	//Set the fade level then hide the holder before the content loads
	if (!jQuery.browser.msie)
	{
		$(id + " .content").fadeTo(1,0);
	}
	$(id + " .content").css("display", "none");
	
	//Load the content
	var now = new Date();
    var url = directory + "buzzList.aspx?nocache=" + now.getTime();
	$(id + " .content").load(url, function(){jumpToCurrentPage(null, id)});
}

function updateList(page, id)
{
   	if (!jQuery.browser.msie)
	{
		$(id + " .content").fadeTo(150,0,function(){paginateList(page, id, false)});
	}
	else
	{
		paginateList(page, id, false);
	}
}

function jumpToCurrentPage(page, id)
{
	var currentPageUrl = document.URL;
    currentPageUrl = currentPageUrl.split("/");
    currentPageUrl = currentPageUrl[currentPageUrl.length-1];
	var currentPageNumber = -1;
	var currentItemNumber = -1;
    
	
    //Grab all of the items
    var items = $(id + " ul li");
	
    if (page == null || page == -1)
    {
        page = 1;
    }
	
	var numberOfItems = items.length;
    var pageLength = listPageSize; //Set externally in the holding document
    var pages = Math.ceil(numberOfItems/pageLength);
	
    for (var i=0; i<numberOfItems; i++)
    {
		//Check if this is the current page
		if (items[i].innerHTML.indexOf(currentPageUrl) >= 0 && currentPageUrl != "")
		{
			currentItemNumber = i+1;
			currentPageNumber = Math.ceil(currentItemNumber/listPageSize);
			break;
		}
	}
	
	//console.log(numberOfItems + ":" + pageLength + ":" + currentItemNumber + ":" + currentPageNumber);
	
	if (currentPageNumber <= pages)
	{
		paginateList(currentPageNumber, id, true);
	}
}

function paginateList(page, id, addControl)
{
    var currentPageUrl = document.URL;
    currentPageUrl = currentPageUrl.split("/");
    currentPageUrl = currentPageUrl[currentPageUrl.length-1];  
	
    //Grab all of the items
    var items = $(id + " ul li");                                                           
    
    //var addControl = false;
    if (page == null || page == -1)
    {
        page = 1;
        //addControl = true;
    }
	
	var numberOfItems = items.length;
    var pageLength = listPageSize; //Set externally in the holding document
    var pages = Math.ceil(numberOfItems/pageLength);
    
    //Iterate through the items and hide all those that aren't on this page
    var lastItem = pageLength*page;
    var firstItem = lastItem - pageLength;
	
    for (var i=0; i<numberOfItems; i++)
    {
        if (i >= lastItem || i < firstItem)
        {
            $(items[i]).css("display", "none");
        }
        else
        {
            $(items[i]).css("display", "block");
            
            //Check if this is the current page
            if (items[i].innerHTML.indexOf(currentPageUrl) >= 0 && currentPageUrl != "")
            {
                $(items[i]).addClass("current");
            }
        }
        $(items[i]).removeClass("topItem");
    }
    //Ensure the top item doesn't get a border
    $(items[firstItem]).addClass("topItem");
	
	//Add corners to any new items that need them (on the first pass only)
	if (addControl)
	{
		addCornersToElements("ul.menu li");
	}
	
    //Add the pagination controls
    if (addControl && pages>1)
    {
        var controlHTML = "<div class=\"controller\">";
        for (var i=1; i<=pages; i++)
        {
            controlHTML += "<a href=\"javascript:updateList(" + i + ",'" + id + "');\">" + i + "</a> ";
        }
        controlHTML += "<div class=\"pin\"></div></div>";
        $(id + " .content").after(controlHTML);
    }
    //Unlight all the buttons and return the list to light the current one
    var pageButtons = $(id + " .controller a").removeClass("current");
    $(pageButtons[page-1]).addClass("current");
    
    //Hide all buttons that are too far away
    for (var i=0; i<=pages; i++)
    {
        $(pageButtons[i]).css("display", "none");
        
        //In the zone of the current page || left end of block || right end of block
        if ((i>=page-3 && i<=page+1) || (i<5 && page<3) || (i>pages-6 && page>pages-3))
        {
            $(pageButtons[i]).css("display", "block");
        }
    }
    
    //Unhide the holder and fade the page in
    $(id + " .content").css("display", "block");
	if (!jQuery.browser.msie)
	{
		$(id + " .content").fadeTo(300,1);
	}
}