var GamePaginator = function(numPerPage) {
    this.numPerPage = numPerPage;
    this.currentPage = 1;
    this.calcPages();
    this.buttonSize = {width:20, height:68};
}

GamePaginator.prototype = {
    setUpGameList : function() {
        var self = this;
    },

    calcPages : function() {
        this.numOfGames = $('.game_list li:visible').length;
        this.totalPages = Math.ceil(this.numOfGames / this.numPerPage);
    },

    enablePaginateButtons : function() {
        var self = this;
        var prevButton = $('.paginate_prev_button').unbind('click');
        var nextButton = $('.paginate_next_button').unbind('click');
        if (self.totalPages <= 1) {
            prevButton.hide();
            nextButton.hide();
            return;
        }
  
        prevButton.mouseover(function() {
                $(this).css('cursor', 'pointer');
            }).click(function() {
                self.moveToPage(self.currentPage - 1);
            }).show();
        nextButton.mouseover(function() {
                $(this).css('cursor', 'pointer');
            }).click(function() {
                self.moveToPage(self.currentPage + 1);
            }).show();

        if (IE6) {
            prevButton.css(self.buttonSize).css('position', 'absolute');
            nextButton.css(self.buttonSize).css('position', 'absolute');
        }

    },

    initPages : function() {
        for (var i = 1; i <= this.totalPages; i++) {
            var a = "<span class='a_page p" + i + "'>" + i + "</span>";
            $('.game_list_block .pages_box div.pages').append(a);
        }
        this.setCurrentPage(1);
    },

    setUp : function() {
        this.setUpGameList();
        this.initPages();
        this.enablePaginateButtons();
    },

    setCurrentPage : function(num) {
        var self = this;
        var target = '.game_list_block .pages_box div.pages span.p' + num;
        $(target).siblings().removeClass('current_page')
            .mouseover(function() {
                $(this).css('cursor', 'pointer');
            })
            .click(function() {
                var toPage = parseInt($(this).text());
                self.moveToPage(toPage);
            });
        $(target).addClass('current_page')
            .mouseover(function() {
                $(this).css('cursor', 'default');
            });
        self.currentPage = num;
    },

    moveToPage : function(toPage) {
        var self = this;
        if (self.totalPages == 1) {
            return;
        }

        if (toPage > self.currentPage && toPage > self.totalPages) {
            return self.moveToPage(1);
        }
        if (toPage < self.currentPage && toPage < 1) {
            return self.moveToPage(self.totalPages);
        }

        if (toPage > self.currentPage && toPage <= self.totalPages) {
            var sIndex = (self.currentPage - 1) * self.numPerPage;
            var eIndex = (toPage - 1) * self.numPerPage;
            $('.game_list li').slice(sIndex, eIndex).hide();
            this.setCurrentPage(toPage);
        } else if (toPage < self.currentPage && toPage >= 1) {
            var sIndex = (toPage - 1) * self.numPerPage;
            var eIndex = (self.currentPage - 1) * self.numPerPage;
            $('.game_list li').slice(sIndex, eIndex).show();
            self.setCurrentPage(toPage);
        }
    }
};


var XDGamePaginator = function(numPerPage) {
    GamePaginator.apply(this, arguments);
    this.currentCat = 'all';
    this.buttonSize = {width:22, height:43};
}

XDGamePaginator.prototype = new GamePaginator();
XDGamePaginator.prototype.initPages = function() {
    var pages = "PAGES <span class='current_page'></span>/<span class='total_pages'></span>";
    $('.pages_box').append(pages);
    this.setCurrentPage(1);
    this.setTabHandler('#GameListContainer .tabs li img:not(.current_tab)');
};
XDGamePaginator.prototype.setTabHandler = function(exp) {
    var e = $(exp);
    var self = this;
    e.unbind('mouseover').unbind('mouseout').unbind('click');
    e.mouseover(function() {
        var src = getHoverImageUrl(getImgSrc($(this)), true);
        setImgSrc($(this), src);
        $(this).css('cursor', 'pointer');
    });
    e.mouseout(function() {
        var src = getHoverImageUrl(getImgSrc($(this)), false);
        setImgSrc($(this), src);
        $(this).css('cursor', 'default');
    });
    e.click(function() {
        $(this).unbind();
        $(this).css('cursor', 'default');

        var catName = $(this).attr('id').slice(0, -3).toLowerCase();
        self.switchCategory(catName);

        var imgObjs = $(this).parent().siblings().find('img');
        var prevTabImg = $('#GameListContainer .tabs .current_tab');
        var src = getHoverImageUrl(getImgSrc(prevTabImg), false);
        setImgSrc(prevTabImg, src);

        prevTabImg.removeClass('current_tab');
        $(this).addClass('current_tab');

        self.setTabHandler(prevTabImg);
    });
};
XDGamePaginator.prototype.setUpTabs = function() {
    var self = this;

    var mouseoverHandler = function() {
        $(this).css('cursor', 'pointer');
    };

    var clickHandler = function() {
        var imgSrc = getImgSrc($(this));
        var newSrc = getHoverImageUrl(imgSrc, true);
        setImgSrc($(this), newSrc);

        $(this).removeClass('hover').unbind();
        $(this).css('cursor', 'default');

        var catName = $(this).attr('id').slice(0, -3).toLowerCase();
        self.switchCategory(catName);

        $(this).parent().siblings().each(function() {
            var img = $(this).find('img');
            var imgSrc = getImgSrc(img);
            var newSrc = getHoverImageUrl(imgSrc, false);
            setImgSrc(img, newSrc);
        });
    };

    $('#GameListContainer .tabs li img')
        .mouseover(mouseoverHandler)
        .click(clickHandler);
};

/**
 * 指定されたカテゴリーのゲームのみを表示します。
 * "all" が指定された場合は、すべてのカテゴリーのゲームを表示します。
 *
 * @param catName
 */
XDGamePaginator.prototype.switchCategory = function(catName) {
    var self = this;
    if (catName != 'all') {
        var target = '.game_list li:not(.' + catName + ')';
        $(target).hide();
        var target = '.game_list li:.' + catName;
        $(target).show();
    } else {
        var target = '.game_list li';
        $(target).show();
    }
    self.currentCat = catName;
    $('.game_list li').removeClass('even').removeClass('odd');
    $('.game_list li:visible').filter('*:even').addClass('even');
    $('.game_list li:visible').filter('*:odd').addClass('odd');
    self.calcPages();
    self.setCurrentPage(1);
    self.enablePaginateButtons();
};

XDGamePaginator.prototype.moveToPage = function(toPage) {
    var self = this;
    if (self.totalPages <= 1) {
        return;
    }

    if (toPage > self.currentPage && toPage > self.totalPages) {
        return self.moveToPage(1);
    }
    if (toPage < self.currentPage && toPage < 1) {
        return self.moveToPage(self.totalPages);
    }
    var target = (self.currentCat == 'all') ? '.game_list li' : '.game_list li:.' + self.currentCat;
    if (toPage > self.currentPage && toPage <= self.totalPages) {
        var sIndex = (self.currentPage - 1) * self.numPerPage;
        var eIndex = (toPage - 1) * self.numPerPage;
        $(target).slice(sIndex, eIndex).hide();
        this.setCurrentPage(toPage);
    } else if (toPage < self.currentPage && toPage >= 1) {
        var sIndex = (toPage - 1) * self.numPerPage;
        var eIndex = (self.currentPage - 1) * self.numPerPage;
        $(target).slice(sIndex, eIndex).show();
        self.setCurrentPage(toPage);
    }
};

/**
 * 指定されたページ数を現在のページとして設定します。
 * ページ表示も更新します。
 */
XDGamePaginator.prototype.setCurrentPage = function(num) {
    var self = this;
    self.currentPage = num;
    $('.pages_box .total_pages').text(self.totalPages);
    $('.pages_box .current_page').text(self.currentPage);
};





var DisGamePaginator = function(numPerPage) {
    GamePaginator.apply(this, arguments);
    this.currentCat = 'all';
}

DisGamePaginator.prototype = new XDGamePaginator();
DisGamePaginator.prototype.initPages = function() {
    for (var i = 1; i <= this.totalPages; i++) {
        var a = "<span class='a_page p" + i + "'>" + i + "</span>";
        $('.game_list_block .pages_box div.pages').append(a);
    }
    this.setCurrentPage(1);
    this.setTabHandler('#GameListContainer .tabs li img');
};
DisGamePaginator.prototype.setCurrentPage = function(num) {
    var self = this;
    var target = '.game_list_block .pages_box div.pages span.p' + num;
    $(target).siblings().removeClass('current_page')
        .mouseover(function() {
            $(this).css('cursor', 'pointer');
        })
        .click(function() {
            var toPage = parseInt($(this).text());
            self.moveToPage(toPage);
        });
    $(target).addClass('current_page')
        .mouseover(function() {
            $(this).css('cursor', 'default');
        });
    self.currentPage = num;
};
DisGamePaginator.prototype.setTabHandler = function(exp) {
    var e = $(exp);
    var self = this;
    e.unbind('mouseover').unbind('mouseout').unbind('click');
    e.mouseover(function() {
        var src = getHoverImageUrl(getImgSrc($(this)), true);
        setImgSrc($(this), src);
        $(this).css('cursor', 'pointer');
    });
    e.mouseout(function() {
        var src = getHoverImageUrl(getImgSrc($(this)), false);
        setImgSrc($(this), src);
        $(this).css('cursor', 'default');
    });
    e.click(function() {
        $(this).unbind();
        $(this).css('cursor', 'default');

        var catName = $(this).attr('id').substr(3).toLowerCase();
        if (catName != 'all') {
            catName = 'platform' + catName;
        }
        self.switchCategory(catName);

        var imgObjs = $(this).parent().siblings().find('img');
        var prevTabImg = $('#GameListContainer .tabs .current_tab');
        var src = getHoverImageUrl(getImgSrc(prevTabImg), false);
        setImgSrc(prevTabImg, src);

        prevTabImg.removeClass('current_tab');
        $(this).addClass('current_tab');

        self.setTabHandler(prevTabImg);
    });
};
DisGamePaginator.prototype.switchCategory = function(catName) {
    var self = this;
    $('.game_list_block p.none').remove();
    $('.game_list_block .game_list,.game_list_block .pages_box').show();

    if (catName != 'all') {
        var target = '.game_list li:not(.' + catName + ')';
        $(target).hide();
        var target = '.game_list li:.' + catName;
        $(target).show();
    } else {
        var target = '.game_list li';
        $(target).show();
    }
    self.currentCat = catName;
    $('.game_list li').removeClass('even').removeClass('odd');
    $('.game_list li:visible').filter('*:even').addClass('even');
    $('.game_list li:visible').filter('*:odd').addClass('odd');
    self.calcPages();
    
    $('.game_list_block .pages_box div.pages').empty();
    for (var i = 1; i <= this.totalPages; i++) {
        var a = "<span class='a_page p" + i + "'>" + i + "</span>";
        $('.game_list_block .pages_box div.pages').append(a);
    }

    self.setCurrentPage(1);
    self.enablePaginateButtons();

    if (self.totalPages < 1) {
        $('.game_list_block .game_list,.game_list_block .pages_box').hide();
        var notice = "<p class='none'>該当するゲームソフトはありません。</p>";
        $('.game_list_block').append(notice);
    }
};
