﻿

$(window).addEvent('domready', function() {

    var PagerSettings = { pagecount: $$('.list-page').length, curpage: 0 };

    $$('.list-page').each(function(item, index) {
        if ($(item).getStyle('display') != 'none')
            PagerSettings.curpage = index;
    });

    function showCurPage(oldpage) {

        // TURN OFF CUR PAGE
        ($$('.list-page')[oldpage]).hide();
        // TURN ON NEW PAGE
        ($$('.list-page')[PagerSettings.curpage]).show();

        if (PagerSettings.curpage == 0)
            $$('.pager a.prev').each(function(item, index) { $(item).addClass('off'); });
        else
            $$('.pager a.prev').each(function(item, index) { $(item).removeClass('off'); });

        if (PagerSettings.curpage == (PagerSettings.pagecount - 1))
            $$('.pager a.next').each(function(item, index) { $(item).addClass('off'); });
        else
            $$('.pager a.next').each(function(item, index) { $(item).removeClass('off'); });
    }


    $$('.pager a').each(function(item, index) {
        if ($(item).hasClass('prev')) {
            $(item).addEvent('click', function() {
                var oldpage = PagerSettings.curpage;
                PagerSettings.curpage = PagerSettings.curpage == 0 ? 0 : PagerSettings.curpage - 1;
                showCurPage(oldpage);
            });
        }
        if ($(item).hasClass('next')) {
            $(item).addEvent('click', function() {
                var oldpage = PagerSettings.curpage;
                PagerSettings.curpage = PagerSettings.curpage == (PagerSettings.pagecount - 1) ? (PagerSettings.pagecount - 1) : PagerSettings.curpage + 1;
                showCurPage(oldpage);
            });
        }
    });



});

