$(document).ready(function() {
    // dimension settings
    var minViewerWidth = 1000;
    var minViewerHeight = 541;
    var viewerAspectRatio = 1.848;
    var viewerPadWidth = 2;
    var viewerPadHeight = 102;

    // counters
    var pageViews = 0;
    var adViews = 0;

    $('#previous').click(function() {
        checkForAd();
        viewer.gotoPreviousPages();
    })

    $('#next').click(function() {
        if (checkForAd() == false)
            viewer.gotoNextPages();
    })

    $('#first').click(function() {
        checkForAd();
        viewer.gotoFirstPages();
    })

    $('#last').click(function() {
        checkForAd();
        viewer.gotoLastPages();
    })

    $('#gotoPage').click(function() {
        var page = $('#pageNum').val();
        var maxPage = viewer.getNumberOfPages();
        if (page) {
            if (page < 1)
                page = 1;
            if (page > maxPage)
                page = maxPage;
            $('#pageNum').val(page);
            checkForAd();
            viewer.gotoPage(page);
        }
    })

    $('#contents').click(function() {
        checkForAd();
        viewer.gotoPage(3);
    });

    var nextArticle = 0;
    var previousArticle = 0;

    function refreshArticlesVars() {
        nextArticle = 0;
        previousArticle = 0;
        var pages = viewer.getCurrentPages();
        $.each(articles, function(page, title) {
            if(page < pages.firstPage) {
                previousArticle = page;
            }
            if(page > pages.lastPage) {
                nextArticle = page;
                return false;
            }
        });
    }

    $('#nextArticle').click(function() {
        if (checkForAd() == false && nextArticle != 0)
            viewer.gotoPage(nextArticle);
    });

    $('#previousArticle').click(function() {
        checkForAd();
        if (previousArticle != 0)
            viewer.gotoPage(previousArticle);
    });

    function checkForAd() {
        if($('#frame1').css('top') == "-20000px") {
            $('.frames').toggleClass('buffer');
            $('#fullpage-ad').attr('src','/ads/fullpage/?issue=' + issue + '&page=' + pages.firstPage);
            return true;
        }
        return false;
    }

    function resizePageElements() {
        $('#viewerContent').resizeToWindow(viewerAspectRatio, viewerPadWidth, viewerPadHeight, minViewerWidth, minViewerHeight);
        $('#fullpage-ad').resizeToWindow(viewerAspectRatio, viewerPadWidth, viewerPadHeight, minViewerWidth, minViewerHeight);
    }

    resizePageElements();
    $('.frames').horizontallyCenter();
    $(window).resize(function() {
        resizePageElements();
        $('.frames').horizontallyCenter();
    })

    viewer.onpublicationload = function(event) {
        if(startPage != 1)
            viewer.gotoPage(startPage);
        $('#lastPage').html(viewer.getNumberOfPages());
        $('#fullpage-ad').attr('src','/ads/fullpage/?issue=' + issue + '&page=' + startPage);
    }

    viewer.oncurrentpageschange = function(event) {
        pageViews++;
        pages = viewer.getCurrentPages();

        // rotate ads
        if (adViews <= adMaxViews && pageViews % adFrequency == 0) {
            adViews++;
            $('.frames').toggleClass('buffer');
        }
        $('#leaderboard-ad').fadeTo(100, 0);
        $('#leaderboard-ad').attr('src', '/ads/leaderboard/?issue=' + issue + '&page=' + pages.firstPage);
        $('#leaderboard-ad').fadeTo(1000, 1);

        // update page number
        $('#curPage').html(viewer.getDisplayableCurrentPages());

        // refresh the next and previous articles
        refreshArticlesVars();
    }

    $('.button-container > *').mousedown(function() {
        $(this).css('font-weight', 'bold');
    });

    $('.button-container > *').mouseup(function() {
        $(this).css('font-weight', 'normal');
    });
});

;(function($) {
    $.fn.resizeTo = function(width, height) {
        $(this).width(width);
        $(this).height(height);
    };

    $.fn.autoResize = function(aspectRatio, maxWidth, maxHeight, minWidth, minHeight) {
        width = minWidth != 0 && minWidth > maxWidth ? minWidth : maxWidth;
        height = minHeight != 0 && minHeight > maxHeight ? minHeight : maxHeight;
        curAspectRatio = width / height;
        if (curAspectRatio > aspectRatio)
            width = height * aspectRatio;
        else if (curAspectRatio < aspectRatio)
            height = width / aspectRatio;
        $(this).resizeTo(width, height);
    };

    $.fn.resizeToWindow = function(aspectRatio, padWidth, padHeight, minWidth, minHeight) {
        var maxWidth = $(window).width() - padWidth;
        var maxHeight = $(window).height() - padHeight;
        $(this).autoResize(aspectRatio, maxWidth, maxHeight, minWidth, minHeight);
    };

    $.fn.horizontallyCenter = function() {
        var itemWidth = $(this).width();
        var windowWidth = $(window).width();
        var left = (windowWidth / 2) - (itemWidth/ 2);
        left = left < 0 ? 0 : left;
        $(this).css('left', left);
    };
})(jQuery);
