﻿///////////////////////////////////////////////////////////////////////////////
//
//  wcoPageManager-Custom.js
//
// © 2007-2010 Wco iEnterprise Solutions Pty Ltd. ALL RIGHTS RESERVED.
// This file is licensed as part of the DataPortal 3.0 Web Presence Solution, for details look here: http://www.wco.com.au
//
///////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/*<remarks>
Name:        HtmlPageManagerObject Class
Method:      constructor
Description: Instantiates a new instance of the pageManager object, making it ready to receive and process calls to the DataPortal Web service.
Parameters:  [none]
</remarks>*/
HtmlPageManagerObject = function () {
  //alert('in custom page manager');
//  // declare the module-level fields for this object
//  this.titlebar = new HtmlTitleBarObject(this);
//  this.languagebar = new HtmlLanguageBarObject(this);
//  this.navigator = new HtmlNavigatorObject(this);
//  //this.footer = new HtmlFooterObject(this);
//  this.websiteData = null;

  this.isInitialised = false;
  this.currentCell = null;

  var html = '<table id="contentContainer" border="0" cellpadding="0" cellspacing="0" style="position:absolute; top:0px; left:0px; width:956px; margin:0; padding:0;">';
  html += '';
  html += '';
  html += '</table>';
  $('#displayElement').append(html);


  
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/*<remarks>
Description: Defines the  methods of the HtmlPageManagerObject Class.
</remarks>*/
HtmlPageManagerObject.prototype = {
  /*<remarks>
  Method:      init
  Description: Initialises the object's members and runs through any required startup procedures.
  Parameters:  [none]
  </remarks>*/
  init: function () {
    this.websiteData = arguments[0];
    var childCollection = arguments[1];

    var table = $('#contentContainer')[0];

    for (var s = 0; s < childCollection.length; s++) {
      var row = table.insertRow(-1);
      var cell = row.insertCell(-1);
      cell.id = 'Section-' + this.websiteData.ClassTypeId + '-' + this.websiteData.Id + '-' + this.websiteData.CustomObjectId + '-' + childCollection[s].ClassTypeId + '-' + childCollection[s].ClassName + '-' + childCollection[s].ClassId + '-' + childCollection[s].Id;
      cell.innerHTML = '<p>' + childCollection[s].Name + '</p>';
      var cellClass = {
        'cursor':'pointer'
      };
      $(cell).css(cellClass);
      $(cell)[0].noWrap = true;

      $(cell).click(function (e) {
        pageManager.fetchContent(this, this.id);
      });
    };

    this.onWindowResize();
    $('#displayElement').show();

  },

  fetchContent: function (cell, data) {
    this.currentCell = cell;
    var parts = data.split('-');
    //alert(data);
    //alert(parts[1] + ', ' + parts[2] + ', ' + parts[3] + ', ' + parts[4] + ', ' + parts[5] + ', ' + parts[6] + ', ' + parts[7]);

    dataportal.setCallback(createDelegate(this, this.displayContent));
    dataportal.fetchCurrentObject(parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7]);
  },

  displayContent: function () {
    var currentObject = arguments[0];
    var childCollection = arguments[1];

    //var parentTable = $('#contentContainer')[0];
    //var containerRow = parentTable.insertRow(this.currentCell.parentNode.rowIndex + 1);
    var containerRow = this.currentCell.parentNode.parentNode.parentNode.insertRow(this.currentCell.parentNode.rowIndex + 1);
    var containerCell = containerRow.insertCell(-1);
    var cellClass = {
      'width':'20px'
    };
    $(containerCell).css(cellClass);
    var containerCell = containerRow.insertCell(-1);
    var html = '<table id="container-' + currentObject.Id + '" border="0" cellpadding="0" cellspacing="0" style="width:100%; margin:0; padding:0;">';
    html += '</table>';
    containerCell.innerHTML = html;

    var table = $('#container-' + currentObject.Id)[0];

    if (currentObject.ClassName == 'Question') {
      var row = table.insertRow(-1);
      var cell = row.insertCell(-1);
      cell.id = 'Question-' + currentObject.Id;
      cell.innerHTML = '<p>' + currentObject.Details + '</p>';
    }

    for (var s = 0; s < childCollection.length; s++) {
      //alert(childCollection[s].Name);
      if (childCollection[s].ClassName == 'Section' || childCollection[s].ClassName == 'Question') {
        var row = table.insertRow(-1);
        var cell = row.insertCell(-1);
        cell.id = 'Section-' + currentObject.ClassTypeId + '-' + currentObject.Id + '-' + currentObject.CustomObjectId + '-' + childCollection[s].ClassTypeId + '-' + childCollection[s].ClassName + '-' + childCollection[s].ClassId + '-' + childCollection[s].Id;
        cell.innerHTML = '<p>' + childCollection[s].Name + '</p>';
        $(cell)[0].noWrap = true;

        var cellClass = {
          'cursor':'pointer'
        };
        $(cell).css(cellClass);
        $(cell).click(function (e) {
          pageManager.fetchContent(this, this.id);
        });
//        if (childCollection[s].ClassName == 'Section') {
//          var cellClass = {
//            'cursor':'pointer'
//          };
//          $(cell).css(cellClass);
//          $(cell).click(function (e) {
//            pageManager.fetchContent(this, this.id);
//          });
//        }
//        else {
//          var cellClass = {
//            'cursor':'default'
//          };
//          $(cell).css(cellClass);
//        }

      }
    }
  },

  onWindowResize: function () {
    // get the document width and height values
    var w = $(window).width();
    var h = $(window).height();
  
    // reposition the content element
    var offset = $("#displayElement").offset();
    var newT = offset.top;
    var newL = ((w - 956) / 2);
    $("#displayElement").offset({ top: newT, left: newL });

  }

}

