// Copyright 2007-2009; ZygTech: Gregory Campbell All Rights Reserved
//
// Support for the ideacenter and products, and menu code

var max_visible_examples = 5;
var max_micro_width = 100;
var macro_spacing = 10;
// width of submenus and menus
var menu_width = 140;
var submenu_width = 160;

var num_examples = new Array();
var micro_divs = new Array();
var cur_micro = new Array();
var example = new Array();
var cur_product = 0;
var cur_row = 0;
var cur_title = "";

function populate_micros() {

  p_idx = 0;
  m_idx = 0;
  micro_name = "micro_p" + p_idx + "_ex" + m_idx;
  product_name = "product" + p_idx;
  while (document.getElementById(product_name)) {
    cur_micro[p_idx] = 0;
    micro_divs[p_idx] = new Array();
    while (document.getElementById(micro_name)) {
      micro_divs[p_idx][m_idx] = micro_name;
      num_examples[p_idx] = m_idx + 1;
      m_idx++;
      micro_name = "micro_p" + p_idx + "_ex" + m_idx;
    }
    if (num_examples[p_idx] <= max_visible_examples) {
      disable_arws(p_idx, 'right');
    }
    m_idx = 0;
    p_idx++;
    micro_name = "micro_p" + p_idx + "_ex" + m_idx;
	product_name = "product" + p_idx;
  }
}

function set_example(product, ex) {
  example[product] = ex;
}

function prep_example_for_cart(product, ex) {

  index = document.forms['price'+product].elements['trader_price'+product].selectedIndex;
  selected_product = document.forms['price'+product].elements['trader_price'+product].options[index].value;
  product_name = escape(selected_product);
  if (!example[product]) {
    AddExampleToCart(product_name, ex);

  }
  else {
    e = example[product];
    AddExampleToCart(product_name, e);

  }
}

function add_product_to_cart(product) {

  index = document.forms['price'+product].elements['trader_price'+product].selectedIndex;
  selected_product = document.forms['price'+product].elements['trader_price'+product].options[index].value;
  product_name = escape(selected_product);

  AddExampleToCart(product_name, '');
}

function highlight_icon(icon_img) {
  icon = document.getElementById(icon_img);
  if (icon.src.search(/disable/) == -1) {
    icon.src = icon.src.replace(/images/, "images/highlight");
  }
}

function restore_icon(icon_img) {
  icon = document.getElementById(icon_img);
  if (icon.src.search(/disable/) == -1) {
    icon.src = icon.src.replace(/highlight/, "");
  }
}

function disable_arws(product, dir) {
//  arw_img = dir + "_arw" + product;
//  arw = document.getElementById(arw_img);
  dbl_arw_img = dir + "_dbl_arw" + product;
  dbl_arw = document.getElementById(dbl_arw_img);
//  arw.src = arw.src.replace(/highlight/, "");
//  arw.src = arw.src.replace(/images/, "images/disable");
  dbl_arw.src = dbl_arw.src.replace(/highlight/, "");
  dbl_arw.src = dbl_arw.src.replace(/images/, "images/disable");
}

function restore_arws(product, dir) {
//  arw_img = dir + "_arw" + product;
//  arw = document.getElementById(arw_img);
  dbl_arw_img = dir + "_dbl_arw" + product;
  dbl_arw = document.getElementById(dbl_arw_img);
//  arw.src = arw.src.replace(/disable/, "");
  dbl_arw.src = dbl_arw.src.replace(/disable/, "");
}

function display_large_image(row, product, title) {
  cur_product = product;
  cur_row = row;
  cur_title = title;

//alert("row: " + cur_row + " product: " + cur_product);

  prod = document.getElementById("li_prod_title");
  if (document.all) {
    prod.innerText = title;
  } else if (prod.textContent) {
      prod.textContent = title;
  }
  div_name = "row" + row;
  div = document.getElementById(div_name);
  big_image = document.getElementById("large_image");

  thumb_name = "thumb" + product;
  thumb = document.getElementById(thumb_name);

    // want to replace micros or thumbs with proofs
  img_loc = thumb.src.replace("micros","proofs");
  img_loc = img_loc.replace("thumbs","proofs");

  large_pic = document.getElementById("large_img");
  large_pic.src = img_loc;
  big_image.style.visibility = "visible";
  if (div) {
    if (parseInt(big_image.style.width) == 1) {

    windowDim = getWindowCenterXY();
    documentScroll = getScrollXY();

    big_image.style.left = (documentScroll[0]+(windowDim[0]/2)) + "px";
    big_image.style.top = (documentScroll[1]+(windowDim[1]/2)) + "px";

      setTimeout("display_large_image(cur_row, cur_product, cur_title)",1);
    }
    if (parseInt(big_image.style.width) < 500) {
      big_image.style.left = parseInt(big_image.style.left) - 20 + "px";
      big_image.style.width = parseInt(big_image.style.width) + 40 + "px";
      big_image.style.top = parseInt(big_image.style.top) - 20 + "px";
      big_image.style.height = parseInt(big_image.style.height) + 40 + "px";
      setTimeout("display_large_image(cur_row, cur_product, cur_title)",1);
    }
  }
}

function display_large_image_w_url(url, title) {
  cur_title = title;

//alert("row: " + cur_row + " product: " + cur_product);

  prod = document.getElementById("li_prod_title");
  if (document.all) {
    prod.innerText = title;
  } else if (prod.textContent) {
      prod.textContent = title;
  }
  big_image = document.getElementById("large_image");

    // want to replace micros or thumbs with proofs
  img_loc = url.replace("micros","proofs");
  img_loc = img_loc.replace("thumbs","proofs");

  large_pic = document.getElementById("large_img");
  large_pic.src = img_loc;
  big_image.style.visibility = "visible";
    if (parseInt(big_image.style.width) == 1) {

    windowDim = getWindowCenterXY();
    documentScroll = getScrollXY();

    big_image.style.left = (documentScroll[0]+(windowDim[0]/2)) + "px";
    big_image.style.top = (documentScroll[1]+(windowDim[1]/2)) + "px";

      setTimeout("display_large_image_w_url(img_loc, cur_title)",1);
    }
    if (parseInt(big_image.style.width) < 500) {
      big_image.style.left = parseInt(big_image.style.left) - 20 + "px";
      big_image.style.width = parseInt(big_image.style.width) + 40 + "px";
      big_image.style.top = parseInt(big_image.style.top) - 20 + "px";
      big_image.style.height = parseInt(big_image.style.height) + 40 + "px";
      setTimeout("display_large_image_w_url(img_loc, cur_title)",1);
    }
}



function hide_large_image() {
  big_image = document.getElementById("large_image");
  big_image.style.visibility = "hidden";
  big_image.style.height = "1px";
  big_image.style.width = "1px";

}

function scroll_micro_right(product) {
var micro = new Array();

//  right_arw_img = "right_arw" + product;
  right_arw_img = "right_dbl_arw" + product;
  right_arw = document.getElementById(right_arw_img);
  if (right_arw.src.search(/disable/) == -1) {
    cur_product = product;
    cur = cur_micro[product];

    for (var i=0; i < max_visible_examples+1; i++) {
        micro[i] = document.getElementById(micro_divs[product][cur+i]);
    }

    if (micro[0]) {

      if (parseInt(micro[0].style.left) > -(max_micro_width + macro_spacing)) {

        for (var i=0; i < max_visible_examples+1; i++) {
            micro[i].style.left = parseInt(micro[i].style.left) - 5 + "px";
        }

        setTimeout("scroll_micro_right(cur_product)",1);
      }
      else  {
        if (cur == 0) {
          restore_arws(cur_product, 'left');
        }
        if (cur >= (num_examples[product] - (max_visible_examples + 1))) {
          disable_arws(cur_product, 'right');
        }
        cur_micro[product]++;
      }
    }
  }
}


function scroll_micro_left(product) {
var micro = new Array();

//  left_arw_img = "left_arw" + product;
  left_arw_img = "left_dbl_arw" + product;
  left_arw = document.getElementById(left_arw_img);
  if (left_arw.src.search(/disable/) == -1) {
    cur_product = product;
    cur = cur_micro[product];
    for (var i=0; i < max_visible_examples+1; i++) {
        micro[i] = document.getElementById(micro_divs[product][cur+(i-1)]);
    }
    if (micro[0]) {
      if (parseInt(micro[1].style.left) < (max_micro_width + macro_spacing)) {
        for (var i=0; i < max_visible_examples+1; i++) {
            micro[i].style.left = parseInt(micro[i].style.left) + 5 + "px";
        }
        setTimeout("scroll_micro_left(cur_product)",1);
      }
      else  {
        if (cur == 1) {
          disable_arws(cur_product, 'left');
        }
        if (cur <= (num_examples[product] - max_visible_examples)) {
          restore_arws(cur_product, 'right');
        }
        cur_micro[product]--;
      }
    }
  }
}


function scroll_micro_right_page(product) {
  cur_product = product;

  num_images = (num_examples[product]) - (cur_micro[product]) - max_visible_examples;

  if (num_images > max_visible_examples) {
    num_images = max_visible_examples;
  }

  for (var i=1; i<=num_images; i++) {
    scroll_micro_right(cur_product);
 }
}

function scroll_micro_left_page(product) {

  cur_product = product;

  num_images = (cur_micro[product]);

  if (num_images > max_visible_examples) {
    num_images = max_visible_examples;
  }

  for (var i=1; i<=num_images; i++) {
    scroll_micro_left(cur_product);
 }
}

function show_prod_info(info) {
  info_txt = document.getElementById(info);
  info_txt.style.visibility='visible'; 
}

function hide_prod_info(info) {
  info_txt = document.getElementById(info);
  info_txt.style.visibility='hidden';
}

function change_thumb(thumb, new_thumb) {

  thumb.src= new_thumb;
}


function FP_swapImg() {
 var doc=document,args=arguments,elm,n; doc.$imgSwaps=new Array(); for(n=2; n<args.length;
 n+=2) { elm=FP_getObjectByID(args[n]); if(elm) { doc.$imgSwaps[doc.$imgSwaps.length]=elm;
 elm.$src=elm.src; elm.src=args[n+1]; } }
}

function FP_preloadImgs() {
 var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();
 for(var i=0; i<a.length; i++) { d.FP_imgs[i]=new Image; d.FP_imgs[i].src=a[i]; }
}

function FP_getObjectByID(id,o) {
 var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);
 else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;
 if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)
 for(n=0; n<c.length; n++) { el=FP_getObjectByID(id,c[n]); if(el) return el; }
 f=o.forms; if(f) for(n=0; n<f.length; n++) { els=f[n].elements;
 for(m=0; m<els.length; m++){ el=FP_getObjectByID(id,els[n]); if(el) return el; } }
 return null;
}

// get center of window (multi-browser safe)
function getWindowCenterXY() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  return [myWidth, myHeight];
}

// determine scrolling (multi-browser safe)
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

var popup = new Object()
// not tested
function CenterPopup(URL, width, height)
{
  // Window dimensions:
  var theWidth, theHeight;
  if (window.innerWidth)
      theWidth=window.innerWidth;
  else if (document.documentElement && document.documentElement.clientWidth)
      theWidth=document.documentElement.clientWidth;
  else if (document.body)
      theWidth=document.body.clientWidth;

  if (window.innerHeight)
      theHeight=window.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight)
      theHeight=document.documentElement.clientHeight;
  else if (document.body)
      theHeight=document.body.clientHeight;

  // Window origin
  var originX, originY;
  if (window.innerHeight) {
      originX=screenX+(window.outerWidth-window.innerWidth);
      originY=screenY+(window.outerHeight-window.innerHeight);
  }
  else {
      originX=screenLeft;
      originY=screenTop;
  }

  var X = theWidth/2;
  var Y = theHeight/2;
  var features='"scrollbars=0, ' +
      'width=' + width + ', ' +
      'height=' + height + ', ' +
      'top=' + parseInt(originY + (Y - (height/2))) + ', ' + //
      'left=' + parseInt(originX + (X - (width/2))) + '"';
  popup = window.open(URL, "PopUp", features);
  popup.focus()
}


//SuckerTree Vertical Menu 1.1 (Nov 8th, 06)
//By Dynamic Drive: http://www.dynamicdrive.com/style/

//Enter id(s) of SuckerTree UL menus, separated by commas
var menuids=["productsMenuTree", "ideacenterTree"]

function buildsubmenus(){

  for (var i=0; i<menuids.length; i++){
    var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul");
      for (var t=0; t < ultags.length; t++){
          ultags[t].parentNode.getElementsByTagName("a")[0].className="subfolderstyle";

  		if (ultags[t].parentNode.parentNode.id==menuids[i]) {
              //if this is a first level submenu
              //dynamically position first level submenus to be width of main menu item
  			ultags[t].style.left=ultags[t].parentNode.offsetWidth+"px";
          } else {
            //else if this is a sub level submenu (ul)
            //position menu to the right of menu item that activated it
  		  ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px";
          }

          ultags[t].parentNode.onmouseover=function(){
              this.getElementsByTagName("ul")[0].style.display="block";
              }

          ultags[t].parentNode.onmouseout=function(){
              this.getElementsByTagName("ul")[0].style.display="none";
              }
      }
          //loop through all sub menus again, and use "display:none" to hide menus (to prevent possible page scrollbars
      for (var t = ultags.length-1; t > -1; t--){
  	   	ultags[t].style.display="none";
      	ultags[t].style.visibility="visible";
  	}
  }
}

if (window.addEventListener)
    window.addEventListener("load", buildsubmenus, false)
else if (window.attachEvent)
    window.attachEvent("onload", buildsubmenus)

