//************ UrlMgr ************//
function UrlMgr ()
{
  this.urlDetectEnabled = false;            // while a URL change is being processed, don't detect changes
                                            // this flag will be enable/disabled by the carouselMgr
  // translate all URLs to execute goToPane instead of how's they are defined
  this.xlateLinksToAnchors ();

  // start looking for url changes via back button
  this.backButtonDetect    ();
}


// xlateLinksToAnchors
// translate URLs from something like /eye-shadow.do to urlMgr.goToPane("eye-shadow")
// We want all urls to be defined to "normal urls" for search engines without javascript
//  but for the user, we don't want urls that will reload the page
UrlMgr.prototype.xlateLinksToAnchors = function ()
{
  var links    = document.links;
  var paneName;
  var ndx;
  var href;

  for (ndx = 0; ndx < links.length; ndx++)
  {
    href = links[ndx].href;
    //logDebug ("href = " + href);

    // only translate struts urls
    if (href.substring (href.length-3) == ".do")
    {
      paneName = href.substring (href.lastIndexOf("/") + 1, href.length-3);
      if (carouselMgr.isKnownPaneName(paneName))
        links[ndx].href = "javascript:urlMgr.goToPane('" + paneName + "')";
      //else
      //  logDebug (" - UrlMgr cannot translate: unknown .DO = " + paneName);
    }
    else
    {
      if (href.indexOf("#") != -1)
      {
        paneName = href.substring (href.indexOf("#") + 1);
        if (paneName.indexOf("MAX_") != -1)
          paneName = href.substring (href.indexOf("MAX_") + 4);
//      paneName = href.substring (href.indexOf("#") + 1);
        if (carouselMgr.isKnownPaneName(paneName))
        {
          //logDebug ("translating " + href);
          links[ndx].href = "javascript:urlMgr.goToPane('" + paneName + "')";
        }
        else
        {
          //logDebug ("-- CANNOT translate " + href + " -- paneName = " + paneName);
        }
      }
    }
  }

  //logDebug (" ----- UrlMgr done ----- ");
}


UrlMgr.prototype.backButtonDetect = function ()
{
  // URL change detection logic works great in Firefox- not so great with IE
  // iframe mechanism used in IE
  if (IE == false)
  {
    var paneName;
    var hash;

    if ((this.urlDetectEnabled) && (carouselMgr.panes[0].bAutoSlide == false))
    {
      // if there's a hash in the URL, it will be a position 0
      paneName = getHash();
      if (paneName == "")
        paneName = "makeup";
      if (paneName != carouselMgr.currentPane)
      {
        //alert ("change detected: paneName = " + paneName + " carouselMgr.currentPane = " + carouselMgr.currentPane);
        carouselMgr.goToPane (paneName);
      }
    }

    // continue to look for URL changes
    setTimeout ("urlMgr.backButtonDetect()", 1000);
  }
}


UrlMgr.prototype.goToPane = function (paneName)
{
  carouselMgr.goToPane (paneName);
}

UrlMgr.prototype.enableUrlDetection = function ()
{
  if (IE)
  {
    if (jQuery("#urlMgrFrame").attr("src") != "")
      window.frames.urlMgrFrame.enableUrlDetection();
  }
  else
    this.urlDetectEnabled = true;            // while a URL change is being processed, don't detect changes
}

UrlMgr.prototype.disableUrlDetection = function ()
{
  if (IE)
  {
    if (jQuery("#urlMgrFrame").attr("src") != "")
      window.frames.urlMgrFrame.disableUrlDetection();
  }
  else
    this.urlDetectEnabled = false;           // while a URL change is being processed, don't detect changes
}
