/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

CampaignDonation = function(campaignId, pageCount, profileBasePath) {

    var _profileBasePath = profileBasePath;
    var _campaignId = campaignId;
    var _pageCount = parseInt(pageCount);
    var _currentPage = 1;
    var _pagesDiv = $('div.paginator span.pages');
    var _prevLink = $('div.paginator span.prev a');
    var _nextLink = $('div.paginator span.next a');
    var _fadeSpeed = 500;

    _init();

    function _init() {
        //set pages
        _nextLink.click(function() {
            if (_currentPage < _pageCount) {
                _currentPage++;
                _changePage(false);
            }
        });
        _prevLink.click(function() {
            if (_currentPage != 1) {
                _currentPage--;
                _changePage(false);
            }
        })

        _addPages(1, _pageCount);
        _updatePageNumber();
        _updatePreviousNext();
    }

    /*
     * Should only be run once.
     */
    function _addPages(startPage, pageCount) {
        _pagesDiv.empty();

        for (var i = startPage; i <= pageCount & i < (startPage + 11); i++) {
            _pagesDiv.append('<a href="javascript: void(0);" id="page_' + i
                + '">' + i + '</a>');
        }

        $('span.pages a').click(function() {
            _currentPage = $(this).attr("id").split("_")[1];
            _changePage();
        });
    }

    function _changePage() {
        var waitHeight = $('div#pleaseWaitSpinner').height();
        var contentHeight = $('div#recent_donations div.content').height();
        var tableHeight = parseInt($('table#donationTable').height());

        $('div#recent_donations div.content').css('height', contentHeight + 'px');

        $('div#pleaseWaitSpinner').css('padding-top', ((tableHeight - waitHeight) / 2) + 'px');

        $('div.paginator').hide();

        $('table#donationTable').fadeOut(_fadeSpeed, function() {

            $('div#pleaseWaitSpinner').fadeIn(_fadeSpeed);
            $.post("/p/campaignDonationListChange?campaign_id=" + campaignId + "&page=" + _currentPage, _changeCallback);

        });
    }

    function _changeCallback(data) {
        _updateRows(data);
        $('div#pleaseWaitSpinner').fadeOut(_fadeSpeed, function() {
            $('div#recent_donations div.content').css('height', '');
            $('table#donationTable').fadeIn(_fadeSpeed);
            $('div.paginator').show();
            //adjust highlighting of page
            _updatePreviousNext();
            //adjust pageNumber
            _updatePageNumber();
        });
    }

    function _updateRows(data) {
        data = eval('(' + data + ')');


        //clear the table rows
        var donationRows = $('tr.donationRow');

        for (var i = 0; i < donationRows.length; i++) {

            if (i < data.payments.length) {
                var payment = data.payments[i];
                var tds = $('td', donationRows[i]);

                //update name
                if (payment.member_id && !payment.isAnonymous) {
                    $(tds[0]).html(
                        '<a class="blue_link" href="' + _profileBasePath + payment.member_id + '">' + payment.name + '</a>'
                    );
                } else if (payment.isAnonymous) {
                    $(tds[0]).html(
                        'Anonymous'
                    );
                } else {
                    $(tds[0]).html(payment.name);
                }

                //update amount
                var amount = payment.amount;

                //shorten to just $xx
                if (/\d{1,}\.00/.test(amount)) {
                    amount = "$" + amount.match(/(\d{1,})\.00/)[1];
                } else {
                    amount = "$" + amount;
                }

                $(tds[1]).html(amount);

                //update date
                $(tds[2]).html(payment.date);

                //update comment
                $(tds[3]).html(payment.comment);
                $(donationRows[i]).show();
            } else {
                //no payment hide the row
                $(donationRows[i]).hide();
            }
        }
    }

    function _updatePreviousNext() {
        if (_currentPage == 1) {
            $('div.paginator span.prev a').hide();
        } else {
            $('div.paginator span.prev a').show();
        }

        if (_currentPage < _pageCount) {
            $('div.paginator span.next a').show();
        } else {
            $('div.paginator span.next a').hide();
        }
    }

    function _updatePageNumber() {
        //readd the page numbers with your number at center
        var pageCount = parseInt(_pageCount);
        var currentPage = parseInt(_currentPage);
        if (pageCount > 10) {

            if ((currentPage + 5) >= pageCount) {
                //make start page 10 less than _pageCount
                _addPages(pageCount - 10, pageCount);
            } else if (_currentPage > 5) {
                _addPages(currentPage - 5, pageCount);
            } else {
                _addPages(1, pageCount);
            }
        }

        $('span.pages a').attr('class', '');
        $('span.pages a#page_' + _currentPage).attr('class', 'selected');
    }
}
