﻿// JScript File
var firstTime = true;


function UpdateScoreboard_callback(response){

    var scoreboard_data  = response.value;
    var LeagueID = scoreboard_data.LeagueID;
    var League = scoreboard_data.League;
    var GameDate = scoreboard_data.GameDate;
    var Season = scoreboard_data.Season;
    var PageType = scoreboard_data.PageType;
    var Week = scoreboard_data.Week;
    var LastUpdateTime = scoreboard_data.LastUpdateTime;
    var RefreshStartTime = scoreboard_data.RefreshStartTime;
    var refresh = false;
    if (scoreboard_data.Refresh == true){
        var sbDiv = document.getElementById("Scoreboard_" + LeagueID);
        if (sbDiv != null){
            sbDiv.innerHTML = scoreboard_data.ScoreBoardHTML;
        }
    }
    else {
    
        //This is used to stop AJAX is this date is not today or yestersday (Only today and yesterday should get livescores updates)
        var runAjax = document.getElementById("runAjax").value
        if (runAjax!= "True")
        {
            return;   
        }
        
        //This is used to stop old AJAX from looping once a user has moved on to another scoreboard
        var cookie_refresh = Get_Cookie("RefreshScores");
        if (RefreshStartTime!=cookie_refresh || cookie_refresh=="")
        {
             return;
        }
        
        if (LeagueID!= 1 && LeagueID!=2)
        {
            var HiddenGameDate = document.getElementById("gamedatetime").value
            if (HiddenGameDate!= GameDate)
            {
                return;   
            }
        }
    
        var GameResponseList = scoreboard_data.GameResponseList;

        for (var i=0; i<GameResponseList.length; i++){
        
  
            var EventID = GameResponseList[i].EventID;           
            var Total = GameResponseList[i].Total;           
            var Spread = GameResponseList[i].Spread;           
            var ScoreStatus = GameResponseList[i].ScoreStatus; 
           
            var OldHomeScore 
            var OldVisitScore 
            var NewHomeScore
            var NewVisitScore
            var NewScoreStatus
            var OldScoreStatus 

            var GraphDivID = "Graph_" + LeagueID +"_"+EventID; 
            var GraphDiv = document.getElementById(GraphDivID);
            var previousStatus= document.getElementById("Status_" + LeagueID + "_" + EventID);
            if  (GraphDiv != null && previousStatus.innerHTML.substring(0,5) != "Final"){    
                var Total = CustomBetgraphOdds(LeagueID,EventID,"total");
                var Spread = CustomBetgraphOdds(LeagueID,EventID,"spread");
          	    BetGraph.UpdateBetGraph( LeagueID, EventID, Total, Spread, ScoreStatus, GameDate, UpdateBetGraph_callback);
            }

            var GameDivID = "Game_" + LeagueID +"_"+EventID; 
        
            var gameDiv = document.getElementById(GameDivID);
            if (gameDiv != null){
            
                OldHomeScore = GetSubElementById(gameDiv,"HomeScore_"+LeagueID +"_"+EventID);               
                 
                OldVisitScore = GetSubElementById(gameDiv,"VisitScore_"+LeagueID +"_"+EventID);         
                OldScoreStatus = GetSubElementById(gameDiv,"Status_"+LeagueID +"_"+EventID);
                
                if (OldHomeScore != null && OldVisitScore != null)
                {      
                    OldHomeScore = OldHomeScore.innerHTML;
                    OldVisitScore = OldVisitScore.innerHTML;   
                }
                
                gameDiv.parentNode.innerHTML = GameResponseList[i].gameHTML;
                
                NewScoreStatus = GetSubElementById(document.getElementById(GameDivID),"Status_"+LeagueID +"_"+EventID);          
                NewHomeScore = GetSubElementById(document.getElementById(GameDivID),"HomeScore_"+LeagueID +"_"+EventID); 
                NewVisitScore = GetSubElementById(document.getElementById(GameDivID),"VisitScore_"+LeagueID +"_"+EventID);
                
                if (NewHomeScore != null && NewVisitScore != null && GameDivID != null)
                {      
                    NewHomeScore = NewHomeScore.innerHTML;
                    NewVisitScore = NewVisitScore.innerHTML;                    
                }
                
                if (NewScoreStatus != null && OldScoreStatus != null)
                {   
                    //We are checking to see if a game just went final, if so we want to refresh the entire 
                    //scoreboard so that this game will move to the bottom
                    
                    OldScoreStatus = OldScoreStatus.innerHTML;
                    NewScoreStatus = NewScoreStatus.innerHTML;
                    var OldIsFinal = OldScoreStatus.indexOf('Final');
                    var NewIsFinal = NewScoreStatus.indexOf('Final');
                    if ( (OldIsFinal == -1) && (NewIsFinal != -1))
                    {
                        refresh = true;
                    }
                    //if it baseball and we are going to the top of the inning then we want it to flash
                    if (LeagueID == 5 && OldScoreStatus.indexOf('Bot') != -1 && NewScoreStatus.indexOf('Top') != -1)
                    {
                        FlashScorebox(GameDivID, flashes, "");
                    }
                }
                
         	                               
                                   
                    var flashes = 5
                    
                     //check for a change in score so that we can make it blink
                    if (OldHomeScore != NewHomeScore){
                        FlashScorebox(GameDivID, flashes, "HomeScore_"+LeagueID +"_"+GameResponseList[i].EventID);
                    }
                    if (OldVisitScore != NewVisitScore){
                        FlashScorebox(GameDivID, flashes, "VisitScore_"+LeagueID +"_"+GameResponseList[i].EventID);
                    }
                   
                }
            }
        }
   
          
                  var ctlConf = document.getElementById("confSelect");
                  var myConf="";
                  if (ctlConf != null)
                    myConf = ctlConf.value;
          
        //if refresh is true then we want to start redraw the scoreboard and start a new cycle of refreshes   
        if (refresh==true)
        {
            NewScoresFeed(LeagueID, GameDate, Season, PageType, Week )
        }
        else
        {    
            window.setTimeout("Scoreboard.UpdateScoreboard("
            +LeagueID+", '"
            +GameDate+"', '"
            +Season+"', "+refresh+", '"
            +LastUpdateTime + "', '"
            +PageType+"','"+RefreshStartTime + "', '"+Week+"','"+myConf+"' , UpdateScoreboard_callback);", 10000 );         
        }
}

function NewScoresFeed(LeagueID, GameDate, Season, PageType, Week ){
            
    //check to see if the cookie currently exists            
    var cookie_refresh = Get_Cookie("RefreshScores");
                    
    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('RefreshScores');
    Set_Cookie( 'RefreshScores', RefreshStartTime, '', '/', '', '' ); 
    
    //get the conference from the ddlb (NCAAF and NCAAB)
    var ctlConf = document.getElementById("confSelect");
    var myConf="";
    if (ctlConf != null)
        myConf = ctlConf.value;
    
    //start the new scores feed
    Scoreboard.UpdateScoreboard(LeagueID, GameDate, Season, 'true', '01-01-1900', PageType,RefreshStartTime,Week, myConf,  UpdateScoreboard_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('RefreshScores');
    }       
}

function AutoRefreshOn(LeagueID, GameDate, Season, Week)
{
   //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 ="Scores Refresh: <a href=\"javascript:AutoRefreshOff('"+LeagueID+"','"+GameDate+"','"+Season+"','"+ Week+"')\">Off</a> | <strong>On</strong>";
       RefreshDiv.innerHTML=temp;
   }
   
   //Start-up the updates again
    Set_Cookie( 'RefreshScores', 'On', '', '/', '', '' ); 
    NewScoresFeed(LeagueID, GameDate, Season, 'Matchups', Week )
}

function AutoRefreshOff(LeagueID, GameDate, Season, Week)
{
    //This function is fired when the Scores Update Off link is click
   Delete_Cookie('RefreshScores');

   // Change the HTML to show the new refresh state
   var RefreshDiv = document.getElementById('scores-note');
   if (RefreshDiv!=null)
   {
       RefreshDiv.innerHTML="Scores Refresh: <strong>Off</strong> | <a href=\"javascript:AutoRefreshOn('"+LeagueID+"','"+GameDate+"','"+Season+"','"+ Week+"')\">On</a>"
   }
}

function UpdateConference(leagueID)
{
    //This function is used to update the conference title when a new option is selected in the ddlb
    var ctlConf = document.getElementById("confSelect");
    var myConf="";
    var myConfValue="";
    if (ctlConf != null)
    {   
        myConf = ctlConf.selectedIndex;
        
        var conferenceTitle = document.getElementById("ConfTitle");
          
        if (conferenceTitle != null)
        {
            var SelValue = ctlConf.options[myConf].text;
            conferenceTitle.innerHTML = SelValue;
        }
        window.focus();
        
        //sets the conference cookie
        Delete_Cookie('SelectedConf_'+leagueID);
        myConfValue = ctlConf.value;
        Set_Cookie('SelectedConf_'+leagueID,myConfValue,'','/','','');
    }
}


