 /**
  * Realtime rapporting js file
  */
  

  
  function getElementsByClassNameCust(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
      oElement = arrElements[i];
      if(oRegExp.test(oElement.className)){
        arrReturnElements.push(oElement);
      }
    }
    return (arrReturnElements)
  }
  
  
  function disableStandingsPlayoffLine(oTr) {
    //alert(oTr.parentNode.parentNode.tagName);
    var aElt = getElementsByClassNameCust(oTr.parentNode.parentNode,'td','rtTableRowLine');
    //alert(aElt.length);
    for(i=0; i<aElt.length; i++) {
      aElt[i].className = "";
    }
  }
  
  
  
  var fadeStart = 1;
  var fadeStop = 10;
  var fadeStep = 3;
  var fadeTime = 50;
  
  function fadeIn(sId,iStep) {
    oElt = document.getElementById(sId);
    if (oElt) {
      if (iStep == fadeStart && oElt.style.display != 'none') return true;
      oElt.style.opacity = iStep/fadeStop;
      oElt.style.filter = 'alpha(opacity=' + iStep*fadeStop + ')';
      if (iStep == fadeStart) {
        oElt.style.display = "";
      }
      if (iStep < fadeStop) {
        iStep+= fadeStep;
        setTimeout("fadeIn('"+sId+"',"+iStep+")",fadeTime);
      }
    }
  }
  
  function fadeOut(sId,iStep) {
    oElt = document.getElementById(sId);
    if (oElt) {
      if (iStep == fadeStop && oElt.style.display == 'none') return true;
      oElt.style.opacity = iStep/fadeStop;
      oElt.style.filter = 'alpha(opacity=' + iStep*fadeStop + ')';
      if (iStep > fadeStart) {
        iStep-= fadeStep;
        setTimeout("fadeOut('"+sId+"',"+iStep+")",fadeTime);
      }
      else {
        oElt.style.display = "none";
      }
    }
  }
  
  
  /********** B : Game shots **********/
  
  var dispType = "All";
  var dispRound = "All";
  var dispPlayerH = "All";
  var dispPlayerA = "All";
  
  var bLoadCoord = false;
  var aCoordX = new Array();
  var aCoordY = new Array();
  var aCoordXSpot = new Array();
  var aCoordYSpot = new Array();
  var aCoordXGoalH = new Array();
  var aCoordYGoalH = new Array();
  var aCoordXGoalHSpot = new Array();
  var aCoordYGoalHSpot = new Array();
  var aCoordXGoalA = new Array();
  var aCoordYGoalA = new Array();
  var aCoordXGoalASpot = new Array();
  var aCoordYGoalASpot = new Array();
  var iPuckSize = 20;
  var bMessage = false;
  var sMessage = "";
  
  function initFilters() {
    new Ajax.Request('rt_game_shots_filters.php?init=1', {
      method: 'get',
      onSuccess: function(transport) {
        //alert(transport.responseText);
        var aResult = transport.responseText.split(";")
        dispType = (aResult[0] != "") ? aResult[0] : "All";
        dispRound = (aResult[1] != "") ? aResult[1] : "All";
        dispPlayerH = (aResult[2] != "") ? aResult[2] : "All";
        dispPlayerA = (aResult[3] != "") ? aResult[3] : "All";
        loadShots();
      }
    });
  }
  
  function setFilters(sFilter,sValue) {
    new Ajax.Request('rt_game_shots_filters.php?set=1&filter=' + sFilter + '&value=' + sValue, {
      method: 'get'
    });
  }
  
  function setFilterType(sValue) {
    if (dispType != sValue) {
      setFilters("Type",sValue);
      dispType = sValue;
      filterShots();
    }
    return true;
  }
  
  function setFilterRound(sValue) {
    if (dispRound != sValue) {
      setFilters("Round",sValue);
      dispRound = sValue;
      displayRoundStats();
      filterShots();
    }
    return true;
  }
  
  function setFilterPlayerH(sValue) {
    if (dispPlayerH != sValue) {
      setFilters("PlayerH",sValue);
      dispPlayerH = sValue;
      if (dispPlayerH != "All" && dispPlayerH != "None") {
        setFilters("PlayerA","None");
        dispPlayerA = "None";
        selectPlayer();
      }
      filterShots();
    }
    return true;
  }
  
  function setFilterPlayerA(sValue) {
    if (dispPlayerA != sValue) {
      setFilters("PlayerA",sValue);
      dispPlayerA = sValue;
      if (dispPlayerA != "All" && dispPlayerA != "None") {
        setFilters("PlayerH","None");
        dispPlayerH = "None";
        selectPlayer();
      }
      filterShots();
    }
    return true;
  }
  
  function displayRoundStats() {
    // stats table
    var aTabs = document.getElementsByTagName("TABLE");
    for (i=0; i<aTabs.length; i++) {
      if (aTabs[i].className.search("\\bround") != -1) {
        var oLink = document.getElementById(aTabs[i].className + "Link");
        var oGKH = document.getElementById(aTabs[i].className + "GKH");
        var oGKA = document.getElementById(aTabs[i].className + "GKA");
        if (aTabs[i].className.search("\\bround"+dispRound+"\\b") != -1) {
          //aTabs[i].style.display = '';
          setTimeout("fadeIn('"+aTabs[i].id+"',fadeStart);",200);
          setTimeout("fadeIn('"+aTabs[i].className+"GKH"+"',fadeStart);",200);
          setTimeout("fadeIn('"+aTabs[i].className+"GKA"+"',fadeStart);",200);
          oLink.style.fontWeight = "bold";
        }
        else {
          //aTabs[i].style.display = 'none';
          fadeOut(aTabs[i].id,fadeStop);
          fadeOut(aTabs[i].className+"GKH",fadeStop);
          fadeOut(aTabs[i].className+"GKA",fadeStop);
          oLink.style.fontWeight = "normal";
        }
      }
    }
  }
  
  
  
  function checkSpot(x,y,t) {
    spot = takenSpot(x,y,t);
    if (spot) {
      var xcoord = spot[0];
      var ycoord = spot[1];
      var iShift = iPuckSize - 2;
      if (bMessage) sMessage += x+","+y+" steps over "+xcoord+","+ycoord+" ("+iShift+")\n";
      
      // get x and y preference order (when looping, to stop at the closest)
      if (x < xcoord) { // if new point more left
        var aX1 = new Array((xcoord - iShift),(xcoord - (iShift/2)),xcoord,(xcoord + (iShift/2)),(xcoord + iShift));
      }
      else {
        var aX1 = new Array((xcoord + iShift),(xcoord + (iShift/2)),xcoord,(xcoord - (iShift/2)),(xcoord - iShift));
      } 
      if (y < ycoord) { // if new point more down
        var aY1 = new Array((ycoord - iShift),(ycoord - (iShift/2)),ycoord,(ycoord + (iShift/2)),(ycoord + iShift));
      }
      else {
        var aY1 = new Array((ycoord + iShift),(ycoord + (iShift/2)),ycoord,(ycoord - (iShift/2)),(ycoord - iShift));
      }
      
      //alert(sMessage +": "+ spot +" donc "+ aX +" et "+ aY)
      if (bMessage) sMessage += aX1+" ("+aX1.length+") and "+aY1+" ("+aY1.length+")\n";
      
      // create coord combinaison
      for(var ii=0; ii<aX1.length; ii++) {
        iX = parseInt(aX1[ii],10);
        for(var jj=0; jj<aY1.length; jj++) {
          iY = parseInt(aY1[jj],10);
          if (bMessage) sMessage += " => x"+ii+":"+iX+",y"+jj+":"+iY+"\n";
          spot = takenSpot(iX,iY,t);
          if (iX && iY && !spot) { // find free spot
            return new Array(iX,iY);
          }
        }
      }
    }
    // if not free spot found, leave it where it is...
    return new Array(x,y);
  }
  
  function takenSpot(x,y,t) { 
    iRange = iPuckSize - 2;
    
    var aX2 = aCoordXSpot;
    var aY2 = aCoordYSpot;
    if (t==1) {
      aX2 = aCoordXGoalHSpot;
      aY2 = aCoordYGoalHSpot;
    }
    if (t==2) {
      aX2 = aCoordXGoalASpot;
      aY2 = aCoordYGoalASpot;
    }
    
    for(var iii=0; iii<aX2.length; iii++) {
      xcoord = parseInt(aX2[iii],10);
      iBoundLeft = xcoord - iRange;
      iBoundRight = xcoord + iRange;
      
      ycoord = parseInt(aY2[iii],10);
      iBoundTop = ycoord - iRange;
      iBoundBot = ycoord + iRange;

      //alert(xcoord + ": " + iBoundLeft +" ? "+ x +" ? "+ iBoundRight +" \n "+ ycoord + ": " + iBoundTop +" ? "+ y +" ? "+ iBoundBot + "\n");
      
      if (x > iBoundLeft && x < iBoundRight) {
        if (y > iBoundTop && y < iBoundBot) {
          return new Array(xcoord,ycoord);
        }
      }
    }
    return false;
  }
  
  
  
  function filterShots() {
  
    aCoordXSpot = new Array();
    aCoordYSpot = new Array();
    aCoordXGoalHSpot = new Array();
    aCoordYGoalHSpot = new Array();
    aCoordXGoalASpot = new Array();
    aCoordYGoalASpot = new Array();

    aTypes = new Array("1","Shot","All");
    for (i=0; i<aTypes.length; i++) {
      var oLink = document.getElementById("type" + aTypes[i] + "Link");
      if (oLink) oLink.style.fontWeight = (aTypes[i] == dispType) ? "bold" : "normal";
    }
  
    //var aImgs = document.getElementsByTagName("IMG");
    var aImgs = document.getElementsByTagName("DIV");
    for (i=0; i<aImgs.length; i++) {
      puck = false;
      disp = '';
      if (aImgs[i].className.search("\\bround") != -1) {
        puck = true;
        if (aImgs[i].className.search("\\bround"+dispRound+"\\b") == -1) {
          disp = 'none';
        }
      }
      if (aImgs[i].className.search("\\btype") != -1) {
        puck = true;
        if (aImgs[i].className.search("\\btype"+dispType+"\\b") == -1) {
          disp = 'none';
        }
      }
      if (aImgs[i].className.search("\\bplayer") != -1) {
        puck = true;
        if (aImgs[i].className.search("\\bteamHome\\b") != -1) {
          if (aImgs[i].className.search("\\bplayer"+dispPlayerH+"\\b") == -1) {
            disp = 'none';
          }
        }
        if (aImgs[i].className.search("\\bteamAway\\b") != -1) {
          if (aImgs[i].className.search("\\bplayer"+dispPlayerA+"\\b") == -1) {
            disp = 'none';
          }
        }
      }
      if (puck) {
      
        //aImgs[i].style.display = disp;
        if (disp == 'none') {
          fadeOut(aImgs[i].id,fadeStop);
        }
        else {
        
          // if image in the goal or in the rink
          if (aImgs[i].id.search("\\bgoal") != -1) {
            iPuckSize = 14;
            if (aImgs[i].className.search("\\bteamHome\\b") != -1) {
              if (bLoadCoord) { // saves initial coord of the shot (not relocated)
                aCoordXGoalH[i] = aImgs[i].style.left.replace("px","");
                aCoordYGoalH[i] = aImgs[i].style.top.replace("px","");
              }
              // check spot and calculate coord if necessary
              finalCoord = checkSpot(aCoordXGoalH[i],aCoordYGoalH[i],1);
              aCoordXGoalHSpot[i] = finalCoord[0]; aCoordYGoalHSpot[i] = finalCoord[1];
            }
            if (aImgs[i].className.search("\\bteamAway\\b") != -1) {
              if (bLoadCoord) { // saves initial coord of the shot (not relocated)
                aCoordXGoalA[i] = aImgs[i].style.left.replace("px","");
                aCoordYGoalA[i] = aImgs[i].style.top.replace("px","");
              }
              // check spot and calculate coord if necessary
              finalCoord = checkSpot(aCoordXGoalA[i],aCoordYGoalA[i],2);
              aCoordXGoalASpot[i] = finalCoord[0]; aCoordYGoalASpot[i] = finalCoord[1];
            }
            oInfo = document.getElementById(aImgs[i].id.replace("goal","goalInfo"));
          }
          else {
            if (bLoadCoord) { // saves initial coord of the shot (not relocated)
              aCoordX[i] = aImgs[i].style.left.replace("px","");
              aCoordY[i] = aImgs[i].style.top.replace("px","");
            }
          
            // when display everything for at least one team, make smaller so it can be readable
            if (dispRound == "All" && dispType == "All" && (dispPlayerH == "All" || dispPlayerA == "All")) {
              iPuckSize = 16; iFontSize = 10; iPadTop = 1;
            }
            else {
              iPuckSize = 20; iFontSize = 12; iPadTop = 2;
            }
            
            // resize the puck image
            aImgs[i].style.width = iPuckSize + "px";
            aImgs[i].style.height = iPuckSize + "px";
            //aImgs[i].style.backgroundSize = iPuckSize + "px";  // have to wait CSS3 for that...
            oJersey = document.getElementById(aImgs[i].id + "Jersey");  
            if (oJersey) {
              oJersey.style.paddingTop = iPadTop + "px"; 
              oJersey.style.fontSize = iFontSize; 
            }
            
            // debug...
            if (aImgs[i].id == "puck2") bMessage = true;
            if (aImgs[i].id == "puck3") bMessage = true;
            if (aImgs[i].id == "puck9") bMessage = true;
            if (aImgs[i].id == "puck92") bMessage = true;

            // check spot and calculate coord if necessary
            finalCoord = checkSpot(aCoordX[i],aCoordY[i],0);
            aCoordXSpot[i] = finalCoord[0]; aCoordYSpot[i] = finalCoord[1];

            oInfo = document.getElementById(aImgs[i].id.replace("puck","puckInfo"));
          }

          // relocate if collision
          aImgs[i].style.left = finalCoord[0] + "px";
          aImgs[i].style.top = finalCoord[1] + "px"; 
          if (oInfo) { // replace the popup info div
            oInfo.style.left = parseInt(finalCoord[0],10) + 15 + "px";
            oInfo.style.top = parseInt(finalCoord[1],10) + 15 + "px";
          }
        
          setTimeout("fadeIn('"+aImgs[i].id+"',fadeStart);",200);
          
          bMessage = false;  
        }
      }
    }
  }
  
  function selectPlayer() {
    if (document.getElementById("filterPlayerH")) {
      var oSelect = document.getElementById("filterPlayerH");
      for (var i=0; i<oSelect.options.length; i++) {
        if (oSelect.options[i].value == dispPlayerH) {
          oSelect.selectedIndex = oSelect.options[i].index;
          break;
        }
      }
    }
    if (document.getElementById("filterPlayerA")) {
      var oSelect = document.getElementById("filterPlayerA");
      for (var i=0; i<oSelect.options.length; i++) {
        if (oSelect.options[i].value == dispPlayerA) {
          oSelect.selectedIndex = oSelect.options[i].index;
          break;
        }
      }
    }
    return true;
  }
  
  function loadShots() {
    selectPlayer();
    displayRoundStats();
    
    bLoadCoord = true;
    aCoordX = new Array();
    aCoordY = new Array();
    aCoordXGoalH = new Array();
    aCoordYGoalH = new Array();
    aCoordXGoalA = new Array();
    aCoordYGoalA = new Array();
    sMessage = "";
    filterShots();
    //alert(sMessage);
    //alert(aCoordX+"\n"+aCoordXSpot);
    //alert(aCoordXGoalA+"\n"+aCoordXGoalASpot);
    bLoadCoord = false;
    
  }
  
  function refreshGameShots(params) {
    var myAjax = new Ajax.Updater(
      {success: "divContentLive"},
      "rt_game_shots_ajax.php", 
      {
        method: 'get', 
        parameters: params,
        onComplete: loadShots
      });
    setTimeout("refreshGameShots('"+params+"');",30000); // 30 seconds
    return true;
  }
  
  
  /********** E : Game shots **********/