﻿///////////////////////////////////////////////////////////////////////////////
//
//  csLocationManager.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
//
///////////////////////////////////////////////////////////////////////////////

//--------------------------------------------------------------------------------
HtmlLocationLinkObject = function(parentObject, index, tableRow, jsonDataNode) {
  this.parent = parentObject;
  this.index = index;
  this.name = 'locationMenu_' + this.index;
  
  this.id = jsonDataNode.objectId;
  this.parentId = jsonDataNode.objectParentId;
  this.concurrencyId = jsonDataNode.objectParentId;
  this.sortOrder = jsonDataNode.objectSortOrder;
  this.classId = jsonDataNode.classId;
  this.className = jsonDataNode.className;
  this.isLeaf = jsonDataNode.classIsLeaf;
  this.title = jsonDataNode.objectName;

  // add a new menu item row to the table
  var cell = tableRow.insertCell(-1);
  cell.className = 'LocationMenu';
  cell.id = this.name;
  if (this.index == 0) {
    cell.style.fontWeight = 'bold';
  }
  cell.innerHTML = this.title;
  this.activeCell = cell;
}

HtmlLocationLinkObject.prototype = {
  select: function() {
    this.activeCell.style.textDecoration = 'underline';
  },
  
  unselect: function() {
    this.activeCell.style.textDecoration = 'none';
  }
}

//--------------------------------------------------------------------------------
HtmlLocationNavigatorObject = function(parentObject) {
  this.parent = parentObject;
  
  var html = '<div id="locationPanel" style="position:absolute; top:122px; left:0px; z-index:1; display:none; cursor:default;">';
  html += '<table id="locationMenuTable" align="left" valign="top" cellpadding="0" cellspacing="0">';
  html += '';
  html += '</table>';
  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.table = positioning.getObject('locationMenuTable');
  this.row = this.table.insertRow(-1);
  
  // declare and initialise the module's object & collection properties
  this.item = new Array();
  
  // initialise the menu
  var data = {objectId: -1,
              objectParentId: 0,
              objectConcurrencyId: 0,
              objectSortOrder: 0,
              classId: 0,
              className: '',
              classIsLeaf: false,
              objectName: 'YOU ARE HERE:'
             }
  this.add(data);
  var data = {objectId: 0,
              objectParentId: 0,
              objectConcurrencyId: 0,
              objectSortOrder: 0,
              classId: 0,
              className: '',
              classIsLeaf: false,
              objectName: 'HOME'
             }
  this.add(data);
}

HtmlLocationNavigatorObject.prototype = {
  /*<remarks>
  Method:      add
  Description: Instantiates a new menu item object and adds it to the location menu collection, from where it is accessible by the page manager.
  Parameters:
               jsonDataNode  [required] The JSON data object containing the menu item data, in the form:
                                          dataNode {
                                            objectId: [value],
                                            objectParentId: [value],
                                            objectConcurrencyId: [value],
                                            objectSortOrder [value],
                                            classId: [value],
                                            className: [value],
                                            classIsLeaf: [value],
                                            objectName: [value]
                                          }
  </remarks>*/
  add: function(jsonDataNode) {
    if (jsonDataNode) {
      // get the index of the new menu item object
      var index = this.item.length;
      
      if (index > 1) {
        var data = {objectId: -1,
                    objectParentId: 0,
                    objectConcurrencyId: 0,
                    objectSortOrder: 0,
                    classId: 0,
                    className: '',
                    classIsLeaf: false,
                    objectName: '>'
                   }
        
        this.item[index] = new HtmlLocationLinkObject(this.parent, index, this.row, data);
        index = this.item.length;
      }
      
      //alert(jsonDataNode.objectId + '\n' + jsonDataNode.objectParentId + '\n' + jsonDataNode.objectConcurrencyId + '\n' + jsonDataNode.objectSortOrder + '\n' + jsonDataNode.classId + '\n' + jsonDataNode.className + '\n' + jsonDataNode.classIsLeaf + '\n' + jsonDataNode.objectName);
      
      // create the new menu item object
      this.item[index] = new HtmlLocationLinkObject(this.parent, index, this.row, jsonDataNode);
      
      // 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);
    }
  },
  
  reset: function(jsonDataNode) {
    // remove the old navigation trail items
    while (this.item.length > 2) {
      this.row.deleteCell(this.item.length - 1);
      this.item.length = (this.item.length - 1);
    }
    
    if (jsonDataNode) {
      this.add(jsonDataNode);
    }
  },
  
  redoLayout: function() {
    var w = windowGeometry.getViewportWidth();
    var h = windowGeometry.getViewportHeight();
    
    positioning.setObjectLeft(this.rootElement, (w/2)-228);
  },
  
  show: function() {
    positioning.showObject(this.rootElement);
  },
  
  hide: function() {
    positioning.hideObject(this.rootElement);
  },
  
  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;
    
    var idx = element.id.substring((element.id.lastIndexOf('_') + 1), element.id.length);
    if (this.item[idx].id > -1 && idx < this.item.length-1) {
      element.style.cursor = 'pointer';
      element.style.textDecoration = 'underline';
    }
  },

  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;

//    var idx = element.id.substring((element.id.lastIndexOf('_') + 1), element.id.length);
//    if (this.item[idx].id > -1 && idx < this.item.length-1) {
//      element.style.cursor = 'default';
//      element.style.textDecoration = 'none';
//    }
    element.style.cursor = 'default';
    element.style.textDecoration = 'none';
  },

  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 idx = new Number(element.id.substring((element.id.lastIndexOf('_') + 1), element.id.length));

    if (this.item[idx].id > -1 && idx < this.item.length-1) {
      //          fetchObject(<Object Id>, <Class Name>, <Class IsLeaf>, <Object Sort Order>, <Object Parent Id>)
      this.parent.fetchObject(this.item[idx].id, this.item[idx].className, this.item[idx].isLeaf, this.item[idx].sortOrder, this.item[idx].parentId);
      //alert('this.parent.fetchObject(' + this.item[idx].id + ', ' + this.item[idx].className + ', ' + this.item[idx].isLeaf + ', ' + this.item[idx].sortOrder + ', ' + this.item[idx].parentId + ')');
      
      if (this.item[idx].id > 0) {
        // remove all trail items after the selected item
        while (this.item.length > (idx + 1)) {
          this.row.deleteCell(this.item.length - 1);
          this.item.length = (this.item.length - 1);
        }
      }
      
      element.style.cursor = 'default';
      element.style.textDecoration = 'none';
    }
  }
  
}
