var camslist=getMHCList();
var querystring=new Array();
var cam=null;

var mhcreq=createXMLRequest("cams.xml", mhcCallback);
var mapReq;

function getChildValue(node, tag) {
  var childNodes=node.getElementsByTagName(tag);
  if(childNodes!=null&&childNodes.length>0&&childNodes[0].firstChild!=null) {
    return childNodes[0].firstChild.data;
  }
  return null;
}

function loadData()
{
  var qs=window.location.search;
  if(qs.indexOf("?")==0) qs=qs.substring(1);
  var qss=qs.split("&");
  for(var i=0; i<qss.length; i++)
  {
    var qse=qss[i].split("=");
    if(qse.length>0)
    {
      if(querystring[qse[0]]==null)
      {
        querystring[qse[0]]=new Array();
      }
      
      if(qse.length>1)
      {
        querystring[qse[0]].push(qse[1]);
      }
      else 
      {
        querystring[qse[0]].push(null);
      }
    }
  }
  
  loadCams();
  
  mhcreq.send(null);
}

function loadCams()
{
  
  if(camslist==null||camslist.length==0)
  {
    document.getElementById("message").style.display="block";
  }
  else
  {
    if(querystring["cam"]==null) 
    {
      cam=null; //camslist[0];
    }
    else
    {
      cam=querystring["cam"];
    }
  }
}

var thumbsAdded=0;
var thumbRow=null;
var xmlDoc=null;

function mhcCallback()
{
  if(mhcreq!=null&&mhcreq.readyState==4)
  {
    if(mhcreq.status==200)
    {
      //find main cam info
      xmlDoc=loadXmlString(mhcreq.responseText);
      var camIds=xmlDoc.getElementsByTagName("id");
      for(var i=0; i<camIds.length; i++)
      {
        if(camIds[i].firstChild!=null&&camIds[i].firstChild.data==cam)
        {
          loadMainCam(camIds[i].parentNode);
        }
        else
        {
          for(var j=0; j<camslist.length; j++)
          {
            if(camIds[i].firstChild!=null&&camIds[i].firstChild.data==camslist[j])
            {
              loadThumb(camIds[i].parentNode);
            }
          }
        }
      }
      for(var i=thumbsAdded; thumbRow!=null&&i<4; i++)
      {
        var cell=document.createElement("TD");
        thumbRow.appendChild(cell);
      }
    }
  }
}

function loadMainCam(node)
{
  var camXml=new CamXmlEntry(); 
  for(var i=0; i<node.childNodes.length; i++)
  {
    var nodeData=(node.childNodes[i].firstChild==null?null:node.childNodes[i].firstChild.data);
    var nodeName=node.childNodes[i].nodeName;
    
    if(nodeName=="id") camXml.id=nodeData;
    if(nodeName=="image") camXml.image=nodeData;
    if(nodeName=="thumbnail") camXml.thumbnail=nodeData;
    if(nodeName=="caption") camXml.caption=nodeData;
    if(nodeName=="description") camXml.description=nodeData;
    if(nodeName=="map") camXml.map=nodeData;
    if(nodeName=="mapid") camXml.mapid=nodeData;
    if(nodeName=="horiz") camXml.horiz=nodeData;
    if(nodeName=="weather") camXml.weather=nodeData;
    if(nodeName=="forecast") camXml.forecast=nodeData;
    if(nodeName=="highway") camXml.highway=nodeData;
    if(nodeName=="reg") camXml.reg=nodeData;
    if(nodeName=="stationName") camXml.stationName=nodeData;
    if(nodeName=="disabled") camXml.disabled=nodeData;
    if(nodeName=="disappear") camXml.disappear=nodeData;
    if(nodeName=="message") camXml.message=nodeData;
    if(nodeName=="longmessage") camXml.longmessage=nodeData;
  }
  if(camXml.isOK())
  {
    var maintb=document.getElementById("mainarea");
    
    if(camXml.horiz.toLowerCase()=="true")
    {
      var theRow=document.createElement("TR");
      var miCell=document.createElement("TD");
      miCell.style.width="450px";
      miCell.style.verticalAlign="top";
      miCell.colSpan="3";
      
      if(camXml.disabled.toLowerCase=="true") {
        miCell.className="disabled";
        var div=document.createElement("div");
        miCell.appendChild(div);
        div.appendChild(document.createTextNode(camXml.longmessage));
        theRow.appendChild(miCell);
      } else {
        var img=document.createElement("img");
        img.src=camXml.image;
        img.alt=img.src;
        miCell.appendChild(img);
        theRow.appendChild(miCell);
      }
      
      var capCell=document.createElement("TD");
      capCell.style.width="150px";
      capCell.style.verticalAlign="top";
      
      var capDiv=document.createElement("DIV");
      capDiv.className="caption";
      capCell.appendChild(capDiv);
      
      var capSpan=document.createElement("span");
      capSpan.className="cameraName";
      capSpan.appendChild(document.createTextNode(camXml.caption));
      capDiv.appendChild(capSpan);
      
      var dscDiv=document.createElement("div");
      dscDiv.appendChild(document.createTextNode(camXml.description));
      capDiv.appendChild(dscDiv);
      
      var remlnk=document.createElement("A");
      remlnk.href="#";
      remlnk.id="addToMyCamsLink";
      remlnk.target="hiddenFrame1";
      remlnk.onclick=function() {
        addMHCCookie(camXml.id);
        window.location.reload();
        return false;
      }
      var remimg=document.createElement("IMG");
      remimg.src="/bchighwaycam/images/plus.gif";
      remlnk.appendChild(remimg);
      var remspn=document.createElement("span");
      remspn.id="mhcSpan";
      remspn.appendChild(document.createTextNode("Remove from My HighwayCams"));
      remlnk.appendChild(remspn);
      capCell.appendChild(remlnk);

      theRow.appendChild(capCell); 
      maintb.appendChild(theRow);
      
      if(camXml.weather!=null||camXml.forecast!=null)
            {
              var lnk=document.createElement("A");
              lnk.href="#";
              lnk.onclick=function() {
                toggleWeatherData();
                return false;
              }
              capCell.appendChild(lnk);
              
              var wdImg=document.createElement("img");
              wdImg.src="/bchighwaycam/images/weatherInformation_OFF.gif";
              wdImg.alt="Toggle Weather Information";
              lnk.appendChild(wdImg);
      }
      
      theRow=document.createElement("TR");
      
      var mapCell=document.createElement("TD");
      mapCell.style.verticalAlign="top";
      mapCell.colSpan="4";
      mapCell.id="insetMap";
      theRow.appendChild(mapCell);
      
      maintb.appendChild(theRow);
      
    }
    else
    {
      var theRow=document.createElement("TR");
      var miCell=document.createElement("TD");
      miCell.style.width="450px";
      miCell.style.verticalAlign="top";
      miCell.colSpan="3";
      if(camXml.disabled.toLowerCase()=="true") { 
        miCell.className="disabled";
        var div=document.createElement("div");
        miCell.appendChild(div);
        div.appendChild(document.createTextNode(camXml.longmessage));
        theRow.appendChild(miCell);
      } else {
        var img=document.createElement("img");
        img.src=camXml.image;
        img.alt=img.src;
        miCell.appendChild(img);
        theRow.appendChild(miCell);
      }
      
      var mapCell=document.createElement("TD");
      mapCell.style.width="150px";
      mapCell.style.verticalAlign="top";
      mapCell.id="insetMap";
      theRow.appendChild(mapCell);
      
      maintb.appendChild(theRow);
      
      theRow=document.createElement("TR");
      var capCell=document.createElement("TD");
      capCell.style.verticalAlign="top";
      capCell.colSpan="3";
      
      var capDiv=document.createElement("DIV");
      capDiv.className="caption";
      capCell.appendChild(capDiv);
      
      var capSpan=document.createElement("span");
      capSpan.className="cameraName";
      capSpan.appendChild(document.createTextNode(camXml.caption));
      capDiv.appendChild(capSpan);
      
      var dscDiv=document.createElement("div");
      dscDiv.appendChild(document.createTextNode(camXml.description));
      capDiv.appendChild(dscDiv);
      
      theRow.appendChild(capCell);
      
      var remCell=document.createElement("TD");
      var remlnk=document.createElement("A");
      remlnk.href="#";
      remlnk.id="addToMyCamsLink";
      remlnk.target="hiddenFrame1";
      remlnk.onclick=function() {
        addMHCCookie(camXml.id);
        window.location.reload();
        return false;
      }
      var remimg=document.createElement("IMG");
      remimg.src="/bchighwaycam/images/plus.gif";
      remlnk.appendChild(remimg);
      var remspn=document.createElement("span");
      remspn.id="mhcSpan";
      remspn.appendChild(document.createTextNode("Remove from My HighwayCams"));
      remlnk.appendChild(remspn);
      remCell.appendChild(remlnk);
      
      theRow.appendChild(remCell);
      maintb.appendChild(theRow);
      
      if(camXml.weather!=null||camXml.forecast!=null)
      {
        theRow=document.createElement("TR");
        maintb.appendChild(theRow);
        var wbCell=document.createElement("TD");
        var lnk=document.createElement("A");
        lnk.href="#";
        lnk.onclick=function() {
          toggleWeatherData();
          return false;
        }
        wbCell.appendChild(lnk);
        
        var wdImg=document.createElement("img");
        wdImg.src="/bchighwaycam/images/weatherInformation_OFF.gif";
        wdImg.alt="Toggle Weather Information";
        lnk.appendChild(wdImg);
        
        theRow.appendChild(wbCell);
      }
    }
    
    //map
    
    var mapimg=document.createElement("img");
    mapimg.id="mapimg";
    mapimg.useMap="#map1";
    mapimg.src=camXml.map;
    
    var map=document.createElement("map");
    map.name="map1";
    map.id=map.name;
    
    var othercams=xmlDoc.getElementsByTagName("cam");
    var node=null;
    for(var i=0; i<othercams.length; i++)
    {
      node=othercams[i];
      var mapid=getChildValue(node, "mapid");
      if(mapid!=null&&mapid==camXml.mapid) {
        var shape=getChildValue(node, "shape");
        var coords=getChildValue(node, "coords");
        
        if(shape!=null&&coords!=null) {
      	  var area=document.createElement("area");
      	  area.shape=shape;
      	  area.coords=coords;
      	  area.href="myHighwaycams.html?cam="+node.getElementsByTagName("id")[0].firstChild.data;
      	  map.appendChild(area);
      	}
      }
      
      /*
      var mapNodes=node.getElementsByTagName("mapid");
      var mapNode=null;
      if(mapNodes!=null) mapNode=mapNodes[0];
      if(mapNode.firstChild.data==camXml.mapid)
      {
        var shape=node.getElementsByTagName("shape")[0].firstChild;
        var coords=node.getElementsByTagName("coords")[0].firstChild;
        if(shape!=null&&coords!=null) {
      	  var area=document.createElement("area");
      	  area.shape=shape.data;
      	  area.coords=coords.data;
      	  area.href="myHighwaycams.html?cam="+node.getElementsByTagName("id")[0].firstChild.data;
      	  map.appendChild(area);
      	}
      }
      */
    }
    document.getElementById("insetMap").appendChild(map);
    document.getElementById("insetMap").appendChild(mapimg);
    //mapUrl=mapUrl.replace("\\", "/");
    //mapReq=createXMLRequest(mapUrl, showMap);
    //mapReq.send(null);
    
    //weather
    
    var weathertb=document.getElementById("weather");
          
    if(camXml.weather!=null||camXml.forecast!=null)
    {
      var wRow=document.createElement("TR");
      var wCell=document.createElement("TD");
      wCell.colSpan="4";
      wRow.appendChild(wCell);
      weathertb.appendChild(wRow);
      
      var wddiv=document.createElement("DIV");
      wddiv.id="weatherData";
      wddiv.style.display="none";
      wCell.appendChild(wddiv);
            
      if(camXml.weather!=null) {
    	if(camXml.forecast!=null) {
    	  //both - gotta make it so it can switch between them
    	  var cwd=document.createElement("DIV");
    	  cwd.id="currentWD";
    	  
    	  wddiv.appendChild(cwd);
    
    	  //loadWD(camXml.weather);
    
    	  //env canada
    	  var hwyName="this highway route";
    	  if(camXml.highway!=null) hwyName="highway "+camXml.highway;
    
    	  var fcd=document.createElement("DIV");
    	  fcd.style.display="none";
    	  fcd.id="forecast";
    	  var fh1=document.createElement("h1");
    	  fh1.style.fontSize="11pt";
    	  fh1.style.marginBottom="5px";
    	  fh1.appendChild(document.createTextNode("Weather Forecast "));
    	  var ecSpan=document.createElement("span");
    	  ecSpan.style.fontWeight="normal";
    	  ecSpan.appendChild(document.createTextNode("(from Environment Canada)"));
    	  fh1.appendChild(ecSpan);
    	  fcd.appendChild(fh1);
    	  
    	  wddiv.appendChild(fcd);
    
    	  var fcs=document.createElement("DIV");
    	  fcs.id="forecastScroll";
    	  fcd.appendChild(fcs);
    
    	  //loadFC(camXml.forecast);
    
    	  var wlnk=document.createElement("A");
    	  wlnk.style.display="block";
    	  wlnk.id="_sw_lnk";
    	  wlnk.appendChild(document.createTextNode(camXml.stationName+" weather station"));
    	  wlnk.href="#";
    	  wlnk.onclick=function()
    	  {
    	    show_weather();
    	    return false;
    	  }
    	  
    	  wddiv.appendChild(wlnk);
    	  var flnk=document.createElement("A");  
    	  flnk.style.display="block";
    	  flnk.id="_sf_lnk";
    	  flnk.appendChild(document.createTextNode("Forecast for "+camXml.highway));
    	  flnk.href="#";
    	  flnk.onclick=function()
    	  {
    	    show_forecast();
    	    return false;
    	  }
    	  wddiv.appendChild(flnk);
    	} else {
    	  // just weather station - find file
    	  var cwd=document.createElement("DIV");
    	  cwd.id="currentWD";
    	  
    	  wddiv.appendChild(cwd);
    	}
      } else {
    	if(camXml.forecast!=null) {
    	  var fcd=document.createElement("DIV");
    	  fcd.id="forecast";
    	  var fh1=document.createElement("h1");
    	  fh1.style.fontSize="11pt";
    	  fh1.style.marginBottom="5px";
    	  fh1.appendChild(document.createTextNode("Weather Forecast "));
    	  var ecSpan=document.createElement("span");
    	  ecSpan.style.fontWeight="normal";
    	  ecSpan.appendChild(document.createTextNode("(from Environment Canada)"));
    	  fh1.appendChild(ecSpan);
    	  fcd.appendChild(fh1);
    
    	  var fcs=document.createElement("DIV");
    	  fcs.id="forecastScroll";
    	  fcd.appendChild(fcs);
    	  
    	  wddiv.appendChild(fcd);
    	} else {
    	  //neither (nothing)
    	}
      }
    
      var warnlnk=document.createElement("A");
      warnlnk.target="_blank";
      warnlnk.href="http://www.weatheroffice.ec.gc.ca/warnings/bc_e.html";
      warnlnk.appendChild(document.createTextNode("British Columbia Weather Warnings"));
      warnlnk.style.display="block";
      
      wddiv.appendChild(warnlnk);
   
      var weatherMapUrl="highwayCamMaps.aspx";
      if(camXml.reg==1) {
        weatherMapUrl="/weather/northern_region.asp";
      } else if(camXml.reg==2) {
    	weatherMapUrl="/weather/south_coast_region.asp";
      } else if(camXml.reg==3) {
    	weatherMapUrl="/weather/interior_region.asp";
      } else if(camXml.reg==4) {
    	weatherMapUrl="/weather/lowermainland.asp";
      } else if(camXml.reg==5) {
    	weatherMapUrl="/weather/south_coast_region.asp";
      }
      var wmaplnk=document.createElement("A");
      wmaplnk.target="_blank";
      wmaplnk.href=weatherMapUrl;
      wmaplnk.appendChild(document.createTextNode("All weather stations (current weather) for this region (map)"));
      wmaplnk.style.display="block";
      
      wddiv.appendChild(wmaplnk);
      
      if(camXml.weather!=null) loadWD(camXml.weather);
      if(camXml.forecast!=null) loadFC(camXml.forecast);
    }
  }
}

function showMap()
{
  if(mapReq!=null&&mapReq.readyState==4)
  {
    if(mapReq.status==200)
    {
      var fetched_xml=String(mapReq.responseText);
      fetched_xml=fetched_xml.replace(/webcam.aspx/gi, "MyHighwaycams.html");
      fetched_xml=fetched_xml.replace(/mapImages/gi, "../mapPages/mapImages");
      
      document.getElementById("insetMap").innerHTML=fetched_xml;
    }
  }
}

function CamXmlEntry(id, image, thumbnail, caption, description, map, mapid, shape, coords, horiz, weather, envCanada, highway, reg, stationName, disabled, disappear, message, longmessage)
{
  this.id=id;
  this.image=image;
  this.thumbnail=thumbnail;
  this.caption=caption;
  this.description=description;
  this.map=map;
  this.mapid=mapid;
  this.shape=shape;
  this.coords=coords;
  this.horiz=horiz;
  this.weather=weather;
  this.envCanada=envCanada;
  this.highway=highway;
  this.reg=reg;
  this.stationName=stationName;
  this.disabled=disabled;
  this.disappear=disappear;
  this.message=message;
  this.longmessage=longmessage;
}
function CamXml_ISOK()
{
  if(this.id==null||this.image==null||this.caption==null||this.description==null||this.map==null) return false;
  return true;
}
CamXmlEntry.prototype.isOK=CamXml_ISOK;

function loadThumb(node)
{
  var thumbCell=document.createElement("TD");
  thumbCell.width="140";
  
  if(thumbsAdded%4==0) thumbRow=document.createElement("TR");
  
  thumbRow.appendChild(thumbCell);
  
  var camXml=new CamXmlEntry();
  
  for(var i=0; i<node.childNodes.length; i++)
  {
    if(node.childNodes[i].nodeName=="id") camXml.id=node.childNodes[i].firstChild.data;
    if(node.childNodes[i].nodeName=="image") camXml.image=node.childNodes[i].firstChild.data;
    if(node.childNodes[i].nodeName=="thumbnail") camXml.thumbnail=node.childNodes[i].firstChild.data;
    if(node.childNodes[i].nodeName=="caption") camXml.caption=node.childNodes[i].firstChild.data;
    if(node.childNodes[i].nodeName=="description") camXml.description=node.childNodes[i].firstChild.data;
    if(node.childNodes[i].nodeName=="map") camXml.map=node.childNodes[i].firstChild.data;
  }
  
  if(camXml.isOK)
  {
    var lnk=document.createElement("A");
    lnk.href="myhighwaycams.html?cam="+camXml.id;
    
    if(camXml.disabled) {
      var div=document.createElement("div");
      div.className="disabled tiny";
      div.appendChild(document.createTextNode(camXml.message));
      
      lnk.appendChild(div);
    } else {
      var src=camXml.thumbnail;
      //src=src.substring(0, src.lastIndexOf("."))+"_tn"+src.substring(src.lastIndexOf("."));
      
      var img=document.createElement("img");
      img.src=src;
      img.alt=img.src;
      
      lnk.appendChild(img);
    }
    var cap=document.createElement("div");
    cap.className="caption tiny";
    cap.appendChild(document.createTextNode(camXml.caption));
    
    lnk.appendChild(cap);
    
    thumbCell.appendChild(lnk);
  }
  
  if(thumbsAdded%4==0) document.getElementById("thumbs").appendChild(thumbRow);
  
  thumbsAdded++;
}
