function CMMap(path, title) {
	this.categoryBaseId = "MHEI:MKT:mortgage";
	this.categoryId;
	this.subCategoryId = "CD1";
	this.lang = "en";
	this.section = "Unclassified";
	this.elementId;
	this.elementCategoryId = "MHEI:MKT:mortgage";
	this.pageTitle;
	this.productSection;
		
	this.parsePath(path);
	if (arguments.length > 1) {
		this.pageTitle = this.normalizeTitle(arguments[1]);
	}
	var page = this.pageTitle ? this.pageTitle : this.section;
	if (this.productSection)
		page = this.productSection + page;
	page = this.lookupPage(path);
	this.pageId = this.categoryBaseId + ":" + this.subCategoryId + this.lang + ";" + page;
	this.categoryId = this.categoryBaseId + ":" + this.subCategoryId + this.lang;
}
CMMap.prototype.lookupPage = function(path) {
	var mypath = path;
	if (mypath.indexOf("/magnoliaAuthor") == 0)
		mypath = mypath.substring(15);
	if (mypath.indexOf("/en") == 0 || mypath.indexOf("/es") == 0)
		mypath = mypath.substring(3);
	var p1 = mypath.indexOf(".html");
	if (p1 > 0)
		mypath = mypath.substring(0, p1);
	var pageId = CMMap.uriMap[mypath];
	return pageId ? pageId : "Unknown";
};
CMMap.prototype.parsePath = function(path) {
	var parts = path.split("/");
	if (parts[1] == "magnoliaAuthor") { // for testing
		parts.shift();
	}
	if (parts[1] == "en" || parts[1] == "es") {
		this.lang = parts[1];
		if (parts.length == 3) {
			var p1 = parts[2].indexOf(".");
			parts[2] = parts[2].substring(0, p1);
		}
		var section = CMMap.pageIdMap[parts[2]];
		if (section) {
			this.section = section;
		}
		var last = parts[parts.length - 1];
		var p1 = last.indexOf(".");
		if (p1 > -1) {
			var file = last.substring(0, p1);
			this.elementId = CMMap.pathElementIdMap[file];
			this.elementCategoryId = "MHEI:MKT:mortgage" + ":CD1" + this.lang;
		}
		if (parts.length == 5) {
			var key = parts[2] + "/" + parts[3];
			this.productSection = CMMap.productSectionMap[key];
		}
	}
};
CMMap.prototype.normalizeTitle = function(title) {
	if ( ! title)
		return "Unknown";
	title = title.replace(/[-|\/\"\?\.']/g, " ");
	var parts = title.split(" ");
	var normalized = "";
	for (var i=0; i<parts.length; i++) {
		if (parts[i].length == 0)
			continue;
		var capital = parts[i].substring(0,1).toUpperCase();
		normalized += capital + parts[i].substring(1);
	}
	return normalized;	
}
CMMap.pageIdMap = {
	"home": "HomePage",
	"our-commitment": "OurCommitment",
	"home-loan-guide": "HomeLoanGuide",
	"home-loan-options": "HomeLoanOptions",
	"service-and-support": "ServiceAndSupport",
	"community-support": "CommunitySupport",
	"en-espanol": "EnEspanol",
	"site-map": "SiteMap",
	"learning-center": "LearningCenter"
};
CMMap.elementIdMap = {
	"accordion2-a0": "HomeBuyAHomeExpand",
	"accordion2-a1": "HomeRefiHomeExpand",
	"accordion2-a2": "HomeEquityInHomeExpand",
	"accordion1-a1": "HomeVwTodayRatesExpand",
	"accordion1-a2": "HomeGetHelpExpand",
	"accordion1-a0": "ExploreLoanOptionsExpand"
};
CMMap.pathElementIdMap = {
	"buy": "HLOBuyaHomeExpand",
	"refinance": "HLORefinanceExpand",
	"borrow": "HLOBorrowExpand",
	"other-options": "HLOOtherOptions"
};
CMMap.productSectionMap = {
	"home-loan-options/buy-a-home": "HLOBuy",
	"home-loan-options/refinance-home": "HLORefi",
	"home-loan-options/borrow-from-your-home": "HLOBorrowFromHome",
	"home-loan-options/other-options": "HLOOther"
};
CMMap.elementBindings = function(path) {
	for (var id in CMMap.elementIdMap) {
		var map = new CMMap(path);
		var data = { elementId: CMMap.elementIdMap[id], categoryId: map.categoryId };
		$("#" + id).bind("click", data, function(event) {
			cmCreatePageElementTag(event.data.elementId, event.data.categoryId, null, null, null);
		});
	}
	$("#productnav li.open a").each(function() {
		var puri = parseUri(this.href);
		var map = new CMMap(puri.path);
		var data = { elementId: map.elementId, categoryId: map.categoryId};
		$(this).bind("click", data, function(event) {
			cmCreatePageElementTag(event.data.elementId, event.data.categoryId, null, null, null);
		});
	});
}
CMMap.pageView = function(path, title) {
	var map = new CMMap(path, title);
	var pageId = (metaPageId != null && metaPageId != "") ? map.categoryId + ";" + metaPageId : map.pageId;
	var categoryId = map.categoryId;
    cmCreatePageviewTag(pageId, null,null, categoryId, false,false,null,false,false,null,null,null,null,null,null,null,null);
}
CMMap.topNavigationLinks = function(path) {
	$("#head #nav1").attr("name", "NavOurCommitment");
	$("#head #nav2").attr("name", "NavHomeLoanGuide");
	$("#head #nav3").attr("name", "NavHomeLoanOptions");
	$("#head #nav4").attr("name", "NavServiceAndSupport");
}
CMMap.setupTagging = function() {
	if (window.location.hostname == "homeloans.bankofamerica.com") {
		cmSetProduction();
	} else {
		cmSetStaging();
	}
}
CMMap.setupTags = function(path, title) {
	CMMap.setupTagging()
	CMMap.pageView(path, title);
	CMMap.elementBindings(path);
	CMMap.topNavigationLinks(path);
}
