/*************  Javascript for the Atlantic Magazine menu system. **************

  Menu items and sub-menus are defined in the subMenus[] array below.

  The 2nd level of the array contains the main menu items.
      The 1st element is the "off" button image.
      The 2nd element is the "on" button image.
      The 3rd element is the URL to link to.
      The 4th and subsequent elements are links to sub-menu arrays.  If there 
          is no sub-menu, The element should be a null;

  The 3rd level contains one array for each item in the submenu.
      The 1st element is the text which appears in the sub-menu
      The 2nd element is the URL to link to.

  This script (scripts/atlMenu.js) and the associated CSS file 
  (styles/atlMenu.css) should be included in the HTML file.

  The function displayMainMenu(); must be run when the page is loaded.
      <body onload="displayMainMenu();">

  Finally, the following snippet of HTML should be placed in the HTML 
  file where the menu should appear.

---------------------------------------------------------------------------
  <div id="menu">
    <table height="28px" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="center">
    <table cellpadding="0" cellspacing="0" border="0">
      <tr id="mainmenu1"></tr>
    </table>
    </td></tr></table>
  </div>


  <div id="submenu2">
    <table width="100%" height="15px" border="0">
      <tr>
        <td id="smOuter" align="center">
          <table cellpadding="0" cellspacing="0" border="0">
            <tr id="submenu"></tr>
          </table>
        </td>
      </tr>
    </table>
  </div>
---------------------------------------------------------------------------
***************************************************************************/

var subMenus = new Array (
                   new Array ('http://www.theatlantic.com/menus/mm_magazine_off.png', 'http://www.theatlantic.com/menus/mm_magazine_on.png', 'http://www.theatlantic.com/magazine',
                       new Array('Current Issue', 'http://www.theatlantic.com/doc/current'),
                       new Array('Back Issues', 'http://www.theatlantic.com/issues/backissues.htm'),
                       new Array('Fiction Issue', 'http://www.theatlantic.com/doc/200808'),
                       new Array('Newsletters', 'http://www.theatlantic.com/a/newsletters.mhtml'),
		       new Array('Subscribe', 'https://w1.buysub.com/servlet/OrdersGateway?cds_mag_code=AMY&amp;cds_page_id=32073'),
		       new Array('Renew', 'https://w1.buysub.com/pubs/AT/AMY/AMY_EmailEntryForm.jsp?cds_page_id=2917&amp;cds_mag_code=AMY&amp;id=1182791388282&amp;lsid=71761209482010172&amp;vid=1')),
                   new Array ('http://www.theatlantic.com/menus/mm_archive_off.png', 'http://www.theatlantic.com/menus/mm_archive_on.png', 'http://www.theatlantic.com/issues/backissues.htm',
                       new Array('Back Issues', 'http://www.theatlantic.com/issues/backissues.htm'),
                       new Array('Flashbacks', 'http://www.theatlantic.com/doc/index/flashbacks'),
                       new Array('Premium Archive', 'http://pqasb.pqarchiver.com/theatlantic/advancedsearch.html')),
                   new Array ('http://www.theatlantic.com/menus/mm_voices_off.png', 'http://www.theatlantic.com/menus/mm_voices_on.png', '#',
                       new Array('Sullivan', 'http://andrewsullivan.theatlantic.com/the_daily_dish/'),
                       new Array('Douthat', 'http://rossdouthat.theatlantic.com/'),
		       new Array('McArdle', 'http://meganmcardle.theatlantic.com/'),
                       new Array('Ambinder', 'http://marcambinder.theatlantic.com/'),
                       new Array('Coates', 'http://ta-nehisicoates.theatlantic.com/'),
                       new Array('Fallows', 'http://jamesfallows.theatlantic.com/'),
                       new Array('Goldberg', 'http://jeffreygoldberg.theatlantic.com/'),
		       new Array('Crook', 'http://clivecrook.theatlantic.com/')),
                   new Array ('http://www.theatlantic.com/menus/mm_current_off.png', 'http://www.theatlantic.com/menus/mm_current_on.png', 'http://thecurrent.theatlantic.com/'), 
                   new Array ('http://www.theatlantic.com/menus/mm_elections08_off.png', 'http://www.theatlantic.com/menus/mm_elections08_on.png', 'http://www.theatlantic.com/election-2008.mhtml',null),
                   new Array ('http://www.theatlantic.com/menus/mm_multimedia_off.png', 'http://www.theatlantic.com/menus/mm_multimedia_on.png', 'http://podcasts.theatlantic.com/', 
						new Array('Slideshows', 'http://podcasts.theatlantic.com/slideshow/'),
						new Array('Videos', 'http://podcasts.theatlantic.com/video/'),
						new Array('Audio', 'http://podcasts.theatlantic.com/audio/')),
                   new Array ('http://www.theatlantic.com/menus/mm_subscribe_off.png', 'http://www.theatlantic.com/menus/mm_subscribe_on.png', 'https://w1.buysub.com/servlet/OrdersGateway?cds_mag_code=AMY&amp;cds_page_id=32073', null)
               );


var lastSelMenuItem = -1;
var lastSubMenuItem = -1;


function mainMenuSel(mIdx) {
	if (navigator.userAgent.indexOf("Safari") != -1) {
		document.getElementById('submenu2').style.visibility = 'hidden';
	}

    if(lastSelMenuItem > -1) {
        mid = 'mmImg' + lastSelMenuItem;
        document.getElementById(mid).src = subMenus[lastSelMenuItem][0];
        document.getElementById('smOuter').innerHTML = 
          '<table cellpadding="0" cellspacing="0" border="0"><tr id="submenu"></tr></table>';
    }

    mid = 'mmImg' + mIdx;
    document.getElementById(mid).src = subMenus[mIdx][1];

    if(subMenus[mIdx][3] != null) {
        showSubMenu(mIdx);
    }

    lastSelMenuItem = mIdx;

	if (navigator.userAgent.indexOf("Safari") != -1) {
		setTimeout("document.getElementById('submenu2').style.visibility = 'visible';", 1);
	}
}


function clearMainMenuItem(mIdx) {
    //  The first line below leaves the selected menu item displayed when you move the cursor to the submenu.
    //  The second line clears the selected menu item when the cursor is moved to the submenu.
    //  Uncomment the behaviour you'd like and comment out the other!
    if(mIdx > -1 && subMenus[mIdx][3] == null) {
    // if(mIdx > -1) {
        mid = 'mmImg' + mIdx;
        document.getElementById(mid).src = subMenus[mIdx][0];
        // lastSelMenuItem = -1;
    }
}



function subMenuSel(sIdx) {
    if(lastSubMenuItem > -1) {
        sid = 'smptrl' + lastSubMenuItem; document.getElementById(sid).style.display = 'none';
        sid = 'smptrr' + lastSubMenuItem; document.getElementById(sid).style.display = 'none';
        sid = 'smitem' + lastSubMenuItem; document.getElementById(sid).style.color = '#909090';
    }

    sid = 'smptrl' + sIdx; document.getElementById(sid).style.display = 'block';
    sid = 'smptrr' + sIdx; document.getElementById(sid).style.display = 'block';
    sid = 'smitem' + sIdx; document.getElementById(sid).style.color = '#303060';

    lastSubMenuItem = sIdx;
}


function showSubMenu(mIdx) {
    var oStr = '';

    oStr += '<table cellpadding="0" cellspacing="0" border="0">';
    oStr += '<tr id="submenu">';

    for(var i = 3; i < subMenus[mIdx].length; i++) {
        oStr += '<td width="10px"><img id="smptrl' + i + '" class="smptr" align="right" src="http://www.theatlantic.com/menus/tnb-sub-left-ptr.png"></td>\n';
        oStr += '<td id="smtd' + i + '" class="smitem"><a id="smitem' + i + '" href="' + 
                 subMenus[mIdx][i][1] + '" onmouseover="subMenuSel(' + i + ');">' +
                 subMenus[mIdx][i][0] + '</a></td>\n';
        oStr += '<td width="10px"><img id="smptrr' + i + '" class="smptr" align="left" src="http://www.theatlantic.com/menus/tnb-sub-right-ptr.png"></td>\n';
    }
    oStr += "</tr></table>";

	if (navigator.userAgent.indexOf("Safari") != -1) {
		document.getElementById('smOuter').style.visibility = 'hidden';
	}
    document.getElementById('smOuter').innerHTML = oStr;
	
	if (navigator.userAgent.indexOf("Safari") != -1) {
		setTimeout("document.getElementById('smOuter').style.visibility = 'visible';", 1);
	}

}



function displayMainMenu() {
    var preLoad = new Array();
    mmRow = document.getElementById('mainmenu1');
	
	// mmRow.style.visibility = 'hidden';
    addNewMenuCell(mmRow, '<img src="http://www.theatlantic.com/menus/mm_left_corner.png" alt="left corner" />');

    for(var i = 0; i < subMenus.length; i++) {
        addNewMenuCell(mmRow, 
          '<a id="mmItem' + i + '" href="' + subMenus[i][2] + '" onmouseover="mainMenuSel(' + i + ');" ' + 
          'onmouseout="clearMainMenuItem(' + i +  ');"> ' +
	  '<img id="mmImg' + i +  '" src="' + subMenus[i][0] + '" /></a>', 'id=mtd'+i, 'className=mitem');

        preLoad[i] = new Image();
        preLoad[i].src = subMenus[i][1];
    }

    addNewMenuCell(mmRow, '<img src="http://www.theatlantic.com/menus/mm_right_corner.png" alt="right corner" />');


	if (navigator.userAgent.indexOf("Safari") != -1) {
		document.getElementById('menu').style.visibility = 'hidden';
		setTimeout("document.getElementById('menu').style.visibility = 'visible';", 10);
	}

	// mmRow.style.visibility = 'visible';
}



function addNewMenuCell(eRow, eText) {
    newCell = document.createElement('td');

    for(var i = 2; i < arguments.length; i++) {
        props = arguments[i].split('=');
        newCell[props[0]] = props[1];
    }

    eRow.appendChild(newCell);
    newCell.innerHTML = eText;
}

