var current_nav;


function buildLeftNav_MenuOptions(nav_options, level, html) {
   html += '<ul>';
   for (var option in nav_options) {
      html += '<li>';

      // class is a JavaScript reserved word, hence _class
      var _class = '';
      if (option == current_nav[level] && level == current_nav.length-1) {
         _class += 'current ';
      }

      // consume href, url, title, tooltip, class
      html += '<a ' +
         'href="' + (nav_options[option].url || nav_options[option].href || '') + '" ' +
         'title="' + (nav_options[option].tooltip || nav_options[option].title || '') + '" ' +
         'class="' + _class + ' ' + (nav_options[option]['class'] || '') + '" ';

      // convert arbitrary arrNAV attributes into HTML attributes
      for (var attrib in nav_options[option]) {
         // exclude the following because they've been consumed or are leftnav specific
         if (['href', 'url', 'title', 'tooltip', 'class', 'options', 'prevnext'].join().search(attrib) > -1) { continue; }
         html += attrib + '="' + nav_options[option][attrib] + '" ';
      }

      html += '>' + option + '</a>';

      if (option == current_nav[level]) {
         html = buildLeftNav_MenuOptions(nav_options[option].options, level+1, html);
      }

      html += '</li>';
   }
   html += '</ul>';

   return html;

}  // buildLeftNav_MenuOptions()


function showLeftNavMenu() {
   var section_name = current_nav[0];
   
   // mgic.com doesn't have a mgic_content, so to be backwards compatible, check to see if it exists
   var mgic_content = document.getElementById('mgic_content');
   if (mgic_content) {
      // websites without leftnav won't call showLeftNavMenu; conditional indenting achieved
      mgic_content.className += ' leftnav_indent';
   }

   var section_name_div = document.getElementById('section_name');
   section_name_div.innerHTML = '<a href="' + (arrNAV[section_name].url || arrNAV[section_name].href || '') + '">' + section_name + '</a>';

   var content_div = document.getElementById('leftnav_content');
   content_div.innerHTML = buildLeftNav_MenuOptions(arrNAV[section_name].options, 1, "");

}  // showLeftNavMenu()


function changeSection(section_name) {
   //current_nav = [section_name];

   //showLeftNavMenu();
   
   window.location.href = arrNAV[section_name].url;

}  // changeSection()


// this could be expanded to support arbitrary levels of dropdowns (dropdowns of dropdowns)
function buildTabs() {
   var html = "";
   var count = 0;
   var dropdown_events;

   for (var section_name in arrNAV) {
      dropdown_events = '';
      
      if (!document.getElementById('leftnav_content') && arrNAV[section_name].options) {
         var tab_dropdown = document.createElement('div');
         tab_dropdown.id = section_name.replace(/[^a-zA-Z]/g, '') + "_dropdown";
         tab_dropdown.className = 'tabs_dropdown';
         
         var tab_html = '';
         for (var option_name in arrNAV[section_name].options) {
            var url = (arrNAV[section_name].options[option_name].url || arrNAV[section_name].options[option_name].href || '');
            tab_html += '<a ' +
               'href="' + url + '" ';
            
            for (var attrib in arrNAV[section_name].options[option_name]) {
               if (['href', 'url', 'options', 'prevnext'].join().search(attrib) > -1) { continue; }
               tab_html += attrib + '="' + arrNAV[section_name].options[option_name][attrib] + '" ';
            }
            
            tab_html += '>'+ option_name + '</a>';
         }
         tab_dropdown.innerHTML = tab_html;
         document.getElementById('mgic_container').appendChild(tab_dropdown);

         dropdown_events = 'mDropDownTabs(event, \'' + tab_dropdown.id + '\');';
      }
      
      var url = (arrNAV[section_name].url || arrNAV[section_name].href || '');
      
      var _class =
         (section_name == current_nav[0] ? 'on ' : '') +
         (count == 0 ? 'first ' : '') +
         (url == '' ? 'disabled ' : '');
      
      html += '<div class="' + _class + '">';
      
      if (url != '') {
         html += '<a ' +
            'href="' + url + '" ' +
            'onmouseover="' + dropdown_events + ' ' + (arrNAV[section_name].onmouseover || '') + '" ';
      
         for (var attrib in arrNAV[section_name]) {
            if (['href', 'url', 'onmouseover', 'options', 'prevnext'].join().search(attrib) > -1) { continue; }
            html += attrib + '="' + arrNAV[section_name][attrib] + '" ';
         }
         
         html += '>' + section_name + '</a>';
      } else {
         html += section_name;
      }
      
      html += '</div>';
      
      count++;
   }
   return html;

}  // buildTabs()


function showTabs() {
   var tabs_div = document.getElementById('tabs');

   tabs_div.innerHTML = buildTabs();

}  // showTabs()

// *---------------------------------------------------------------------* //
// These functions are used by mgichome.com and mgiccasa.com to display 
// Previous/Next navigation links at the bottom of their screens.

function flattenOptions(label, menu) {
	var skip = false;
	if (typeof(menu.prevnext) != 'undefined') {
		skip = !(menu.prevnext);
	}

	var results = skip ? [] : [{'key':label, 'url':(menu.url || menu.href || '')}];

	if (menu.options) {
		for (var option in menu.options) {
			results = results.concat(flattenOptions(label + option, menu.options[option]));
		}
	}

	return results;

}  // flattenOptions()


function showPrevNext(lang) {
	if ((lang == null) || (typeof(lang) != 'string'))
		lang = 'eng';
	else if ((lang.match(/^eng/) == null) && (lang.match(/^span/) == null))
		lang = 'eng';

	// build a "flat" version of arrNAV
	var flatNAV = [];
	for (var tab in arrNAV) {
		flatNAV = flatNAV.concat(flattenOptions(tab, arrNAV[tab]));
	}

	// find the current_nav in the "flat" array
	var curr_key = current_nav.join('');
	var curr_index = -1;
	for (var i = 0; i < flatNAV.length; i++) {
		if (flatNAV[i].key == curr_key) {
			curr_index = i;
		}
	}

	// find previous and next pages
	var prev_index = -1;
	var next_index = -1;
			
	if (curr_index > -1) {
		if (curr_index > 0)
			prev_index = curr_index - 1;

		if (curr_index < flatNAV.length-1)
			next_index = curr_index + 1;
	}

	if ((prev_index < 0) && (next_index < 0)) return;

	// display html labels
	var prev_label;
	var next_label;
	var prev_tooltip;
	var next_tooltip;
	if (lang.match(/^span/) != null) {
		prev_label = 'Atrás';
		next_label = 'Adelante';

		prev_tooltip = 'página anterior';
		next_tooltip = 'próxima página';
	}
	else {
		prev_label = 'Previous';
		next_label = 'Next';

		prev_tooltip = 'previous page';
		next_tooltip = 'next page';
	}

   var prevnext = '';
   if (prev_index < 0) {
      prevnext = '<a href="' + flatNAV[next_index].url + '" title="' + next_tooltip + '">' + next_label + '<img src="AKAMAIREWRITE/images/nextarrow.gif" align="absmiddle" border="0" height="20" width="21"></a>';
   }
   else if (next_index < 0) {
      prevnext = '<a href="' + flatNAV[prev_index].url +'" title="' + prev_tooltip + '"><img src="AKAMAIREWRITE/images/backarrow.gif" align="absmiddle" border="0" height="20" width="21">' + prev_label + '</a>';
   }
   else {
      prevnext = '<a href="' + flatNAV[prev_index].url +'" title="' + prev_tooltip + '"><img src="AKAMAIREWRITE/images/backarrow.gif" align="absmiddle" border="0" height="20" width="21">' + prev_label + '</a>' +
                 ' | ' +
                 '<a href="' + flatNAV[next_index].url + '" title="' + next_tooltip + '">' + next_label + '<img src="AKAMAIREWRITE/images/nextarrow.gif" align="absmiddle" border="0" height="20" width="21"></a>';
   }

   var prevnext_div = document.getElementById('prevnext');
   prevnext_div.innerHTML = prevnext;

}  // showPrevNext()
