﻿///////////////////////////////////////////////////////////////////////////////
//
//  csLanguageManager.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
//
///////////////////////////////////////////////////////////////////////////////

//--------------------------------------------------------------------------------
HtmlLanguageMenuObject = function(parentObject, index, table, row, languageName) {
  // declare and initialise the module's members
  this.parent = parentObject;
  this.index = index;
  this.name = 'languageMenu_' + this.index;
  
  this.title = 'English';
  this.languageName = languageName;
  this.languageId = languageName.substring(0, 2);
  switch (this.languageId) {
    case 'zh':
      this.title = 'Chinese';
      break;
    case 'ja':
      this.title = 'Japanese';
      break;
    case 'ko':
      this.title = 'Korean';
      break;
    default:
      this.title = 'English';
      break;
  }
  
  // add a new men item row to the table
  var cell = row.insertCell(-1);
  cell.className = 'LanguageLink';
  cell.id = this.name;
  cell.innerHTML = this.title;
  this.activeCell = cell;
}

HtmlLanguageMenuObject.prototype = {
  select: function() {
    //this.activeCell.style.fontWeight = 'bold';
    this.activeCell.className = 'LanguageLinkSelected';
  },
  
  unselect: function() {
    //this.activeCell.style.fontWeight = 'normal';
    this.activeCell.className = 'LanguageLink';
  }
}

//--------------------------------------------------------------------------------
HtmlLanguageCollection = function(parentObject) {
  this.parent = parentObject;
  
  // declare and instantiate the module's UI object properties
  var html = '';
  html += '<table cellpadding="0" cellspacing="0">';
//  html += '<tr>';
//  html += '<td class="LanguageMenuTitle">';
//  html += 'LANGUAGE:';
//  html += '</td>';
//  html += '<td class="LanguageLinkSelected">';
//  html += 'English';
//  html += '</td>';
//  html += '<td class="LanguageLink" onclick="javascript:pageManager.init(\'zh-CN\');">';
//  html += 'Chinese';
//  html += '</td>';
//  html += '<td class="LanguageLink">';
//  html += 'Japanese';
//  html += '</td>';
//  html += '<td class="LanguageLink">';
//  html += 'Korean';
//  html += '</td>';
  html += '<tr>';
  html += '</table>';
  var newContent = document.createElement('DIV');
  newContent.id = 'newContent';
  newContent.innerHTML = html;
  newContent.style.display = 'none';
  this.rootElement = newContent.childNodes[0];
  this.parent.languageMenuElement.appendChild(this.rootElement);
  this.table = this.rootElement;
  // add a new men item row to the table
  this.row = this.table.insertRow(-1);
  var cell = this.row.insertCell(-1);
  cell.className = 'LanguageMenuTitle';
  cell.innerHTML = 'LANGUAGE:';
  
  // declare and initialise the module's object & collection properties
  this.item = new Array();
  this.currentObject = null;
  this.previousObject = null;
  this.selectedObject = null;
  
  //this.init();
}

HtmlLanguageCollection.prototype = {
  add: function(languageName) {
    // get the index of the new menu item object
    var index = this.item.length;
    
    // create the new menu item object
    //alert(dataNode.innerHTML);
    this.item[index] = new HtmlLanguageMenuObject(this.parent, index, this.table, this.row, languageName);
    
    // and hook up the mouse events for the new menu item object
    var menuItem = this.item[index].activeCell;
    menuItem.onmouseover = positioning.createDelegate(this, this.onMouseOver);
    menuItem.onmouseout = positioning.createDelegate(this, this.onMouseOut);
    menuItem.onclick = positioning.createDelegate(this, this.onMouseUp);
  },
  
  init: function(currentLanguageName) {
    this.add('en-AU');
    this.add('zh-CN');
    this.add('ja-JP');
    this.add('ko-KR');
    
    this.setCurrentObject(currentLanguageName);
  },
  
  setCurrentObject: function(languageName) {
    // ensure any previously selected menu item is unhighlighted
    if (this.currentObject != null) {
      this.currentObject.unselect();
    }
    
    var languageId = languageName.substring(0, 2);
    //alert('languageName = ' + languageName + '\nlanguageId = ' + languageId);
    for (var i=0; i<this.item.length; i++) {
      if (this.item[i].languageId == languageId) {
        // set the current object to the menu item just selected
        this.currentObject = this.item[i];
        // and highlight the menu item
        this.currentObject.select();
        return void(0);
      }
    }
  },

  onMouseOver: function(e) {
    var element;

    if (!e) var e = window.event;
    if (e.target) element = e.target;
    else if (e.srcElement) element = e.srcElement;
    if (element.nodeType == 3) element = element.parentNode;
    
    if (this.currentObject == null) {
      //element.style.fontWeight = 'bold';
      element.className = 'LanguageLinkOver';
    }
    else {
      if (element.id != this.currentObject.name) {
        //element.style.fontWeight = 'bold';
        element.className = 'LanguageLinkOver';
      }
    }
  },

  onMouseOut: function(e) {
    var element;

    if (!e) var e = window.event;
    if (e.target) element = e.target;
    else if (e.srcElement) element = e.srcElement;
    if (element.nodeType == 3) element = element.parentNode;

    if (this.currentObject == null) {
      //element.style.fontWeight = 'normal';
      element.className = 'LanguageLink';
    }
    else {
      if (element.id != this.currentObject.name) {
        //element.style.fontWeight = 'normal';
        element.className = 'LanguageLink';
      }
    }
  },

  onMouseUp: function(e) {
    var element;

    if (!e) var e = window.event;
    if (e.target) element = e.target;
    else if (e.srcElement) element = e.srcElement;
    if (element.nodeType == 3) element = element.parentNode;
    
    var doAction = false;
    if (this.currentObject == null) {
      doAction = true;
    }
    else {
      if (element.id != this.currentObject.name) {
        doAction = true;
      }
    }
    
    if (doAction) {
      var idx = element.id.substring((element.id.lastIndexOf('_') + 1), element.id.length);
      //this.setCurrentObject(idx);
      // ensure any previously selected menu item is unhighlighted
      if (this.currentObject != null) {
        this.currentObject.unselect();
      }
      // set the current object to the menu item just selected
      this.currentObject = this.item[idx];
      // and highlight the menu item
      this.currentObject.select();
      
      pageManager.init(this.currentObject.languageName);
      //this.parent.fetchObject(this.currentObject.id, this.currentObject.className, this.currentObject.isLeaf, this.currentObject.index, 87699);
    }
  }
  
}

