//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// 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.2 $ $Author: sitemike $ $Date: 2010/05/03 20:30:39 $6/17/2005
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var home='file:///Users/ezeqcohen/cm/sandbox/test/';
var home_url=home+"aolhelp/";

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(imgHeader,imgFooter,url,altText){brandingNode = new sp_brandingNode(imgHeader,imgFooter, url, altText);}
    this.insertHeader = function(head){menuNode.push(head);}
    
    var contactNode;
    this.insertContactNode=function(contactN){contactNode=contactN;};
    this.getContactNode=function(){return contactNode;};
}

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(imgHeaderSrc, imgFooterSrc, h, altText) {
    var m_imageH = imgHeaderSrc;
    var m_imageF = imgFooterSrc;
    var m_href = h;
    var m_altText = altText;
    this.getHref = function(){return m_href;}
    this.getImgHeader = function(){return m_imageH;}
    this.getImgFooter = function(){return m_imageF;}
    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 '});
    theA = mc_buildObj('a', brandingDiv, {'href':theBranding.getHref()});
    theImg = mc_buildObj('img', theA, {'src':theBranding.getImgHeader(),'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':theBranding.getImgFooter(),'border':0, 'alt':theBranding.getAltText()});
}

function setSearch() {
   
}

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;
        }
        
    }
    insertHeaderContact(menuList);
}

function insertHeader(headObject, targetObject) {
  var header = mc_buildObj('div', targetObject, {'h': headObject.getTitle(),'onmouseover':do_activateTitle,'onmouseout':hoverTimerOut});
  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 insertHeaderContact(targetObject) {
	  aId = 'contact_aol';
	  var contactO = theDoc.getContactNode();
	  var header = mc_buildObj('div', targetObject, {'h': contactO.getTitle(),'id':aId,'onmouseover':'','onmouseout':'', 'onclick':''});
	  rha = mc_buildObj ('a', header, {'href': contactO.getUrl(), 'alt':contactO.getTitle(), 'innerHTML':contactO.getTitle()});
	}

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
  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
}

function hoverTimerOut(){hoverTimer = setTimeout("hideDrop()",100);}
function hoverTimerWhoa(){if (hoverTimer) clearTimeout(hoverTimer);}
function hideDrop(divMe){
  hoverTimerWhoa();
  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){
    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 case for SP 4:  check for home page
    if ( page == 'file:///Users/ezeqcohen/cm/sandbox/test/aolhelp/' ) {
        menus[ 0 ].className += " home ";
        found = 1;
    }

    for ( var i = 0; i < menus.length; ++i ) {
      if ( found == 0 ) {
          if ( menus[ i ].getElementsByTagName( 'a' )[ 0 ].toString() == 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 ) {
                if ( popUpMenuLinks[ j ].toString() == page ) {
                    found = 1;
                    menus[ i ].className += " home ";
                }
            }
        }// if popup Menu
      }//if found == 0
    }//for i in menus
    // default to "Main"
    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.
  }
}

