﻿var pageSize = 25;
function pageData(e, listtype, cat) {
    var skip = e == 1 ? 0 : (e * pageSize) - pageSize;
    var currentSelectedItem = jQuery('.paginate_active').attr("id");
    jQuery('.paginate_active').addClass("paginate_button").removeClass("paginate_active");
    jQuery('#' + currentSelectedItem).removeClass();
    jQuery('#' + currentSelectedItem).addClass("paginate_button");
    jQuery('#pageListNo_' + e).addClass("paginate_active");
    jQuery.ajax({
        url: "default.aspx/FetchHomePageGridListing",
        data: "{skip:" + skip + ",take:" + pageSize + ",listtype:" + listtype + ",cat:" + cat + "}",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        async: true,
        cache: false,
        //crossDomain: true,
        beforeSend: function() {
            jQuery("#example_info").show();
            jQuery("#tbodyListData").block({
                message: "\<img src='http://www.FootballAlive.com/images/rotatingloading.gif'\>",
                css: { border: 'none', background: 'none' }
            });
            jQuery("#example_infoserver1").hide();
            jQuery("#example_infoserver2").hide();
            jQuery("#example_infoserver3").hide();
        },
        success: function(msg) {
            printCustomer(msg);
        },
        complete: function() {
            jQuery("#tbodyListData").unblock();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            if (errorThrown != "No Transport") {
                jQuery("#example_info").hide();
                if (cat == 1) { jQuery("#example_infoserver1").show(); }
                else if (cat == 2) { jQuery("#example_infoserver2").show(); }
                else if (cat == 3) { jQuery("#example_infoserver3").show(); }
                jQuery("#example_infoserver").hide();
                jQuery("#example_paginateDataList").hide();
                jQuery("#tbodyListData").unblock();
                jQuery("#example").width("100%");
                jQuery("#example_infoserver1").width("100%");
                jQuery("#example_infoserver2").width("100%");
                jQuery("#example_infoserver3").width("100%");
            }
        }
    });
    return false;
}
//THIS IS CUSTOM FUNCTION FOR FETCHING DATA
//MADE BY MANPREET SINGH
function ChangeTab(obj) {
    var selID = jQuery(obj).attr("id");
    var arr = selID.split("_");
    selID = arr[1];
    jQuery("#hdnSelTab").val(selID);
    jQuery("#spanSelTabValue").val(jQuery(obj).val());
    jQuery("#litablist_1").removeClass();
    jQuery("#litablist_2").removeClass();
    jQuery("#litablist_3").removeClass();
    jQuery('#litablist_' + selID).addClass("tabberactive");
    FecthMatchListingData();
}
function FecthMatchListingData() {
    var listType = jQuery("#DropDownList1").val();
    var cat = jQuery("#hdnSelTab").val();
    if (listType == "-- All Leagues / Countries --") {
        listType = 0;
    }
    jQuery.ajax({
        url: "default.aspx/FetchHomePageGridListing",
        data: "{skip:0,take:" + pageSize + ",listtype:" + cat + ",cat:" + listType + "}",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        async: true,
        cache: false,
        //crossDomain: true,
        beforeSend: function () {
            jQuery("#example_info").show();
            jQuery("#tbodyListData").block({
                message: "If it doesnt load, kindly click on HOME button. Inconvience is regretted.\<br\>\<img src='http://www.FootballAlive.com/images/rotatingloading.gif'\>",
                css: { border: 'none', background: 'none' }
            });
            jQuery("#tbodyListData").block({
                message: "\<img src='http://www.FootballAlive.com/images/rotatingloading.gif'\>",
                css: { border: 'none', background: 'none' }
            });
            jQuery("#example_infoserver1").hide();
            jQuery("#example_infoserver2").hide();
            jQuery("#example_infoserver3").hide();
        },
        success: function(msg) {
            var customers = msg.d;
            var total = 0;
            if (customers.length > 0) {
                total = customers[0].TotalRecords;
            }
            if (total > 0) {
                printCustomer(msg);
                jQuery("#example_paginateDataList").text("");
                var pageTotal = Math.ceil(total / pageSize);
                jQuery("#example_paginateDataList").append("<span>");
                for (var i = 0; i < pageTotal; i++) {
                    if (i == 0) {
                        jQuery("#example_paginateDataList").append("<span class='paginate_active' id='pageListNo_" + (i + 1) + "' onClick=\"pageData(" + (i + 1) + "," + cat + "," + listType + ")\" >" + (i + 1) + "</span>");
                    } else {
                        jQuery("#example_paginateDataList").append("<span class='paginate_button' id='pageListNo_" + (i + 1) + "' onClick=\"pageData(" + (i + 1) + "," + cat + "," + listType + ")\" >" + (i + 1) + "</span>");
                    }
                }
                jQuery("#example_paginateDataList").append("</span>");
            }
            else {

                jQuery("#example_paginateDataList").text("No matches were found. Try searching for the match instead");
            }

        },
        complete: function() {
            jQuery("#tbodyListData").unblock();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            if (errorThrown != "No Transport") {
                jQuery("#example_info").hide();
                if (cat == 1) { jQuery("#example_infoserver1").show(); }
                else if (cat == 2) { jQuery("#example_infoserver2").show(); }
                else if (cat == 3) { jQuery("#example_infoserver3").show(); }
                jQuery("#example_infoserver").hide();
                jQuery("#example_paginateDataList").hide();
                jQuery("#tbodyListData").unblock();
                jQuery("#example").width("100%");
                jQuery("#example_infoserver1").width("100%");
                jQuery("#example_infoserver2").width("100%");
                jQuery("#example_infoserver3").width("100%");
            }
        }
    });
    return false;
}
//CUSTOM FUNCTION ENDS
// This function accepts a customer object
// and prints the results to the div element. 
function printCustomer(msg) {
    jQuery("#tbodyListData").text("");
    var customers = msg.d;
    for (var i = 0; i < customers.length; i++) {
        if ((i % 2) == 0) {
            jQuery("#tbodyListData").append("<tr class='even'><td style='width: 90px'><img style='height: 11px; width: 16px; border-width: 0px;' alt='" + customers[i].flag + "' src='http://www.footballalive.com/images/Flags/" + customers[i].flag + "'" + "title='" + customers[i].flag + "' class='with-tip' >&nbsp;&nbsp;<span >" + customers[i].date + "</span>" +
        "</td><td>-</td><td><a href='http://www.footballalive.com/Highlights/" + customers[i].id + "/" + customers[i].titlelink + "-Highlights-Live-Streaming.aspx'><img style='height: 12px; width: 12px; border-width: 0px;' alt='" + customers[i].title + " (Score " + customers[i].score + ") - " + customers[i].cat + "'" +
        "src='http://www.footballalive.com/images/Football.png' title='" + customers[i].title + " (Score " + customers[i].score + ") - " + customers[i].cat + "'" +
        "></a> <a style='color: Blue; text-decoration: none' href='http://www.footballalive.com/Highlights/" + customers[i].id + "/" + customers[i].titlelink + "-Highlights-Live-Streaming.aspx'" +
        "title=\"" + (customers[i].labels) + "\" class='with-tip' >" +
        customers[i].title + "</a></td><td style='width: 40px'><span>" + customers[i].score + "</span></td></tr>");
        }
        else {
            jQuery("#tbodyListData").append("<tr class='odd'><td style='width: 90px'><img style='height: 11px; width: 16px; border-width: 0px;' alt='" + customers[i].flag + "' src='http://www.footballalive.com/images/Flags/" + customers[i].flag + "'" + "title='" + customers[i].flag + "' class='with-tip' >&nbsp;&nbsp;<span >" + customers[i].date + "</span>" +
        "</td><td>-</td><td><a href='http://www.footballalive.com/Highlights/" + customers[i].id + "/" + customers[i].titlelink + "-Highlights-Live-Streaming.aspx'><img style='height: 12px; width: 12px; border-width: 0px;' alt='" + customers[i].title + " (Score " + customers[i].score + ") - " + customers[i].cat + "'" +
        "src='http://www.footballalive.com/images/Football.png' title='" + customers[i].title + " (Score " + customers[i].score + ") - " + customers[i].cat + "'" +
        "></a> <a style='color: Blue; text-decoration: none' href='http://www.footballalive.com/Highlights/" + customers[i].id + "/" + customers[i].titlelink + "-Highlights-Live-Streaming.aspx'" +
        "title=\"" + (customers[i].labels) + "\" class='with-tip' >" +
        customers[i].title + "</a></td><td style='width: 40px'><span>" + customers[i].score + "</span></td></tr>");
        }
    }
}
jQuery(document).ready(function () {
    FecthMatchListingData();

});
