/* Parses URL Pathname
	Author: Jamie L. Marin, Senior Web Developer
	Date: June !5, 2005
*/

/* Sets varibles for URI pathname and pathname length */
var browserURI = location.pathname;
var uriLength = browserURI.length;

/* Creates Array */
var directories = new Array( );

/* Find out indexes of first, next, and last slashes */
var startSlash = browserURI.indexOf('/');
var nextSlash =  browserURI.indexOf('/', startSlash + 1);
var lastSlash = browserURI.lastIndexOf('/');
var slashCount = 0;

/* test for one deep section */ 
if (startSlash == lastSlash)
{
	directories[slashCount] = location.pathname.slice(startSlash + 1);
}


/* Loop to define sections from 1 to N */
var pathElement;
while (startSlash != lastSlash || nextSlash != -1)
{
	directories[slashCount] = location.pathname.substring(startSlash + 1,nextSlash);
	if (lastSlash + 1 != uriLength)
		directories[slashCount +1] = browserURI.slice(nextSlash + 1);
	startSlash = nextSlash;
	nextSlash =  browserURI.indexOf('/', startSlash + 1);
	slashCount++;
}
if (typeof omnitureScrubPattern == "undefined"){omnitureScrubPattern = "";}

/* scrub unwanted elements (such as lookbook IDs) from the path */
var scrubbed = false;
if (omnitureScrubPattern != "") {
  if (omnitureScrubPattern =="$") {
   /* special token indicating "remove last element" */
   directories.splice(directories.length-1,1);
   scrubbed = true;
  }
  else {
    for (var i=0; i<directories.length; i++) {
      var element = directories[i];
      if (scrub(element)) {
        directories.splice(i,1);
        i--;
        scrubbed = true;
      }
    }
  }
}

/* a hack for printer-friendly pages */
if (directories[0] == 'print') {
   directories.splice(1,directories.length-1);
}

/* Set User Friendly Variables */
var firstDir = directories[0];
var lastDir = directories[directories.length-1];

/* End parsing code */

/* set Omni-friendly path */

var omniHierarchy = "";
for (var i=0; i<directories.length; i++) {
    omniHierarchy += directories[i];
    if (i != directories.length - 1)
        omniHierarchy += ",";
}

/* set prop5-prop9 of omniture code */

var setProp3;
var setProp4="";
var setProp5;
var setProp6;
var setProp7;
var setProp8;
var setProp9;
var setPageName;
var setPageType;
var setEvents;

if (directories[0] == "") {
    setProp6 = "homepage";
    omniHierarchy="homepage";
    var setPageName="homepage";
}
if (directories.length >= 1 && directories[0] != "")
    setProp6 = directories[0];
if (directories.length >= 2)
    setProp7 = setProp6 + '/' + directories[1];
if (directories.length >= 3)
    setProp8 = setProp7 + '/' + directories[2]; 
if (directories.length >= 4)
    setProp9 = setProp8 + '/' + directories[3];

if (directories[1]=="lookbooks" || directories[1]=="mylookbooks")
    setProp5 = "lookbook";

if (scrubbed) {
  setPageName = location.protocol;
  setPageName = setPageName + "//";
  setPageName = setPageName + location.host;
  for (var i = 0; i < directories.length; i++) {
    setPageName = setPageName + "/" + directories[i];
  }
  setPageName = setPageName + "/";
}

if (location.pathname.indexOf("/print/") != -1) {
  setPageName = location.protocol;
  setPageName = setPageName + "//";
  setPageName = setPageName + location.host;
  setPageName = setPageName + "/print/";
  setProp6 = "print";
  omniHierarchy="print";
  setProp4 = location.pathname.substr(7);
  setProp5='print friendly slideshow';
}

function scrub (s) {
  var patterns = omnitureScrubPattern.split(";");
  for (var i = 0; i < patterns.length; i++) {
    if (s.match(patterns[i])) {
      return true;
    }
  }
  return false;
}


