﻿function ChangeGridPage(id)
{
    var currentPage = parseInt(id.substring(5));
    var lastPage = $("ul#paging > li:not(\".edge_button\")").length - 1;

    $("div#thumbnails .champion_grid_page").css("display", "none");
    
    var pageToShow = "div#thumbnails #bio-" + id;        
    $(pageToShow).css("display", "block");

    $("ul#paging > li#page-previous").unbind();    
    $("ul#paging > li#page-previous").click(function() {
        ChangeGridPage("page-" + (currentPage - 1));
    });

    $("ul#paging > li#page-next").unbind();
    $("ul#paging > li#page-next").click(function() {
        ChangeGridPage("page-" + (currentPage + 1));
    });

    // if a champion overlay is open, close it.
    CloseChampionOverlay();

    ChangePage(currentPage, lastPage);    
}

$(document).ready(function() {
    AddPagingHoverEffects();    
    
    $("ul#paging > li.edge_button").click(function() {
        if(this.id == "page-first")
        {
            ChangeGridPage("page-0");
        }
        else if(this.id == "page-last")
        {   
            var last = $("ul#paging > li:not(\".edge_button\")").length - 1;
            ChangeGridPage("page-" + last);
        }
    });

    $("ul#paging > li:not(\".edge_button\")").click(function() {
        ChangeGridPage(this.id);
    });

    $("div#thumbnails div.thumbnail_wrapper:nth-child(3n)").css("padding-right", "0px");        

    $("div#thumbnails div.thumbnail_wrapper").click(function() {
        ShowChampionOverlay(this.id);        
    });
    
    $("div.popup_bio_content div.popup_header_wrapper a.close_button").click(function() {
        CloseChampionOverlay();        
    });
    
    if(ReadPagingUrl() > -1)
    {
        var pg = ReadPagingUrl();
        ChangeGridPage("page-" + pg);
    }
    else 
    {
        ChangeGridPage("page-0");
    }
});
