/*
  15.08.2006 SK:
    Class names for styling open (visible) and closed (hidden) items.
*/
var strVisible = 'products-submenu-visible';
var strHidden = 'products-submenu-hidden';

var strClosed = 'products-menu-closed';
var strOpen = 'products-menu-open';

/*
  15.08.2006 SK:
    The name of the category cookie.
*/
var strCategoryCookieName = 'categoryTreeStatus';
var chrSplitter = ':';

/**
 * @description Handles the open and close functionality on products menu.
 *
 * @author Sami Keskinen
 * @since 15.08.2006
 *
 * @return nothing.
 */
function toggleVisible(objItem) {
  /*
    15.08.2006 SK:
      Display/hide category contents.
  */
  if (objItem.className == strClosed) {
    showCategory(objItem);
  }
  else {
    hideCategory(objItem);
  }

  setCategoryCookie();
}

/**
 * @description Hides the given category contents.
 *
 * @author Sami Keskinen
 * @since 15.08.2006
 *
 * @param objCategory A reference to a category element.
 *
 * @return nothing.
 */
function hideCategory(objCategory) {
  objCategory.className = strClosed;

  objContents = getCategoryContents(objCategory);
  objContents.className = strHidden;

  return;
}

/**
 * @description Unhides the given category contents.
 *
 * @author Sami Keskinen
 * @since 15.08.2006
 *
 * @param objCategory A reference to a category element.
 *
 * @return nothing.
 */
function showCategory(objCategory) {
  objCategory.className = strOpen;

  objContents = getCategoryContents(objCategory);
  objContents.className = strVisible;

  return;
}

/**
 * @description Searches for the associated category contents element.
 *
 * @author Sami Keskinen
 * @since 15.08.2006
 *
 * @param objCategory A reference to the target category.
 *
 * @return A reference to the category contents relating to given category.
 */
function getCategoryContents(objCategory) {
  /*
    15.08.2006 SK:
      IE and Mozilla handle .nextSibling properties a bit differently,
      so each movement must be validated to get the desired functionality.
  */
  objContents = objCategory.nextSibling;
  strSubClass = objContents.className;

  if (!((strSubClass == strVisible) ||
    (strSubClass == strHidden))) {
    objContents = objContents.nextSibling;
  }

  return (objContents);
}

/**
 * @description Eliminates double clicks.
 *
 * @author Sami Keskinen
 * @since 15.08.2006
 *
 * @return nothing.
 */
function noAction() {
  return false;
}

/**
 * @description Iterates through products menu and creates an array of
 *              all openable/closable product categories.
 *
 * @author Sami Keskinen
 * @since 15.08.2006
 *
 * @return An array of product categories.
 */
function getCategoryElements() {
  objMenu = document.getElementById('products-menu-items');
  arrMenuChildren = objMenu.getElementsByTagName('div');

  arrCategories = new Array();

  /*
    15.08.2006 SK:
      Iterate through all children to collect categories.
  */
  for (iItem = 0; iItem < arrMenuChildren.length; iItem++) {
    objChild = arrMenuChildren[iItem];

    if ((objChild.className == strOpen) || (objChild.className == strClosed)) {
      arrCategories.push(objChild);
    }
  }

  return(arrCategories);
}

/**
 * @description Logs the opened and closed nodes to a cookie.
 *
 * @author Sami Keskinen
 * @since 15.08.2006
 *
 * @return Boolean value, indicating the success of setting a cookie.
 */
function setCategoryCookie() {
  arrCategories = getCategoryElements();

  var strCookie = '';

  for (iCategory = 0; iCategory < arrCategories.length; iCategory++) {
    if (arrCategories[iCategory].className == strOpen) {
      strCookie = strCookie + '1';
    }
    else {
      strCookie = strCookie + '0';
    }

    if (!(iCategory == (arrCategories.length - 1))) {
      strCookie = strCookie + chrSplitter;
    }
  }

  return(setCookie(strCategoryCookieName, strCookie));
}

/**
 * @description Iterates through all menu items and shows/hides category
 *              details by last view.
 *
 * @author Sami Keskinen
 * @since 15.08.2006
 *
 * @return Boolean true on a successful set, false otherwise.
 */
function parseCategoryCookie() {
  /*
    15.08.2006 SK:
      Get cookie data and category elements.
  */
  strCookie = getCookie(strCategoryCookieName);
  if (!(isString(strCookie))) {
    return true;
  }

  var arrCookie = strCookie.split(chrSplitter);
  var arrCategories = getCategoryElements();

  /*
    15.08.2006 SK:
      Check if cookie is not valid.
  */
  if (arrCookie.length != arrCategories.length) {
    deleteCookie(strCategoryCookieName);
    return false;
  }

  /*
    15.08.2006 SK:
      Cycle through the the cookie's categories and set the
      category elements on the way.
  */
  for (iCategory = 0; iCategory < arrCategories.length; iCategory++) {
    if (arrCookie[iCategory] == '1') {
      showCategory(arrCategories[iCategory]);
    }
    else {
      hideCategory(arrCategories[iCategory]);
    }
  }

  return true;
}
