﻿$(document).ready(function() {
    FormatThumbnails();
    InitializePaging();        
});


function FormatThumbnails()
{
    var thumbnailSelector = "div.article_page > ul > li";

    $(thumbnailSelector + ":nth-child(3n)").css("padding-right", "0");
}

function InitializePaging()
{
    var pageSelector = "div.article_page";
        
    $(pageSelector + ":gt(0)").hide();    
    $(pageSelector + ":first").addClass("first_page");
    $(pageSelector + ":last").addClass("last_page");
    $(pageSelector + ":first").addClass("current_page");    
    
    $("a.previous_page").click(function() {
        ChangePage('p');
    });   
    
    $("a.next_page").click(function() {
        ChangePage('n');
    });   
    
    $("a.previous_page").hide();
}

function ChangePage(direction)
{
    var currentPage = $("div.current_page").first();
    
    if(currentPage != null)
    {        
        var currentPageNumber = parseInt($("#current-page-top").text());
        var newPageNumber;
        
        if(direction == 'p')
        {
            newPageNumber = currentPageNumber - 1;
        }
        else if(direction == 'n')
        {
            newPageNumber = currentPageNumber + 1;
        }                 
    
        var nextPage = GetSibling($(currentPage), 'n');
        var prevPage = GetSibling($(currentPage), 'p');
        var pageToShow;
        
        if(direction == 'p' && prevPage != null)
        {        
            pageToShow = prevPage;
        }
        else if(direction == 'n' && nextPage != null)
        {        
            pageToShow = nextPage;
        }   
        else
        {
            pageToShow = null;
        }     
    
        if(pageToShow != null)
        {
            SwapPages(currentPage, pageToShow);
            SetPageNumber(newPageNumber);
        }
        else
        {
            ShowPagingErrorMessage('page to show');
        }
    }
    else
    {
        ShowPagingErrorMessage();
    }       
}

function SwapPages(currentPage, pageToShow)
{
    SetCurrentPage(currentPage, pageToShow);
    SetButtons(pageToShow);
    AnimateNewPage(currentPage, pageToShow);
}

function SetCurrentPage(currentPage, pageToShow)
{
    $(currentPage).removeClass("current_page");
    $(pageToShow).addClass("current_page");
}

function SetPageNumber(n)
{
    $("span#current-page-top").text(n);
    $("span#current-page-bottom").text(n);
}

function SetButtons(pageToShow)
{
    $("a.previous_page").hide();
    $("a.next_page").hide();

    if(GetSibling(pageToShow, 'p') != null)
    {
        $("a.previous_page").show();
    }
    
    if(GetSibling(pageToShow, 'n') != null)
    {        
        $("a.next_page").show();
    } 
    
    if($("a.next_page:visible").length == 0)
    {
        $("a.previous_page").css("border-right-width", "0");
    }       
    else
    {
        $("a.previous_page").css("border-right-width", "1px");
    }
}

function AnimateNewPage(currentPage, pageToShow)
{
    $(currentPage).hide();  
    $(pageToShow).show();
    LoadThumbnailImages(pageToShow);
    AddVideoIcons(pageToShow);
}

function LoadThumbnailImages(container)
{
    var loaderCssClass = "article_thumbnail_loader";

    $(container).find("." + loaderCssClass).each(function() {
        LoadThumbnailImage($(this), loaderCssClass, 130, 198);
    });
}

function LoadThumbnailImage(loader, loaderClass, defaultHeight, defaultWidth)
{
    var iSrc = $(loader).attr("src");
    var iAlt = $(loader).attr("alt");
    var iHeight = $(loader).attr("height");
    var iWidth = $(loader).attr("width");
    
    var img = new Image();
    
    $(img)
        .load(function() {
            $(this).hide();
            $(loader).replaceWith(this);
            $(this).fadeIn('medium');            
        })    
        .attr({
            'src' : iSrc,
            'alt': (iAlt != undefined) ? iAlt : '',
            'title' : (iAlt != undefined) ? iAlt : '',
            'height' : (iHeight != undefined) ? iHeight : defaultHeight,
            'width' : (iWidth != undefined) ? iWidth : defaultWidth        
        });              
}

function GetSibling(page, direction)
{
    var pageToReturn = null;

    if(direction == 'p')
    {
        if(!$(page).hasClass("first_page"))
        {
            pageToReturn = $(page).prev("div.article_page");
        }
    }
    else if(direction == 'n')
    {
        if(!$(page).hasClass("last_page"))
        {
            return $(page).next("div.article_page");
        }
    }   

    return pageToReturn;    
}

function ShowPagingErrorMessage(m)
{
    alert('Error: ' + m);
}


