var sort = "r";
var genre = "";
var page = 1;
var totalPages = -1;
function getTotalPages() {
    $.ajax({ type: "POST",
        url: "Services/PaginationHandler.ashx",
        data: "g=" + genre,
        success: function (data) {
            totalPages = Math.ceil(data / 25);
            pagination();
        }
    });
}
function pagination() {
    $("#pagination").html('');
    var startpag = 0;
    var endpag = 0;
    if (page < 3) {
        startpag = 1;
    } else {
        startpag = page - 2;
    }
    if ((totalPages - startpag) < 4) {
        endpag = totalPages;
    } else {
        endpag = startpag + 4;
    }
    for (i = startpag; i <= endpag; i++) {
        if (i == page) {
            $("#pagination").append('<a class="RIB" style="font-weight:bold;font-size:18px"">' + i + '</a>');
        } else {
            $("#pagination").append('<a class="stylePointHover RIB" style="font-weight:bold;font-size:18px" onclick="page=' + i + ';callGameService();pagination();">' + i + '</a>');
        }
    }
}
function resetother() {
    $("#mdRandom").css("background-image", "url(Images/bars.png)");
    $("#mdTop").css("background-image", "url(Images/bars.png)");
    $("#mdHigh").css("background-image", "url(Images/bars.png)");
    $("#mdLatest").css("background-image", "url(Images/bars.png)");
    $("#mdMost").css("background-image", "url(Images/bars.png)");
    $("#RandomGames").css("color", "#004600");
    $("#TopGames").css("color", "#004600");
    $("#MostPlayed").css("color", "#004600");
    $("#HighestRated").css("color", "#004600");
    $("#Latest").css("color", "#004600");
}
function resetgenres() {
    $("#All").css("color", "#44FF00");
    $("#Strategy").css("color", "#44FF00");
    $("#RPG").css("color", "#44FF00");
    $("#Arcade").css("color", "#44FF00");
    $("#Fighting").css("color", "#44FF00");
    $("#Racing").css("color", "#44FF00");
    $("#Shooter").css("color", "#44FF00");
    $("#Sports").css("color", "#44FF00");
    $("#Skill").css("color", "#44FF00");
    $("#Other").css("color", "#44FF00");
}
function sortclick(th) {
    page = 1;
    resetother();
    $(th).css("color", "#000000");
    callGameService();
    if (sort == "r") {
        $("#LinkButtonRandom").css('display', 'block');
        $("#pagination").css('display', 'none');
    } else {
        if (totalPages < 0) {
            getTotalPages();
        }
        pagination();
        $("#LinkButtonRandom").css('display', 'none');
        $("#pagination").css('display', 'block');
    }
}
function genreclick(th) {
    page = 1;
    resetgenres();
    $(th).css("color", "#FFB300");
    callGameService(); 
    if (sort == "r") {
        $("#LinkButtonRandom").css('display', 'block');
        $("#pagination").css('display', 'none');
    } else {
        if (totalPages < 0) {
            getTotalPages();
        }
        pagination();
        $("#LinkButtonRandom").css('display', 'none');
        $("#pagination").css('display', 'block');
    }
}
function callGameService() {
    var c = false;
    setTimeout(function () {
        if (c == false) {
            $("#loaddiv").css('display', 'block');
        }
    }, 500);
    $.ajax({ type: "POST",
        url: "Services/GamesHandler.ashx",
        data: "r=" + sort + "&g=" + genre + "&p=" + page,
        success: function (data) {
            var lasti = 0;
            $("#gamebox0").css("display", "none");
            $.each(data.games, function (i, item) {
                $("#imageLink" + i + " > img").attr("src", "Images/cool-game-images-loader.gif");
                $("#imageLink" + i + " > img").attr({ src: 'Games/Thumbnails/' + item.Pic + '.jpg', alt: item.Name });
                $("#imageLink" + i).attr({ href: 'games.aspx?id=' + item.Link, title: item.Name });
                $("#gameLink" + i).attr({ href: 'games.aspx?id=' + item.Link, title: item.Name }).text(item.Name);
                $("#Added" + i).text("Added: " + item.Date);
                $("#Genre" + i).text("Genre: " + item.Genre);
                $("#Played" + i).html("Played: <span style='color:Red'>" + item.Played + "</span> Times");
                $("#gamebox" + i).css("display", "block");
                for (q = 1; q <= 5; q++) {
                    $("#I" + i + "R" + q).removeClass();
                    if (item.Rating <= (q - 1)) {
                        $("#I" + i + "R" + q).addClass("ems");
                    } else if (item.Rating <= ((q - 1) + 0.5)) {
                        $("#I" + i + "R" + q).addClass("has");
                    } else {
                        $("#I" + i + "R" + q).addClass("fus");
                    }
                }
                lasti = i;
            });
            while (lasti < 25) {
                lasti++;
                $("#gamebox" + lasti).css("display", "none");
            }
            c = true;
            $("#loaddiv").css('display', 'none');
        }
    });
}
$(document).ready(function() {
    $("#RandomGames").click(function() {
        sort = "r";
        sortclick(this);
        $("#mdRandom").css("background-image", "none");
    });
    $("#TopGames").click(function() {
        sort = "t";
        sortclick(this);
        $("#mdTop").css("background-image", "none");
    });
    $("#MostPlayed").click(function() {
        sort = "p";
        sortclick(this);
        $("#mdMost").css("background-image", "none");
    });
    $("#HighestRated").click(function() {
        sort = "h";
        sortclick(this);
        $("#mdHigh").css("background-image", "none");
    });
    $("#Latest").click(function() {
        sort = "l";
        sortclick(this);
        $("#mdLatest").css("background-image", "none");
    });
    $("#All").click(function() {
        genre = "";
        genreclick(this);
    });
    $("#Strategy").click(function() {
        genre = "1";
        genreclick(this);
    });
    $("#RPG").click(function() {
        genre = "2";
        genreclick(this);
    });
    $("#Arcade").click(function() {
        genre = "3";
        genreclick(this);
    });
    $("#Fighting").click(function() {
        genre = "4";
        genreclick(this);
    });
    $("#Racing").click(function() {
        genre = "5";
        genreclick(this);
    });
    $("#Shooter").click(function() {
        genre = "6";
        genreclick(this);
    });
    $("#Sports").click(function() {
        genre = "7";
        genreclick(this);
    });
    $("#Skill").click(function() {
        genre = "8";
        genreclick(this);
    });
    $("#Other").click(function() {
        genre = "9";
        genreclick(this);
    });

    $("#LinkButtonRandom").click(function() {
        callGameService();
    });
    $("#ctl00_ctl00_C1_TextBoxMainSearch").keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $("#ctl00_ctl00_C1_ButtonMainSearch").click();
            return false;
        } else {
            return true;
        }
    });
});
