// JScript File
//Used with scoreboard control on Team/Matchup Pages


function UpdateGame_callback(response){


    //This is used to stop AJAX if this date is not today or yestersday (Only today and yesterday should get livescores updates)
        var runAjax = document.getElementById("runAjax");
        if (runAjax != null){
            //if runAjax is not turned on, kill this execution
            if (runAjax.value!= "True")
            {
                return;   
            }
        }
        

    var game_data  = response.value;
   
    var League = game_data.League;
    var TeamAbbr = game_data.TeamAbbr;
    var Controlname = game_data.GameControlType;
    var RefreshStartTime = game_data.RefreshStartTime;
    var timezone = game_data.timezone;
    
    var LeagueID = game_data.LeagueID;
    if (LeagueID == null){
          var ctlLeagueID = document.getElementById("LeagueID");                 
                  if (ctlLeagueID != null)
                    LeagueID = ctlLeagueID.value;
    }
  
    
    
    var EventID = game_data.EventID;
     if (EventID == null){
          var ctlEventID = document.getElementById("EventID");               
                  if (ctlEventID != null)
                    EventID = ctlEventID.value;
    }
    
    if(document.getElementById("HomeScore_" + LeagueID+"_"+EventID) != null){
	    var OldHomeScore=document.getElementById("HomeScore_" + LeagueID+"_"+EventID).innerHTML;
	    var OldVisitScore=document.getElementById("VisitScore_" + LeagueID+"_"+EventID).innerHTML;
    }
    var NewHomeScore;
    var NewVisitScore;
        var sbDiv = document.getElementById("Game_" + LeagueID+"_"+EventID);
        if (sbDiv != null)
        {
       		sbDiv.parentNode.innerHTML = game_data.gameHTML;
       		showDate();
        }
    if(document.getElementById("HomeScore_" + LeagueID+"_"+EventID) != null){
	    NewHomeScore=document.getElementById("HomeScore_" + LeagueID+"_"+EventID).innerHTML;
	    NewVisitScore=document.getElementById("VisitScore_" + LeagueID+"_"+EventID).innerHTML;
    }
    if(OldHomeScore != NewHomeScore && OldHomeScore != null){
    	FlashScorebox("Game_" + LeagueID+ "_" + EventID ,'5' ,"HomeScore_"+LeagueID+"_"+EventID);
    }
    if(OldVisitScore != NewVisitScore && OldHomeScore != null){
    	FlashScorebox("Game_" + LeagueID+ "_" + EventID ,'5' ,"VisitScore_"+LeagueID+"_"+EventID);
    }
    
    
    
       //This is used to stop old AJAX from looping once a user has moved on to another scoreboard
        var cookie_refresh = Get_Cookie("RefreshIframeScores");
        if (cookie_refresh=="")
        {
             return;
        }
   
      
   
    window.setTimeout("Scoreboard.UpdateGame('"
            +League+"', '"
            +TeamAbbr+"', '"
            +RefreshStartTime+"', '"
            +Controlname+"', '"
            +timezone+"', UpdateGame_callback);", 30000 );
            	
            
        
}


function NewScoresFeed(League, TeamAbbr, controlName, timezone ){
            
    //check to see if the cookie currently exists            
    var cookie_refresh = Get_Cookie("RefreshIframeScores");
                    
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    var time = d.getTime();
    var RefreshStartTime = curr_date+"-"+curr_month+"-"+curr_year+"-"+time
    
    Delete_Cookie('RefreshIframeScores');
    Set_Cookie( 'RefreshIframeScores', RefreshStartTime, '', '/', '', '' ); 
           
        
    //start the new scores feed
    Scoreboard.UpdateGame(League, TeamAbbr, RefreshStartTime, controlName, timezone, UpdateGame_callback);
            
    //cookie didn't exist before so scores were turned off original, delete cookie to make sure the updates stay off      
    if (cookie_refresh=="")
    {   
        Delete_Cookie('RefreshIframeScores');
    }       
}

function AutoRefreshOn(League, TeamAbbr, controlName, timezone)
{
   //This function is fired when the Scores Update On link is click
    
   // Change the HTML to show the new refresh state
   var RefreshDiv = document.getElementById('scores-note');
   if (RefreshDiv!=null)
   {
        var temp ="Refresh: <a href=\"javascript:AutoRefreshOff('"+League+"','"+TeamAbbr+"','"+controlName+"')\">Off</a> | <strong>On</strong>";
       RefreshDiv.innerHTML=temp;
   }
   
   //Start-up the updates again
    Set_Cookie( 'RefreshIframeScores', 'On', '', '/', '', '' ); 
    NewScoresFeed(League, TeamAbbr, controlName, timezone )
    showDate();
    //alert
}

function AutoRefreshOff(League, TeamAbbr, controlName, timezone)
{
    //This function is fired when the Scores Update Off link is click
   Delete_Cookie('RefreshIframeScores', '/', '');

   // Change the HTML to show the new refresh state
   var RefreshDiv = document.getElementById('scores-note');
   if (RefreshDiv!=null)
   {
       RefreshDiv.innerHTML="Refresh: <strong>Off</strong> | <a href=\"javascript:AutoRefreshOn('"+League+"','"+TeamAbbr+"','"+controlName+"','"+timezone+"')\">On</a>"
   }
   
   
}
