/**********************************************************************
 * Copyright (c) 2007 Express Design Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted subject to the following conditions:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL EXPRESS DESIGN INC. BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 **********************************************************************/

var contents = get("contents");    // embedded page content
var headlogo = get("headlogo");    // main heading graphic


/**********************************************************************
 * menu items
 **********************************************************************/
function makeMI(mi, page, graphic)
{
  mi.page    = page;
  mi.graphic = graphic;

  mi.activate = function() {            // make this item clickable
    this.active = true;
    this.style.cursor = "pointer";
    this.style.color = "blue";
    this.style.textDecoration = "none";
  }

  mi.deactivate = function() {          // make this item unclickable
    this.active = false;
    this.style.cursor = "default";
    this.style.color = "black";
    this.style.textDecoration = "underline";
  }

  mi.setpage = function() {
    var currentItem = getCurrentItem();
    if (currentItem)
      currentItem.activate();
    contents.setHTMLFrom(this.page);
    this.deactivate();
    window.headlogo.src = this.graphic;
    setCookie("curItem", this.id);
  }

  mi.addListener("mouseover", mi);
  mi.domouseover = function(e) {
    if (this.active)
      this.style.color = "red";
  }

  mi.addListener("mouseout", mi);
  mi.domouseout = function(e) {
    if (this.active)
      this.style.color = "blue";
  }

  mi.addListener("click", mi);
  mi.doclick = function(e) {
    if (this.active)
      this.setpage();
  }

  mi.setCanSelect(false);

  mi.activate();

  return mi;
}

function getCurrentItem() {
  var current = getCookie("curItem");
  if (!current)
    return null;
  else
    return window[current];
}

var miProducts  = makeMI(get("miProducts"),
                         "products.html",
                         "lib/im/headlogo.jpg");

var miSupport   = makeMI(get("miSupport"),
                         "support.html",
                         "lib/im/headlogo.jpg");

var miCommunity = makeMI(get("miCommunity"),
                         "community.html",
                         "lib/im/community.jpg");

if (!getCurrentItem())
  miProducts.setpage();
else
  getCurrentItem().setpage();

var mBar = get("menuBar");
mBar.setCanSelect(false);

headlogo.setCanSelect(false);


/**********************************************************************
 * each menu item pops up a description pane on mouseover
 **********************************************************************/
function MenuItemPop(item, desc) {
  this.item   = item;
  this.popDir = "up";  // any other value pops down

  this.div = create("div", document.body);
  this.div.hide();
  this.div.absp();
  this.div.style.borderStyle = "outset";
  this.div.style.borderWidth = "3px";
  this.div.style.textAlign = "center";
  this.div.style.padding = "5px";

  this.bg = create("div", this.div);
  this.bg.absp();
  this.bg.moveTo(0, 0);
  this.bg.style.backgroundColor = "black";
  this.bg.setOpacity(0.50);
  this.bg.style.zIndex = 1;

  this.txt = create("span", this.div);
  this.txt.relp();
  this.txt.setHTML(desc);
  this.txt.style.fontFamily = "Lucida Grande, Helvetica, sans-serif";
  this.txt.style.fontSize = "8pt";
  this.txt.style.fontWeight = "bold";
  this.txt.style.color = "#FFFF00";
  this.txt.style.backgroundColor = "transparent";
  this.txt.style.zIndex = 2;

  this.bg.resizeTo(this.div.getOWidth()-5, this.div.getOHeight()-5);

  makeListener(this);

  this.item.addListener("mouseover", this);
  this.domouseover = function(e) {
    var ipos = this.item.getPageXY();
    var popy = (this.popDir == "up") ?
          ipos.y - this.div.getOHeight() - 8 :
          ipos.y + this.item.getOHeight() + 8;
    this.div.moveTo(ipos.x + this.item.getOWidth() / 2
                      - this.div.getOWidth() / 2,
                    popy);
    this.div.show();
  }

  this.item.addListener("mouseout", this);
  this.domouseout = function(e) {
    this.div.hide();
  }
}

var miProductsPop
  = new MenuItemPop(miProducts,
         "Browse software products and<br/>download free trial versions");

var miSupportPop
  = new MenuItemPop(miSupport,
                    "Request customer support<br/>or provide other feedback");

var miCommunityPop
  = new MenuItemPop(miCommunity,
     "Free and beta software,<br/>tutorials, opinions, etc.");
