// Premium Workwear website functions - including menu controls and image zoomer
// 091112

var stayopen = '';
var currentmenu = '';
var menuID = '';
var posleft = 0;
var postop = 0;

function openMenu(menuID, menuTabID, offset) {
  if (currentmenu != '') { stayopen = 0; closeMenu(); }
  if (document.getElementById) { var element = document.getElementById(menuID).style }
  else if (document.all) { var element = document.all[menuID].style }
  else if (document.layers) { var element = document.layers[menuID] }

  if (document.getElementById) { var tab = document.getElementById(menuTabID) }
  else if (document.all) { var tab = document.all[menuTabID] }
  else if (document.layers) { var tab = document.layers[menuTabID] }

  findPos(tab);

  var setleft = posleft;
  var settop = postop;
  element.left = (setleft-4+offset)+'px';
  element.top = (settop+32)+'px';

  if (offset == -1) {
    // Align right-hand-side sub menu
    if (document.getElementById) { var br = document.getElementById('sbr6') }
    else if (document.all) { var br = document.all['sbr6'] }
    else if (document.layers) { var br = document.layers['sbr'] }
    findPos(br);
    offset = -(posleft - setleft - 146 + 25);
    if (offset > 0) { offset = 0 }
    element.left = (setleft-4+offset)+'px';
    }

  element.visibility = 'visible';
  stayopen = 1;
  currentmenu = menuID;
}

function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
  curleft = obj.offsetLeft;
  curtop = obj.offsetTop;
  while (obj = obj.offsetParent) {
    curleft += obj.offsetLeft;
    curtop += obj.offsetTop;
  }
}
posleft = curleft;
postop = curtop;
}

function closeMenu() {
  if (stayopen == 1) { setTimeout("closeMenu()", 1000); return; }
  if (!currentmenu) { return }

  if (document.getElementById) { var element = document.getElementById(currentmenu).style }
  else if (document.all) { var element = document.all[currentmenu].style }
  else if (document.layers) { var element = document.layers[currentmenu] }

  element.visibility = 'hidden';
  currentmenu = '';
}

function printMenustart(menuname) {
document.write('<table id="' + menuname + '" style="position: absolute; top: 163px; left: 200px; visibility: hidden; background: none; z-index: 2; border: none" onMouseOver="stayopen=1" onMouseOut="stayopen=0" cellpadding=0 cellspacing=0 border=0>');
document.write('<tr><td width=2 bgcolor=#3b4235><img src="images/mask.gif" width=2 height=2 border=0></td><td width=2 bgcolor=#ffffff><img src="images/mask.gif" width=2 height=2 border=0></td>');
document.write('<td><table cellpadding=0 cellspacing=0 border=0>');
}

function printMenuitem(link, item, styleCode) {
document.write("<tr><td class=submenu"+styleCode+" nowrap onClick=\"top.location='"+link+"'\" onMouseOver=\"stayopen=1; this.className='submenuHi"+styleCode+"'\" onMouseOut=\"this.className='submenu"+styleCode+"'\">&nbsp;<a href=\""+link+"\" class=menu>" + item + "</a>&nbsp;</td></tr>\n");
}

function printMenuend(styleCode) {
document.write('</table></td><td width=2 bgcolor=#ffffff><img src="images/mask.gif" width=2 height=2 border=0></td><td width=2 bgcolor=#3b4235><img src="images/mask.gif" width=2 height=2 border=0></td></tr>');
document.write('<tr height=10><td colspan=5><table cellpadding=0 cellspacing=0 border=0 width=100%><tr height=10><td width=10><img src="menu/sbl'+styleCode+'.gif" width=10 height=10 border=0></td>');
document.write('<td background="menu/sb'+styleCode+'.gif"><img src="images/mask.gif" width=10 height=10 border=0></td>');
document.write('<td width=10><img id="sbr'+styleCode+'" src="menu/sbr'+styleCode+'.gif" width=10 height=10 border=0></td></tr></table></td></tr>');
document.write("</table>");
}

// Pop-up
function popup(mylink, windowname, width, height) {
if (!window.focus) { return true }
var href;
if (typeof(mylink) == 'string') { href=mylink }
else { href=mylink.href }
window.open(href, windowname, 'width='+width+',height='+height+',status=no,scrollbars=yes,resizable=yes');
return false;
}

function popup(mylink, windowname, width, height)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width='+width+',height='+height+',status=no,scrollbars=yes,resizable=yes');
return false;
}

// Image Zoomer
var mx = 0;
var my = 0;
var imgx = 0;
var imgy = 0;
var imgw = 0;
var imgh = 0;
var onhilight = 0;

// Update value of these before use
var zimgw = 0;
var zimgh = 0;
var zoomdivw = 300;
var zoomdivh = 300;
var zoomdivmidx = 0;
var zoomdivmidy = 0;
var scale = 1;

// Activate zoomer
function zoomOn() {
imgw = mainimg.width;
imgh = mainimg.height;

scale = zimgw / imgw; // Calculate scale

// Position zoomer
findPos(mainimg);
imgx = posleft;
imgy = postop;
zoomer.style.left=imgx + imgw + 5 + 'px';
zoomer.style.top=imgy+'px';
zoomdivh = 350; // Set preferred zoom div height
if (zoomdivh<300 && imgh>298) { zoomdivh = 300 }
if (zoomdivh>(zimgh-2)) { zoomdivh = zimgh-2 }
zoomdivmidy = zoomdivh / 2;
zoomdivw = parseInt(zoomer.style.width);
if (zoomdivw > zimgw) { zoomdivw = zimgw }
if (zoomdivw > 415) { zoomdivw = 415 }
zoomdivmidx = zoomdivw / 2;
zoomer.style.width=zoomdivw + 'px';
zoomer.style.height=zoomdivh + 'px';
zoomer.style.visibility='visible'; // Show zoom image
hilight.style.width = Math.floor(zoomdivw / scale) - 2 + 'px';
hilight.style.height = Math.floor(zoomdivh / scale) - 2 + 'px';
hilight.style.visibility='visible'; // Show hilight on main image
}

// Deactivate zoomer
function zoomOff() {
if (onhilight) { return }
zoomer.style.visibility='hidden';
hilight.style.visibility='hidden';
}

function zoomPic(e) {
// Update view of zoomed image

// Get images sizes, calculate scale

// Get mouse position, main image position and cursor offset
mousePos(e);

var coffx = mx - imgx;
var coffy = my - imgy;

// Calculate background offset values
var zoffx = Math.floor(-coffx * scale) + zoomdivmidx;
var zoffy = Math.floor(-coffy * scale) + zoomdivmidy;

if (zoffx > 0) { zoffx = 0 }
else if (zoffx < -zimgw + zoomdivw) { zoffx = -zimgw + zoomdivw }
if (zoffy > 0) { zoffy = 0 }
else if (zoffy < -zimgh + zoomdivh) { zoffy = -zimgh + zoomdivh }

var hix = Math.floor(mx - (parseInt(hilight.style.width) / 2));
var hiy = Math.floor(my - (parseInt(hilight.style.height) / 2));

if (hix > imgx + imgw - parseInt(hilight.style.width) - 2) { hix = imgx + imgw - parseInt(hilight.style.width) - 2 }
else if (hix < imgx) { hix = imgx }
if (hiy > imgy + imgh - parseInt(hilight.style.height) - 2) { hiy = imgy + imgh - parseInt(hilight.style.height) - 2 }
else if (hiy < imgy) { hiy = imgy }

// Show crop of main image in zoomer div
zoomer.style.background='url('+zoomimg+')';
zoomer.style.backgroundRepeat='no-repeat';
zoomer.style.backgroundAttachment='scroll';
zoomer.style.backgroundPosition=zoffx+'px '+zoffy+'px';

hilight.style.left = hix+'px';
hilight.style.top = hiy+'px';
}

function mousePos(e) {
// Put the mouse position relative to the document in mx and my
e = e || window.event;
if (e.pageX || e.pageY) {
  mx = e.pageX;
  my = e.pageY;
}
else if (e.clientX || e.clientY) {
  mx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  my = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  }
}

function registerEventHandler(node, event, handler) {
if (typeof node.addEventListener == "function") { node.addEventListener(event, handler, false) }
else { node.attachEvent("on" + event, handler) }
}
