﻿var searchValue = "";
var subcID;

function search(event) {
    if (event.keyCode == 13) {
        searchValue = $("#123")[0].value;
        $.getJSON("search.aspx?sport=" + sport + "&value=" + searchValue,
            function(json) {
                setMenu(json);
            });
    }
}
function searchE() {
    searchValue = $("#123")[0].value;
    $.getJSON("search.aspx?sport=" + sport + "&value=" + searchValue,
            function(json) {
                setMenu(json);
            });
}

function setMenu(data) {
    var jsonString = "";
    jQuery.each(data.Leagues, function() {
    jsonString = jsonString + "<li onclick='getLeague(" + this.ID + ")' id='lm_" + this.ID
        + "' style='list-style-type: disc; list-style-image:url(media/images/arrow_blue_leagues.gif); list-style-position: outside;'>" +
            "<a href='#'>" + this.Name + "</a></li>";
    });
    $("#left_menu").empty();
    $("#left_menu").append(jsonString);
}

function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox

     if(key == 13)
          return false;
     else
          return true;
}
function beckToLeague() {
    resizeIDArrayFixtures = "";
    closedIDArray = "";
    prepareForResults();
    $.post("page_type_menu.aspx?page=league&sport=" + sport,
        function(data) {
            $("#page_type_menu").html(data);
            checkSelectLeague();
        });
    if (intval == "") {
        showDataLeague();
        intval = setInterval("showDataLeague()", refreshInterval);
    }
    else {
        stop_Int();
        showDataLeague();
        intval = setInterval("showDataLeague()", refreshInterval);
    }
    $("#123")[0].value = searchValue;
    $.getJSON("search.aspx?sport=" + sport + "&value=" + searchValue,
            function(json) {
                setMenu(json);
            });
    if (intvalTicker == "") {
        showDataTicker();
        intvalTicker = setInterval("showDataTicker()", 5000);
    }
    else {
        stop_IntTicker();
        showDataTicker();
        intvalTicker = setInterval("showDataTicker()", 5000);
    }
}

function getLeague(subcontestIDIN) {
    subcID = subcontestIDIN;
    resizeIDArrayFixtures = "";
    closedIDArray = "";
    refreshInterval = 600000;
    type = "leaguefinished";
    $.post("page_type_menu.aspx?page=league&sport=" + sport,
        function(data) {
            $("#page_type_menu").html(data);
        });
    
    if (intval == "") {
        showDataLeague();
        intval = setInterval("showDataLeague()", refreshInterval);
    }
    else {
        stop_Int();
        showDataLeague();
        intval = setInterval("showDataLeague()", refreshInterval);
    }
    checkSelectLeague();   
}

function showDataLeague() {
    $.post("left_menu.aspx?type=" + type + "&id=" + subcID + "&sport=" + sport + "&resize=" + resizeIDArrayFixtures + "&close=" + closedIDArray,
            function(data) {
                $("#accordians").html(data);
                checkMainHeight();
            });
}

function refreshLeaguePage(typeIN) {
    type = typeIN;
    if (type == "leaguelive") {
        refreshInterval = 30000;
    } else {
        refreshInterval = 600000;
    }
    resizeIDArrayFixtures = "";
    closedIDArray = "";
    if (intval == "") {
        showDataLeague();
        intval = setInterval("showDataLeague()", refreshInterval);
    }
    else {
        stop_Int();
        showDataLeague();
        intval = setInterval("showDataLeague()", refreshInterval);
    }
    checkSelectLeague();
}
function resetLeaguePage() {
    resizeIDArrayFixtures = "";
    closedIDArray = "";
    if (intval == "") {
        showDataLeague();
        intval = setInterval("showDataLeague()", refreshInterval);
    }
    else {
        stop_Int();
        showDataLeague();
        intval = setInterval("showDataLeague()", refreshInterval);
    }
}

function checkSelectLeague() {
    try {
        var elem;
        elem = document.getElementById("leaguelive");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("leaguefinished");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("leagueupcoming");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("liveOnTv");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("football");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("basketball");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("hockey");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("nfl");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("tennis");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("cricket");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("handball");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("volleyball");
        if (elem != null) {
            elem.className = "match";
        }
        elem = document.getElementById("rugby");
        if (elem != null) {
            elem.className = "match";
        }
        if (type == "leaguelive") {
            elem = document.getElementById("leaguelive");
            if (elem != null) {
                elem.className = "active";
            }
        } else if (type == "leaguefinished") {
            elem = document.getElementById("leaguefinished");
            if (elem != null) {
                elem.className = "active";
            }
        } else if (type == "leagueupcoming") {
            elem = document.getElementById("leagueupcoming");
            if (elem != null) {
                elem.className = "active";
            }
        }
    }
    catch (err) { }
}