﻿///////////////////////////////////////////////////////////////////////////////
//
//  csSiteMenuManager.js
//
// © 2007-2009 Wco iEnterprise Solutions Pty Ltd. ALL RIGHTS RESERVED.
// This file is licensed as part of the DataPortal 2.0 Managed Web Presence Solution, for details look here: http://www.wco.com.au
//
///////////////////////////////////////////////////////////////////////////////

//--------------------------------------------------------------------------------
HtmlSiteMenuObject = function(parentObject, index, table, dataNode) {
  // declare and initialise the module's members
  this.parent = parentObject;
  this.index = index;
  this.name = 'siteMenu_' + this.index;
  
  //this.id = dataNode.attributes[0].nodeValue;
  this.id = dataNode.attributes.getNamedItem('id').nodeValue;
  this.parentId = dataNode.childNodes[0].innerHTML;
  this.concurrencyId = dataNode.childNodes[1].innerHTML;
  this.sortOrder = dataNode.childNodes[2].innerHTML;
  this.classId = dataNode.childNodes[3].innerHTML;
  this.className = dataNode.childNodes[4].innerHTML;
  this.isLeaf = dataNode.childNodes[5].innerHTML;
  this.title = dataNode.childNodes[6].innerHTML;
  // add a new menu item row to the table
  var row = table.insertRow(-1);
  var cell = row.insertCell(-1);
  if (dataportal.isFrutigerInstalled) {
    cell.className = 'SiteMenuF';
  }
  else {
    cell.className = 'SiteMenu';
  }
  cell.id = this.name;
  cell.innerHTML = (this.index + 1) + '. ' + this.title.toUpperCase();
  this.activeCell = cell;
  // add a new separator row to the table
  var row = table.insertRow(-1);
  var cell = row.insertCell(-1);
  cell.className = 'SiteMenuSeparator';
  cell.innerHTML = '------------------------------------------------------------';
}

HtmlSiteMenuObject.prototype = {
  select: function() {
    this.activeCell.style.fontWeight = 'bold';
  },
  
  unselect: function() {
    this.activeCell.style.fontWeight = 'normal';
  }
}

//--------------------------------------------------------------------------------
HtmlSiteMenuCollection = function(parentObject) {
  this.parent = parentObject;
  
  // declare and instantiate the module's UI object properties
  var html = '<div id="siteMenuPanel" style="position:absolute; top:1px; left:0px; width:240px; height:119px; z-index:99; cursor:default; display:block;">';
    html += '<div style="position:absolute; top:0px; left:0px; width:100%; height:100%; display:none;">';
      html += '<table id="siteMenuTable" cellpadding="0" cellspacing="0" style="width:100%; height:100%;">';
        html += '<tr>';
          html += '<td class="SiteMenuSeparator">';
            html += '------------------------------------------------------------';
          html += '</td>';
        html += '</tr>';
      html += '</table>';
    html += '</div>';
    
    html += '<div style="position:absolute; top:48px; left:66px; width:108px; height:22px; background-image:url(\'ssl/images/bkg_activity_dlg.png\'); background-repeat:no-repeat; display:block;">';
      html += '<img src="ssl/images/activity_sm.gif" style="position:absolute; top:3px; left:5px; width:16px; height:16px; border:none 0px;" />';
      html += '<div class="ActivityTitle" style="position:absolute; top:3px; left:28px;">';
      html += 'Loading...';
      html += '</div>';
    html += '</div>';
    
  html += '</div>';
  var newContent = document.createElement('DIV');
  newContent.id = 'newContent';
  newContent.innerHTML = html;
  newContent.style.display = 'none';
  this.rootElement = newContent.childNodes[0];
  this.parent.contentPane.appendChild(this.rootElement);
  this.menuElement = this.rootElement.childNodes[0];
  this.activityElement = this.rootElement.childNodes[1];
  this.table = positioning.getObject('siteMenuTable');
  
  // declare and initialise the module's object & collection properties
  this.item = new Array();
  this.currentObject = null;
  this.previousObject = null;
  this.selectedObject = null;
  
}

HtmlSiteMenuCollection.prototype = {
  /*<remarks>
  Method:      add
  Description: Instantiates a new menu item object and adds it to the site menu collection, from where it is accessible by the page manager.
  Parameters:
               xhtmlDataNode  [required] The DOM node containing the menu item data, in the form:
                                          <div id="[Object Id]">
                                            <div>[Object Parent Id]</div>
                                            <div>[Object Concurrency Id]</div>
                                            <div>[Object Sort Order]</div>
                                            <div>[Class Id]</div>
                                            <div>[Class Name]</div>
                                            <div>[Class IsLeaf]</div>
                                            <div>[Object Name]</div>
                                          </div>
  </remarks>*/
  add: function(xhtmlDataNode) {
    // get the index of the new menu item object
    var index = this.item.length;
    
    // create the new menu item object
    this.item[index] = new HtmlSiteMenuObject(this.parent, index, this.table, xhtmlDataNode);
    
    // and hook up the mouse events for the new menu item object
    var menuItem = this.item[index].activeCell;
    menuItem.onmouseover = positioning.createDelegate(this, this.onMouseOver);
    menuItem.onmouseout = positioning.createDelegate(this, this.onMouseOut);
    menuItem.onclick = positioning.createDelegate(this, this.onMouseUp);
  },
  
  /*<remarks>
  Method:      redoLayout
  Description: Reformats the ui elements whenever the browser window is resized.
  Parameters:
               [none]
  </remarks>*/
  redoLayout: function() {
    var w = windowGeometry.getViewportWidth();
    
    positioning.setObjectLeft(this.rootElement, (w/2)-478);
  },
  
  show: function() {
    positioning.hideObject(this.activityElement);
    positioning.showObject(this.menuElement);
  },
  
  hide: function() {
    positioning.hideObject(this.menuElement);
    positioning.showObject(this.activityElement);
  },
  
  isInitialised: function() {
    if (this.currentObject == null) {
      return (false);
    }
    else {
      return (true);
    }
  },
  
  setCurrentObject: function(index) {
    // ensure any previously selected menu item is unhighlighted
    if (this.currentObject != null) {
      this.currentObject.unselect();
    }
    // set the current object to the menu item just selected
    this.currentObject = this.item[index];
    // and highlight the menu item
    this.currentObject.select();
    // and reset the Location Navigator list
    var data = {objectId: this.currentObject.id,
                objectParentId: this.currentObject.parentId,
                objectConcurrencyId: this.currentObject.concurrencyId,
                objectSortOrder: this.currentObject.index,
                classId: this.currentObject.classId,
                className: this.currentObject.className,
                classIsLeaf: this.currentObject.isLeaf,
                objectName: this.currentObject.title.toUpperCase()
               }
    this.parent.locationNavigator.reset(data);
  },

  onMouseOver: function(e) {
    var element;

    if (!e) var e = window.event;
    if (e.target) element = e.target;
    else if (e.srcElement) element = e.srcElement;
    if (element.nodeType == 3) element = element.parentNode;
    
    if (this.currentObject == null) {
      element.style.fontWeight = 'bold';
    }
    else {
      if (element.id != this.currentObject.name) {
        element.style.fontWeight = 'bold';
      }
    }
  },

  onMouseOut: function(e) {
    var element;

    if (!e) var e = window.event;
    if (e.target) element = e.target;
    else if (e.srcElement) element = e.srcElement;
    if (element.nodeType == 3) element = element.parentNode;

    if (this.currentObject == null) {
      element.style.fontWeight = 'normal';
    }
    else {
      if (element.id != this.currentObject.name) {
        element.style.fontWeight = 'normal';
      }
    }
  },

  onMouseUp: function(e) {
    var element;

    if (!e) var e = window.event;
    if (e.target) element = e.target;
    else if (e.srcElement) element = e.srcElement;
    if (element.nodeType == 3) element = element.parentNode;
    
    var doAction = false;
    if (this.currentObject == null) {
      doAction = true;
    }
    else {
      if (element.id != this.currentObject.name) {
        doAction = true;
      }
    }
    
    if (doAction) {
      var idx = element.id.substring((element.id.lastIndexOf('_') + 1), element.id.length);
      this.select(idx);
//      this.setCurrentObject(idx);
//      
//      //          fetchObject(<Object Id>, <Class Name>, <Class IsLeaf>, <Object Sort Order>, <Object Parent Id>)
//      this.parent.fetchObject(this.currentObject.id, this.currentObject.className, this.currentObject.isLeaf, this.currentObject.index, 87699);
//      var data = {objectId: this.currentObject.id,
//                  objectParentId: this.currentObject.parentId,
//                  objectConcurrencyId: this.currentObject.concurrencyId,
//                  objectSortOrder: this.currentObject.index,
//                  classId: this.currentObject.classId,
//                  classIsLeaf: this.currentObject.isLeaf,
//                  objectName: this.currentObject.title.toUpperCase()
//                 }
//      this.parent.locationNavigator.reset(data);
    }
  },
  
  select: function(idx) {
      this.setCurrentObject(idx);
      
      //          fetchObject(<Object Id>, <Class Name>, <Class IsLeaf>, <Object Sort Order>, <Object Parent Id>)
      this.parent.fetchObject(this.currentObject.id, this.currentObject.className, this.currentObject.isLeaf, this.currentObject.index, 87699);
//      var data = {objectId: this.currentObject.id,
//                  objectParentId: this.currentObject.parentId,
//                  objectConcurrencyId: this.currentObject.concurrencyId,
//                  objectSortOrder: this.currentObject.index,
//                  classId: this.currentObject.classId,
//                  className: this.currentObject.className,
//                  classIsLeaf: this.currentObject.isLeaf,
//                  objectName: this.currentObject.title.toUpperCase()
//                 }
//      this.parent.locationNavigator.reset(data);
  
  }
  
}
