// Global variables
var imageLoadFlag = 0;
var imageCache = new Array();
var menuLoadFlag = 0;
var menuCount = 0;
var onMenu = 0;
var winW, winH;
var mT = 0;
var mF = "";
var htBuffer = "";

// Defines Y-coordinate for menu
var m_x, m_y;


// Simple browser detection (seperate into:  NS4.x, IE4.x, DOCOM[5+] browsers)
var ns = (navigator.appName == 'Netscape');
var gen3 = (parseInt(navigator.appVersion) >= 3 && parseInt(navigator.appVersion) < 4);
var ns4 = (ns && parseInt(navigator.appVersion) >= 4 && parseInt(navigator.appVersion) < 5);
var ie = (navigator.appName == 'Microsoft Internet Explorer');
var ie3 = (ie && navigator.appVersion < 4);
var ie4 = (ie && parseInt(navigator.appVersion) >= 4);
var docom = (document.getElementById);
var win2k = (navigator.userAgent.indexOf('Windows NT 5.0') != -1);
var ie4mac = (ie && !docom && navigator.userAgent.indexOf('Macintosh') != -1);
var dhtmlBrowser = (ie4 || ns4 || docom);
var win31 = (navigator.appVersion.indexOf("Win16") != -1)
var SHOW, HIDE;


if (ie4 || docom) {
  SHOW = "visible";
  HIDE = "hidden";
} else if (ns4) {
  SHOW = "show";
  HIDE = "hide";
}

// Capture mouse coordinates
function hMsMv(e) {
  m_x = (ns) ? e.pageX : event.clientX + document.body.scrollLeft;
  m_y = (ns) ? e.pageY : event.clientY + document.body.scrollTop;
  return false;
}


function moveThumb() {
  if (thumbTop < 67)
    thumbTop = 67;
  else if (thumbTop > 197)
    thumbTop = 197;
  eleYMove('cThumb', thumbTop);
  scrollTop = (thumbTop - 68) / scrollTicks;
  cScrollTo(scrollTop);
}

function dragScroll()
{

  if (dragt > 0) {
    if (distYt == 0)
      distYt = m_y - thumbTop;
    thumbTop = m_y - distYt;
    moveThumb();
  }

  st = setTimeout("dragScroll()",30);
  return false;
}

function clingScroll()
{
  thumbTop = m_y;
  moveThumb();
}



// writes the HTML for the Floating Menus

function generateMenu() {
 if (dhtmlBrowser) {
  var dU = generateMenu.arguments[0];
  var dN = "lT" + dU;
  document.write('<DIV ID="' + dN + '"><IMG SRC="/images/spacer.gif" WIDTH=125 HEIGHT=1 ALT=""><BR><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=125>');
  for (var divReps = 1; divReps < generateMenu.arguments.length; divReps++) {
    document.write('<TR><TD VALIGN="TOP"><IMG SRC="images/co1_dot.gif" WIDTH=16 HEIGHT=13 ALT=""></TD>');
    document.write('<TD><SPAN CLASS="lM"><A CLASS="lM" onMouseOver="clearTimeout(mT);" onMouseOut="menuTimeOff(' + generateMenu.arguments[divReps] + '</SPAN></TD></TR>');
  }
  document.write('</TABLE></DIV>');
  if (!ie4 && docom) document.write('<DIV ID="lM' + dU + '"></DIV>');
  menuCount++;
  if (menuCount == 3) menuLoadFlag = 1;
 }
}

function finishMenu(mId) {
 var dH;
 var dN = "lT" + mId;
 dH = eleHeight(dN);
 dN = "lM" + mId;

 if (ie4 || ns4) {
   var offM = dH - ((mId == 6) ? 34 : 14);
   if (offM < 1) offM = 1;
   document.write('<DIV ID="' + dN + '"><IMG SRC="images/co1_' + ((mId == 7) ? "bg" : "") + 'navtop.gif" WIDTH=150 HEIGHT=' + ((mId == 7) ? "41" : "28") + '><BR>');
   document.write('<IMG SRC="images/co1_navback.gif" WIDTH=150 HEIGHT=' + offM + '><BR><IMG SRC="images/co1_navbottom.gif" WIDTH=150 HEIGHT=10></DIV>');
   menuTrans(dN, 85);
 } else if (docom) {
   if (mId == 7) {   // modify for large menu
     writeImageToElement(dN, "images/co1_navback.gif", 150, 41);
     dH -= 12;
   } else
     writeImageToElement(dN, "images/co1_navtop.gif", 170, 28);
   writeImageToElement(dN, "images/co1_navtop.gif", 150, dH - 14);
   writeImageToElement(dN, "images/co1_navbottom.gif", 150, 10);
   // menuTrans(dN, 85);
 }

 if (mId == 3) menuLoadFlag = 1;

}

function menuSwap(imNum, aMem, vizProp) {
  if (menuLoadFlag && imageLoadFlag) {
    if (vizProp == SHOW) {
      if (mF) {
        eval(mF);
        clearTimeout(mT);
      }
      menuTrans('l' + imNum,85);
    } else
      menuTrans('l' + imNum,100);
    mF = "";
    var imName = "lN" + imNum;
    if (ns4) eval('document.l' + imNum + '.document[imName].src = imageCache[aMem].src');
    else document[imName].src = imageCache[aMem].src;
    if (ie && win2k) {  // Windows 2000 IE fix -- will not correctly hide semi-transparent elements
      if (vizProp == SHOW) { menuTrans('lM' + imNum,85); }
      else { menuTrans('lM' + imNum,100); }
    }
    vizElement('lM' + imNum, vizProp);
    vizElement('lT' + imNum, vizProp);
    onMenu = imNum;
  }
}

function menuTrans(mId, grade) {
  if (ie4 && !ie4mac) {
    eval('document.all.' + mId + '.style.filter="alpha(opacity=' + grade + ')"');
  }
}

function menuTimeOff(imNum, aMem) {
  if (menuLoadFlag && imageLoadFlag) {
    mF = "menuSwap(" + imNum + ", " + aMem + ", HIDE)";
    mT = setTimeout(mF, 2000);
  }
}


function eleHeight(eleId)
{
   if (ie4)
     return eval('document.all.' + eleId + '.offsetHeight');
   else if (ns4)
     return eval('document.' + eleId + '.document.height');
   else if (docom)
     return document.getElementById(eleId).offsetHeight;
}

function eleClip(eleId, x0, y0, x1, y1)
{
   if (ns4) {
     var ele = eval('document.' + eleId);
     ele.clip.top = y0;
     ele.clip.right = x1;
     ele.clip.bottom = y1;
     ele.clip.left = x0;
   } else if (ie4) {
     clipStr = 'rect(' + y0 + ',' + x1 + ',' + y1 + ',' + x0 + ')';
     if (ie4) ele = eval('document.all.' + eleId + '.style.clip = "' + clipStr + '"');
   } else if (docom) document.getElementById(eleId).style.clip = 'rect(' + y0 + ',' + x1 + ',' + y1 + ',' + x0 + ')';

}

function eleYMove(eleId, offY)
{
  if (ns4) eval('document.' + eleId + '.top = ' + (offY));
  else if (ie4) eval('document.all.' + eleId + '.style.top = ' + offY);
  else if (docom) document.getElementById(eleId).style.top = offY;
}

function vizElement(eleId, vizProp)
{
  if (docom) document.getElementById(eleId).style.visibility = vizProp;
  else if (ie4) eval('document.all.' + eleId + '.style.visibility = "' + vizProp + '"');
  else if (ns4) eval('document.' + eleId + '.visibility = "' + vizProp + '"');
}

// Abstract function to handle DOM and 4.0 browser gap
function writeImageToElement(eleId, imPath, imW, imH) {
  var ele = document.getElementById(eleId);
  var imRef = document.createElement("IMG");
  imRef.setAttribute("src", imPath);
  imRef.setAttribute("width", imW);
  imRef.setAttribute("height", imH);
  ele.appendChild(imRef);
  var brRef = document.createElement("BR");
  ele.appendChild(brRef);
}

function cacheImages() {
 if (document.images) {
  for (var loadProg = 0; loadProg < cacheImages.arguments.length; loadProg++) {
    cI = cacheImages.arguments[loadProg];
    imageCache[loadProg] = new Image();
    imageCache[loadProg].src = 'images/co1_' + cI + '.gif';
  }
 imageLoadFlag = 1;
 }
}

function setup() {
 if (dhtmlBrowser) {
  cacheImages('home', 'home_on', 'customer_services', 'customer_services_on', 'signup', 'signup_on', 'faq', 'faq_on','company_info', 'company_info_on', 'clothing_care','clothing_care_on');

  if (!ie4 && docom) {
    document.getElementById('cScrt').style.left = -200;
    document.getElementById('cThumb').style.left = -200;
    for (var i = 0; i < menuCount; i++)
      finishMenu(i);
  }

  if (document.all) this.focus();
  if (ns4) {
    winW = window.innerWidth;
    winH = window.innerHeight;
    window.onresize = reDraw;
  }

 }
}

function reDraw() {
  if (ns4 && (winW!=window.innerWidth || winH!=window.innerHeight))
    history.go(0);
}

window.onload = setup;
window.offscreenBuffering = true;
if (dhtmlBrowser) {
  document.onmousemove = hMsMv;
  if (ns4) document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
}
