﻿///////////////////////////////////////////////////////////////////////////////
//
//  wcoPageManager.js
//
// © 2007-2008 Wco iEnterprise Solutions. 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
//
///////////////////////////////////////////////////////////////////////////////

var pageManager = {};



///////////////////////////////////////////////////////////////////////////////

busySignal = function(thisElement, parentObject) {
  this.sourceElement = thisElement;
  this.parent = parentObject;
  
  this.getWidth = function() {return positioning.getObjectWidth(this.sourceElement);}
  this.getHeight = function() {return positioning.getObjectHeight(this.sourceElement);}
  
}

busySignal.prototype = {
  show: function() {
    positioning.showObject(this.sourceElement);
  },
  
  hide: function() {
    positioning.hideObject(this.sourceElement);
  }

}

///////////////////////////////////////////////////////////////////////////////

thumbnailPane = function(thisElement, nextElement, parentObject, menuPane, teaserPane) {
  this.sourceElement = thisElement;
  this.nextElement = nextElement;
  this.parent = parentObject;
  this.menuElement = menuPane;
  this.teaserElement = teaserPane;
  
  //this.nextElement.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=50)';
  
  this.getWidth = function() {return positioning.getObjectWidth(this.sourceElement);}
  this.getHeight = function() {return positioning.getObjectHeight(this.sourceElement);}
  
}

thumbnailPane.prototype = {
  show: function() {
    if (positioning.isObjectHidden(pageManager.thumbnailPane.sourceElement)) {
      //alert('showing the thumbnail pane');
      positioning.showObject(pageManager.thumbnailPane.sourceElement);
      positioning.showObject(pageManager.thumbnailPane.teaserElement);
      //positioning.showObject(this.nextElement);
    }
    positioning.showObject(pageManager.thumbnailPane.menuElement);
  },
  
  hide: function() {
    positioning.hideObject(this.sourceElement);
    //positioning.hideObject(this.nextElement);
    positioning.hideObject(this.teaserElement);
  },
  
  insertMenu: function(html) {
    this.menuElement.innerHTML = '';
    this.menuElement.innerHTML = html;
    this.show();
  },

  addImage: function(html) {
    //this.removeImage();
    //this.sourceElement.innerHTML = html;
    if (this.nextElement.hasChildNodes()) {
      this.nextElement.removeChild(this.nextElement.childNodes[0]);
    }
    this.nextElement.innerHTML = html;
  },
  
  removeImage: function() {
    if (this.sourceElement.hasChildNodes()) {
      this.sourceElement.removeChild(this.sourceElement.childNodes[0]);
    }
  },
  
  insertTeaserText: function(html) {
    this.teaserElement.innerHTML = html;
    // what we are going to do is fadein the upper nextElement over the top of the sourceElement
    // and when the fadein is complete, simply copy the content of the now-visible nextElement
    // to the covered sourceElement and then hide the nextElement - all un-noticed by the website user
    positioning.fadeIn(this.nextElement, this.showTeaser, 'slow');
  },
  
  showTeaser: function() {
    // swap the thumbnail elements
    if (pageManager.thumbnailPane.sourceElement.hasChildNodes()) {
      pageManager.thumbnailPane.sourceElement.removeChild(pageManager.thumbnailPane.sourceElement.childNodes[0]);
    }
    pageManager.thumbnailPane.sourceElement.innerHTML = pageManager.thumbnailPane.nextElement.innerHTML;
    pageManager.thumbnailPane.nextElement.style.display = 'none';
    
    // display the teaser
    if (positioning.getObjectTop(pageManager.thumbnailPane.teaserElement) != 349) {
      positioning.slideObjectDown(pageManager.thumbnailPane.teaserElement, 349, 'slow');
    }
  },
  
  _showTeaser: function() {
  
  },
  
  hideTeaser: function(callBack) {
    // hide the teaser
    //positioning.slideObjectUp(this.teaserElement, 68, 'slow', pageManager._fetchObject);
    positioning.slideObjectUp(pageManager.thumbnailPane.teaserElement, 68, 'slow', callBack);
  }

}

///////////////////////////////////////////////////////////////////////////////

displayPane = function(thisElement, parentObject, detailPane, submenuPane, bodytextPane, sidebarPane) {
  this.sourceElement = thisElement;
  this.parent = parentObject;
  this.detailElement = detailPane;
  this.menuElement = submenuPane;
  this.bodytextElement = bodytextPane;
  this.sidebarElement = sidebarPane;
  
  this.getWidth = function() {return positioning.getObjectWidth(this.sourceElement);}
  this.getHeight = function() {return (positioning.getObjectHeight(this.sourceElement) + positioning.getObjectHeight(this.detailElement));}
  
}

displayPane.prototype = {
  show: function() {
    positioning.fadeIn(this.sourceElement, null, 'fast');
    positioning.fadeIn2(this.detailElement);
    //positioning.showObject(this.sourceElement);
    //positioning.showObject(this.detailElement);
  },
  
  hide: function(callback) {
    if (!positioning.isObjectHidden(pageManager.displayPane.sourceElement)) {
      //alert('displayPane.hide.callback =\n' + callback);
      positioning.fadeOut(pageManager.displayPane.sourceElement, callback, 'fast');
      positioning.fadeOut2(pageManager.displayPane.detailElement);
//      positioning.hideObject(this.sourceElement);
//      positioning.hideObject(this.detailElement);
    }
  },
  
  insertMenu: function(html) {
    this.menuElement.innerHTML = '';
    this.menuElement.innerHTML = html;
  },

  addImage: function(html) {
    this.removeImage();
    this.sourceElement.innerHTML = html;
  },
  
  removeImage: function() {
    if (this.sourceElement.hasChildNodes()) {
      this.sourceElement.removeChild(this.sourceElement.childNodes[0]);
    }
  },
  
  insertBodytext: function(html) {
    this.bodytextElement.innerHTML = '';
    this.bodytextElement.innerHTML = html;
    positioning.scrollObjectToTop(this.bodytextElement);
  },

  removeBodytext: function() {
    this.bodytextElement.innerHTML = '';
  },

  insertSidebar: function(html) {
    while (this.sidebarElement.children.count > 1) {
      this.sourceElement.removeChild(this.sourceElement.childNodes[1]);
    }
    this.sidebarElement.innerHTML = this.sidebarElement.innerHTML + html;
  }

}

///////////////////////////////////////////////////////////////////////////////

breadcrumbsPane = function(thisElement, parentObject) {
  this.sourceElement = thisElement;
  this.parent = parentObject;
}

breadcrumbsPane.prototype = {
  addList: function(listElementHTML) {
    this.removeList();
    this.sourceElement.innerHTML = listElementHTML;
  },
  
  removeList: function() {
    while (this.sourceElement.hasChildNodes()) {
      this.sourceElement.removeChild(this.sourceElement.childNodes[0]);
    }
  }
}

///////////////////////////////////////////////////////////////////////////////

FetchItem = function(parentObject, objId, objClass, objClassIsLeaf) {
  this.parent = parentObject;
  this.objectId = objId;
  this.objectClass = objClass;
  this.objectClassIsLeaf = objClassIsLeaf;
}

FetchItem.prototype = {
  getId: function() {
    return this.objectId;
  },
  
  getClass: function() {
    return this.objectClass;
  },
  
  getIsLeaf: function() {
    return this.objectClassIsLeaf;
  }
}

///////////////////////////////////////////////////////////////////////////////

ContentObject = function(parentObject, objId, contentString) {
  this.parent = parentObject;
  this.id = objId;
  this.content = contentString;
}

ContentObject.prototype = {
  getContent: function() {
    return this.content;
  }
}

///////////////////////////////////////////////////////////////////////////////

cycler = function(parentObject) {
  this.parent = parentObject;
  
  this.sliderTimerInterval = 30000;
  this.sliderTimerId = window.setTimeout('pageManager.cycle()', this.sliderTimerInterval);
}

cycler.prototype = {
  cycleNext: function(listElementHTML) {
  },
  
  cyclePrevious: function() {
  }
}









///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
  
/*<remarks>
Method: init
Description: Initialises the pageManager object, making it ready to receive and process calls.
Parameters:
  [none]
</remarks>*/
pageManager.init = function() {
  // declare and initialise the pageManager's members
  this.contentPane = positioning.getObject('contentPane');
  this.progressBusySignal = new busySignal(positioning.getObject('progressBusySignal'), this);
  this.thumbnailPane = new thumbnailPane(positioning.getObject('thumbnailPane1'), positioning.getObject('thumbnailPane2'), this, positioning.getObject('menuPane'), positioning.getObject('teaserPane'));
  this.displayPane = new displayPane(positioning.getObject('displayPane'), this, positioning.getObject('detailPane'), positioning.getObject('submenuPane'), positioning.getObject('bodytextPane'), positioning.getObject('sidebarPane'));
  
  // declare the current object properties
  this.currentObjectId = 0;
  this.currentObjectClass = '';
  this.currentObjectClassIsLeaf = false;

  // declare the background fetch engine's properties
  this.contentCollection = new Array();
  this.backgroundFetchList = new Array();
  this.backgroundObject = null;
  this.backgroundObjectId = 0;
  
  // declare the content cycle engine's properties
  this.cycleList = null;
  this.cycleIsActive = false;
  this.cycleIndex = 0;
  this.cycleTimerInterval = 0;
  
  // initialise the content pane in the browser
  this.redoLayout(this.contentPane);
  
  // initialise the communication link to the DataPortal
  dataportal.init(this, '3b6c3924-8372-4818-85e0-924aa12e7dea', 9, 'en-AU');
  
  // ******************************************************************************************** //
  // and activate the website by calling the DataPortal wizard and retrieving the Home Page section data
  this.fetchObject(167693, 'Home Page', false);
}

pageManager.redoLayout = function() {
  positioning.centerObjectInViewport(pageManager.contentPane);
  if (positioning.isObjectHidden(pageManager.contentPane)) {
    positioning.showObject(pageManager.contentPane);
  }
}

pageManager.initCycle = function() {
  if (pageManager.cycleList != null) {
    pageManager.cycleIsActive = true;
  }
}

pageManager.cancelCycle = function() {
  window.clearInterval(pageManager.cycleTimerId);
  pageManager.cycleTimerId = 0;
  pageManager.cycleIsActive = false;
}

pageManager.cycle = function() {
  if (pageManager.cycleTimerId > 0) {window.clearInterval(pageManager.cycleTimerId)};
  
  //alert('pageManager.cycleIndex = ' + pageManager.cycleIndex + '\npageManager.cycleList.length = ' + pageManager.cycleList.length);
  pageManager.cycleIndex = pageManager.cycleIndex + 1;
  //alert('pageManager.cycleIndex = ' + pageManager.cycleIndex);
  if (pageManager.cycleIndex == pageManager.cycleList.length) {
    pageManager.cycleIndex = 0;
  }
  var nextObjId = pageManager.cycleList[pageManager.cycleIndex];
  //alert('nextObjId = ' + nextObjId);
  
  pageManager.fetchObject(nextObjId, 'Website Section', false);
}

pageManager.showDisplay = function() {
  this.cancelCycle();
  
  //this.thumbnailPane.hide();
  //this.displayPane.show();
  this.thumbnailPane.hideTeaser(pageManager._showDisplay);
}

pageManager._showDisplay = function() {
  
  pageManager.displayPane.show();
  
}

pageManager.showThumbnail = function(callback) {
  pageManager.initCycle();
  
  pageManager.displayPane.hide(callback);
  
  //pageManager.thumbnailPane.show();
}

pageManager.backgroundLoadObject = function() {
  // reset the background object properties
  this.backgroundObject = null;
  this.backgroundObjectId = 0;
  
  // now, pop the next item from the list
  if (this.backgroundFetchList.length > 0) {
    
    // get the background object properties
    var objProps = this.backgroundFetchList.shift();
    var objId = objProps[0]; //this.backgroundFetchList.shift();
    var objClass = objProps[1]; //'Website Section';
    var objClassIsLeaf = objProps[2]; false;
    //alert('About to background load object\nid = ' + objId + '\nclass = ' + objClass + '\nisLeaf = ' + objClassIsLeaf);
    
    // confirm that this background object is not already captured
    var isContentCaptured = false;
    var contentLength = this.contentCollection.length;
    for (var i=0; i<contentLength; i++) {
      if (this.contentCollection[i].id == objId) {
        isContentCaptured = true;
      }
    }
    
    // and then proceed with a background call to the dataportal ONLY if there are no explicit fetch calls currently processing...
    if (!isContentCaptured) {
      if (dataportal.request == null) {
        // remember the background object properties
        this.backgroundObject = objProps;
        this.backgroundObjectId = objId;
        
        // set the dataportal callback function to point to the background capture
        dataportal.setCallback(this.backgroundCaptureContent);
        
        if (objClassIsLeaf) {
          dataportal.fetchObject(objId, objClass, objClassIsLeaf, false, 'Service', 50, 1);
        }
        else {
          dataportal.fetchObject(objId, objClass, objClassIsLeaf, false, 'Service', 50, 1);
        }
      }
      else {
        // put the background object properties back on the stack so we can try again later
        this.backgroundFetchList.unshift(objProps);
      }
    }
    else {
      // this objId already captured, so try the next one on the stack
      this.backgroundLoadObject();
    }
    
  }
}

pageManager.backgroundCaptureContent = function(content) {
//  if (pageManager.backgroundObjectId == 167716 || pageManager.backgroundObjectId == 167717) {
//    alert(content);
//  }
  
  var isContentCaptured = false;
  //alert('pageManager.contentCollection = ' + pageManager.contentCollection);
  var contentLength = pageManager.contentCollection.length;
  for (var i=0; i<contentLength; i++) {
    if (pageManager.contentCollection[i].id == pageManager.backgroundObjectId)
      isContentCaptured = true;
  }
  if (!isContentCaptured) {
    // add the new content to the content collection
    pageManager.contentCollection[contentLength] = new ContentObject(pageManager, pageManager.backgroundObjectId, content);
    // also check for any child ids to be added to the background processing list
    if (content.match(/newIdList/g) != null) {
      //alert(content);
      var arrayIds = content.substring( content.indexOf('>', content.indexOf('<div id="newIdList"')) + 1, content.indexOf('</', content.indexOf('<div id="newIdList">')) );
      //alert('Adding ids: [' + arrayIds + '] to the background load list!');
      // add ids to the background load list
      eval('pageManager.backgroundFetchList.push(' + arrayIds + ')');
      //alert('List now contains:\n' + pageManager.backgroundFetchList);
    }
    // and pre-load any images before moving on
    if (content.match(/<img/g) != null) {
      //alert((content.indexOf('src="', content.indexOf('<img')) + 5) + '\n' + content.indexOf('"', content.indexOf('<img src="') + 10));
      var img = content.substring( content.indexOf('src="', content.indexOf('<img')) + 5, content.indexOf('"', content.indexOf('<img src="') + 10) );
      //alert(img);
      pageManager.preloadImages(img);
    }
    //alert('Object Id: ' + pageManager.contentCollection[contentLength].id + ' successfully captured!');
  }
  
  // and continue processing the background object list
  pageManager.backgroundLoadObject();
}

pageManager.preloadImages = function() {

  if (document.images) { //check for compatible browser
    //var imgFiles = pageManager.preloadImages.arguments; //get the array of images to be preloaded into memory
    var imgFiles = arguments; //get the array of images to be preloaded into memory
    if (document.preloadArray == null) {document.preloadArray = new Array();} //create the document array to hold the images
    var i = document.preloadArray.length; //create the array counter
    
    //load the images into the document array
    with (document) {
      for (var j=0; j<imgFiles.length; j++) {
        preloadArray[i] = new Image();
        preloadArray[i++].src = imgFiles[j];
      }
    }
  }
}

pageManager.fetchObject = function(objId, objClass, objClassIsLeaf) {
  // first - if necessary, cancel the cycle timer
  if (pageManager.cycleTimerId > 0) {
    pageManager.cancelCycle();
  }
  
  // second - only proceed if the passed objId does not refer to the current object
  if (objId == 0) {return false;}
  
//  // and process any Website Section selections
//  if (objClass == 'Website Section') {
//    if (objId == this.currentObjectId) {
//      if (pageManager.cycleTimerId == 0 && pageManager.cycleList != null) {
//        this.cycleTimerId = window.setTimeout(pageManager.cycle, 1000);
//      }
//      
//      this.showDisplay();
//      
//      return false;
//    }
//    else {
//      // find this objId in the cycleList and set the cycleIndex accordingly
//      if (pageManager.cycleList != null) {
//        for (var i=0; i < pageManager.cycleList.length; i++) {
//          if (pageManager.cycleList[i] == objId) {
//            pageManager.cycleIndex = i;
//          }
//        }
//      }
//      alert('about to call the showThumbnail method');
//      this.showThumbnail();
//    }
//  }
  
  // and process any Website Section selections
  if ((objClass == 'Website Section') && (objId == this.currentObjectId)) {
    if (pageManager.cycleTimerId == 0 && pageManager.cycleList != null) {
      this.cycleTimerId = window.setTimeout(pageManager.cycle, 1000);
    }
    
    this.showDisplay();
    
    return false;
  }
  
  if ((objClass == 'Website Section') && (objId != this.currentObjectId)) {
    // find this objId in the cycleList and set the cycleIndex accordingly
    if (pageManager.cycleList != null) {
      for (var i=0; i < pageManager.cycleList.length; i++) {
        if (pageManager.cycleList[i] == objId) {
          pageManager.cycleIndex = i;
        }
      }
    }
    
    // remember the current object properties
    this.currentObjectId = objId;
    this.currentObjectClass = objClass;
    this.currentObjectClassIsLeaf = objClassIsLeaf;
    
  }
  
  if ((objClass == 'Website Section') && (!positioning.isObjectHidden(pageManager.displayPane.sourceElement))) {
    ///alert('about to call the showThumbnail method');
    this.showThumbnail(pageManager._fetchObjectCont);
    
  }
  else {
    // remember the current object properties
    this.currentObjectId = objId;
    this.currentObjectClass = objClass;
    this.currentObjectClassIsLeaf = objClassIsLeaf;
    
    if (objClass == 'Website Section') {
      pageManager.initCycle();
    }
    
    if (pageManager.currentObjectClass == 'Website Section' && !positioning.isObjectHidden(pageManager.thumbnailPane.teaserElement)) {
      pageManager.thumbnailPane.hideTeaser(pageManager._fetchObject);
    }
    else {
      pageManager._fetchObject();
    }
    
  }
  
}

pageManager._fetchObjectCont = function() {

  if (pageManager.currentObjectClass == 'Website Section' && !positioning.isObjectHidden(pageManager.thumbnailPane.teaserElement)) {
    pageManager.thumbnailPane.hideTeaser(pageManager._fetchObject);
  }
  else {
    pageManager._fetchObject();
  }
  
}

pageManager._fetchObject = function() {
  
  // check if the content for this objectId has already been added to the ContentCollection
  var contentIndex = -1;
  var contentLength = pageManager.contentCollection.length;
  for (var i=0; i<contentLength; i++) {
    if (pageManager.contentCollection[i].id == pageManager.currentObjectId) {
      contentIndex = i;
    }
  }
  
  
  // remove the current body text - so the user knows something is going on and is about to change!
  pageManager.displayPane.removeBodytext();
  
  if (contentIndex != -1) {
    
    // CONTENT IS AVAILABLE
    // content has already been loaded and is ready for display - pass the content to the displayObject method...
    pageManager.displayObject(pageManager.contentCollection[contentIndex].getContent());
    
  }
  else {
    
    // CONTENT IS REQUIRED
    // the content for this object is not available so proceed with a fetch from the DataPortal
    // - but check if there is an existing request is progress
    if (dataportal.request != null) {
      // existing request processing - cancel it before proceeding with new request
      dataportal.cancelRequest();
      // also handle any interrupted background loading...
      if (pageManager.backgroundObjectId != 0) {
        // put the background object properties back on the stack so we can try again later
        pageManager.backgroundFetchList.unshift(pageManager.backgroundObject);
        pageManager.backgroundObject = null;
        pageManager.backgroundObjectId = 0;
      }
    }

    pageManager.progressBusySignal.show();
    
    // and, now, initiate the fetch
    dataportal.executeCallback = null;
    if (pageManager.currentObjectClassIsLeaf) {
      dataportal.fetchObject(pageManager.currentObjectId, pageManager.currentObjectClass, pageManager.currentObjectClassIsLeaf, false, 'Service', 50, 1);
    }
    else {
      dataportal.fetchObject(pageManager.currentObjectId, pageManager.currentObjectClass, pageManager.currentObjectClassIsLeaf, false, 'Service', 50, 1);
    }
  
  }
  
}

pageManager.displayError = function(content) {
  var newContent = document.createElement('DIV');
  newContent.id = 'newContent';
  newContent.innerHTML = content;
  newContent.style.display = 'block';
  
  document.body.style.backgroundColor = '#ffffff';
  document.body.innerHTML = '';
  document.body.appendChild(newContent);
}

pageManager.displayObject = function(content) {
  // retrieve the returned content and configure a working object
  //alert(content);
  
  // if this is the first time that this content has been recovered,
  // then add it to the content collection before proceeding with the display
  var isContentCaptured = false;
  var contentLength = this.contentCollection.length;
  for (var i=0; i<contentLength; i++) {
    if (this.contentCollection[i].id == this.currentObjectId)
      isContentCaptured = true;
  }
  if (!isContentCaptured) {
    this.contentCollection[contentLength] = new ContentObject(this, this.currentObjectId, content);
    //alert(this.contentCollection[contentLength].id);
  }
  
  var newContent = document.createElement('DIV');
  newContent.id = 'newContent';
  newContent.innerHTML = content;
  newContent.style.display = 'none';
  this.contentPane.appendChild(newContent);

  // look for the new thumbnail image
  if (positioning.getObject('newThumbnailImage') != null) {
    this.thumbnailPane.addImage(positioning.getObject('newThumbnailImage').innerHTML);
    //this.showThumbnail();
  }
  
  // look for the new thumbnail teaser text
  if (positioning.getObject('newTeaserText') != null) {
    this.thumbnailPane.insertTeaserText(positioning.getObject('newTeaserText').innerHTML);
  }
  
  // look for the new menu list
  if (positioning.getObject('newMenuList') != null) {
    this.thumbnailPane.insertMenu(positioning.getObject('newMenuList').innerHTML);
  }
  
  if (positioning.getObject('newDisplayImage') != null) {
    this.displayPane.addImage(positioning.getObject('newDisplayImage').innerHTML);
  }
  
  // look for the new submenu list
  if (positioning.getObject('newSubmenuList') != null) {
    this.displayPane.insertMenu(positioning.getObject('newSubmenuList').innerHTML);
  }
  
  // look for the new body text
  if (positioning.getObject('newBodytext') != null) {
    this.displayPane.insertBodytext(positioning.getObject('newBodytext').innerHTML);
  }
  
  // if this is the first call to the dataportal then initialise the pageManager cycler engine
  if (content.match(/newSession/g) != null && !isContentCaptured) {
    //this.cycleList = new Array(154722, 154723, 154736);
    if (positioning.getObject('newCyclerList') != null) {
      var arrayIds = positioning.getObject('newCyclerList').innerHTML;
      //alert('arrayIds = ' + arrayIds);
      this.cycleList = eval('new Array(' + arrayIds + ')');
      //alert('List now contains:\n' + pageManager.cycleList);
      // and activate the timer to cycle through the website sections
      this.cycleIsActive = true;
      this.cycleIndex = 0;
      this.cycleTimerInterval = 30000;
    }
  }
  
  // check for any child object IDs which can be added to the background fetch list
  if (positioning.getObject('newIdList') != null && !isContentCaptured) {
    var arrayIds = positioning.getObject('newIdList').innerHTML;
    //alert('Adding ids: (' + arrayIds + ') to the background load list!');
    // add ids to the background fetch list
    eval('this.backgroundFetchList.push(' + arrayIds + ')');
    //alert('List now contains:\n' + pageManager.backgroundFetchList);
  }
  
  // and, finally, remove the working copy of the new content and hide the busy signal
  this.contentPane.removeChild(newContent);
  this.progressBusySignal.hide();
  
  // CALL THE SECTION CYCLER
  if (this.cycleIsActive) {
    this.cycleTimerId = window.setTimeout(pageManager.cycle, this.cycleTimerInterval);
  }
  
  // if relevant, CALL THE BACKGROUND LOADER
  if (this.backgroundFetchList.length != 0 && this.backgroundObjectId == 0) {
    this.backgroundLoadObject();
  }
  
}

