var isIE = /*@cc_on!@*/false;
var tracker=1;
var indexMax = 40;
var timeout;
var photoURL;
var nhlURL = "proxy.php?url=http://www.cbssports.com/nhl/photos/";
var ihlURL = "proxy.php?url=http://www.cbssports.com/nhl/photos/ihl";
var ncaaURL = "proxy.php?url=http://www.cbssports.com/collegehockey/photos/ncaah";
var styleAttribute = "style";
if (isIE)
 styleAttribute = "cssText";


function getRemoteContent()
{
 var hiddenphotos = document.getElementById('hiddenphotos');
 hiddenphotos.innerHTML = "";

 var xmlhttp;
 if (window.XMLHttpRequest)
 {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
 }
 else if (window.ActiveXObject)
 {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 else
 {
  alert("Your browser does not support XMLHTTP!");
 }
 xmlhttp.open("GET", photoURL, true);
 xmlhttp.onreadystatechange=function()
 {
  if(xmlhttp.readyState==4)
  {
   if(xmlhttp.status==200)
   {
    //// get html from url
    bodyText = xmlhttp.responseText;

    //// only get html within the body tag
    var start = bodyText.indexOf("<body>")+6;
    var end = bodyText.indexOf("</body>");
    bodyText = bodyText.substring(start,end).replace(/delaysrc/g,"src");

    //// populate the hidden div with the imported html
    var hiddenphotos = document.getElementById('hiddenphotos');
    hiddenphotos.innerHTML = bodyText;
   }
  }
 }
 xmlhttp.send(null);
 return true;
}

function populateDivWithPhotos()
{
    var hiddenPhotosLength = document.getElementById('hiddenphotos').innerHTML.length;
    var populated = false;


    //// populate the div_links section with images and links
    var linksDiv = document.getElementById('div_links');
    linksDiv.innerHTML = "";
    for (i=1;i<=40;i++)
    {
     //// get image from cbs sportsline imported content
     var img = document.getElementById(""+i);
     if (img != null)
     {
      //// create a div that containts the image
      var div = document.createElement("div");
      setStyle(div,"text-align: center;");
      var a = document.createElement("a");
      a.setAttribute("href","javascript:getLargePhoto("+i+");");
      setStyle(img,"border: none;");
      a.appendChild(img);
      div.appendChild(a);
      linksDiv.appendChild(div);

      //// create a div that contains the image alt text
      var div = document.createElement("div");
      setStyle(div,"text-align: center; font-size: 6pt;");
      a = document.createElement("a");
      a.setAttribute("href","javascript:getLargePhoto("+i+");");
      var atext = document.createTextNode(img.alt);
      a.appendChild(atext);
      div.appendChild(a);
      linksDiv.appendChild(div);

      //// add spacing between images
      var br = document.createElement("br");
      linksDiv.appendChild(br);
      linksDiv.appendChild(br);

      populated = true;
      indexMax = i;
     }
    }

    if (populated || hiddenPhotosLength > 0)
    {
     clearInterval(timeout);
     getLargePhoto(1);
    }
}

function getLargePhoto(index)
{
 tracker = index;
 var photoDiv = document.getElementById('div_photo');
 photoDiv.innerHTML = "";
 var smallPhoto = document.getElementById(index);
 if(smallPhoto != null)
 {
  var largePhoto = document.createElement("img");
  setStyle(largePhoto,"vertical-align: middle;");
  largePhoto.setAttribute("src",smallPhoto.src.replace(/_small/,"_large"));
  photoDiv.appendChild(largePhoto);
 }
 else
 {
  photoDiv.innerHTML = "We're sorry. <br>No recent photos are available for this gallery."
 }
}

function runOnLoad()
{
 alert("All photos depicted in the gallery are protected by copyright and are owned or controlled by \nSportsLine.com Inc. or the party credited as the owner. ModSquadHockey.com is a private \nnon-profit site and its contents are provided for non-commercial use only. By using this \npicture browser you declare to respect SportsLine's Terms of Service. ModSquadHockey.com \nis not affiliated in any way with SportsLine.com.");

 team = document.getElementById("team_pulldown");
 changeTeam(team.options[team.selectedIndex].value);
 // changeLeagueToNHL();
}

window.onload = runOnLoad;

function setStyle(element, css)
{
    if (isIE)
	element.style.cssText = css;
    else
	element.setAttribute("style",css);
}

function changeLeagueToNHL()
{
 setPhotoURL(nhlURL);
 showElement("team_div");
}

function changeLeagueToIHL()
{
 setPhotoURL(ihlURL);
 hideElement("team_title");
 hideElement("team_div");
}

function changeLeagueToNCAA()
{
 setPhotoURL(ncaaURL);
 hideElement("team_title");
 hideElement("team_div");
}

function setPhotoURL(url)
{
 photoURL = url;
 reloadPage();
}

function reloadPage(){
 var hiddenPhotosDiv = document.getElementById('hiddenphotos');
 hiddenPhotosDiv.innerHTML = "";
 getRemoteContent();
 timeout = setInterval("populateDivWithPhotos()",300);
 //getLargePhoto(tracker);
}

function next(){
 tracker++;
 if(tracker>indexMax)
  tracker=1;
 getLargePhoto(tracker);
}

function back(){
 tracker--;
 if(tracker<1)
  tracker=indexMax;
 getLargePhoto(tracker);
}

function changeTeam(team)
{
 if (team != "")
  setPhotoURL("proxy.php?url=http://www.cbssports.com/nhl/teams/photos/"+team);
 else
  setPhotoURL(nhlURL);
}

function hideElement(name)
{
 var div = document.getElementById(name);
 setStyle(div,"visibility: hidden");
}

function showElement(name)
{
 var div = document.getElementById(name);
 setStyle(div,"visibility: visible");
}
