﻿///////////////////////////////////////////////////////////////////////////////
//
//  csTopicManager.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
//
///////////////////////////////////////////////////////////////////////////////

//--------------------------------------------------------------------------------
HtmlTopicObject = function(parentObject, index, id) {
  this.parent = parentObject;
  this.index = index;
  this.name = 'question_' + this.index;
  
  this.id = id;
  this.parentId = this.parent.id;
  this.concurrencyId = -1;
  this.className = 'Question';
  this.isLeaf = true;
  this.title = '';
  this.description = '';
  
  // declare and instantiate the pageManager's UI elements
  var html = '<div id="TopicBody' + this.index + '" style="position:absolute; top:0px; left:0px; width:100%; height:100%; text-align:left; display:none;">';
    html += '<div style="position:absolute; top:0px; left:0px; display:block;">';
    html += '</div>';
      
    html += '<div style="position:absolute; top:285px; left:200px; width:108px; height:22px; background-image:url(\'ssl/images/bkg_activity_dlg.png\'); background-repeat:no-repeat; display:block;">';
      html += '<img src="ssl/images/activity_sm.gif" style="position:absolute; top:3px; left:5px; width:16px; height:16px; border:none 0px;" />';
      html += '<div class="ActivityTitle" style="position:absolute; top:3px; left:28px;">';
      html += 'Loading...';
      html += '</div>';
    html += '</div>';
    
  html += '</div>';
  var newContent = document.createElement('DIV');
  newContent.id = 'newContent';
  newContent.innerHTML = html;
  newContent.style.display = 'none';
  this.rootElement = newContent.childNodes[0];
  this.parent.bodyViewportElement.appendChild(this.rootElement);
  this.bodyElement = this.rootElement.childNodes[0];
  this.activityElement = this.rootElement.childNodes[1];
  
}

HtmlTopicObject.prototype = {
  init: function(dataNode) {
    // and retrieve the object properties
    this.id = dataNode.childNodes[0].innerHTML;
    this.concurrencyId = dataNode.childNodes[1].innerHTML;
    this.title = dataNode.childNodes[2].innerHTML;
    this.description = dataNode.childNodes[3].innerHTML;
    
    // apply the required formatting to the body text
    var bodyText = dataNode.childNodes[4].innerHTML.replace(/<P>/g, '<P class="BodyText1">');
    // display the topic content
    var html = '<div>';
    if (dataportal.isFrutigerInstalled) {
      html += '<p class="SubHeading2F">';
    }
    else {
      html += '<p class="SubHeading2">';
    }
    html += this.title;
    html += '</p>';
    html += '<hr color="#444444" noshade="true" size="0.1" style="height:0.1px; margin:6px 0 0 0; padding:0;" />';
    html += bodyText;
    
//    html += '<P class="BodyText1">';
//    for (var p in navigator) {
//      html += p + ': ' + navigator[p] + '<br/>';
//    }
//    html += '</P>';
    
    html += '</div>';
    var newContent = document.createElement('DIV');
    newContent.id = 'newContent';
    newContent.innerHTML = html;
    newContent.style.display = 'none';
    var newElement = newContent.childNodes[0];
    this.bodyElement.appendChild(newElement);
    
    // and hide the activity icon
    positioning.hideObject(this.activityElement);
  },
  
  show: function() {
    //positioning.showObject(this.bodyElement);
    positioning.showObject(this.rootElement);
  },
  
  hide: function() {
    //positioning.hideObject(this.bodyElement);
    positioning.hideObject(this.rootElement);
  },
  
  showActivity: function() {
    //alert('About the show the body text element');
    positioning.showObject(this.activityElement);
  },
  
  hideActivity: function() {
    positioning.hideObject(this.activityElement);
  },
  
  setTextAlign: function(alignment) {
    this.bodyElement.style.textAlign = alignment;
  }
}

//--------------------------------------------------------------------------------
HtmlTopicCollection = function(parentObject) {
  this.parent = parentObject;
  
  // declare and instantiate the module's UI elements
  
  // declare and initialise the module's members
  this.item = new Array();
  this.currentObject = null;
  this.previousObject = null;
  this.selectedObject = null;
}

HtmlTopicCollection.prototype = {
  /*<remarks>
  Method:      add
  Description: Instantiates a new section object and adds it to the section collection, from where it is accessible by the page manager.
  Parameters:
               id        [required] - 
               tabIndex [required} - 
  </remarks>*/
  add: function(id, tabIndex) {
    //alert('topicId = ' + id + '\ntopicIndex = ' + tabIndex);
    // first, check if this object has already been added to the collection
    for (var i=0; i<this.item.length; i++) {
      if (this.item[i].id == id) {
        // object found!
        // return this object's concurrency id
        return (this.item[i].concurrencyId);
      }
    }
    
    // Object not found...
    // so instantiate a new topic tab object ready for initialising with data returned from the DataPortal
    tabIndex = this.item.length;  // get the index of the new tab
    this.item[tabIndex] = new HtmlTopicObject(this.parent, tabIndex, id);
    
    // and return the new topic object's default concurrency id (= 0)
    return (this.item[tabIndex].concurrencyId);
  },
  
  init: function(dataNode) {
    // call the init method of the relevant topic tab
    var id = dataNode.childNodes[0].innerHTML;
    for (var i=0; i<this.item.length; i++) {
      if (this.item[i].id == id) {
        // initialise the new subject page object
        this.item[i].init(dataNode);
        return void(0);
      }
    }
  },
  
  setCurrentObject: function(id) {
    // first, if necessary, hide the current topic tab object
    if (this.currentObject != null) {
      this.currentObject.hide();
    }
    
    if (id == 0) {
      // use index=0 to identify the requested topic
      // set the new current topic tab object
      this.currentObject = this.item[0];
    }
    else {
      // use the id to identify the requested object
      // now, identify the requested topic tab object
      for (var i=0; i<this.item.length; i++) {
        if (this.item[i].id == id) {
          // set the new current topic tab object
          this.currentObject = this.item[i];
        }
      }
    }
    
    this.currentObject.show();
    if (this.currentObject.index == 0) {
      this.parent.showDialog();
    }
    else {
      this.parent.hideDialog();
    }
    
    this.parent.pagemenu.setCurrentObject(this.currentObject.index);
  }
  
}
