var current_item = 'atlas_copco';
var selected_class = 'logo_selected';
var unselected_class = 'logo';

var current_leftmost_column = 0;
var displayed_columns = 5;
var column_count = 7;
var column_lookup = ({});

var history_changed = function(newLocation, historyData)
{
  detect_item();

//  if (newLocation != null)
//  {
//    show_item(newLocation, false);
//  }
}

function ensureColumnVisible(item)
{
  var item_name = item.replace(/-/g, '_');
  
  if (column_lookup != null && column_lookup[item_name] != undefined)
  {
    var columnId = column_lookup[item_name];
    while(columnId < current_leftmost_column)
    {
      moveLeft(true);
    }
    while(columnId > current_leftmost_column + displayed_columns - 1)
    {
      moveRight(true);
    }
  }
}

function detect_item()
{
  var section = location.hash;
  if (section != null && section.length > 1 && section.charAt(0) == '#')
  {
    section = section.substr(1);
    ensureColumnVisible(section);
    show_item(section, false);
  }
}

window.dhtmlHistory.create({
        toJSON: function(o) {
                return Object.toJSON(o);
        }
        , fromJSON: function(s) {
                return s.evalJSON();
        }
});

window.onload = function() {
        dhtmlHistory.initialize();
        dhtmlHistory.addListener(history_changed);
        detect_item();
};

function show_item(item, add_to_history)
{
  if (item == null || item == '') return;

  if (item != current_item)
  {
    var current_item_name = 'item_' + current_item.replace(/-/g, '_');
    var item_name = 'item_' + item.replace(/-/g, '_');

    if (current_item != '') $(current_item_name).className = unselected_class;

    $(item_name).className = selected_class;
    current_item = item;
    if (add_to_history) dhtmlHistory.add(item, null);
  }
}

function preSelectCallback(item)
{
}

function checkSelection(columnGoing, direction)
{
  var item_name = 'item_' + current_item.replace(/-/g, '_');
  var partnerDiv = document.getElementById(item_name);
  var columnDiv = partnerDiv.parentNode;
  if (columnDiv == null || columnDiv == undefined) return;
  var columnId = columnDiv.id;
  if (columnId == columnGoing)
  {
    var idNumber = columnId.substr(7);
    if (direction == 'left')
    {
      --idNumber;
    }
    else
    {
      ++idNumber;
    }

    var newColumn = document.getElementById('column_' + idNumber);
    for (var i = 0; i < newColumn.childNodes.length; ++i)
    {
      var childNode = newColumn.childNodes[i];
      if (childNode.nodeType == 1 && childNode.nodeName.toLowerCase() == 'div')
      {
        show_item(childNode.id.substr(5), true);
        break;
      }
    }
  }
}

function shrinkElem(elem_id, dir, dur)
{
  Effect.Shrink(elem_id, ({ direction: dir, duration: dur }));
}

function growElem(elem_id, dir, dur)
{
  Effect.Grow(elem_id, ({ direction: dir, duration: dur }));
}

function moveLeft(isSetup)
{
  if (current_leftmost_column == 0) return;
  var column_id = 'column_' + (current_leftmost_column + displayed_columns - 1);
  var elem = document.getElementById(column_id);
if (!isSetup) checkSelection(column_id, 'left')
  var dur = (isSetup) ? 0 : 1.0;
  --current_leftmost_column;
  var column_id2 = 'column_' + current_leftmost_column;

  shrinkElem(column_id, 'bottom-right', dur);
  growElem(column_id2, 'bottom-left', dur);
}

function moveRight(isSetup)
{
  if (dur == null) dur = 1;
  if (current_leftmost_column + displayed_columns >= column_count) return;
  var column_id = 'column_' + current_leftmost_column;
  var column_id2 = 'column_' + (current_leftmost_column + displayed_columns);
  var dur = (isSetup) ? 0 : 1.0;
  if (!isSetup)  checkSelection(column_id, 'right')
  ++current_leftmost_column;

  shrinkElem(column_id, 'bottom-left', dur);
  growElem(column_id2, 'bottom-right', dur);
}

