/**
 * wco/menus.js: a class representing a collection Popup Menu objects
 *
 * This module provides the properties and methods needed to create, position,
 * and manipulate sets of popup menus on a web page.
**/
// Check for Level 1 Core DOM conformance
if (document.implementation) {
  if (document.implementation.hasFeature) {
    if (!document.implementation.hasFeature('html', '1.0')) {
      alert('INCOMPATIBLE BROWSER\n\nYour browser does not support all the features required by this website.\nThe page will continue to load but please be aware that some of this site\'s functions\nmay not work, or they may produce unexpected results.\nP\nlease upgrade your browser to the latest version.');
    }
  }
  else {
    alert('INCOMPATIBLE BROWSER\n\nYour browser does not support all the features required by this website.\nThe page will continue to load but please be aware that some of this site\'s functions\nmay not work, or they may produce unexpected results.\n\nPlease upgrade your browser to the latest version.');
  }
}
else {
  alert('INCOMPATIBLE BROWSER\n\nYour browser does not support all the features required by this website.\nThe page will continue to load but please be aware that some of this site\'s functions\nmay not work, or they may produce unexpected results.\n\nPlease upgrade your browser to the latest version.');
}

/**
 * Initialise the 'Wco' NAMESPACE:
**/
var wco;
if (!wco) wco = {};
// and check that the Layers module has been loaded
if (!wco.Layers)
  alert('LOAD PAGE FAILURE\n\nwco/Layers.js has not been loaded - the page will not load correctly and will produce errors! Please report this error to the website management.');

/**
 * Create the 'Menus' MODULE in the Wco namespace:
**/
wco.Menus = {};

/**
 * Define the module's CONSTRUCTOR FUNCTION:
**/
wco.Menus = function (data) {
  // Define the layer collection
  this.menuCollection = new Array();
  
  // and initialise instance properties
  if (data) {
    this.originY = data.top || 0;
    this.originX = data.left || 0;
    this.menuWidth = data.menuWidth || 150;
  }
  else {
    this.originY = 0;
    this.originX = 0;
    this.menuWidth = 150;
  }
}

/**
 * Define any INSTANCE PROPERTIES:
**/


/**
 * Define any INSTANCE METHODS:
**/
// This instance method returns a comma-separated string showing the ID of each layer object in the collection.
wco.Menus.prototype.toString = function() {
  var list = 'MENUS[';
  for (var i=0; i<this.menuCollection.length; i++) {
    if (i > 0) {
      list += ', ';
    }
    list += this.menuCollection[i].id + '::' + this.menuCollection[i].name;
  }
  if (list == 'MENUS(') {list += '(empty)';}
  list += ']';
  
  return list;
}

wco.Menus.prototype.activate = function() {
  for (var i=0; i<this.menuCollection.length; i++) {
    this.menuCollection[i].style.display = 'block';
  }
}

// This instance method takes a data array containing the initial properties of a layer,
// creates a new Layer object and adds it to the Layers collection.
wco.Menus.prototype.add = function(data) {
  // Initialise and add the new menu to the collection
  
  // make sure the module user has passed a data object
  // - if not, then instantiate an empty object and use defaults throughout
  //   the remainder of this method
  if (data) {
    if (data.menu) {
      if (typeof data.menu == 'object') {
        // read the menu object
        var menu = data.menu;
        
        // create and initialise the menu container for the new menu
        var temp = document.createDocumentFragment();
        var menuContainer = document.createElement('div');
        menuContainer.id = 'wco_menu_' + this.menuCollection.length;
        menuContainer.name = menu.name || 'Menu ' + this.menuCollection.length;
        menuContainer.className = 'wcoMenu';
        menuContainer.onmouseover = function() {this.className += ' over'}
        menuContainer.onmouseout = function() {this.className = 'wcoMenu';
        if (wco.Menus.currentSubmenu) if (wco.Menus.currentSubmenu.className.indexOf('over') != -1) wco.Menus.currentSubmenu.className = '';
        }
        //menuContainer.onmouseover = wco.Menus.onmouseoverMenu;
        menuContainer.onclick = function() {this.className = 'wcoMenu'}
        
        // create and initialise the master table that will hold the complete menu
        var table = document.createElement('table');
        table.border = 0;
        table.cellpadding = 0;
        table.cellspacing = 0;
        //table.style.border = '1px solid #000000';
        var tbody = document.createElement('tbody');
        
        // create and initialise the top-level menu item
        var row = document.createElement('tr');
        var col = document.createElement('td');
        col.onclick = wco.Menus.onmouseclickMenu;
        col.name = menu.name;
        col.wcoObjectId = menu.objectId;
        col.wcoParentId = menu.parentId;
        col.wcoPaginationClassName = menu.paginationClassName;
        col.wcoPageNo = menu.pageNo;
        col.appendChild(document.createTextNode(menu.name || 'Menu ' + this.menuCollection.length));
        row.appendChild(col);
        tbody.appendChild(row);
        
        if (data.subMenus) {
          if (typeof data.subMenus == 'object'){
            // create and initialise the child table that will hold the submenu items
            row = document.createElement('tr');
            col = document.createElement('td');
            var submenuContainer = document.createElement('div');
            submenuContainer.className = 'wcoSubmenu';
            var table1 = document.createElement('table');
            table1.border = 0;
            table1.cellpadding = 0;
            table1.cellspacing = 0;
            var tbody1 = document.createElement('tbody');
            
            //read the array of submenu objects
            for (var m = 0; m < data.subMenus.length; m++) {
              // retrieve the data for this submenu
              var sm = data.subMenus[m]; //alert(sm.name);
              
              // create and initialise each of the submenu items
              var row1 = document.createElement('tr');
              var col1 = document.createElement('td');
              col1.onmouseover = wco.Menus.onmouseoverSubmenu;
              col1.onclick = wco.Menus.onmouseclickMenu;
              col1.name = sm.name;
              col1.wcoObjectId = sm.objectId;
              col1.wcoParentId = sm.parentId;
              col1.wcoPaginationClassName = sm.paginationClassName;
              col1.wcoPageNo = sm.pageNo;
              col1.appendChild(document.createTextNode(sm.name));
              row1.appendChild(col1);
              tbody1.appendChild(row1);
            }
            
            table1.appendChild(tbody1);
            submenuContainer.appendChild(table1);
            col.appendChild(submenuContainer);
            row.appendChild(col);
            tbody.appendChild(row);
          }
        }
        
        // add the menu to the table
        table.appendChild(tbody);
        
        // the table is complete - insert the table into the menu container
        // and add the new menu to both the HTML page and the Menus collection
        menuContainer.appendChild(table);
        var newMenu = temp.appendChild(menuContainer);
        document.body.appendChild(temp);
        this.menuCollection[this.menuCollection.length] = newMenu;
        
        // Also add the new menu to the Layers collection so we have control over positioning and display
        pageLayers.add ({id:newMenu.id, position:'absolute', width:this.menuWidth, height:'auto', top:this.originY, left:(this.originX + (this.menuWidth * (this.menuCollection.length - 1))), zIndex:1000, display:'block', align:'center', isCenteredX:true});
      }
    }
  }
}

/**
 * Define any CLASS (SHARED) METHODS:
**/
wco.Menus.onmouseoverSubmenu = function(e) {
  e = (e) ? e : ((window.event) ? event : null);
  if (e) {
    var oSource;
    if (e.srcElement) oSource = e.srcElement;
    if (e.target) oSource = e.target;
    
    if (wco.Menus.currentSubmenu) if (wco.Menus.currentSubmenu.className.indexOf('over') != -1) wco.Menus.currentSubmenu.className = '';
    
    oSource.className = 'over';
    wco.Menus.currentSubmenu = oSource;
  }
}

wco.Menus.onmouseclickMenu = function(e) {
  e = (e) ? e : ((window.event) ? event : null);
  if (e) {
    var oSource;
    if (e.srcElement) {oSource = e.srcElement;}
    if (e.target) {oSource = e.target;}
    
    var sDestination;
    if (arguments[1]) {
      var iParentId = arguments[2] || 0;
      var iPageNo = arguments[3] || 1;
      var sPaginationClassName = arguments[4] || 'Product';
      sDestination = 'ffs4.aspx?oid=' + arguments[1] + '&pid=' + iParentId + '&pno=' + iPageNo + '&cnm=' + sPaginationClassName;
    }
    else {
      sDestination = 'ffs4.aspx?oid=' + oSource.wcoObjectId + '&pid=' + oSource.wcoParentId + '&pno=' + oSource.wcoPageNo + '&cnm=' + oSource.wcoPaginationClassName;
    }
    //alert(sDestination);
    
    switch (oSource.tagName) {
      case 'A':
        oSource.href = sDestination;
        break;
      case 'IMG':
        if (oSource.parentElement) {
          if (oSource.parentElement.tagName == 'A') {
            oSource.parentElement.href = sDestination;
          }
          else {
            window.location.href = sDestination;
          }
        }
        else {
          window.location.href = sDestination;
        }
        break;
      default:
        window.location.href = sDestination;
        break;
    }
  }
}

wco.Menus.showStore = function(e, iObjectId, iParentId) {
  e = (e) ? e : ((window.event) ? event : null);
  if (e) {
    var oSource;
    if (e.srcElement) {oSource = e.srcElement;}
    if (e.target) {oSource = e.target;}
    
    var oPopupWindow;
    var sDestination = 'ffs4.aspx';
    if (iObjectId) sDestination += '?oid=' + iObjectId + '&pid=' + iParentId + '&pop=true';
    if (e.altKey) sDestination += '&xml=true';
    
    if (window.screenLeft) {
      oPopupWindow = window.open(sDestination, null, "height=520,width=625,left=" + (window.screenLeft + 150) + ",top=" + (window.screenTop + 25) + ",location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
    }
    else {
      oPopupWindow = window.open(sDestination, null, "height=520,width=625,screenX=" + (window.pageXOffset + 150) + ",screenY=" + (window.pageYOffset + 25) + ",location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no");
    }
    oPopupWindow.focus();
  }
}

/**
 * Define any CLASS (SHARED) PROPERTIES:
**/
// This class property allows the developer to retrieve the Name of this module.
wco.Menus.DESCRIPTION = 'Menus';
wco.Menus.TYPE = 'CLASS';
// These class properties allow the developer to retrieve the Version details of this module.
wco.Menus.VERSION = '1.0.0';
wco.Menus.MAJORVERSION = 1;
wco.Menus.MINORVERSION = 0;
wco.Menus.REVISIONNO = 0;
wco.Menus.COMPANY = 'Wco iEnterprise Solutions';
wco.Menus.COPYRIGHT = '© Copyright 2007 Wco iEnterprise Solutions';
wco.Menus.COMPANYURL = 'www.wco.com.au';
wco.Menus.COMPANYEMAIL = 'support@wco.com.au';
wco.Menus.DEVELOPERSIGNATURE = 'GAW';

wco.Menus.currentMenu;
wco.Menus.currentSubmenu;

/**
* Instantiate and initialise the PAGEMENUS OBJECT for the current page
var pageMenus = new wco.Menus();
**/


