//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2005 America Online, Inc.  All rights reserved.
// This software contains valuable confidential and proprietary information
// of America Online, Inc. and is subject to applicable licensing agreements.
// Unauthorized reproduction, transmission or distribution of this file and
// its contents is a violation of applicable laws.
//
//           A M E R I C A   O N L I N E   C O N F I D E N T I A L
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// $Revision: 1.8 $ $Author: mcasias $ $Date: 2006/04/26 18:22:03 $6/17/2005
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function sp_Menu () {
    var searchNode;
    var brandingNode;
    var menuNode = new Array();
    this.getSearchNode = function() { return searchNode; }
    this.getBrandingNode = function (){ return brandingNode; }
    this.getMenuNode = function() { return menuNode; }
    this.createSearchNode = function(theTitle, theAction, theInvocationType){searchNode = new sp_searchNode(theTitle, theAction, theInvocationType);}
    this.createBrandingNode=function(img,url,altText){brandingNode = new sp_brandingNode(img, url, altText);}
    this.insertHeader = function(head){menuNode.push(head);}
}

function sp_searchNode(t, a, hiddenParams) {
    var m_title = t;
    var m_action = a;
    var m_hiddenParams = hiddenParams;
    var m_links = new Array();
    this.getTitle = function(){return m_title;}
    this.getAction= function(){return m_action;}
    this.getHiddenParams= function(){return m_hiddenParams;}
}

function searchLink(linkTitle, linkURL) {
    var m_title = linkTitle;
    var m_url = linkURL;
    this.getValue = function(val){
        if (val == "title") return m_title;
        else return m_url;
    }
}

function sp_brandingNode(imgSrc, h, altText) {
    var m_image = imgSrc;
    var m_href = h;
    var m_altText = altText;
    this.getHref = function(){return m_href;}
    this.getImg = function(){return m_image;}
    this.getAltText = function(){return m_altText;}
}

function sp_headerItem(t, h, rating) {
    var m_title = t;
    var m_url = h;
    var m_rating = rating;
    var m_menuItemArray = new Array();
    this.getTitle = function(){return m_title;}
    this.getUrl = function(){return m_url;}
    this.getRating = function(){return m_rating;}
    this.getMenuItems = function(){return m_menuItemArray;}
    this.addMenuItem = function(newtitle, newurl, newrate) {
        menu = new sp_menuItem(newtitle, newurl, newrate);
        m_menuItemArray.push(menu);
    }
}

function sp_menuItem(t, h, rating) {
    var m_title = t;
    var m_url = h;
    var m_rating = rating;
    this.getTitle = function(){return m_title;}
    this.getUrl = function(){return m_url;}
    this.getRating = function(){return m_rating;}
}

// Public Variables
var targetNode, _sp, menu, theDoc, sns_variable,dumper, menuList, hoverTimer,thePositionLeft, thePositionLeft;
var searchForms = new Array();
var activeDrop = null;
var currentDrop = null;
var activeTitle = null;
//sns_variable = 2; /// DEBUG FOR SN

function loadMenu(spObject, targetID, footer, landing) {
    _sp = spObject;
    menu = footer;
    targetNode = mc_buildObj('div', targetID, {'className':(menu)?'bb_header':'bb_footer', 'id':targetID+'_h'})
    if (!targetNode) return;
    theDoc = spObject; // Sets theDoc variable to the XML Document object. 
    if (targetID == 'sw_menu_top'){
       setBranding();
    }
    else{
       setBrandingFoot();
       setSearch(); 
    }
    displayMenu();
    if (landing) setHomeByURL(landing);
}

function setBranding() {
    theBranding = theDoc.getBrandingNode();
    brandingDiv = mc_buildObj('div', targetNode, {'className':' branding head_gradient'});
    theA = mc_buildObj('a', brandingDiv, {'href':theBranding.getHref()});
    theImg = mc_buildObj('img', theA, {'src':'/help/img/logo2.gif','border':0, 'alt':theBranding.getAltText()});
}

function setBrandingFoot() {
    theBranding = theDoc.getBrandingNode();
    brandingDiv = mc_buildObj('div', targetNode, {'className':' branding'});
    theA = mc_buildObj('a', brandingDiv, {'href':theBranding.getHref()});
    theImg = mc_buildObj('img', theA, {'src':'/help/img/logo_foot.gif','border':0, 'alt':theBranding.getAltText()});
}

function setSearch() {
    theSearch = theDoc.getSearchNode();
    searchDiv = mc_buildObj('span', brandingDiv, {'className':'search'})
    if (menu) {
        srch_left = mc_buildObj('div', searchDiv, {'className':'search_left'});
        srch_right = mc_buildObj('div', searchDiv, {'className':'search_right'});
    }
    titleDiv = mc_buildObj('p', searchDiv, {'className':'search_title'});  // title
    tl = mc_buildObj('p', titleDiv, {'for':'search_text','innerHTML':theSearch.getTitle()});
    formArea = mc_buildObj('form', searchDiv, {'name':targetNode.id+'_search','id':targetNode.id+'_search','action':theSearch.getAction(),'onsubmit':"return validateSearch('"+targetNode.id+"_search')",'method':'post'}); // form
    searchForms.push(formArea);
    formTextIn = mc_buildObj('input',formArea, {'type':'text','name':'searchString','class':'textIn','maxLength':100,'alt':'Enter Search Term'});
    formHidIn = mc_buildObj('input',formArea, {'type':'hidden','name':'ViewableNotSearchable','value':'UMVT_SEARCHABLE_1_1'});    
    ht = theSearch.getHiddenParams();
    for (var i in ht ) formHiddenText = mc_buildObj('input', formArea,{'name':i,'type':'hidden','value':ht[i]});  // hidden field for search
    formSubmitBtn = mc_buildObj('input', formArea, {'type':'button','class':'search_submit','value':'Search','onclick':"validateSearch('"+targetNode.id+"_search')"});  // submit
    linkP = mc_buildObj('div', searchDiv, {'className':'searchLink'});
/*
    linkP1 = mc_buildObj('a', linkP, {'href':searchLink1.getValue("url"),'innerHTML':searchLink1.getValue("title")});
    if (searchLink2 && sns_variable != 2) {
      linkPS = document.createTextNode(" | ");
      linkP.appendChild(linkPS);
      linpP2 = mc_buildObj('a', linkP, {'href':searchLink2.getValue("url"),'innerHTML':searchLink2.getValue("title")});
    }
*/
}

function validateSearch(tID) { 
  s =$(tID).searchString.value;
  if (s.replace(/ /g, '') == '') { alert((!_sp.searchError)? "Please enter a search string":_sp.searchError);return false;}
  else $(tID).submit();
}

function displayMenu(){
    dumper = mc_buildObj('div', targetNode); // Create DIV where we will place the hidden menus.
    menuList = mc_buildObj('div', targetNode, {'className':'menuList'});
    theMenu = theDoc.getMenuNode();                                     // Get menu node from XML
    for (i = 0 ; i < theMenu.length; i++) {                             // loop through array of header nodes
        var myMenuHead = theMenu[i];
        var mh;
        switch (myMenuHead.getRating()) {                               // test SNS value
            case 2 : if (sns_variable == 2) mh = insertHeader(myMenuHead, menuList); break;
            case 1 : if (sns_variable == 1) mh= insertHeader(myMenuHead, menuList); break;
            default : mh = insertHeader(myMenuHead, menuList); break;
        }
    }
}

function insertHeader(headObject, targetObject) {
  var header = mc_buildObj('div', targetObject, {'h': headObject.getTitle(),'onmouseover':do_activateTitle,'onmouseout':hoverTimerOut, 'onclick':hitURL});
  aId = targetNode.id + "_" + i;
  ha = mc_buildObj ('a', header, {'href': headObject.getUrl(), 'id':aId, 'alt':headObject.getTitle(), 'innerHTML':headObject.getTitle()});
  if (menu) {                                   // If the show menu is on * from loadMenu() parameters
    var mINodes = theMenu[i].getMenuItems();    // Get array of menuItem SubNodes of the header Node
    var mILength = mINodes.length;              // get length of returned array
    if (mILength > 0) {                         // if there are any menuItem nodes
      header.sub = 1;                           // Set sub property of header *used in activateTitle()
      ha.className = "withDrop ";         // set div class to display down arrow
      var menuDiv = mc_buildObj('div', dumper, {'className':'menu_drop','onmouseover':hoverTimerWhoa,'onmouseout':hoverTimerOut,'id':aId + '_menu'});
      for (k = 0 ; k < mILength; k++ ) {        // for each
        var myNode = mINodes[k];                // Get the particular node
        var mp;
        rating_var = myNode.getRating();
        switch (myNode.getRating()) {
          case 2 : if (sns_variable == 2) mp = insertMenu(myNode, menuDiv); break;
          case 1 : if (sns_variable == 1) mp= insertMenu(myNode, menuDiv); break;
          default : mp = insertMenu(myNode, menuDiv); break;
        }
      }
    }
  } else if (i == 0 ) header.className = "first"; // First header should not have a left border
}

function insertMenu(theNode, targetObj) {
    var mp = mc_buildObj('p', targetObj, {'onmouseover':highlightDrop,'onclick':hitURL});
    var mi = mc_buildObj('a', mp, {'href':theNode.getUrl(),'innerHTML':theNode.getTitle()});
    return mp;
}
// end build functions

function hitURL(){window.location = this.getElementsByTagName('a')[0].href; }

function do_editClass(d) {
  editClass = d.className;
  editClass = editClass.replace(/active/g, "");
  editClass = editClass.replace(/ $/, "");
  d.className = editClass;                      // make it inactive
  d = null;
}

function highlightDrop(){
  dropDiv = this;
  if (activeDrop) do_editClass(activeDrop);
  activeDrop = dropDiv;
  activeDrop.className += "active";
}

function do_activateTitle(){activateTitle(this.getElementsByTagName('a')[0].id);}
function activateTitle(togID){
  hoverTimerWhoa();
  theDiv = $(togID).parentNode;                 // Gets the id of the moused area
  var sub = (theDiv.sub == 1) ? true : false;   // check if sub menu is available for div
  if (activeTitle) do_editClass(activeTitle);
  theDiv.className +=' active';                 // make this div active.
  activeTitle=theDiv;                           // make the active div equal to this one
  if (currentDrop) hideDrop(theDiv);            // else if there is a shown menu.. hide it
  if (sub)showDrop(togID);                      // if there is a submenu.. show it
}

function hoverTimerOut(){hoverTimer = setTimeout("hideDrop()",100);}
function hoverTimerWhoa(){if (hoverTimer) clearTimeout(hoverTimer);}
function hideDrop(divMe){
  hoverTimerWhoa();

    var searchSelectBox = document.getElementById( 'temp_product' );
    if ( searchSelectBox ) {
        searchSelectBox.className = 
            searchSelectBox.className.replace( / ?invisible/g, "" );
    }

  if (currentDrop){currentDrop.style.visibility = "hidden";currentDrop = null; }
  if (activeDrop)do_editClass(activeDrop);
  if (activeTitle && activeTitle != divMe) do_editClass(activeTitle);
}

function getPosition(anElement) {
  thePositionLeft = 0;thePositionTop = 0;
  var tempElement = anElement;
  while (tempElement.tagName.toLowerCase() != "body") {
    thePositionLeft += tempElement.offsetLeft;
    thePositionTop += tempElement.offsetTop;
    tempElement = tempElement.offsetParent;
  }
}

function showDrop(divID){

    var searchSelectBox = document.getElementById( 'temp_product' );
    var menuNumber = divID.replace( /^.*(\d+)$/, "$1" );

    if ( searchSelectBox && menuNumber > 4 && menuNumber < 8 ) {
        searchSelectBox.className = "invisible";
    }

    hoverTimerWhoa();
    dropID = divID + "_menu";
    theDrop = $(dropID);
    if (!theDrop) return;
    if (currentDrop && currentDrop != theDrop) currentDrop.style.visibility = "hidden";
    myImg = $(divID).parentNode;
    getPosition(myImg);
    theDrop.style.left = thePositionLeft + "px";
    theDrop.style.top = thePositionTop + 24 + "px";
    theDrop.style.visibility = "visible";
    currentDrop = theDrop;
}

function sp_searchUpdate(t) {for (var i in searchForms) searchForms[i].searchString.value = t; document.body.focus(); }

function mc_buildObj(targetType, targetObj, propArray, targetDoc) { // 
    if (!targetType) return false;
    if (targetType.toLowerCase() == 'input') {
      if (!targetObj || !propArray) return false;  // if there's no target or properties, don't build it.. 
      s = '<'+targetType+ ' ';
      for (var i in propArray) s += i+'="'+propArray[i]+'" ';
      s += '/>';
      targetObj.innerHTML += s;
      return false;
    }
    if (!targetDoc) targetDoc = document;
    theObj = targetDoc.createElement(targetType);
    if (propArray){
        for (var i in propArray) {
          if (i == 'style') {l = propArray[i];for (var j in l) theObj.style[j] =l[j];}
          else theObj[i] = propArray[i];
        }
    }
    if (targetObj) {
      if (typeof(targetObj) == "string")targetObj = $(targetObj);
      targetObj.appendChild(theObj);
    }
    return theObj;
}

function $(d) {return document.getElementById(d);} // prototype returns DOM object by ID



function setHomeByURL(l) {
    var page = window.location.toString();
    var found = 0;
    var menus = menuList.getElementsByTagName( "div" );

    // special cases for SP 4:  check for home page
    if ( page.indexOf( 'supportcentral' ) > -1 ) {
        menus[ 0 ].className += " home ";
        found = 1;
    }
    // special cases for SP 4:  check for browse.do.  That indicates a help topics page.
    //
    // Also check for externalID - that indicates an article page.  As a note, it should be
    // in the Query String
    //
    // Also check for bbid - that indicates a Best Bet article

    else if ( page.indexOf( "browse.do" ) > -1 || page.indexOf( 'externalId' ) > -1 ||
              page.indexOf( 'bbid' ) > -1 ) {
        menus[ 1 ].className += " home ";
        found = 1;

        //Set IDs in case this is really a Connecting to AOL page
        menus[ 1 ].id = 'helpTopicsMenu';
        menus[ 3 ].id = 'connectingToAolMenu';
    }

    // since we're doing a regex match, escape metacharacters
    page = page.replace( /([\/\.\?\\\]\[\)\(\^\$\*\{\}\<\>\+\|])/g, "\\$1" );

    // check for an ending slash on the page URL
    if ( page.match( "^.*/$" ) ) {
        page += "?";
    }
    else {
        page += "/?";
    }
    page = "^" + page + "$";

    //alert( window.location.toString() );
    //alert( page );

    for ( var i = 0; i < menus.length; ++i ) {
      if ( found == 0 ) {
          //alert( menus[ i ].getElementsByTagName( 'a' )[ 0 ].toString() + "\n" + page + "\n" +  menus[ i ].getElementsByTagName( 'a' )[ 0 ].toString().match( page ));
          if ( menus[ i ].getElementsByTagName( 'a' )[ 0 ].toString().match( page ) ) {
                menus[ i ].className += " home ";
                found = 1;
            }
        popUpMenu = document.getElementById( menus[ i ].getElementsByTagName( 'a' )[ 0 ].id + '_menu' );

        if ( popUpMenu != null ) {
            popUpMenuLinks = popUpMenu.getElementsByTagName( 'a' );
            for ( var j = 0; j < popUpMenuLinks.length; ++j ) {
               //alert( popUpMenuLinks[ j ].toString() + "\n" + page + "\n" +  popUpMenuLinks[ j ].toString().match( page ));
                if ( popUpMenuLinks[ j ].toString().match( page ) ) {
                    found = 1;
                    menus[ i ].className += " home ";
                }
            }
        }// if popup Menu
      }//if found == 0
    }//for i in menus

    if (found == 0) {
        setHome(l); //no url match, using landing value match
    }
}

function setHome(l) {
  divArray = menuList.getElementsByTagName('div');
  for (i=0; i < divArray.length; i++) {
    if (divArray[i].h.toUpperCase() == l.toUpperCase()) {divArray[i].className += 'home ';return; } // space added for addition of 'active' class.
  }
}
