﻿function HidePanel(id) {
    if(id.indexOf("#") == -1)
        id = "#" + id;
    $(id).fadeOut("fast");
}

function ShowPanel(id) {
    if (id.indexOf("#") == -1)
        id = "#" + id;
    $(id).css("display", "none");
    $(id).fadeIn("fast");
}

function HideMainPlayer() {
    $(".compare-players-wrapper").css("display", "block");
    $(".main-player").fadeOut("fast");
    $(".main-player-wrapper").css("display", "none");
}

function ShowPlayersCompare() {
    $("#txtComparingMode").val(true);
    window.setTimeout("$('.compare-players').fadeIn('slow');", 800)
}

function SwitchPlayer() {
    $(".main-player").fadeOut("fast");
    $(".compare-player-top").fadeOut("fast");
}

function HideComparePlayer() {
    $(".compare-player-bottom").fadeOut("fast");
}

function ShowShortProfile(targetID) {
    $(".short-profile-expander").slideUp("fast");
    $(targetID).slideDown("fast");
}

function ShowMainPlayer() {
    var InCompareMode = $("#txtComparingMode").val();
    if (InCompareMode == "true") {
        $(".compare-player-top").fadeIn("slow");
    }
    else {
        $(".main-player").fadeIn("slow");
    }
}

function ShowComparePlayer() {
    $(".compare-player-bottom").fadeIn("slow");
}

function ShowModal() {
    $('#Modal').modal();
    if ($('#Modal').height() > 500) {
        $.modal.close();
        $('.modal-content').css("height", "400");
        $('#Modal').modal();
    }
    FixPNG();
}

function ShowSidebarPanel(id) {
    $(".sidebar-widget-links li").removeClass("selected");
    $(".sidebar-widget-panel").css("display", "none");
    $(id).fadeIn("slow");
}

function FixPNG() {
    if ((jQuery.browser.msie) && (jQuery.browser.version == 6.0))
        $(".png").ifixpng();
}

function PlayVideo() {
    $('a.media').media({ width: 988, height: 183, autoplay: true, params: { LOOP: false} });
}


//Wagon wheel ===============================================================================================================
var WagonWheelCanvas;
var Lines;
var Scores;
var BatsMen;
var Bowlers;

function InitializeWagonWheel() {
    if (WagonWheelCanvas != null)
        WagonWheelCanvas.clear();

    WagonWheelCanvas = Raphael("wheelcontainer", 360, 360);

    Lines = new Array();
    Scores = new Array();
    BatsMen = new Array();
    Bowlers = new Array();

    $("INPUT[type='checkbox']").attr('checked', true);
}

function DrawWagonWheelLine(x, y, colour, score, comment, batsmanID, bowlerID) {
    var line = WagonWheelCanvas.path("M180 165L" + x + " " + y);
    line.attr({ stroke: colour, "stroke-width": 2 });
    line.attr("cursor", "pointer");
    if (score == 2 || score == 3)
        score = 23;

    Scores[Scores.length] = score;
    BatsMen[BatsMen.length] = batsmanID;
    Bowlers[Bowlers.length] = bowlerID;

    line.mousemove(function(event) {
        line.attr({ "stroke-width": 4 });
        $("#BallComment").html(comment);
    });
    line.mouseout(function(event) {
        line.attr({ "stroke-width": 2 });
    });
    Lines[Lines.length] = line;
}

function ShowHideLines() {
    ShowAllLines();

    var selectedBatsmanID = $(".batsmen").val();
    var selectedBowlerID = $(".bowlers").val();
    var show6s = $("#chk6s").attr('checked');
    var show4s = $("#chk4s").attr('checked');
    var show23s = $("#chk23s").attr('checked');
    var show1s = $("#chk1s").attr('checked');

    for (var lineIndex = 0; lineIndex < Lines.length; lineIndex++) {
        var line = Lines[lineIndex];
        var score = Scores[lineIndex];
        var batsmanID = BatsMen[lineIndex];
        var bowlerID = Bowlers[lineIndex];
        switch (score) {
            case 6:
                if (!show6s)
                    line.hide();
                break;
            case 4:
                if (!show4s)
                    line.hide();
                break;
            case 23:
                if (!show23s)
                    line.hide();
                break;
            case 1:
                if (!show1s)
                    line.hide();
                break;
        }

        if (selectedBatsmanID != "All Batsmen") {
            if (selectedBatsmanID != batsmanID)
                line.hide();
        }

        if (selectedBowlerID != "All Bowlers") {
            if (selectedBowlerID != bowlerID)
                line.hide();
        }
    }
}

function ShowAllLines() {
    for (var lineIndex = 0; lineIndex < Lines.length; lineIndex++) {
        Lines[lineIndex].show();
    }
}


//Overs graph ===============================================================================================================
var team1Points;
var team2Points;
var team1PointsEmpty;
var team2PointsEmpty;
var team1Name;
var team2Name;

function DrawRPOGraph() {
    $('#pnlOvers').empty();
    $('#graphType').val("Totals");
    var graphData = new Array();
    var showGridLine = true;
    graphData[0] = team1PointsEmpty;
    graphData[1] = team2PointsEmpty;

    if ($("#graphTeam1").val().indexOf('off') > -1)
        graphData[0] = team1Points;
    if ($("#graphTeam2").val().indexOf('off') > -1)
        graphData[1] = team2Points;
    if ($("#graphGridLine").val() == "Grid on")
        showGridLine = false;

    $.jqplot('pnlOvers', graphData, {
        legend: { show: true, location: 'ne' },
        title: 'Total runs per over',
        series: [
                { label: team1Name, renderer: $.jqplot.BarRenderer, shadow: false },
                { label: team2Name, renderer: $.jqplot.BarRenderer, shadow: false }
            ],
        axes: {
            xaxis: { renderer: $.jqplot.CategoryAxisRenderer, tickOptions: { showGridline: false }, min: 0, max: 50 },
            yaxis: { min: 0, tickOptions: { showGridline: showGridLine} }
        },
        seriesColors: ['#305979', '#F7EB51'],
        seriesDefaults: { rendererOptions: { barWidth: 5, barMargin: 0, barPadding: 0} }
    });
}

function DrawRunRateGraph() {
    $('#pnlOvers').empty();
    var graphData = new Array();
    var showGridLine = true;
    graphData[0] = team1PointsEmpty;
    graphData[1] = team2PointsEmpty;

    if ($("#graphTeam1").val().indexOf('off') > -1) {
        var team1Totals = new Array();
        var team1RunRate = new Array();
        for (var x = 0; x < team1Points.length; x++) {
            team1Totals[x] = team1Points[x];
            if (x > 0)
                team1Totals[x] = team1Totals[x] + team1Totals[x - 1];
            team1RunRate[x] = team1Totals[x] / (x + 1);
        }
        graphData[0] = team1RunRate;
    }
    if ($("#graphTeam2").val().indexOf('off') > -1) {
        var team2Totals = new Array();
        var team2RunRate = new Array();
        for (var y = 0; y < team2Points.length; y++) {
            team2Totals[y] = team2Points[y];
            if (y > 0)
                team2Totals[y] = team2Totals[y] + team2Totals[y - 1];
            team2RunRate[y] = team2Totals[y] / (y + 1);
        }
        graphData[1] = team2RunRate;
    }
    if ($("#graphGridLine").val() == "Grid on")
        showGridLine = false;

    $.jqplot('pnlOvers', graphData, {
        legend: { show: true, location: 'se' },
        title: 'Run rate',
        series: [
        { label: team1Name, showMarker: false },
        { label: team2Name, showMarker: false }
    ],
        axes: {
            xaxis: { renderer: $.jqplot.CategoryAxisRenderer, tickOptions: { showGridline: false }, min: 0, max: 50 },
            yaxis: { min: 0, tickOptions: { showGridline: showGridLine} }
        },
        seriesColors: ['#305979', '#F7EB51']
    });
}

function DrawTotalsGraph() {
    $('#pnlOvers').empty();
    var graphData = new Array();
    var showGridLine = true;
    graphData[0] = team1PointsEmpty;
    graphData[1] = team2PointsEmpty;

    if ($("#graphTeam1").val().indexOf('off') > -1) {
        var team1Totals = new Array();
        for (var x = 0; x < team1Points.length; x++) {
            team1Totals[x] = team1Points[x];
            if (x > 0)
                team1Totals[x] = team1Totals[x] + team1Totals[x - 1];
        }
        graphData[0] = team1Totals;
    }
    if ($("#graphTeam2").val().indexOf('off') > -1) {
        var team2Totals = new Array();
        for (var y = 0; y < team2Points.length; y++) {
            team2Totals[y] = team2Points[y];
            if (y > 0)
                team2Totals[y] = team2Totals[y] + team2Totals[y - 1];
        }
        graphData[1] = team2Totals;
    }
    if ($("#graphGridLine").val() == "Grid on")
        showGridLine = false;

    $.jqplot('pnlOvers', graphData, {
        legend: { show: true, location: 'nw' },
        title: 'Totals',
        series: [
        { label: team1Name, showMarker: false },
        { label: team2Name, showMarker: false }
    ],
        axes: {
            xaxis: { renderer: $.jqplot.CategoryAxisRenderer, tickOptions: { showGridline: false }, min: 0, max: 50 },
            yaxis: { min: 0, tickOptions: { showGridline: showGridLine} }
        },
        seriesColors: ['#305979', '#F7EB51']
    });
}

function ToggleGraphOptions(sender) {
    if (sender.value.indexOf("off") > -1)
        sender.value = sender.value.replace("off", "on");
    else
        sender.value = sender.value.replace("on", "off");

    if (sender.value.indexOf("Totals") > -1)
        sender.value = "Run rate";
    else if (sender.value.indexOf("Run rate") > -1)
        sender.value = "RPO";
    else if (sender.value.indexOf("RPO") > -1)
        sender.value = "Totals";

    if ($("#graphType").val() == "Totals")
        DrawRPOGraph();
    if ($("#graphType").val() == "Run rate")
        DrawTotalsGraph();
    if ($("#graphType").val() == "RPO")
        DrawRunRateGraph();
}

function ToggleGraphHelp(sender) {
    if (sender.value == "Help") {
        sender.value = "Graph";
        $("#pnlOvers").css("display", "none");
        $("#pnlHelp").fadeIn("slow");
    }
    else {
        sender.value = "Help";
        $("#pnlHelp").css("display", "none");
        $("#pnlOvers").fadeIn("slow");
    }
}

//Search ===============================================================================================================
function Search(keywords) {
    while (keywords.indexOf(" ") > -1) {
        keywords = keywords.replace(" ", "+");
    }
    keywords = URLEncode(keywords);
    
    var resultsURL = 'http://www.google.com/cse?cx=001527922052692755623%3A_hejigf3ehs&cof=FORID%3A10&ie=UTF-8&q=' + keywords;
    document.getElementById("frameResults").setAttribute("src", resultsURL);
}

function DoSearch() {
    Search($("#txtKeywords").val());
    $('.search-modal').modal();
    $("#txtKeywords").val("search the site...");
    $("#txtPopupKeywords").focus();
}

function DoPopupSearch() {
    Search($("#txtPopupKeywords").val());
    $("#txtPopupKeywords").val("");
}

function DoEnterSearch(myfield, e) {

    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    if (keycode == 13) {
        DoSearch();
    }
}

function DoPopupEnterSearch(myfield, e) {

    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    if (keycode == 13) {
        DoPopupSearch();
    }
}

function URLEncode(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
            x += match[1].length;
        } else {
            if (clearString[x] == ' ')
                output += '+';
            else {
                var charCode = clearString.charCodeAt(x);
                var hexVal = charCode.toString(16);
                output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return output;
}
