Glc = {
  authToken : null,

	// Get or set authenticity token
	authenticityToken : function(token) {
		if (token != null) Glc.authToken = token;
		return Glc.authToken;
	}	  
}

Glc.load = function() {
 positionFooter();
	  $(window)
	    .scroll(positionFooter)
	    .resize(positionFooter);
	   
	  function positionFooter() {
	    var docHeight = $(document.body).height() - $("#sticky-footer-push").height();
	    if(docHeight < $(window).height()){
	      var diff = $(window).height() - docHeight;
	      if (!$("#sticky-footer-push").length > 0) {
	        $("#footer").before('<div id="sticky-footer-push"></div>');
	      }
	      $("#sticky-footer-push").height(diff);
	    }  
	  }  
    positionFooter();
    
    //weird bug with twitter feed... the image src for the avatar isn't working
    setTimeout(Glc.setAvatar, 2000);
}

Glc.setAvatar = function() {    
    $(".twtr-profile-img").attr("src","http://a1.twimg.com/profile_images/745019520/curlingrock_normal.png");
}

Glc.Gamedays = {}
Glc.Gamedays.Edit = {}

Glc.Gamedays.Edit.load = function() {

  $("#games_date").datepicker({ dateFormat: 'yy-mm-dd' });
  $(".timeentry").timeEntry({show24Hours:true, spinnerImage:'', timeSteps:[1,15,0]});  

  Glc.Gamedays.Edit.generateDeleteGameEvents();
  $("#btn_add_game").click( function() {
    $.post("/admin/leagues/" + $("#league_id").val() + "/gamedays/" + $("#gameday_id").val() + "/games",
      { 
        "no_team_a": $("#team_a").val(),
        "no_team_b": $("#team_b").val(),
        "ice": $("#ice").val(),
        "time": $("#time").val(),
        authenticity_token:Glc.authenticityToken(),
        _method: "post"
      },
      function(data) {
        $("#new_game_message").html("");
        $("#team_a, #team_b, #ice, #time").removeClass("error")
        status_message=$("<span>").html(data.message);
        if(data.response == "error") {
          status_message.addClass("error");
          $(data.fields_to_highlight).addClass("error");
        } else {
            btn_delete = $("<a>").attr("href","#").attr("id",data.new_game_id).html("<img src='/images/delete.png' />").addClass("delete");
            
            
            //Ajouter la nouvelle ligne dans la table HTML                        
            td1 = $("<td>").html(data.team_a);
            td2 = $("<td>").html(data.team_b);
            td3 = $("<td align='center'>").html(data.ice);
            td4 = $("<td align='center'>").html(data.time);
            td5 = $("<td align='center'>").html(btn_delete);
            tr = $("<tr>").attr("id","game_" + data.new_game_id).append(td1).append(td2).append(td3).append(td4).append(td5);
            $("table#games tbody").append(tr);            
            
            Glc.Gamedays.Edit.generateDeleteGameEvents();
                        
            //clear row
            $("#team_a").val("");
            $("#team_b").val("");
            $("#ice").val("");
            $("#time").val("");
            $("#team_a").focus();
            
            status_message.addClass("success");
        }
        $("#new_game_message").append(status_message);
      }, "json")
    return false;
  });    
}

Glc.Gamedays.Edit.generateDeleteGameEvents = function() {
  $("a.delete").click(function() { 
    var btn_id = $(this).attr("id");
    $.post("/admin/leagues/" + $("#league_id").val() + "/gamedays/" + $("#gameday_id").val() + "/games/" + btn_id,
          { 
            authenticity_token:Glc.authenticityToken(),
            _method: "delete"
          },
          function(data) {
            if(data.deleted==true)
              $("tr#game_" + btn_id).fadeOut("slow",function(){$(this).remove();});
          },"json");
          return false;  
  });  
}
