function addEvent(obj, type, fn) {
  if (obj.addEventListener) {
    switch (obj.tagName) { 
    case 'body':
      var mappings = { mousewheel: 'DOMMouseScroll', load: 'DOMContentLoaded' };
    default:
      var mappings = { mousewheel: 'DOMMouseScroll' };
    };

    if (mappings[type]) {
      type = mappings[type];
    };
    obj.addEventListener( type, fn, false );
  } else if (obj.attachEvent) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
    obj.attachEvent( "on"+type, obj[type+fn] );
  }
}

function cancelBubble(e) {
  if (e.stopPropagation) {
    e.stopPropagation();
  } else {
    e.cancelBubble = true;
  };
}

function cancelEvent(e) {
  if (e.preventDefault) { 
    e.preventDefault();
  } else {
    e.returnValue = false;
  };
}

function getEventTarget(e) {
  var target;
  if (e.target) {
    target = e.target;
  } else if (e.srcElement) {
    target = e.srcElement;
  };
 
  if (target.nodeType == 3) { // fix for Safari bug
    target = target.parentNode;  
  };

  return target;
}

function removeEvent(obj, type, fn) {
  if (obj.removeEventListener) {
    if (type == 'mousewheel') {
      type = 'DOMMouseScroll';
    };

    obj.removeEventListener( type, fn, false );
  } else if (obj.detachEvent) {
    obj.detachEvent( "on"+type, obj[type+fn] );
    obj[type+fn] = null;
    obj["e"+type+fn] = null;
  }
};

Array.prototype.each = function(callback) {
  for (var i=0; i<this.length; i++) {
    callback(this[i], i);
  };
}

function getElementComputedStyle(element, property) {
  if (document.defaultView && document.defaultView.getComputedStyle) {
    return getElementComputedStyleMozilla(element, property);
  }
 
  // external stylesheet for Explorer and Opera 9
  if (element.currentStyle) {
    return getElementComputedStyleIE(element, property);
  }
 
  return "";
}

function getElementComputedStyleIE(element, property) {
  return element.currentStyle[propertyCssToPropertyDom(property)];
}

function getElementComputedStyleMozilla(element, property) {
  return document.defaultView.getComputedStyle(element, "").getPropertyValue(property);
}

function propertyCssToPropertyDom(property) {
  var i;
  while ((i = property.indexOf("-")) != -1) {
    property = property.substr(0, i) + property.substr(i+1,1).toUpperCase() + property.substr(i+2);
  };
  return property;
}

var Class = {};

Class.extend = function(subClass, baseClass) {
  function inheritance() {};
  inheritance.prototype = baseClass.prototype;

  subClass.prototype = new inheritance();
  subClass.prototype.constructor = subClass;
  subClass.baseConstructor = baseClass;
  subClass.superClass = baseClass.prototype;
}

Class.extend(FeaturePasswordHelpValue, Object);

function FeaturePasswordHelpValue(element, text) {
  this._cleared = false;
  this._element = element;
  this._overlay = this.createOverlay(element, text);
  this._text = text;
  this._type = element.type;

  var _this = this;
  addEvent(element, "focus", function(e) { _this.onFocus(e); });
  addEvent(element, "blur", function(e) { _this.onBlur(e); });

  // Forced clearing of the password field in Firefox
  setTimeout(function() { element.value = ""; }, 50);
}

FeaturePasswordHelpValue.prototype.createOverlay = function(element, text) {
  var overlay = document.createElement("div");
  overlay.className = "overlay_help";
  overlay.style.position = "absolute";
  overlay.style.top = (element.offsetTop + 1).toString() + "px";
  overlay.style.left = (element.offsetLeft + 1).toString() + "px";
  overlay.style.width = (element.offsetWidth - 6).toString() + "px";
  overlay.style.height = (element.offsetHeight - 6).toString() + "px";

  // Copy styles
  ["font-family", "font-size", "font-weight", "margin", "padding", "padding-left", "line-height"].each(function(property) {
    var value = getElementComputedStyle(element, property);
    overlay.style[propertyCssToPropertyDom(property)] = value;
  });  
  
  overlay.appendChild(document.createTextNode(text));
  element.offsetParent.appendChild(overlay);

  var _this = this;
  addEvent(overlay, "click", function(e) { _this.onOverlayClick(e); });
 
  return overlay;
}

FeaturePasswordHelpValue.prototype.hideOverlay = function() {
  this._overlay.style.display = "none";
}

FeaturePasswordHelpValue.prototype.showOverlay = function() {
  this._overlay.style.display = "block";
}

// CONTROLLER

FeaturePasswordHelpValue.prototype.onBlur = function(e) {
  if (!this._cleared) {
    if (this._element.value != '') {
      this._cleared = true;
    } else {
      this.showOverlay();
    };
  };
}

FeaturePasswordHelpValue.prototype.onFocus = function(e) {
  this.hideOverlay();
}

FeaturePasswordHelpValue.prototype.onOverlayClick = function(e) {
  this.hideOverlay();
  this._element.focus();
}

function isPNGHackNeeded() {
  var arVersion = navigator.appVersion.split("MSIE")
  var version = parseFloat(arVersion[1])
  return (version > 5.5 && version < 7.0 && document.body.filters);
}

// correctly handle PNG transparency in Win IE 5.5 & 6.
function correctPNG() {
  if (!isPNGHackNeeded()) { return; };

  for (var i=0; i<document.images.length; i++) {
    var img = document.images[i]
    var imgName = img.src.toUpperCase()
    if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
      if (correctPNGNode(img)) { i = i-1; };
    }
  }
}

function correctPNGNode(node) {
  if (!isPNGHackNeeded()) { return false; };

  switch (node.tagName) {
    case "IMG":
      return correctPNGNodeImg(node);
    case "INPUT":
      if (node.type != "image") { return false; };
      return correctPNGNodeInput(node);
    default:
      return false;
  };

  if (img.tagName != "IMG") {
    return false;
  };
}

function correctPNGNodeImg(img) {
  if (!isPNGHackNeeded()) { return false; };

  var width = (img.width || parseInt(img.currentStyle.width));
  var height = (img.height || parseInt(img.currentStyle.height)); 

  if (isNaN(width) || isNaN(height)) {
    return false;
  };  
 
  // Note the font-size: 1px; this prevents short images from being expanded to the current 
  // font size

  var newNode = document.createElement("span");
  newNode.id = img.id;
  newNode.className = img.className;
  newNode.title = img.title || img.alt;
  newNode.style.cssText = img.style.cssText;
  newNode.style.display = "inline-block";
  if (img.align == "left") { newNode.style.styleFloat = "left"; };
  if (img.align == "right") { newNode.style.styleFloat = "right"; };
  newNode.style.width = width.toString() + "px";
  newNode.style.height = height.toString() + "px";
  newNode.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale')";
  newNode.style.cursor = "pointer";
  newNode.style.fontSize = "1px";
  img.parentNode.replaceChild(newNode, img);

  return true;
}

function correctPNGNodeBg(div) {
  if (!isPNGHackNeeded()) { return false; };

  var fullSrc = div.currentStyle.backgroundImage;
  var src = fullSrc.match(/\(["'](.*)["']\)/)[1];
  div.style.backgroundImage = "none";  

  var overlay = document.createElement("div");
  overlay.style.position = "absolute";
  overlay.style.top = (div.offsetTop + 1).toString() + "px";
  overlay.style.left = (div.offsetLeft + 1).toString() + "px";
  overlay.style.width = (div.offsetWidth - 6).toString() + "px";
  overlay.style.height = (div.offsetHeight - 6).toString() + "px";
  overlay.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + src + "\', sizingMethod='crop')";
  overlay.style.zIndex = div.currentStyle.zIndex - 1;

  div.offsetParent.appendChild(overlay);

  return true;
}

function correctPNGNodeInput(input) {
  if (!isPNGHackNeeded()) { return false; };

  if (input.offsetWidth == 0) {
    return false;
  };  

  input.style.position = "relative";

  var src = input.src;

  var overlay = document.createElement("div");
  overlay.style.position = "absolute";
  overlay.style.top = (input.offsetTop + 1).toString() + "px";
  overlay.style.left = (input.offsetLeft + 1).toString() + "px";
  overlay.style.width = (input.offsetWidth - 6).toString() + "px";
  overlay.style.height = (input.offsetHeight - 6).toString() + "px";
  overlay.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + src + "\', sizingMethod='crop')";
  overlay.style.zIndex = input.currentStyle.zIndex - 1;

  input.offsetParent.appendChild(overlay);
  Opacity.set(input, 0);

  return true;
}

jQuery(document).ready(function() {
  correctPNG();

  if (document.getElementById("customer_login_password")) {
    new FeaturePasswordHelpValue(document.getElementById("customer_login_password"), "password");
  };

  jQuery("#menu > li").hover(function() { jQuery(this).addClass("hover"); }, function() { jQuery(this).removeClass("hover") });
  jQuery("#menu li ul li").hover(function() { jQuery(this).addClass("hover"); }, function() { jQuery(this).removeClass("hover") });

  jQuery("#customer_login_email").focus(function() { 
    if (document.getElementById("customer_login_email").value == "your e-mail address") {
      document.getElementById("customer_login_email").value = "";
    };
  });

  jQuery("#customer_login_email").blur(function() { 
    if (document.getElementById("customer_login_email").value == "") {
      document.getElementById("customer_login_email").value = "your e-mail address";
    };
  });
});
