// indexes 0=url, 1=pagename 2=source 3=type
var sUrlToPageNameMappingArray = [
	'/mystyle/shows/runninginheels/index.jsp shows:running-in-heels null flash',
	'/mystyle/shows/runninginheels/ shows:running-in-heels null flash',
	'/mystyle/shows/whosewedding/beontv/index.jsp shows:whosewedding:beontv null form'
];

// MS-414
// Hijack Prototype for Java like equalsIgnoreCase function
// implemented by myEqualsIgnoreCase to normalize both
// strings to compare in lower case
//
String.prototype.equalsIgnoreCase=myEqualsIgnoreCase;
function myEqualsIgnoreCase(arg)
{               
    return (new String(this.toLowerCase())==(new String(arg)).toLowerCase());
}
// As long as the path context is sandwiched between
// the 2 hints, then it is inferred from convention
// that that must be the name of the show
//
// e.g. http://www.mystyle.com/mystyle/shows/giulianaandbill/videos/index.jsp
//
// hint1 will be "shows"
// hint2 will be "videos"
//
function sGetShowNameFromPathname(hint1, hint2) 
{
    var pathname = window.location.pathname;
    pathname = pathname.replace(/(\/)+/g, "/"); // Allow for multiple slashes 
    var pathArray = pathname.split("/");
    var match1 = -1;
    var match2 = -1;
    var showName = "";
    
    for(var i=0; i<pathArray.length; i++)
    {
        //alert("pathArray[" + i + "] = " + pathArray[i]); // For debugging only
        
        if (hint1 != null && hint1.equalsIgnoreCase(pathArray[i])) {
            match1 = i;
        }
        if (hint2 != null && hint2.equalsIgnoreCase(pathArray[i])) {
            match2 = i;
            break;
        }
    }

    // This is my poor man logic to determine exactly the show name that
    // is expected to be sandwiched between the 2 hints
    if ( (match1 < match2) && ((match1+2) == match2) ) {
        showName = pathArray[match1+1];
    }
    
    return showName;
}
// MS-414

// MS-414
// e.g. Pretty url blog --> 
//
// http://<...>/mystyle/b3711_who_broke_bank_kim_lauren.html
// http://<...>////mystyle////b3711_who_broke_bank_kim_lauren.html
//
// and thus:
//
// ... RegExp(/^b[0-9]+_(.)+/i) ...
//
function sIsPathnamePrettyURLBlogPattern()
{
    var pathname = window.location.pathname;
    pathname = pathname.replace(/(\/)+/g, "/"); // Allow for multiple slashes 
    var pathArray = pathname.split("/");
    var temp1 = pathArray[1]; // TODO
    var temp2 = pathArray[2]; // TODO
    var isPrettyURLBlog = false;
    
    if (temp1.equalsIgnoreCase("mystyle") && temp2 != null) {
        pattern1 = new RegExp(/^b[0-9]+_(.)+/i);
        isPrettyURLBlog = pattern1.test(temp2);
    }
    
    // For debugging only
    /*
    alert("pathArray[1]=" + temp1); 
    alert("pathArray[2]=" + temp2);
    alert("isPrettyURLBlog=" + isPrettyURLBlog);
    */
    // For debugging only
    
    return isPrettyURLBlog;
}
// MS-414

//property  initialization
//BEGINS HERE
//functions and variables to see if a window.location.pathname should be checked for dynamic page naming rules
function sIsPathnameIncluded(sPath)
{
	//if (window.location.pathname.length >= sPath.length && window.location.pathname.substring(0,sPath.length) == sPath)
	if (window.location.pathname.indexOf(sPath) >= 0)
		return true;
	else
		return false;
}

//function to remove html tags, replace white spaces with dashes, and to format to lowercase
function sFormatToPageTitle(strInputCode){
 	//s/<[a-zA-Z\/][^>]*>//g   <(.|\n)*?>
 		return strInputCode.replace(/<\/?[^>]+(>|$)/g, "").replace(/:/g, "_").replace(/\s/g, "-").toLowerCase();

}

//Dynamically name the page based on the URL
var sQueryStringArray = window.location.toString().toQueryParams();
s.pageName =""; //initialize pageName to make sure it wasn't set by anything else
if (sIsPathnameIncluded("/videos/"))
{
	s.prop15="video";
	s.pageName = "videos";

    // MS-414
    var temp = sGetShowNameFromPathname("shows", "videos");
    //alert("show name = " + temp); // For debugging only   
    if (temp != null && temp.length > 0) {
        s.pageName = temp + ":" + s.pageName; 
    }
    // MS-414

	if (sQueryStringArray != null && sQueryStringArray.franchise != null)
		s.prop7 = sQueryStringArray.franchise;
}
else
{
	//some special cases about what the page type will be
	if (sIsPathnameIncluded("/blog/"))
		s.prop15 = "blog";
	else if (window.location.pathname == "/mystyle/" || sIsPathnameIncluded("/mystyle/index.jsp"))
	{
		if (sQueryStringArray != null && sQueryStringArray.categoryName != null)
		{
			s.pageName = "blog";
			if(sFormatToPageTitle(sQueryStringArray.categoryName).length > 0)
				s.pageName += ":"+sFormatToPageTitle(sQueryStringArray.categoryName);
			//if(sFormatToPageTitle(sQueryStringArray.pageNum).length > 0) // MS-414
			if(sQueryStringArray.pageNum != null && sFormatToPageTitle(sQueryStringArray.pageNum).length > 0) // MS-414
				s.pageName += ":"+sFormatToPageTitle(sQueryStringArray.pageNum);
			s.prop15 = "blog";
		} else if (sQueryStringArray != null && sQueryStringArray.searchKeyword != null)
		{
			s.pageName = "search";
			s.prop15 = "search";
			s.eVar23 = sFormatToPageTitle(sQueryStringArray.searchKeyword)
			s.events = "event1";
			var blogRoll = document.getElementById('blog_roll');
			if(blogRoll != null)
			{
				if(blogRoll.childNodes == null || blogRoll.childNodes.length <= 3)
				{
					s.events = s.events + ",event3";
				}
			}

		}
		else
		{
			s.prop15 = "home";
			s.pageName="home";
		}
	}
	else if (sIsPathnameIncluded("/detail/"))
		s.prop15 = "detail";
    // MS-414
    else if (sIsPathnamePrettyURLBlogPattern())
        s.prop15 = "detail";
    // MS-414
	else if (sIsPathnameIncluded("/print/"))
		s.prop15 = "print";
	else if (sIsPathnameIncluded("/games/"))
		s.prop15 = "game";
	/* MS-414, logic of tracking photo in photoGallery.js
	else if (sIsPathnameIncluded("/photos/gallery.jsp"))
		s.prop15 = "gallery";
    */
	else if (sIsPathnameIncluded("/photos/index.jsp"))
		s.prop15 = "landing";
	else if (sIsPathnameIncluded("/photos/"))
		s.prop15 = "photo";
	else if (sIsPathnameIncluded("/shows/index.jsp"))
		s.prop15 = "landing";
	else if (sIsPathnameIncluded("/dressmynest/beontv/index.jsp"))
		s.prop15 = "form";
	else if (sIsPathnameIncluded("/shows/"))
		s.prop15 = "shows";
	else if (sIsPathnameIncluded("/sweepstakes/odddaygiveaway/error.jsp"))
		s.prop15 = "error";
	else if (sIsPathnameIncluded("/sweepstakes/odddaygiveaway/thanks.jsp"))
		s.prop15 = "thanks";
	else if (sIsPathnameIncluded("/sweepstakes/odddaygiveaway/rules.jsp"))
		s.prop15 = "page";
	else if (sIsPathnameIncluded("/sweepstakes/odddaygiveaway/") && !sIsPathnameIncluded("/sweepstakes/odddaygiveaway/rules.jsp"))
		s.prop15 = "form";
	else if (sIsPathnameIncluded("/syndication/faq.jsp"))
		s.prop15 = "page";
	else
		s.prop15 = "landing";

	//if (sIsPathnameIncluded("/detail/"))
	if (sIsPathnameIncluded("/detail/") || sIsPathnamePrettyURLBlogPattern()) // MS-414
	{
		s.pageName = "detail:" + sFormatToPageTitle(document.title.replace(" - mystyle.com", ""));
	}
	else if (sIsPathnameIncluded("/print/"))
	{
		s.pageName = "print:" + sFormatToPageTitle(document.title.replace(" - mystyle.com", ""));
	}
	/* MS-414, logic of tracking photo in photoGallery.js
	else if (sIsPathnameIncluded("/photos/gallery.jsp"))
	{
		s.pageName = sFormatToPageTitle(document.title.replace(" - Photo Gallery - mystyle.com", ""));
		s.pageName = s.pageName.replace(/(_)+/g, "-"); // MS-414
	}
	*/
	else if (sIsPathnameIncluded("/photos/index.jsp") && $('category_header').down('h3').down('span') != null)
	{
		s.pageName = $('category_header').down('h3').down('span').firstChild.nodeValue.replace(/\s/g, "-").toLowerCase() + ":photos";
		s.pageName = s.pageName.replace(/(_)+/g, "-"); // MS-414
	}
	else if (s.pageName == null || s.pageName == "")
	{
		var strPageName = window.location.pathname;

		//get rid of the trailing / if there is one
		if (strPageName.length > 0 && strPageName.substring(strPageName.length-1,strPageName.length) == "/")
			strPageName = strPageName.substring(0, strPageName.length-1);

		//all the custom string replacement rules go here
		strPageName = strPageName.replace("/mystyle/", "/").replace("/index.jsp", "").replace("/includes/", "").replace("/shows/", "").replace("/index.html", "").replace(".jsp", "").replace(".html", "").replace("/", ":").replace("_", "-");

		//get rid of the leading : if there is one
		if (strPageName.length > 0 && strPageName.substring(0,1) == ":")
			strPageName = strPageName.substring(1, strPageName.length);

		if (sQueryStringArray != null)
		{
			if(sQueryStringArray.cid != null)
				strPageName = strPageName + ":" + sQueryStringArray.cid;
			if(sQueryStringArray.game != null)
				strPageName = strPageName + ":" + sQueryStringArray.game;
			if(sQueryStringArray.pageNum != null)
				strPageName = strPageName + ":" + sQueryStringArray.pageNum;
		}
		s.pageName = strPageName;
	}
}

// all "/on" pages should be type shows regardless
if (sIsPathnameIncluded("/on/shows/"))
	s.prop15 = "shows";


//stick page content source name in front of the page name if it's not already there
if (s.prop7 != null && s.prop7 != "")
{
	if (s.pageName.length < s.prop7.length || s.pageName.substring(0,s.prop7.length) != s.prop7)
		s.pageName = s.prop7 + ":" + s.pageName
}
//end of dynamic page naming, the following is static mapping that overrides any dynamic rules or variables passed into nielsen.jsp


//note that if the page is in the mapping array, all the pagename logic that was set above is overwrittens
for (var index = 0; index < sUrlToPageNameMappingArray.length; ++index) {
  var item = $w(sUrlToPageNameMappingArray[index]);
  if (item[0] == window.location.pathname)
  {
    s.pageName = item[1]; // page name
    s.prop7 = item[2] != "null" ? item[2] : ""; // page content source
    s.prop15 = item[3]; // page type
    break;
  }
}
//tack on the author if there is one
if (sQueryStringArray != null)
{
	if(sQueryStringArray.author != null)
		s.pageName = s.pageName + ":by-author:" + sQueryStringArray.author.replace("+","-");
}

s.prop16 = sGetTitlesByCssClass(sTrackingCssNameWidgets);
s.prop19 = sGetTitlesByCssClass(sTrackingCssNameVideos);

//initialize %page viewed
if (s.events == null || s.events == "")
	s.events="event22";
else
	s.events = s.events + ",event22";

/*
 * The following are also set by sSetDynamicEventProperties()
 * Any other properties that need to be set after the page loads
 * duriung a dynamic event like scrolling should also be set here as well so that
 * values are re-initialized properly
 *
 * sSetPercentSeen(sGetScrollPercentage()); //set page viewed by default when page first loads
 * s.prop26="scroll:" + s.pageName.replace(/\//g,":"); //traffic variable for pages to detect time btw scrolls
 * s.products=";;;;event22=" + sGetPercentSeen()  + "|event4=" +  sGetNumberOfProducts(); //initial value sent with page view
 */
sSetPercentSeen(sGetScrollPercentage()); //set page viewed by default when page first loads
s.prop26="scroll:" + s.pageName.replace(/\//g,":"); //traffic variable for pages to detect time btw scrolls
s.products=";;;;event22=" + sGetPercentSeen()  + "|event4=" +  sGetNumberOfProducts(); //initial value sent with page view


document.observe('dom:loaded', function() {
	Event.observe(window, 'scroll', sScrollingEvent);
	Event.observe(window, 'resize', sScrollingEvent);
	//Event.observe(window, 'scroll', sSetDynamicEventProperties);
	//Event.observe(window, 'resize', sSetDynamicEventProperties);
});

//Property and page initialization
//ENDS HERE
