// if I'm not part of the frameset I belong to, redirect the request to the frameset page and reload the current page to the "Content" frame
if (parent.frames.length < 3 ||
  parent.frames[0].name != "Top" ||
  parent.frames[1].name != "Navi" ||
  parent.frames[2].name != "Content")
{
  s = location.href.replace(/SportTracksPlugins.*/, "SportTracksPlugins/index.html?content=") + encodeURI(location.href);
   if (location.protocol != "file:" &&
      location.hostname != "127.0.0.1")
    location.href = s;
}

anchor = location.search.replace(/.*section=/, "#");
if (anchor.substr(0, 1) == "#")
  location.href = location.href.replace(/\?.*/, "") + anchor;

// mark the navigation item with this id in the "Navi" frame
function SetNavi(id)
{
  try
  {
    parent.SetNaviItem(id);
    parent.Navi.NaviItemSelected(id);
  } catch (e)
  {
  }
}

// do an Ajax like request to get the release information from the XML file of a plugin
// called from the "onLoad" handler of all plugin overview pages
var xmlRequest;
function GetReleaseInfoForPlugin(pluginName)
{
  try
  {
    if(location.protocol == "file:" || location.hostname == "127.0.0.1")
      return;

    // append random number to let the browser skip the cache
    url = encodeURI("/SportTracksPlugins/DownloadArea/" + pluginName + "?rnd=" + Math.random());

    if (window.XMLHttpRequest)
      xmlRequest = new XMLHttpRequest();
    else if (window.ActiveXObject)
      xmlRequest = new ActiveXObject("Microsoft.XMLHTTP" );
    else
      alert("Sorry, your web browser does not support Ajax!" );

    if (xmlRequest.overrideMimeType)
        xmlRequest.overrideMimeType('text/xml');

    xmlRequest.open("GET", url, true );
    xmlRequest.onreadystatechange = ReleaseInfoCallbackFunctionReleaseInfo;

    try
    {
     xmlRequest.setRequestHeader('Cache-Control', 'no-cache');
    } catch (e1)
    {
    }

    xmlRequest.send(null );
  } catch (e2)
  {
    alert("Error: " + e2);
  }
}

// do an Ajax like request to get the release information from the XML file of a plugin
// called from the "onLoad" handler of all plugin newsandhistory pages
// DUPLICATED CODE; FIXME!
function GetReadmeForPlugin(pluginName)
{
  try
  {
    if(location.protocol == "file:" || location.hostname == "127.0.0.1")
      return;

    // append random number to let the browser skip the cache
    url = encodeURI("/SportTracksPlugins/DownloadArea/" + pluginName + "?rnd=" + Math.random());

    if (window.XMLHttpRequest)
      xmlRequest = new XMLHttpRequest();
    else if (window.ActiveXObject)
      xmlRequest = new ActiveXObject("Microsoft.XMLHTTP" );
    else
      alert("Sorry, your web browser does not support Ajax!" );

    if (xmlRequest.overrideMimeType)
        xmlRequest.overrideMimeType('text/plain');

    xmlRequest.open("GET", url, true );
    xmlRequest.onreadystatechange = ReleaseInfoCallbackFunctionReadme;

    try
    {
     xmlRequest.setRequestHeader('Cache-Control', 'no-cache');
    } catch (e1)
    {
    }


    xmlRequest.send(null );
  } catch (e)
  {
    alert("Error: " + e);
  }
}
function ReleaseInfoCallbackFunctionReadme ()
{
  if (xmlRequest.readyState == 4) {
    if (200 != xmlRequest.status)
      alert("Error: " + xmlRequest.status + ": " + xmlRequest.statusText );
    else
    {
      try
      {
        // get the information from the XML file that we've just downloaded ...
        result = xmlRequest.responseText;

        // ... and paste it into the corresponding fields of the document
        document.getElementById("readme").innerHTML = "<pre>" + result + "</pre>";
      } catch (e)
      {
        alert("Error: " + e);
      }
    }
  }
}

function ReleaseInfoCallbackFunctionReleaseInfo ()
{
  if (xmlRequest.readyState == 4) {
    if (200 != xmlRequest.status)
      alert("Error: " + xmlRequest.status + ": " + xmlRequest.statusText );
    else
    {
      try
      {
        // get the information from the XML file that we've just downloaded ...
        result = xmlRequest.responseXML;
        var fullNode = result.getElementsByTagName("CurrentFull")[0];
        if (fullNode != null)
        {
          var fullVersion = fullNode.getAttribute("version");
          var fullBuild = fullNode.getAttribute("build");
          var fullReleaseDate = fullNode.getAttribute("releaseDate");
          var fullDownloadURL = fullNode.getAttribute("downloadURL");
          var alternateDownloadURL = fullNode.getAttribute("alternateDownloadURL");
          if (alternateDownloadURL == null)
            alternateDownloadURL = fullDownloadURL;
          var fullComment = fullNode.getAttribute("comment");
          // ... and paste it into the corresponding fields of the document
          document.getElementById("FullVersion").innerHTML = fullVersion;
          document.getElementById("FullBuild").innerHTML = fullBuild;
          document.getElementById("FullReleaseDate").innerHTML = fullReleaseDate;
          // yes it's exchanged :)
          document.getElementById("FullDownloadLink").href = alternateDownloadURL;
          document.getElementById("AlternateDownloadLink").href = fullDownloadURL;
          document.getElementById("FullComment").innerHTML = fullComment;
        } else
        {
          var n = document.getElementById("FullDownload");
          n.parentNode.removeChild(n);
        }

        var betaNode = result.getElementsByTagName("CurrentBeta")[0];
        if (betaNode != null)
        {
          var betaVersion = betaNode.getAttribute("version");
          var betaBuild = betaNode.getAttribute("build");
          var betaReleaseDate = betaNode.getAttribute("releaseDate");
          var betaDownloadURL = betaNode.getAttribute("downloadURL");
          var betaComment = betaNode.getAttribute("comment");

          if (fullNode == null ||
              betaReleaseDate.replace(/-/g, "") > fullReleaseDate.replace(/-/g, ""))
          {
            document.getElementById("BetaVersion").innerHTML = betaVersion;
            document.getElementById("BetaBuild").innerHTML = betaBuild;
            document.getElementById("BetaReleaseDate").innerHTML = betaReleaseDate;
            document.getElementById("BetaDownloadLink").href = betaDownloadURL;
            document.getElementById("BetaComment").innerHTML = betaComment;
          } else
          {
            var n = document.getElementById("BetaDownload");
            n.parentNode.removeChild(n);
          }
        }
      } catch (e)
      {
        alert("Error: " + e);
      }
    }
  }
}

function SwitchLanguage(language_code)
{
  Switch(parent.Navi, language_code);
  Switch(parent.Content, language_code);
  Switch(parent.Top, language_code);
}

function Switch(window, language_code)
{
  try
  {
    if (language_code == "en")
      language_code = "";
    else
      language_code = language_code + "/";

    current_href = window.location.href;
    new_href = current_href.replace(/\/SportTracksPlugins\/[a-z][a-z]/, "/SportTracksPlugins");
    new_href = new_href.replace(/\/SportTracksPlugins\//, "/SportTracksPlugins/" + language_code);

    if (new_href != current_href)
      //window.location.href = new_href;
      window.location.replace(new_href);
  } catch (e)
  {
  }
}

