﻿///////////////////////////////////////////////////////////////////////////////
//
//  wcoSlideshowManager.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
//
///////////////////////////////////////////////////////////////////////////////

//--------------------------------------------------------------------------------
HtmlSlideObject = function(parentObject, index, parentElement, xhtmlDataNode) {
  this.parent = parentObject;
  this.index = index;
  this.name = 'slide_' + this.index;
  
  //alert(xhtmlDataNode.outerHTML);
  //this.id = xhtmlDataNode.attributes[0].nodeValue;
  this.id = xhtmlDataNode.attributes.getNamedItem('id').nodeValue;
  this.sortOrder = xhtmlDataNode.childNodes[0].innerHTML;
  this.imageFilename = xhtmlDataNode.childNodes[1].innerHTML;
  this.imageWidth = xhtmlDataNode.childNodes[2].innerHTML; //this.imageWidth = 220;
  this.imageHeight = xhtmlDataNode.childNodes[3].innerHTML; //this.imageHeight = 146;
  this.caption = xhtmlDataNode.childNodes[4].innerHTML;
  
  var left = this.imageWidth;
  if (this.index == 0) {
    left = 0;
  }
  var html = '<div style="background-color:#cccccc; position:absolute; left:' + left + 'px; top:0px; width:' + this.imageWidth + 'px; height:' + (this.imageHeight + 60) + 'px; margin:0; padding:0; z-index:0; display:block;">';
  html += '<p style="margin:0; padding:0;">';
  html += '<img src="ssl/images/' + this.imageFilename + '" style="width:' + this.imageWidth + 'px; height:' + this.imageHeight + 'px; border:none 0px;" />';
  html += '</p>';
  html += '<p class="Caption">';
  html += this.caption;
  html += '</p>';
  html += '</div>';
  var newContent = document.createElement('DIV');
  newContent.id = 'newContent';
  newContent.innerHTML = html;
  newContent.style.display = 'none';
  this.rootElement = newContent.childNodes[0];
  parentElement.appendChild(this.rootElement);
  
}

HtmlSlideObject.prototype = {

}

//--------------------------------------------------------------------------------
HtmlSlideCollection = function(parentObject, parentElement, width, height) {
  this.parent = parentObject;
  
  //height += 60;
  var html = '<div style="background-color:#cccccc; position:absolute; left:0px; top:0px; width:' + width + 'px; height:' + height + 'px; margin:0; padding:0; overflow:hidden; cursor:default; display:block;">';
  html += '';
  html += '';
  html += '';
  html += '';
  html += '';
  html += '';
  html += '';
  html += '</div>';
  var newContent = document.createElement('DIV');
  newContent.id = 'newContent';
  newContent.innerHTML = html;
  newContent.style.display = 'none';
  this.rootElement = newContent.childNodes[0];
  parentElement.appendChild(this.rootElement);
  
  // declare and initialise the module's object & collection properties
  this.item = new Array();
  
  this.timerId = null;
  this.currentSlideIndex = 0;
  this.previousSlideIndex = 0;
}

HtmlSlideCollection.prototype = {
  add: function(xhtmlDataNode) {
    //alert(xhtmlDataNode.childNodes[0].innerHTML + '\n' + xhtmlDataNode.childNodes[1].innerHTML + '\n' + xhtmlDataNode.childNodes[2].innerHTML + '\n' + xhtmlDataNode.childNodes[3].innerHTML + '\n' + xhtmlDataNode.childNodes[4].innerHTML);
    // get the index of the new menu item object
    var index = this.item.length;
    
    // create the new menu item object
    this.item[index] = new HtmlSlideObject(this.parent, index, this.rootElement, xhtmlDataNode);
  },
  
  init: function(xhtmlDataNode) {
    // retrieve each image from the collection and add it to the slideshow manager
    //alert(xhtmlDataNode.childNodes[0].outerHTML);
    for (var i=0; i<xhtmlDataNode.childNodes.length; i++) {
      var imageData = xhtmlDataNode.childNodes[i];
      this.add(imageData);
    }
  },
  
  start: function() {
    this.timerId = window.setTimeout(positioning.createDelegate(this, this.slide), 2500);
  },
  
  stop: function() {
    window.clearTimeout(this.timerId);
    this.timerId = null;
    positioning.slide2ObjectStop();
    for (var i=0; i<this.item.length; i++) {
      this.item[i].rootElement.style.zIndex = 0;
      if (i == 0) {
        this.item[i].rootElement.style.left = '0px';
      }
      else {
        this.item[i].rootElement.style.left = this.item[i].imageWidth + 'px';
      }
    }
  },
  
  slide: function() {
    var idx = this.currentSlideIndex + 1;
    if (idx >= this.item.length) {
      idx = 0;
    }
    
    this.item[this.currentSlideIndex].rootElement.style.zIndex = 0;
    
    this.item[idx].rootElement.style.zIndex = 1;
    this.previousSlideIndex = this.currentSlideIndex;
    this.currentSlideIndex = idx;
    positioning.slide2ObjectLeft(this.item[idx].rootElement, 0, 'normal', positioning.createDelegate(this, this._slideCompleted));
  },
  
  _slideCompleted: function() {
    // reset the previous slide
    this.item[this.previousSlideIndex].rootElement.style.left = this.item[this.previousSlideIndex].imageWidth + 'px';
    // and then call the next slide
    this.timerId = window.setTimeout(positioning.createDelegate(this, this.slide), 5000);
  }

}
