// 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 Souvenirs Menu

function printMenu() {
document.write('<table cellpadding=5 cellspacing=0 border=0 width=190>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Arsenal-Merchandise\')"><a href="Football-Merchandise/Arsenal-Merchandise" class=menulink>Arsenal Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Barcelona-Merchandise\')"><a href="Football-Merchandise/Barcelona-Merchandise" class=menulink>Barcelona Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Celtic-Merchandise\')"><a href="Football-Merchandise/Celtic-Merchandise" class=menulink>Celtic Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Chelsea-Merchandise\')"><a href="Football-Merchandise/Chelsea-Merchandise" class=menulink>Chelsea Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/England-Merchandise\')"><a href="Football-Merchandise/England-Merchandise" class=menulink>England Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Everton-Merchandise\')"><a href="Football-Merchandise/Everton-Merchandise" class=menulink>Everton Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Headwear\')"><a href="Football-Merchandise/Headwear" class=menulink>Headwear</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Juventus-Merchandise\')"><a href="Football-Merchandise/Juventus-Merchandise" class=menulink>Juventus Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Liverpool-Merchandise\')"><a href="Football-Merchandise/Liverpool-Merchandise" class=menulink>Liverpool Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Man-City-Merchandise\')"><a href="Football-Merchandise/Man-City-Merchandise" class=menulink>Man City Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Man-Utd-Merchandise\')"><a href="Football-Merchandise/Man-Utd-Merchandise" class=menulink>Man Utd Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Newcastle-Merchandise\')"><a href="Football-Merchandise/Newcastle-Merchandise" class=menulink>Newcastle Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Real-Madrid-Merchandise\')"><a href="Football-Merchandise/Real-Madrid-Merchandise" class=menulink>Real Madrid Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/Spurs-Merchandise\')"><a href="Football-Merchandise/Spurs-Merchandise" class=menulink>Spurs Merchandise</a></td></tr>');
document.write('<tr height=25><td class=nav onMouseOver="this.className=\'navHi\'" onMouseOut="this.className=\'nav\'" onClick="jumpto(\'Football-Merchandise/West-Ham-Merchandise\')"><a href="Football-Merchandise/West-Ham-Merchandise" class=menulink>West Ham Merchandise</a></td></tr>');

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

function printSubs() {

}

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