 /* globals */
 var a_bodyOnClickHandlers = new Array(0);

 /* Handler for all onclicks */
 function a_bodyOnClickHandler(event) {
  var evt = (event!=null)?event:window.event;
  var i = 0;
  for (i=0; i<a_bodyOnClickHandlers.length; i++) {
    var ret = a_bodyOnClickHandlers[i](evt);
    // Need the null check since IE can do diff things
    // on true of false return
    if (ret!=null) {
      return ret;
    }
  }
}

 /*
  Add an global onclick handler. Your handler will recieve
  and event. It *Must* return 1 of three values
  - frue - Stop other handlers and let the event process
  - false - Stop other handlers and let the event NOT process
  - null - Let the other handlers process
*/
function a_addHandler(aFunction) {
  this.a_bodyOnClickHandlers.push(aFunction);
}

/*
  Spawn a popup. returns false to cancel the click.
  Usage:
  DEFAULT
    <a href="wacky.html"
       onclick="return a_popup(this)">
      Foo
    </a>
  Set width/height
    <a href="wacky.html"
       onclick="return a_popup(this, 200, 300)">
      Foo
    </a>

  Custom style - not resizeable
    <a href="wacky.html"
       onclick="return a_popup(this, 200, 300, 'titlebar,scrollbars=yes,resizable=no')">
      Foo
    </a>

  Custom style - and text
    <a href="wacky.html"
       onclick="return a_popup(this, 200, 300, 'titlebar,scrollbars=yes,resizable=no', 'doh')">
      Foo
    </a>
*/
function a_popup() {
  var a_element=null;
  var a_width=null;
  var a_height=null;
  var a_windowStyle=null;
  var a_customText=null;

  switch (a_popup.arguments.length) {
    case 5:
      a_customText = a_popup.arguments[4];
    case 4:
      a_windowStyle = a_popup.arguments[3];
    case 3:
      a_height = a_popup.arguments[2];
    case 2:
      a_width = a_popup.arguments[1];
    case 1:
      a_element = a_popup.arguments[0];
    default:
  }

  var aTarget = a_element.target;

  if (aTarget==null || aTarget=="") {
    aTarget = "pop";
  }

  /* Because we can't do real design work - only kludge things up */
  if (a_element.href!=null && a_element.href.match(/\/(fs|dar|floor_selector)\.asp/)) {
    aTarget = "eieio";
  }

  if (a_windowStyle==null) {
    a_windowStyle = "titlebar,scrollbars=yes,resizable=yes";
  }

  if (a_width==null || a_width <=0) {
    a_width=400;
  }
  if (a_height==null || a_height <=0) {
    a_height=400;
  }

  a_windowStyle+= (",left="+((screen.availWidth - a_width) / 2));
  a_windowStyle+= (",width="+a_width);

  a_windowStyle+= (",top="+(((screen.availHeight - a_height) / 2 ) - 30));
  a_windowStyle+= (",height="+a_height);



  var w;
  if (a_customText!=null) {
    w = window.open('', aTarget, a_windowStyle);
    try {
      w.document.close();
      w.document.open();
      w.document.writeln(a_customText);
      try {
        w.document.close();
      } catch(e){}
    } catch(e){}
  } else {
    w = window.open(a_element.href, aTarget, a_windowStyle);
  }
  w.focus();

  return false;
}



/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, path, expires, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 * Returns null if cookie does not exist.
 */
function getCookie(name) {
  try {
    var cookies = document.cookie;
    var index = cookies.indexOf(name + "=");
    if (index == -1) return null;
    index = cookies.indexOf("=", index) + 1;
    var endstr = cookies.indexOf(";", index);
    if (endstr == -1) endstr = cookies.length;
    return unescape(cookies.substring(index, endstr));
  } catch(e) {}
  return null;
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 */
function deleteCookie(name, path) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/* Function which decides whether to show the interstial or not.
*/
function a_doInterstitial(clickedElement, localOverride) {
  // Do we have a userTrackingId
  if (getCookie("UT")==null) {
    return false;
  }
  // Are we logged in ?
  if (getCookie("LIC")!=null) {
    return false;
  }
  // Is the current location in /resam || /rescabam ?
  if (window.location.href.match(/\/resam\/.+/) || window.location.href.match(/\/rescabam\/.+/)) {
    return false;
  }
  // Have we shown the interstitial
  if (getCookie("interstitial")!=null) {
    return false;
  }
  /*
     If the interstitial attribute is set - we want to ignore the global handler and
     use the local one set on the tag
     and isLocalHandler will be undefined
   */

  if ($(clickedElement).readAttribute('interstitial')!=null && (typeof(localOverride)=="undefined")) {
    return false;
  }

  if (localOverride) {
   return true;
  }
  if ('A'==clickedElement.tagName &&
      clickedElement.href.match(/\.pdf$/i)) {
    return true;
  }
  // Checked if the selected item was an image. Then see if it has a surrounding A TAG
  if ('IMG'==clickedElement.tagName) {
    var p = clickedElement.parentNode;
    if(p!=null)
      return 'A' == p.tagName && p.href.match(/\.pdf$/i);
  }

  return false;
}



/*
  The onClick trap for the interstitial. Trap the click and decide whether
  we need to popover the interstitial. The decision to popover is done via
  a_doInterstitial.

  Geeks and power users - we apologize to you for doing this.
*/
function a_InterstitialTrap(event, localOverride) {
  var evt = event;
  if (evt == null) {
    evt = window.event;
  }

  if (evt==null) {
    return;
  }

  var clickedElement = (evt.srcElement!=null)?evt.srcElement:evt.target;
  if (clickedElement==null) {
    return;
  }
  if (localOverride) {
    clickedElement=localOverride;
  }


  // Ignore if right or middle click
  var rightclick = false;
  if (evt.which) {
    rightclick = (evt.which == 3||evt.which == 2);
  } else if (evt.button) {
    // in moz middle=1 button we can't check that since
    // 1 is left for MS. (One of the few times: ms did good, moz(w3c) was dumb)
    rightclick = (evt.button == 2||evt.button == 4);
  }
  if (rightclick) {
    return;
  }



  if (a_doInterstitial(clickedElement, localOverride)) {

    if ('IMG'==clickedElement.tagName && 'A' == clickedElement.parentNode.tagName) {
      clickedElement = clickedElement.parentNode;
    }

      var interstitialDivTagId = 'interstitial';

    // set seen it cookie to the path

    setCookie("interstitial",
          "Y",
          window.location.href.match(/:\/\/[^\/]+(\/[^\/]+)/)[1]);
    var interstitialDivTag = document.getElementById(interstitialDivTagId);
    var i_width = 485;
    var i_height = 375;
    /* At this point assume if we can do this - iFrames are allowed. */
    var iHtml = '<iframe sr';

    if (clickedElement.href.match(/html$/)) {
      // instead of launching the pdf - we'll do a callback to the parents
      iHtml = iHtml + "c='interstitial.asp?onclick=" + $(clickedElement).readAttribute('interstitial');
    } else {
      iHtml = iHtml + "c='interstitial.asp?pdf=";
      // pdf is the absolute path without the server
      var pdf = clickedElement.href.match(/:\/\/[^\/]+(\/.+)/)[1];
      iHtml = iHtml + escape(pdf);
    }



    iHtml = iHtml + "' style='width:100%; height: 100%' ";
    iHtml = iHtml + "scrolling='yes' id='interstitialFrame' name='interstitialFrame'></iframe>";


    if (interstitialDivTag==null) {
      interstitialDivTag = document.createElement('DIV');
      interstitialDivTag.style.position='absolute';
      interstitialDivTag.style.margin='0 0 0 0';
      interstitialDivTag.style.border = "1px ridge Black";
      interstitialDivTag.style.width =  i_width + 'px';
      interstitialDivTag.style.height = i_height + 'px';
      interstitialDivTag.style.zIndex = 102;
      interstitialDivTag.id = "interstitial";
      document.body.insertBefore(interstitialDivTag, document.body.firstChild);
    }

    interstitialDivTag.innerHTML = iHtml;
    interstitialDivTag.style.left=((document.body.clientWidth - i_width)/3 + "px");
    interstitialDivTag.style.top=((document.body.scrollTop + (document.body.clientHeight  - i_height)/3) + "px");

    interstitialDivTag.style.visibility='visible';
  } else {
    return;
  }

  evt.returnValue = false;
  return false;
}


 function closeInterstitial() {
     var intDiv = parent.document.getElementById("interstitial");
     intDiv.style.visibility='hidden';
 }


 function a_getLanguageId() {
  var pg = window.location.pathname;
  var re = pg.match(/^\/.+\/.+\/.+\/(.+)\/.+\/.*/);
  return re[1];
}


/* Open the Cart window in the specific URL */
function a_doCartAction(theUrl) {
  var width = 625;
  var height = 580;
  var x = (screen.availWidth - width) / 2
    var y = ((screen.availHeight - height) / 2 ) - 30;

  theUrl = theUrl + "&languageId=" + a_getLanguageId();

  var w = window.open(theUrl,
            'CART',
            "status=yes,width="+width+
              ",height="+height+
              ",left="+x+",top="+y+",titlebar,scrollbars=yes,resizable=yes,toolbar=yes");
  w.focus();
  return false;
}

/* view the Cart */
function a_viewCart() {
  return a_doCartAction('/resna/cart_action.jsp?command=view');
}

/* Add an item number to cart */
function a_addCart(itemId) {
  s_events="scAdd";
  s_products=";" + itemId + ";1;";
  try {
    s_gs("armecommerceus");
  }catch(e){/*doh!*/}

  return a_doCartAction('/resna/cart_action.jsp?command=add&itemId=' + itemId);
}



startList=function(){
  if (document.all&&document.getElementById&&document.getElementById("mainMenuBar")) {
    // Add a shim to hide select items for drop down menus.
    if (navigator.appVersion.substr(22,3)!="5.0" && window.mainMenuBar != undefined) {
      window.mainMenuBar.innerHTML=('<iframe id="menushim" src="about:blank" scrolling="no" frameborder="0" style="position:absolute;display:none;filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);"></iframe>' + window.mainMenuBar.innerHTML);
    }
    navRoot=document.getElementById("mainMenu");
    if(navRoot != undefined){
    for (i=0;i<navRoot.childNodes.length;i++){
      node=navRoot.childNodes[i];
      if (node.nodeName=="LI"&&node.getElementsByTagName("UL").length>0){
        node.onmouseover=function(){
          if(this.className.indexOf("over")==-1){
            this.className+=" over";
            hideDropdowns(this.getElementsByTagName("UL")[0],true);
          }
        }
        node.onmouseout=function(){
          if(!isInParent(event.toElement, this)){
            this.className=this.className.replace(" over","");
            hideDropdowns(this.getElementsByTagName("UL")[0],false);
          }
        }
      }
    }
  }
  }
}

function isInParent(el, parent){
  if (!el || ! parent)
    return false;

  var aEls=parent.getElementsByTagName(el.tagName)
  if (!aEls)
      return false;
  if(aEls.length==0)
    return false;
  for(var i=0;i<aEls.length;i++){
    if(el==aEls[i])
      return true;
  }
  return false;
}
function hideDropdowns(obj, bool){
  if (navigator.appVersion.substr(22,3)=="5.0"){
    if(bool)
      hideSelects();
    else
      showSelects();
    return;
  }
  var mnuShim=document.getElementById("menushim");
  if(bool){
    mnuShim.style.left=getPageOffsetLeft(obj)+"px";
    mnuShim.style.top=getPageOffsetTop(obj)+"px";
    mnuShim.style.width=obj.offsetWidth+"px";
    mnuShim.style.height=obj.offsetHeight+"px";
    obj.style.zIndex="101";
    mnuShim.style.zIndex="0";
    mnuShim.style.display="block";
  } else
    mnuShim.style.display="none";
}
function getPageOffsetLeft(el){
  var x;
  x=el.offsetLeft;
  if (el.offsetParent!=null)
    x+=getPageOffsetLeft(el.offsetParent);
  return x;
}
function getPageOffsetTop(el){
  var y;
  y=el.offsetTop;
  if (el.offsetParent!=null)
    y+=getPageOffsetTop(el.offsetParent);
  return y;
}
function hideSelect(id){
  var el=document.getElementById(id);
  if (el!=null)
    el.className+=" hide";
}
function hideSelects(){
  var oSelects=document.getElementsByTagName("select");
  for(var i=0;i<oSelects.length;i++)
    oSelects[i].className+=" hide";
}
function showSelects(){
  var oSelects=document.getElementsByTagName("select");
  for(var i=0;i<oSelects.length;i++)
    oSelects[i].className=oSelects[i].className.replace(" hide","");
}
function addEvent(obj, evType, fn) {
  // adds an eventListener for browsers which support it
  // Written by Scott Andrew: nice one, Scott
  if (obj.addEventListener){
    obj.addEventListener(evType,fn,true);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType,fn);
    return r;
  } else {
    return false;
  }
}

/* theParams EX: gIntroTarget=main&gTargetScreen=fs&gProduct=LAM&gFilter0=Best */
function popSelector(theParams, theSelect) {
    var theName = theSelect.name;
    var theValue = theSelect.options[theSelect.selectedIndex].value
    if(theValue != null && theValue != "") {
      //get the language and country id
    var pg = window.location.pathname;
    // //resflram/na/home/en/us/floor_selector.asp
    var re = pg.match(/^\/.+\/.+\/.+\/(.+)\/(.+)\/.*/);

        var url = "/resflram/na/home/" + re[1] + "/" + re[2] + "/floor_selector.asp?" + theParams + "&" + theName + "=" + theValue;
        var a_width=800;
        var a_height=600;
        var a_windowStyle="titlebar,scrollbars=yes,resizable=yes";

        a_windowStyle+= (",left="+((screen.availWidth - a_width) / 2));
        a_windowStyle+= (",width="+a_width);

        a_windowStyle+= (",top="+(((screen.availHeight - a_height) / 2 ) - 30));
        a_windowStyle+= (",height="+a_height);

        var w = window.open(url, 'eieio', a_windowStyle);
        w.focus();
        theSelect.selectedIndex=0;
    }
    return false;
}

/** Used by flash for the home pages */
function gotoWebLink(url) {
  // construct url
  window.location.href=url;
}

addEvent(window,"load",startList);
document.onclick = a_bodyOnClickHandler;
a_addHandler(a_InterstitialTrap);


