// Functions for Team Direct website including: menu controls, image zoomer

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

function showMenu(menuTabID, menuID) {
  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);

  element.left = (posleft+188)+'px';
  element.top = (postop-3)+'px';

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

function hideMenu(menuID) {
  stayopen = 0;
  if (stayopen == 1) { setTimeout("hideMenu()", 1000); return; }
  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]
  }

  setTimeout("closeMenu()", 1000);
  element.stayopen = 0;
}

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 = '';
}


// Block right clicking
var clickmessage='Note that all images and product photos are copywright Team Direct.\nPlease contact us if you wish to use any of our images.';

function disableclick(e) {
  if (document.all) {
    if (event.button==2||event.button==3) {
      if (event.srcElement.tagName=='IMG'){ alert(clickmessage); return false; }
    }
  } else if (document.layers) {
    if (e.which == 3) { alert(clickmessage); return false; }
  } else if (document.getElementById) {
    if (e.which==3&&e.target.tagName=='IMG'){ alert(clickmessage); return false; }
  }
}

function associateimages() {
  for(i=0; i<document.images.length;i++) { document.images[i].onmousedown=disableclick; }
}

if (document.all) { document.onmousedown=disableclick }
else if (document.getElementById) { document.onmouseup=disableclick }
else if (document.layers) { associateimages() }

var message='';

function clickIE() { if (document.all) { (message); return false;} }

function clickNS(e) {
if (document.layers||(document.getElementById&&!document.all))
  {
  if (e.which==2||e.which==3) { (message); return false; }
  }
}

if (document.layers) {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS; }
else { document.onmouseup=clickNS;document.oncontextmenu=clickIE; }

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;
}

// Zoomer variables
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;

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 = imgh - 2; // Match main image height
if (zoomdivh<300) { zoomdivh = 300 }
zoomdivmidy = zoomdivh / 2;
zoomdivw = parseInt(zoomer.style.width);
if (zoomdivw > zimgw) { zoomdivw = zimgw }
zoomdivmidx = zoomdivw / 2;
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
}

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) }
}

function popup(mylink, windowname, width, height)
{
window.focus();
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;
}

// Data for Team Direct Menu

function printMenu() {
document.write('<table cellpadding=5 cellspacing=0 border=0 width=190>');
document.write('<a id="menu6" href="Football-Team-Kits---Nike" class=menulink onMouseOver="showMenu(\'menu6\', \'sub6\')" onMouseOut="hideMenu(\'sub6\')">Football Team Kits - Nike</a>');
document.write('<a id="menu12" href="Basketball-Team-Kits---Nike" class=menulink onMouseOver="showMenu(\'menu12\', \'sub12\')" onMouseOut="hideMenu(\'sub12\')">Basketball Team Kits - Nike</a>');
document.write('<a id="menu7" href="Football-Team-Kits---Prostar" class=menulink onMouseOver="showMenu(\'menu7\', \'sub7\')" onMouseOut="hideMenu(\'sub7\')">Football Team Kits - Prostar</a>');
document.write('<a id="menu13" href="Rugby-Team-Kits---Nike" class=menulink onMouseOver="showMenu(\'menu13\', \'sub13\')" onMouseOut="hideMenu(\'sub13\')">Rugby Team Kits - Nike</a>');
document.write('<a id="menu14" href="Running---Nike-" class=menulink onMouseOver="showMenu(\'menu14\', \'sub14\')" onMouseOut="hideMenu(\'sub14\')">Running - Nike </a>');
document.write('<a id="menu3" href="Clothing-and-Textiles" class=menulink onMouseOver="showMenu(\'menu3\', \'sub3\')" onMouseOut="hideMenu(\'sub3\')">Clothing & Textiles</a>');
document.write('<a id="menu1" href="Accessories" class=menulink onMouseOver="showMenu(\'menu1\', \'sub1\')" onMouseOut="hideMenu(\'sub1\')">Accessories</a>');
document.write('<a id="menu8" href="Bags-and-Wallets" class=menulink onMouseOver="showMenu(\'menu8\', \'sub8\')" onMouseOut="hideMenu(\'sub8\')">Bags & Wallets</a>');
document.write('<a id="menu10" href="Sports-Equipment" class=menulink onMouseOver="showMenu(\'menu10\', \'sub10\')" onMouseOut="hideMenu(\'sub10\')">Sports Equipment</a>');

document.write('</table>');
return true;
}

function printSubs() {
document.write('<div id="sub6" style="position: absolute; left: 0px; top: 0px; padding: 3px; visibility: hidden; z-index: 2" onMouseOver="stayopen=1" onMouseOut="stayopen=0">');
document.write('<a href="Football-Team-Kits---Nike/Base-Layers-2010" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Base Layers 2010</a>');
document.write('<a href="Football-Team-Kits---Nike/Goalkeeper-2010" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Goalkeeper 2010</a>');
document.write('<a href="Football-Team-Kits---Nike/Jerseys-2010" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Jerseys 2010</a>');
document.write('<a href="Football-Team-Kits---Nike/Shorts-2010" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Shorts 2010</a>');
document.write('<a href="Football-Team-Kits---Nike/Socks-2010" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Socks 2010</a>');
document.write('<a href="Football-Team-Kits---Nike/Training-Wear---Club" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Training Wear - Club</a>');
document.write('<a href="Football-Team-Kits---Nike/Training-Wear---Team" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Training Wear - Team</a>');
document.write('<a href="Football-Team-Kits---Nike/Training-Wear_Federation" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Training Wear-Federation</a>');
document.write('</div>');
document.write('<div id="sub12" style="position: absolute; left: 0px; top: 0px; padding: 3px; visibility: hidden; z-index: 2" onMouseOver="stayopen=1" onMouseOut="stayopen=0">');
document.write('<a href="Basketball-Team-Kits---Nike/Alley-Hoop-Kit" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Alley Hoop Kit</a>');
document.write('<a href="Basketball-Team-Kits---Nike/Gold-Kit" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Gold Kit</a>');
document.write('<a href="Basketball-Team-Kits---Nike/Mid_Air-Kit" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Mid-Air Kit</a>');
document.write('<a href="Basketball-Team-Kits---Nike/Reversible-Kit" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Reversible Kit</a>');
document.write('<a href="Basketball-Team-Kits---Nike/Training-Wear" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Training Wear</a>');
document.write('</div>');
document.write('<div id="sub7" style="position: absolute; left: 0px; top: 0px; padding: 3px; visibility: hidden; z-index: 2" onMouseOver="stayopen=1" onMouseOut="stayopen=0">');
document.write('<a href="Football-Team-Kits---Prostar/Base-Layers" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Base Layers</a>');
document.write('<a href="Football-Team-Kits---Prostar/Goalkeeper" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Goalkeeper</a>');
document.write('<a href="Football-Team-Kits---Prostar/Jerseys" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Jerseys</a>');
document.write('<a href="Football-Team-Kits---Prostar/Officials" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Officials</a>');
document.write('<a href="Football-Team-Kits---Prostar/Rain-Wear" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Rain Wear</a>');
document.write('<a href="Football-Team-Kits---Prostar/Shorts" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Shorts</a>');
document.write('<a href="Football-Team-Kits---Prostar/Socks" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Socks</a>');
document.write('<a href="Football-Team-Kits---Prostar/Training-Wear" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Training Wear</a>');
document.write('</div>');
document.write('<div id="sub13" style="position: absolute; left: 0px; top: 0px; padding: 3px; visibility: hidden; z-index: 2" onMouseOver="stayopen=1" onMouseOut="stayopen=0">');
document.write('<a href="Rugby-Team-Kits---Nike/Game-Socks" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Game Socks</a>');
document.write('<a href="Rugby-Team-Kits---Nike/Jerseys-and-Tops" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Jerseys & Tops</a>');
document.write('<a href="Rugby-Team-Kits---Nike/Shorts-and-Pants" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Shorts & Pants</a>');
document.write('</div>');
document.write('<div id="sub14" style="position: absolute; left: 0px; top: 0px; padding: 3px; visibility: hidden; z-index: 2" onMouseOver="stayopen=1" onMouseOut="stayopen=0">');
document.write('<a href="Running---Nike-/Jackets" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Jackets</a>');
document.write('<a href="Running---Nike-/Shorts" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Shorts</a>');
document.write('<a href="Running---Nike-/Tops" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Tops</a>');
document.write('<a href="Running---Nike-/Trousers" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Trousers</a>');
document.write('<a href="Running---Nike-/Underwear" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Underwear</a>');
document.write('</div>');
document.write('<div id="sub3" style="position: absolute; left: 0px; top: 0px; padding: 3px; visibility: hidden; z-index: 2" onMouseOver="stayopen=1" onMouseOut="stayopen=0">');
document.write('<a href="Clothing-and-Textiles/Headwear" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Headwear</a>');
document.write('<a href="Clothing-and-Textiles/Misc" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Misc</a>');
document.write('<a href="Clothing-and-Textiles/Shorts" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Shorts</a>');
document.write('<a href="Clothing-and-Textiles/Socks" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Socks</a>');
document.write('<a href="Clothing-and-Textiles/Sweatshirts" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Sweatshirts</a>');
document.write('<a href="Clothing-and-Textiles/Swimwear" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Swimwear</a>');
document.write('<a href="Clothing-and-Textiles/Tshirts" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Tshirts</a>');
document.write('</div>');
document.write('<div id="sub1" style="position: absolute; left: 0px; top: 0px; padding: 3px; visibility: hidden; z-index: 2" onMouseOver="stayopen=1" onMouseOut="stayopen=0">');
document.write('<a href="Accessories/Balls" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Balls</a>');
document.write('<a href="Accessories/Boot-Care" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Boot Care</a>');
document.write('<a href="Accessories/Equipment" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Equipment</a>');
document.write('<a href="Accessories/Protection-and-Supports" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Protection & Supports</a>');
document.write('</div>');
document.write('<div id="sub8" style="position: absolute; left: 0px; top: 0px; padding: 3px; visibility: hidden; z-index: 2" onMouseOver="stayopen=1" onMouseOut="stayopen=0">');
document.write('<a href="Bags-and-Wallets/Bags" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Bags</a>');
document.write('<a href="Bags-and-Wallets/Wallets" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Wallets</a>');
document.write('</div>');
document.write('<div id="sub10" style="position: absolute; left: 0px; top: 0px; padding: 3px; visibility: hidden; z-index: 2" onMouseOver="stayopen=1" onMouseOut="stayopen=0">');
document.write('<a href="Sports-Equipment/Clothing" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Clothing</a>');
document.write('<a href="Sports-Equipment/Equipment" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Equipment</a>');
document.write('<a href="Sports-Equipment/Nets-and-Posts" class=menulink onMouseOver="stayopn=1" onMouseOut="stayopen=0">Nets & Posts</a>');
document.write('</div>');

}

function jumpto(jumplink) {
top.location='http://www.teamdirect.co.uk/'+jumplink;
}
