/**
 * This javascript file handles the preview for the next matchday
 * or the just passed one.
 * The actual matchday javascript file is read, the information
 * is displayed using background-positioning.
 * A little hack is required for internet explorer to display
 * the transparent pngs.
 */

/**
 * JSON definition of club id's and
 * image position in the background image
 */
clubs = {
  "liga1" : {
    "156" : 3,
    "171" : 16,
    "387" : 14,
    "684" : 2,
    "810" : 5,
    "1902": 8,
    "164" : 15,
    "683" : 10,
    "157" : 13,
    "159" : 9,
    "167" : 12,
    "161" : 1,
    "808" : 17,
    "162" : 18,
    "160" : 6,
    "169" : 4,
    "431" : 11,
    "172" : 7    
  },
  "liga2" : {
    "163" : 17,
    "816" : 6,
    "432" : 1,
    "165" : 2,
    "812" : 3,
    "1772": 4,
    "815" : 5,
    "1738": 7,
    "809" : 8,
    "1765": 9,
    "751" : 10,
    "804" : 11,
    "155" : 12,
    "818" : 13,
    "1743": 14,
    "166" : 15,
    "680" : 16,
    "2012": 18   
  }
}
 
/**
 * Reads the javscript file and displays 
 * the content onto the divs. The divs are
 * "spieltagsvorschauliga1" and
 * "spieltagsvorschauliga2"
 */
function populateMatchdayPreview() {
  //get matchday data, depending on the available elements
  if (document.getElementById('spieltagsvorschauliga1')) {
    //populate the league1 preview
    populateLeagueElement("liga1");
  }  
  if (document.getElementById('spieltagsvorschauliga2')) {
    //populate the league2 preview
    populateLeagueElement("liga2");
  } 
}

/**
 * Builds an attribute, returns it
 * to the calling element
 *
 * @param type the type of the attribute
 * @param the attribute value
 */
function buildAttribute(type, value) {
  var at = document.createAttribute(type);
  at.nodeValue = value;
  return at;
}

/**
  * Writes a bunch of images to the given element
  * from the given league by inspecting the
  * javascript array of the current matchday
  *
  * @param league the league to watch at
  */
function populateLeagueElement(league) {
  //select proper array
  if (league == "liga1") {
    var arr = matchdetails;
  } else {
    var arr = matchdetail2;
  }
  var i = 0;
  //cycle
  for (var key in arr) {
    i++;
    //save the team id's
    var homeID = arr[key]['oti_home'];
    var awayID = arr[key]['oti_away'];
    //add home image to dom
    var home = document.createElement('div');
    home.setAttributeNode(buildAttribute('class', 'home'));
    //add guest image to dom
    var guest = document.createElement('div');
    guest.setAttributeNode(buildAttribute('class', 'guest'));
    //add the link to the match
    var link = document.createElement('a');
    link.setAttributeNode(buildAttribute('href', 'javascript:openMatchDay("'+league+'", '+key+');'));
    //tooltip and id
    var date = new Date();
    date.setTime(((arr[key]['match_timestamp']) * 1000) + 900000);
    var date2 = new Date();
    if (date < date2) {
      title = "Analyse";
    } else {
      title = "Vorschau";
    }
    link.setAttributeNode(buildAttribute('id', "vorschauElement" + key));
    link.setAttributeNode(buildAttribute('title', title));
    //add an additional class, if left or right
    if (i == 1) {
      link.setAttributeNode(buildAttribute('class', 'matchDayLeft'));
    }
    if (i == 9) {
      link.setAttributeNode(buildAttribute('class', 'matchDayRight'));
    }
    //double point
    var span = document.createElement('span');
    span.innerHTML = ":";
    //add home and guest to the link
    link.appendChild(home);
    link.appendChild(span);
    link.appendChild(guest);
    //add link to container
    document.getElementById('spieltagsvorschau' + league).appendChild(link);
    //change position according to team id array
    var x = (clubs[league][homeID] - 1) * -22;
    if (league == "liga1") {
      var y = 0;
    } else {
      var y = -22;
    }
    if (home.style.backgroundPositionX) {
      home.style.backgroundPositionX = x + "px";
      home.style.backgroundPositionY = y + "px";
    } else {
      home.style.backgroundPosition = x + 'px ' + y + 'px'; 
    }
    //change position according to team id array
    var x = (clubs[league][awayID] - 1) * -22;
    if (league == "liga1") {
      var y = 0;
    } else {
      var y = -22;
    }
    if (guest.style.backgroundPositionX) {
      guest.style.backgroundPositionX = x + "px";
      guest.style.backgroundPositionY = y + "px";
    } else {
      guest.style.backgroundPosition = x + 'px ' + y + 'px'; 
    }
  }
}

/**
 * Moves the user to either the prereport
 * or the analysis, depending on the matchday
 * date. 15 minutes after matchstart link to
 * analysis
 *
 * @param league the league to display
 * @param omi the match id
 */
function openMatchDay(league, omi) {
  //select proper array
  if (league == "liga1") {
    var arr = matchdetails;
  } else {
    var arr = matchdetail2;
  }
  //set league
  if (league == "liga1") {
    league = "liga";
  }
  //get date
  var date = new Date();
  date.setTime(((arr[omi]['match_timestamp']) * 1000) + 900000);
  var date2 = new Date();
  if (date < date2) {
    //call analysis
    document.location = '/de/'+league+'/matches/'+matchDaySeason+'/index.php?omi='+omi+'&reiter=a&tag='+arr[omi]['matchday'];
  } else {
    //call prereport
    document.location = '/de/'+league+'/matches/'+matchDaySeason+'/index.php?omi='+omi+'&reiter=v&tag='+arr[omi]['matchday'];
  }
}