﻿///////////////////////////////////////////////////////////////////////////////
//
//  csPageMenuManager.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
//
///////////////////////////////////////////////////////////////////////////////

//--------------------------------------------------------------------------------
HtmlPageMenuObject = function(parentObject, index, table, dataNode) {
  // declare and initialise the module's members
  this.parent = parentObject;
  this.index = index;
  this.name = 'pageMenu_' + this.index;
  
  //this.id = dataNode.attributes[0].nodeValue;
  this.id = dataNode.attributes.getNamedItem('id').nodeValue;
  this.parentId = this.parent.id;
  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 men item row to the table
  var row = table.insertRow(-1);
  var cell = row.insertCell(-1);
  if (dataportal.isFrutigerInstalled) {
    cell.className = 'PageMenuF';
  }
  else {
    cell.className = 'PageMenu';
  }
  cell.style.backgroundImage = "url('ssl/images/keyline_pg_horizontal.png')";
  cell.style.backgroundPosition = 'left top';
  cell.style.backgroundRepeat = 'repeat-x';
  cell.id = this.name;
  if (this.index == 0) {
    cell.innerHTML = (this.parent.index + 1) + '. ' + this.title.toUpperCase();
  }
  else {
    cell.innerHTML = (this.parent.index + 1) + '.' + this.index + ' ' + this.title.toUpperCase();
  }
  this.activeCell = cell;
}

HtmlPageMenuObject.prototype = {
  select: function() {
    //this.activeCell.style.backgroundColor = '';
    this.activeCell.style.backgroundImage = "url('ssl/images/keyline_pg_horizontal.png')";
    this.activeCell.style.backgroundPosition = 'left top';
    this.activeCell.style.backgroundRepeat = 'repeat-x';
    this.activeCell.style.color = '';
    this.activeCell.style.fontWeight = 'bold';
  },
  
  unselect: function() {
    //this.activeCell.style.backgroundColor = '';
    this.activeCell.style.backgroundImage = "url('ssl/images/keyline_pg_horizontal.png')";
    this.activeCell.style.backgroundPosition = 'left top';
    this.activeCell.style.backgroundRepeat = 'repeat-x';
    this.activeCell.style.color = '';
    this.activeCell.style.fontWeight = 'normal';
  },
  
  setTitle: function(title) {
    this.title = title;
    if (this.index == 0) {
      this.activeCell.innerHTML = (this.parent.index + 1) + '. ' + this.title.toUpperCase();
    }
    else {
      this.activeCell.innerHTML = (this.parent.index + 1) + '.' + this.index + ' ' + this.title.toUpperCase();
    }
  }
}

//--------------------------------------------------------------------------------
HtmlPageMenuCollection = function(parentObject) {
  this.parent = parentObject;
  
  // declare and instantiate the module's UI object properties
  var html = '<table cellpadding="0" cellspacing="0" style="width:100%;">';
    html += '';
  html += '</table>';
  var newContent = document.createElement('DIV');
  newContent.id = 'newContent';
  newContent.innerHTML = html;
  newContent.style.display = 'none';
  this.rootElement = newContent.childNodes[0];
  this.parent.menuElement.appendChild(this.rootElement);
  this.table = this.rootElement;
  
  // declare and initialise the module's object & collection properties
  this.item = new Array();
  this.currentObject = null;
  this.previousObject = null;
  this.selectedObject = null;
}

HtmlPageMenuCollection.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:
               dataNode  [required] The DOM node containing the menu item data, in the form:
                                    <div id="[Object Id]">
                                      <div>[Object Name]</div>
                                    </div>
  </remarks>*/
  add: function(dataNode) {
    // get the index of the new menu item object
    var index = this.item.length;
    
    // create the new menu item object
    this.item[index] = new HtmlPageMenuObject(this.parent, index, this.table, dataNode);
    
    // 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);
  },
  
  addAll: function(collNode) {
    for (var i=0; i<collNode.childNodes.length; i++) {
      var dataNode = collNode.childNodes[i];
      // instantiate the new menu item object
      this.add(dataNode);
    }

    // and insert the bottom dashed-line separator bar
    var row = this.table.insertRow(-1);
    var cell = row.insertCell(-1);
    cell.className = 'PageMenu';
    cell.style.height = '1px';
    cell.style.backgroundImage = "url('ssl/images/keyline_pg_horizontal.png')";
    cell.style.backgroundPosition = 'left top';
    cell.style.backgroundRepeat = 'repeat-x';
  },
  
  /*<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.showObject(this.rootElement);
  },
  
  hide: function() {
    positioning.hideObject(this.rootElement);
  },
  
  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();
  },

  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;
    
    this.parent.leftColumnElement.style.zIndex = 10;
    if (this.currentObject == null) {
      //element.style.backgroundColor = '#ffffff';
      element.style.backgroundImage = "url('ssl/images/bkg_75.png')";
      element.style.backgroundRepeat = 'repeat';
      element.style.color = '#333333';
      element.style.fontWeight = 'bold';
    }
    else {
      if (element.id != this.currentObject.name) {
        //element.style.backgroundColor = '#ffffff';
        element.style.backgroundImage = "url('ssl/images/bkg_75.png')";
        element.style.backgroundRepeat = 'repeat';
        element.style.color = '#333333';
        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;

    //this.parent.displayImageElement.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=100)';
    this.parent.leftColumnElement.style.zIndex = 1;
    //this.table.style.backgroundImage = '';
    if (this.currentObject == null) {
      //element.style.backgroundColor = '';
      element.style.backgroundImage = "url('ssl/images/keyline_pg_horizontal.png')";
      element.style.backgroundPosition = 'left top';
      element.style.backgroundRepeat = 'repeat-x';
      element.style.color = '';
      element.style.fontWeight = 'normal';
    }
    else {
      if (element.id != this.currentObject.name) {
        //element.style.backgroundColor = '';
        element.style.backgroundImage = "url('ssl/images/keyline_pg_horizontal.png')";
        element.style.backgroundPosition = 'left top';
        element.style.backgroundRepeat = 'repeat-x';
        element.style.color = '';
        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.setCurrentObject(idx);
      
      //alert('this.parent.parent.fetchObject(' + this.currentObject.id + ', ' + this.currentObject.className + ', ' + this.currentObject.isLeaf + ', ' + this.currentObject.index + ', ' + this.parent.id + ')');
      this.parent.parent.fetchObject(this.currentObject.id, this.currentObject.className, this.currentObject.isLeaf, this.currentObject.index, this.parent.id);
      //alert('YOU ARE HERE: ' + this.parent.title.toUpperCase() + ' > ' + this.currentObject.title.toUpperCase());
      var data = {objectId: this.parent.id,
                  objectParentId: this.parent.parentId,
                  objectConcurrencyId: this.parent.concurrencyId,
                  objectSortOrder: this.parent.index,
                  classId: this.parent.classId,
                  className: this.parent.className,
                  classIsLeaf: this.parent.isLeaf,
                  objectName: this.parent.title.toUpperCase()
                 }
      this.parent.parent.locationNavigator.reset(data);
      if (this.currentObject.id != this.parent.id) {
        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.parent.locationNavigator.add(data);
      }
    }
  }
  
}
