/**
 * wco/layers.js: a class representing a collection DHTML layer objects
 *
 * This module provides the properties and methods needed to create, position,
 * and manipulate sets of layers on a web page.
**/

/**
 * Initialise the 'Wco' NAMESPACE:
**/
var wco;
if (!wco) wco = {};

/**
 * Create the 'Layers' MODULE in the Wco namespace:
**/
wco.Layers = {};

/**
 * Define the module's CONSTRUCTOR FUNCTION:
**/
wco.Layers = function (data) {
  // Define the layer collection
  this.layerCollection = new Array();
  
  // and initialise instance properties
  if (data) {
    this.isPageLayoutCentered = data.isPageLayoutCentered || false;
    this.pageViewPaneWidthMin = data.pageViewPaneWidthMin || 1014;
  }
  else {
    this.isPageLayoutCentered = false;
    this.pageViewPaneWidthMin = 1014;
  }
  
  if (this.isPageLayoutCentered) {
    window.onresize = this.redoPageLayout;
  }
}

/**
 * Define any INSTANCE PROPERTIES:
**/



/**
 * Define any INSTANCE METHODS:
**/
// This instance method sets one or more named flags passed in a string array.
wco.Layers.prototype.setFlags = function(data) {
  if (data) {
    this.isPageLayoutCentered = data.isPageLayoutCentered || this.isPageLayoutCentered;
  }
  
  if (this.isPageLayoutCentered) {
    window.onresize = this.redoPageLayout;
  }
  else {
    window.onresize = '';
  }
}

// This instance method re-positions all relevant layers whenever the window is resized.
wco.Layers.prototype.redoPageLayout = function() {
  //alert(document.body.clientWidth + ' | ' + document.body.offsetWidth);
  // As Netscape doesn't recognise 'document.body.offsetWidth', we will have to work with the 'clientWidth' property
  var layers = pageLayers.layerCollection;
  var centreX = document.body.clientWidth / 2;

  for (var i=0; i<layers.length; i++) {
    //alert('id = ' + this.layerCollection[i].id);
    if (layers[i].isCenteredX) {
      var thisLayer = layers[i];
      //alert('id = ' + thisLayer.id);
      var thisLayerXOffset = (pageLayers.pageViewPaneWidthMin / 2) - thisLayer.centredXOffset;
      //alert('thisLayer.centredXOffset = ' + thisLayer.centredXOffset);
      //alert('thisLayerXOffset = ' + thisLayerXOffset);
      //alert('thisLayer.style.left = ' + (centreX - thisLayerXOffset) + 'px');
      thisLayer.style.left = (centreX - thisLayerXOffset) + 'px';
    }
  }
}

// This instance method takes the ID of a layer in the collection
// and returns its x coordinate as an integer.
wco.Layers.prototype.getLayerPixelLeft = function(id) {
  var result = 0;
  var layer;
  
  for (var i=0; i<this.layerCollection.length; i++) {
    if (this.layerCollection[i].id == id) {
      layer = this.layerCollection[i];
      if (layer.style.pixelLeft) {
        result = layer.style.pixelLeft;
      }
      else if (layer.currentStyle) {
        result = layer.currentStyle.left;
      }
      else if (layer.style) {
        result = layer.style.left;
      }
      else {
        result = layer.left;
      }
    }
  }
  
  if (isNaN(result)) {
    if (result.indexOf('%') > -1) {
      result = result.substring(0, result.length-1);
    }
    else if (result.indexOf('px') > -1) {
      result = result.substring(0, result.length-2);
    }
    else {
      result = 0;
    }
  }
  
  return result;
}

// This instance method takes the ID of a layer in the collection
// and returns its x coordinate as an integer.
wco.Layers.prototype.getLayerPixelTop = function(id) {
  var result = 0;
  var layer;
  
  for (var i=0; i<this.layerCollection.length; i++) {
    if (this.layerCollection[i].id == id) {
      layer = this.layerCollection[i];
      if (layer.style.pixelTop) {
        result = layer.style.pixelTop;
        alert('id = ' + layer.id + ' | layer.style.pixelTop = ' + result);
      }
      else if (layer.currentStyle) {
        result = layer.currentStyle.top;
        alert('id = ' + layer.id + ' | layer.currentStyle.top = ' + result);
      }
      else if (layer.style) {
        result = layer.style.top;
        alert('id = ' + layer.id + ' | layer.style.top = ' + result);
      }
      else {
        result = layer.top;
        alert('id = ' + layer.id + ' | layer.top = ' + result);
      }
    }
  }
  
  if (isNaN(result)) {
    if (result.indexOf('%') > -1) {
      result = result.substring(0, result.length-1);
    }
    else if (result.indexOf('px') > -1) {
      result = result.substring(0, result.length-2);
    }
    else {
      result = 0;
    }
  }
  
  return result;
}

// This instance method returns a comma-separated string showing the ID of each layer object in the collection.
wco.Layers.prototype.toString = function() {
  var list = 'LAYERS[';
  for (var i=0; i<this.layerCollection.length; i++) {
    if (i > 0) {
      list += ', ';
    }
    list += this.layerCollection[i].id;
  }
  if (list == 'LAYERS(') {list += '[empty]';}
  list += ']';
  
  return list;
}

// This instance method takes the ID of a layer in the collection and returns the Layer object.
// If no Layer object matching the passed ID is found then a null object is returned.
wco.Layers.prototype.getLayer = function(id) {
  for (var i=0; i<this.layerCollection.length; i++) {
    if (this.layerCollection[i].id == id) {
      return this.layerCollection[i];
    }
  }
  
  return null;
}

// This instance method takes an integer identifying the index of a layer in the collection and returns a Layer object.
// If no layer exists at the index position then null object is returned.
wco.Layers.prototype.getLayerByIndex = function(index) {
  if (this.layerCollection[index]) {
    return this.layerCollection[index];
  }
  else {
    return null;
  }
}

// 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.Layers.prototype.add = function(data) {
  // Initialise and add the new layer to the collection
  
  // create a new layer object
  var newLayer = new Object();

  // capture a working copy of the XHTML object
  newLayer = wco.Layers.getObjectFromId(data.id);
    
  // set the DHTML style properties for this layer
  newLayer.style.position = data.position || 'absolute';
  if (data.width) {
    if (isNaN(data.width)) {
      // this is not a number - just apply the string
      newLayer.style.width = data.width.toString();
    }
    else {
      // this is a number - assume 'pixels' as the unit
      newLayer.style.width = data.width + 'px';
    }
  }
  else {
    newLayer.style.width = 'auto';
  }
  if (data.height) {
    if (isNaN(data.height)) {
      // this is not a number - just apply the string
      newLayer.style.height = data.height.toString();
    }
    else {
      // this is a number - assume 'pixels' as the unit
      newLayer.style.height = data.height + 'px';
    }
  }
  else {
    newLayer.style.height = 'auto';
  }
  if (data.left) {newLayer.style.left = data.left + 'px';} else {newLayer.style.left = 'auto';}
  if (data.top) {newLayer.style.top = data.top + 'px';} else {newLayer.style.top = 'auto';}
  if (data.zIndex) {newLayer.style.zIndex = data.zIndex;}
  if (data.background) {newLayer.style.background = data.background;}
  if (data.align) {newLayer.style.textAlign = data.align;}
  newLayer.style.overflow = data.overflow || 'visible';
  newLayer.style.overflowY = data.overflowY || 'visible';
  newLayer.style.overflowX = data.overflowX || 'visible';
  newLayer.style.display = data.display || 'inline';
  newLayer.style.border = data.border || '';
  
  // set the custom object management properties for this layer
  newLayer.isCenteredX = data.isCenteredX || false;
  newLayer.centredXOffset = 0;
  if (newLayer.isCenteredX) {newLayer.centredXOffset = data.left;}
  
  // add the object to the layer collection
  this.layerCollection[this.layerCollection.length] = newLayer;
}

/**
 * Define any CLASS (SHARED) METHODS:
**/
// This class method takes the ID of a layer in the collection
// and returns the relevant DOM object.
wco.Layers.getObjectFromId = function(id) {
  if (document.getElementById) {
  	return(document.getElementById(id));
  }
  else if (document.all) {
	  return(document.all[id]);
  }
  else if (document.layers) {
   	return(document.layers[id]);
  }
  else {
    return(null);
  }
}

/**
 * Define any CLASS (SHARED) PROPERTIES:
**/
// This class property allows the developer to retrieve the Name of this module.
wco.Layers.DESCRIPTION = 'Layers';
wco.Layers.TYPE = 'CLASS';
// These class properties allow the developer to retrieve the Version details of this module.
wco.Layers.VERSION = '1.0.0';
wco.Layers.MAJORVERSION = 1;
wco.Layers.MINORVERSION = 0;
wco.Layers.REVISIONNO = 0;
wco.Layers.COMPANY = 'Wco iEnterprise Solutions';
wco.Layers.COPYRIGHT = '© Copyright 2007 Wco iEnterprise Solutions';
wco.Layers.COMPANYURL = 'www.wco.com.au';
wco.Layers.COMPANYEMAIL = 'support@wco.com.au';
wco.Layers.DEVELOPERSIGNATURE = 'GAW';

/**
 * Instantiate the PAGELAYERS OBJECT for the current page
**/
var pageLayers = new wco.Layers();

