
/***************************************************************************
 *   Copyright (C) 2008, Paul Lutus                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

if (typeof document.attachEvent!='undefined') {
  window.attachEvent('onload',init);
  window.attachEvent('onunload',save_cookie);
  document.attachEvent('onclick',mouseClick);
}
else {
  window.addEventListener('load',init,false);
  window.addEventListener('unload',save_cookie,false);
  document.addEventListener('click',mouseClick,false);
}

function read_cookie(path)
{
  var result = "";
  var a = document.cookie.indexOf(path);
  if(a != -1) {
    a += path.length + 1;
    var b = document.cookie.indexOf(";",a);
    if(b == -1) {
      b = document.cookie.length;
    }
    result = unescape(document.cookie.substring(a,b));
  }
  return result;
}

var cookie_array = null;

function get_cookie()
{
  var cookie = read_cookie(window.location.pathname);
  if (cookie != ''){
    cookie_array = cookie.split(" ");
  }
}

function save_cookie() {
  var values = oldListItem.id + " " + oldContentPane.id;
  var exp = new Date(new Date().getTime() + 30 * 24 * 60 * 60 * 1000);
  document.cookie=window.location.pathname + "=" + values + "; expires=" + exp.toGMTString();
}

var contentPane_array = null;
var contentPane_first = null;
var listItem_array = null;
var listItem_first = null;
var oldContentPane = null;
var oldListItem = null;

function hide_all_content_panes()
{
  for(i = 0;i < contentPane_array.length;i++) {
    contentPane_array[i].style.visibility = 'hidden';
  }
  oldContentPane = null;
}

function show_new_content(pane)
{
  if(pane == null) {
    pane = contentPane_first;
  }
  if(oldContentPane != null) {
    oldContentPane.style.visibility = 'hidden';
  }
  pane.style.visibility = 'visible';
  oldContentPane = pane;
}

function set_new_list_item(item)
{
  if(item == null) {
    item = listItem_first;
  }
  if(oldListItem != null) {
    oldListItem.style.background = "#404040";
    oldListItem.style.color = "#ffffff";
  }
  if(item != null) {
    item.style.background = "#ffffff";
    item.style.color = "#000000";
  }
  oldListItem = item;
}

function show_default()
{
  show_new_content(oldContentPane);
  set_new_list_item(oldListItem);
}

function init()
{
  var first_list = "index0";
  var first_layer = "layer0";
  get_cookie();
  if(cookie_array) {
    first_list = cookie_array[0];
    first_layer = cookie_array[1];
  }
  contentPane_array = new Array();
  listItem_array = new Array();
  array = document.getElementsByTagName("div");
  for(i = 0;i < array.length;i++) {
    if (array[i].className == "listItem") {
      listItem_array.push(array[i]);
      if(array[i].id == first_list) {
        listItem_first = array[i];
      }
    }
    else if (array[i].className == "contentPage") {
      contentPane_array.push(array[i]);
      if(array[i].id == first_layer) {
        contentPane_first = array[i];
      }
    }
  }
  
  hide_all_content_panes();
  show_default();
}

function mouseClick(e)
{
  evt=e?e:event;
  target=evt.target?evt.target:evt.srcElement;
  if(target.className == "listItem") {
    set_new_list_item(target);
    id = target.id;
    sid = new RegExp('index(\\d+)').exec(id);
    s = "layer" + sid[1];
    var newContent = document.getElementById(s);
    show_new_content(newContent);
  }
}


