﻿function gameReady()
{
    $('#jqmalert').jqm();
    fetchGamePieChart();
    $('.prdbtn').click(function() {
          gamePredict(this.id);
          return false;
    });
    
    $(".bigtxt").keydown(function(ev) {
        if (ev.keyCode == 13) {
            gamePredict(this.id);
            return false;
        }
    });        
    $('.bigtxt:first').focus();
}


//fetches the game control pie chart
function fetchGamePieChart()
{
    var flashDiv = null;
    var gid = "";
    flashDiv = ebid("flashchart");
    if((flashDiv == null) || (typeof(flashDiv) == undefined)) {
        return;
    }
    gid = $("#flashchart").attr('class').split('_').slice(-1); 
    var so = new SWFObject("/amCharts/ampie/ampie.swf", "ampie", "240", "180", "8", "#FFFFFF");
    so.addVariable("path", "/amCharts/ampie/");
    so.addVariable("settings_file", encodeURIComponent("/amCharts/ampie/settings/ampie_landing.xml"));
    so.addVariable("data_file", encodeURIComponent("/Games/GamePieData.ashx?GID=" + gid + "&t=" + getTimeStr() ));
    so.addParam("wmode", "transparent")
    so.write("flashchart");
}

function gamePredict(eid)
{
    var cid = "";
    var arr = null;
    
    try
    {
        arr = eid.split('_');
        cid = arr[1];
        
        //validation
        if(!validateGamePrediction(cid)) {
            return false;
        }
        
        //get values
        var home = ebid('homes_' + cid).value;
        var away = ebid('aways_' + cid).value;
        var stream = serializeHeadToHead(cid,home,away);
        
        //show ajax loader
        promptHtml('<img src="/images/ajax-loader-big.gif" alt="Loading" />',false);
        
        //execute the request
        $.post("/JsonApi/JsonPredictHeadToHead.ashx", { snjp: stream },
            function(data) {
                switch(data.ret)
                {
                    case 0:
                        promptClose();
                        window.location.href = window.location.href;
                        break;
                                                
                    case 3:
                        promptClose();
                        prompt("ERR_NEED_LOGIN");
                        break;
                        
                    case 1:
                    case 2:
                    default:
                        promptClose();
                        prompt("ERR_GENERAL_PREDICTION");
                        break;                                        
                
                }
                
            },"json"
        );        
        
        
    }
    catch(ex)
    {
        return false;
    }
    return true;
}

//validates the prediction
function validateGamePrediction(cid)
{
    var home = -1;
    var away = -1;
    
    home = ebid('homes_' + cid);
    away = ebid('aways_' + cid);
    
    return validatePredictionFields(home.value,away.value);
}



