/*
 *  Navigation scripts for the Canadian Wind Energy Atlas
 *  (c) Copyright 2003 Environment Canada
 *
 *  by inVisu inc., 2004
 *
 */

var curField = defaultField;
var curHeight = defaultHeight;
var curSeason = defaultSeason;
var isReady = false;            // Internally set to 'true' when page is done loading.

// Browser-independant, returns the DHTML <SPAN> element's style for the specified ID.
function getSpanStyle(id) {
    if (document.layers)
        return document.layers[id];
    if (document.all) {
        var s = document.all[id];
        if (s && s.style)
            return s.style;
    }
    if (document.getElementById) {
        var s = document.getElementById(id);
        if (s && s.style)
            return s.style;
    }
    return null;
}

// Called when the page is done loading, we then set up dynamic stuff.
function pageLoaded() {
    isReady = true;
    changeMap();
}

function pageResized() {
}

function changeMap() {
   // Update legend...
   var bigmap;
   if ( curField == 'provinces' )
      bigmap = "../images/bigmap_" + curField + "_" + lang + ".png";
   else if ( hasHeight[curField] )
      bigmap = "../images/bigmap_" + curField + "_" + curSeason + "_" + curHeight + ".png";
   else
      bigmap = "../images/bigmap_" + curField + ".png";
   document.images['bigmap'].src = bigmap;

   // Update area map
   var area;
  for (i = 0; rows[i]; i++) {
    for (j = 0; rows[i][j]; j++) {
      area = document.getElementById("area" + rows[i][j]);
//      if(!area)	document.write("javascript error in area handling rows[i][j]=" + rows[i][j]);
	if(!area)
	  continue;

      if ( curField != 'provinces' )
      {
         if ( hasHeight[curField] )
            area.href = navURL + "?no=" + rows[i][j] + "&field=" + curField + "&height=" + curHeight + "&season=" + curSeason;
         else
            area.href = navURL + "?no=" + rows[i][j] + "&field=" + curField + "&height=" + curHeight;
      } else {
	area.href = navURL + "?no=" + rows[i][j] + "&field=" + 'E1' + "&height=" + defaultHeight + defaultSeason;
      }
    }
  }

  setRadios();
}

function setRadios() {
  var i = 0, h;
  var r = null;        // For the height radio button.
  var rs = null;       // For the season radio button.

  // Get radio button object for height
  if (document.forms && document.forms[1] && document.forms[1].elements && document.forms[1].elements.height)
    r = document.forms[1].elements.height;
  if (!r && document.getElementById) {
    r = document.getElementById("dummy");

    if (r && r.height)
      r = r.height;
    else
      r = null;
  }

  if (document.forms && document.forms[1] && document.forms[1].elements && document.forms[1].elements.season)
    rs = document.forms[1].elements.season;
  if (!rs && document.getElementById) {
    rs = document.getElementById("dummy");

    if (rs && rs.season)
      rs = rs.season;
    else
      rs = null;
  }

    var color;
    var fontStyle;
    var isdisabled;

    if ( hasHeight[curField] )
    {
      color = "#000";
      fontStyle = "normal";
      isdisabled=false;
    } else {
       color = "#999";
       fontStyle = "italic";
       isdisabled=true;
    }

    i=0;

    for ( h in heightDir )
    {
       s = getSpanStyle('span' + h);
       if (s) {
          s['color'] = color;
          s['fontStyle'] = fontStyle;
       }
       if (r)
          r[i].disabled = isdisabled;
       
       i++;
    }

    i = 0;
    for ( h in seasonDir )
    {
       s = getSpanStyle('span' + h);
       if (s) {
          s['color'] = color;
          s['fontStyle'] = fontStyle;
       }
       if (rs)
          rs[i].disabled = isdisabled;

      i++;
    }
}

// Callback function when the current field is modified by the user.
function setField(f) {
  if (!isReady)
    return;

  if (f != curField) {
    curField = f;
    setLoc();
  }
}


// Callback function when the current height is modified by the user.
function setHeight(h) {
  if (!isReady)
    return;

  if (h != curHeight) {
    curHeight = h;
    setLoc();
  }
}

// Callback function when the current season is modified by the user.
function setSeason(s) {
  if (!isReady)
    return;

  if (s != curSeason) {
    curSeason = s;
    setLoc();
  }
}

// Returns an URL for the current image/overlay/options and stuff.
function getURL(base) {
   var url;
   if ( hasHeight[curField] )
      url = base + "?field=" + curField + "&height=" + curHeight + "&season=" + curSeason;
   else
      url = base + "?field=" + curField;
   return url;
}


// Callback function for update button. Updates the location in the location bar.
// Updating the "window.location" calls "pageLoaded()" which, in turn, calls "changeMap()"
function setLoc() {
    window.location = getURL(baseURL);
}
