// File: feed_chooser.js

//var feed = new Array;
//feed[0] = "http://www.gisuser.com/index2.php?option=com_rss&no_html=1"; // GISUser
//feed[1] = "http://portal.opengeospatial.org/rss/newsfeed.rss"; // OGC
//feed[2] = "";

var mainMenu = new NavMenu(null, null, null, null, null);
var newsMenu = new NavMenu(null, null, null, null, null);
var prodMenu = new NavMenu(null, null, null, null, null);
var newsFeed;
var newsItemMenu = new NavMenu(null, null, null, null, null);

function init () {
   if (piIsIE) {
      // Set width of <a> elements to 100% in IE, but not Firefox.
      // In IE, this makes the entire width of the element mouse sensitive, instead of
      // just the text area.  In Firefox, setting width to 100% makes elements with
      // margins and padding extend beyond their containing <div>, so we don't.
      set_width_prop("navmenuitem", "100%");
      set_width_prop("newsmenuitem", "100%");
      //set_width_prop_by_id("page", "902px");
      set_margin_top_prop("maincontentscroll", "2");
   }

   mainMenu = new NavMenu("navmenu", "a", "navmenuitem", "nmihighlight", "nmiactive"); // control the "a" elements that are children of "navmenu"

   newsMenu = new NavMenu("newsmenu", "a", "newsmenuitem", "newsmihighlight", "newsmiactive"); // control the "a" elements that are children of "newsmenu"
   if (newsMenu) {
      newsMenu.setClickFunc(load_feed); 
      newsMenu.click(newsMenu.getItem(0)); // activate first menu item
   }

   prodMenu = new NavMenu("producttabbar", "a", "producttab", "pthighlight", "ptactive");
   if (prodMenu && prodMenu.numItems() > 0) {
      prodMenu.setTargetFrame("maincontentframe");
      // Get tab number passed as URL arg, i.e., http://.../page.html?tab=2
      var argArr = parseArgs();
      var tabNum = argArr["tab"];
      if (tabNum == null) tabNum = 0;
      else if (tabNum >= prodMenu.numItems()) tabNum = prodMenu.numItems() - 1;
      var myClickFunc = function (item) {
         if (item && item.href) { prodMenu.loadIframe(item.href); return false; }
         return true;
      };
      prodMenu.setClickFunc(myClickFunc);
      prodMenu.click(prodMenu.getItem(tabNum)); // activate first menu item
      //load_iframe("maincontentframe", /*"nversePhoto.html"*/prodMenu.getItem(tabNum).href);
      //prodMenu.loadIframe(prodMenu.getItem(tabNum).href);
   }
}

function load_feed (item) {
   // Get url
   try {
      //get_xml_file(item.href, "pagemiddleright");
      newsFeed = new NewsFeed(item.href, "pagemiddleright");
   } catch (e) {
      //alert("Browser won't allow access to RSS feed");
   }
   return false;
}

function load_iframe (id, url)
{
   try {
      var ifrm = document.getElementById(id);
      if (ifrm) {
         ifrm.src = url;
      }
   } catch (e) {
      //alert("Couldn't find target iframe: " + id);
   }
}

/*
function load_rss_feeds () {
   //load_rss_feed(feed[0], 'feed0');
   //load_rss_feed(feed[1], 'feed1');
   //load_rss_feed(feed[2], 'feed2');
   
   set_choices('pagemiddleleft', 'pagemiddleright');
   activate_choice(0);
}

var feed_names = new Array();
var feed_urls  = new Array();

feed_names[0] = "Precision Lightworks"; feed_urls[0] = "http://localhost/rss/local_articles.rss.xml";
feed_names[1] = "GISUser.com";          feed_urls[1] = "http://www.gisuser.com/index2.php?option=com_rss&amp;no_html=1";
feed_names[2] = "GISCafe";              feed_urls[2] = "http://www10.giscafe.com/rss/news.xml?section=CorpNews";
feed_names[3] = "OGC";                  feed_urls[3] = "http://portal.opengeospatial.org/rss/newsfeed.rss";
feed_names[4] = "Web3D Consortium";     feed_urls[4] = "http://www.web3d.org/index.xml";
feed_names[5] = "GEOPlace.com";         feed_urls[5] = "http://www.geoplace.com/RSS/GeoFeed.xml";
feed_names[6] = "ModSim.org";           feed_urls[6] = "http://www.modsim.org/backend.php";

function set_choices (seldivname, contentdivname) {
   var content = document.getElementById(seldivname);
   if (content != null) {
      var html = '<div class="sectiontitle"><span class="sectiontitle">Industry News</span></div>';
      for (var i = 0; i < feed_names.length; i++) {
         html += '<div class="newsfeedselector">'
              +  '<span class="newsfeedselector"';
         html += ' onmouseover="highlight_choice(this);"';
         html += ' onmouseout="unhighlight_choice(this);"';
//         html += ' onclick="set_choices(\''
//              +  seldivname + '\', \'' + contentdivname + '\', ' + i + '); get_xml_file(\''
//              +  feed_urls[i] + '\', \'' + contentdivname + '\');">';
         html += ' onclick="activate_choice(this); get_xml_file(\''
              +  feed_urls[i] + '\', \'' + contentdivname + '\');">';
         html += feed_names[i];
         html += '</span></div>';
      }
      content.innerHTML = html;
   }
}

function activate_choice (elem) {
   if (elem.id != "active") {
      var activeElem = document.getElementById("active");
      if (activeElem != null) {
         activeElem.id = "";
      }
      elem.id = "active";
      //elem.onclick();
   }
}

function highlight_choice (elem) {
   if (elem.id != "active") {
      elem.id = "highlight";
   }
}

function unhighlight_choice (elem) {
   if (elem.id != "active") {
      elem.id = "";
   }
}

function highlight_nav (elem) {
   var activeLinksElem = elem.getElementsByTagName("a");
   if (activeLinksElem != null && activeLinksElem.length > 0) {
      var href = activeLinksElem[0].href;
      if (href != null) {
         window.status = href;
      }
   }
}

function unhighlight_nav (elem) {
   window.status = "";
}
*/
// Find all items with class className, and set their width style property to w
function set_width_prop (className, w) {
   var items = getElementsByClass(className);
   if (items) {
      var n = items.length;
      for (var i = 0; i < n; i++) {
         items[i].style.width = w;
      }
   }
}

// Find all items with class className, and set their width style property to w
function set_width_prop_by_id (idName, w) {
   var item = document.getElementById(idName);
   if (item && item != null) {
      item.style.width = w;
   }
}

/*// Find all items with class className, and set their width style property to w
function inc_width_prop_by_id (idName, dw) {
   var item = document.getElementById(idName);
   if (item && item != null) {
      item.style.width = item.style.width + dw;
alert("width=" + item.style.width);
   }
}*/

// Find the item by its ID, and set its margin-top style property to m
function set_margin_top_prop (idName, m) {
   var item = document.getElementById(idName);
   if (item != null) {
      item.style.marginTop = m;
   }
}

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Dustin Diaz :: http://www.dustindiaz.com/ */

function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if (node == null)
    node = document;
  if (tag == null)
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if (pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

// Extract the query args from a URL and return them in an
// associative array
function parseArgs () {
   var argString = location.search.substring(1, location.search.length);
   var argArray = new Array();
   var pairs = argString.split(/&/);
   for (var i = 0; i < pairs.length; i++) {
      var keyVal = pairs[i].split('=');
      if (!keyVal || keyVal.length != 2) continue;
      var key = unescape(keyVal[0]);
      var val = unescape(keyVal[1]);
      val = val.replace(/\+/g, ' ');
      argArray[key] = val;
   }
   return argArray;
}
