if (Drupal.jsEnabled) {
  // On Document Ready
  $(document).ready(function () {
    $("#gamesPageTabs").show();
    // React to a radio button in the "modalPredictionsBox.php" thickBock being clicked.
    var SM = false;
    $("input[@name='win_draw']").click(function(){
      var adb = $(this).attr('id');
      var sA = $("#score_a").val();
      var sB = $("#score_b").val();

      if (adb=='win_draw_a'){
        if (sA<sB){
          $("#score_a").val(sB);
          $("#score_b").val(sA);
        }
        if (sA==sB && sB>0) $("#score_b").val(0);
        if (sA==0 && sB==0) $("#score_a").val(1);
      }
      else if (adb=='win_draw_b'){
        if (sA>sB){
          $("#score_a").val(sB);
          $("#score_b").val(sA);
        }
        if (sA==sB && sB>0) $("#score_a").val(0);
        if (sA==0 && sB==0) $("#score_b").val(1);
      }
      else {
        $("#score_b").val(sA);
      }
    });
    // React when a score is typed into the score box, and set the appropriet radio box
    $("#score_a").add("#score_b").keyup(function(){
      var sA = $("#score_a").val();
      var sB = $("#score_b").val();
      if (sA>sB){
        $("#win_draw_a").attr("checked","checked");
      }
      else if (sB>sA){
        $("#win_draw_b").attr("checked","checked");
      }
      else {
        $("#win_draw_draw").attr("checked","checked");
      }
      return false;
    });

    // Submit clicked
    $("#mySubmit").click(function(){
      var uid = $("#user_id").val();
      var gid = $("#game_id").val();
      var tida = $("#teamA").text();
      var tidb = $("#teamB").text();
      var win_draw_a = $("#win_draw_a").val();
      var win_draw_b = $("#win_draw_b").val();
      var win_draw = $("input[@name='win_draw']:checked").val();
      var scoreA = $("#score_a").val();
      var scoreB = $("#score_b").val();
      var emailChallenge = $("#emailChallenge").val();
      var pred_props = gid +":"+ tida +":"+ tidb +":"+ win_draw_a +":"+ win_draw_b +":"+ scoreA +":"+ scoreB;
      // Reset the submit animation
      $("#mySubmit").hide();
      $("#loadingAnimation").show();
      // Update prediction values on the web page
      $("#predBtn_"+gid).attr("pred_props",pred_props);
      /*
        var totalP = $("#predictionBar").attr("totalP");
        var homeBar = $("#homeBar").attr("count"); if(homeBar==undefined) homeBar=0;
        var drawBar = $("#drawBar").attr("count"); if(drawBar==undefined) drawBar=0;
        var awayBar = $("#awayBar").attr("count"); if(awayBar==undefined) awayBar=0;
        //$("#homeBar").css({width: homeBar+"%"}); $("#homeBar").html( Math.round(homeBar,2)+"%");
        //$("#drawBar").css({width: drawBar+"%"}); $("#drawBar").html( Math.round(drawBar,2)+"%");
        //$("#awayBar").css({width: awayBar+"%"}); $("#awayBar").html( Math.round(awayBar,2)+"%");
        // Compute Percent and change the bars width and text.
        // homeBar = (homeBar / totalP)*100;
        // drawBar = (drawBar / totalP)*100;
        // awayBar = (awayBar / totalP)*100;
        //var edit_mode = ( $("#predBtn_"+gid).attr("title") == "edit prediction" );
        //if ( edit_mode ) totalP--;
        //alert("totalP: "+ totalP +", homeBar : "+ homeBar +", drawBar : "+ drawBar +", awayBar : "+ awayBar);
      */
      $.post("/ajax.php?scr=ajax/saveUserPredictions.php", { uid: ""+uid+"", gid: ""+gid+"", win_draw: ""+win_draw+"", scoreA: ""+scoreA+"", scoreB: ""+scoreB+"", emails: ""+emailChallenge+""}, function(data){
        if(data.length>0) {
          if(data.substr(0,4)!="Done"){
            $("#loadingAnimation").replaceWith("<div><br/><h3>Sorry!</h3>Your prediction for this game was not recorded.<br/> This may be because the game has already kicked off.</div>");
            $("#predBtn_"+gid).parent().empty();
          }
          else {
            tb_remove();
            $("#mySubmit").show();
            $("#loadingAnimation").hide();
            //-- Make changes to html on web page
            $("#youPredScoreA").text(scoreA);
            $("#youPredScoreB").text(scoreB);
            $("#predBtn_"+gid).html("<span>edit prediction</span>");
            $("#predBtn_"+gid).attr("title","edit prediction");
            $("#predBtn_"+gid).click(function(){
              $("#score_a").val( scoreA );
              $("#score_b").val( scoreB );
            });
            //-- Reload the prediction
            $("#tab_gamePagePredictionList").html( '<div style="width:100%; text-align:center;">' + $("#loadingAnimation").html() + '</div>' );
            $("#tab_gamePagePredictionList").load("/ajax/gamePagePredictionList/"+gid+"/"+tida+"/HT/AT", {}, function(data){
            $("#tab_gamePagePredictionList").html(data);
            });
          }
        }
      });

    });

    // Close the TB window if the close button is clicked...
    $("#mySkip").click(function(){
      $("#win_draw_draw").attr("checked","checked");
      $("#score_a").text("0");
      $("#score_b").text("0");
      tb_remove();
    });

    $("#sg_edit-submit").click(function(){
      $("#sg_edit-submit").hide();
      $("#loadingAnimation").show();
    });

/*
    $("#gamesPageTabs").tabs({
      fxSlide: false,
      fxFade: true,
      fxSpeed: 'normal',
      disabled: [0],
      fxAutoHeight: false,
      remote: true,
      bookmarkable: false
    });
*/

  });

  // Popup function
  function mypop(id){
    if (id!=null){
      // Fetch the prediction propertys for this popup link and assign it to the modal box fields
      pred_props = $(id).attr("pred_props");
      var pred_props_array = pred_props.split(":");
      $("#game_id").val(pred_props_array[0]);
      $("#teamA").text(pred_props_array[1]);
      $("#teamB").text(pred_props_array[2]);
      $("#win_draw_a").val(pred_props_array[3]);
      $("#win_draw_b").val(pred_props_array[4]);
      $("#score_a").val(pred_props_array[5]);
      $("#score_b").val(pred_props_array[6]);
      // Tick the correct radio options
      scoreA = $("#score_a").val();
      scoreB = $("#score_b").val();
      if (scoreA>scoreB){
        $("#win_draw_a").attr("checked","checked");
      }
      else if (scoreA<scoreB){
        $("#win_draw_b").attr("checked","checked");
      }
      else {
        $("#win_draw_draw").attr("checked","checked");
      }
    }
    // Show the modal box
    tb_show(null, "#TB_inline?height=305&width=310&modal=true&inlineId=myOnPageContent", false);
    // Ignore the link click on the page
    return false;
  };

};
