function isZTestServer() {
	if(domain == "hcusgr606" || domain == "hcusgr611" || domain == "hcusgr609" || domain == "qa.zondervan.com" || domain == "hcusgr614" || domain == "zondervanqacms") {
		return true;
	}
	return false;
}
function createExploreAttributes(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) {
	return a1 + "-_-" + a2 + "-_-" + a3 + "-_-" + a4 + "-_-" + a5 + "-_-" + a6 + "-_-" + a7 + "-_-" + a8 + "-_-" + a9 + "-_-" + a10 + "-_-" + a11 + "-_-" + a12 + "-_-" + a13 + "-_-" + a14 + "-_-" + a15;
}
function getPageId() {
	if(pathname == "/cultures/en-us/product/productdetail.htm") {
		return "zon:" + pathname + " | " + $.query.get("ProdID");
	}
	else if(domain == "www.zondervan.com" || isZTestServer()) {
		return "zon:" + pathname;
	}
	else {
		return "zon:" + url;
	}
}
function getAuthorNamesOnly() {
	if(domain == "www.zondervan.com" || isZTestServer()) {
		if(pathname == "/cultures/en-us/product/productdetail.htm") {
			var cmAuthorCount = $(".productDetailAuthorCell").length;
			var cmAuthors = "[|";
			for(i=0; i < cmAuthorCount; i++) {
				cmAuthors += $(".productDetailAuthorCell").eq(i).find(".header").text() + "|";
			}
			cmAuthors += "]";
			//remove nulls
			if(cmAuthors == "[|]") {
				cmAuthors = "";
			}
			return cmAuthors;
		}
		if(pathname == "/cultures/en-us/authors/author.htm") {
			var cmAuthors = "[|" + $(".header").text() + "|]";
			return cmAuthors;
		}
	}
	return "";
}
function getAuthorsExplore() {
	if(domain == "www.zondervan.com" || isZTestServer()) {
		if(pathname == "/cultures/en-us/product/productdetail.htm") {
			var cmAuthorCount = $(".productDetailAuthorCell").length;
			var cmAuthors = "[|";
			for(i=0; i < cmAuthorCount; i++) {
				cmAuthors += $(".productDetailAuthorCell").eq(i).find("a:first").attr("href").split("=")[1] + "|";
			}
			cmAuthors += "]";
			cmAuthors += "[|";
			for(i=0; i < cmAuthorCount; i++) {
				cmAuthors += $(".productDetailAuthorCell").eq(i).find(".header").text() + "|";
			}
			cmAuthors += "]";
			//remove nulls
			if(cmAuthors == "[|][|]") {
				cmAuthors = "";
			}
			return cmAuthors;
		}
		if(pathname == "/cultures/en-us/authors/author.htm") {
			var cmAuthors = "[|" + $.query.get("ContributorID") + "|][|" + $(".header").text() + "|]";
			return cmAuthors;
		}
	}
	return "";
}
function getFormatExplore() {
	if(domain == "www.zondervan.com" || isZTestServer()) {
		return $("#PDPMain_pdpGroup1 .format").text();
	}
}
function getLanguageExplore() {
	if(domain == "www.zondervan.com" || isZTestServer()) {
		if (jsg_theme == "Vida") {
			return "spanish";
		}
		else {
			return "english";
		}
	}
}
function getCategoryId() {
	var cat = "";
	if(domain == "www.zondervan.com" || isZTestServer()) {
	//zondervan-only stuff
		if(pathname == "/cultures/en-us" || pathname == "/cultures/en-us/") {
			return "A001";
		}
		if(pathname == "/cultures/en-us/ebook" || pathname == "/cultures/en-us/ebook/") {
			return "B005";
		}
		if(pathname == "/cultures/en-us/vida" || pathname == "/cultures/en-us/vida/") {
			return "B014";
		}
		if(pathname == "/cultures/en-us/buy/home/index.htm") {
			return "A016";
		}
		//check against array data if not caught by special cases
		for(i=0; i<=32; i++) {
			if(pathname.match(categories[i][0].toString())) {
				cat = categories[i][1];
			}
		}
		if(cat != "") {
			return cat;
		}
	}
	else {
	//NON-z.com
		//temporary
		return "A038";
	}
	//BOTH Z.COM and externals
	return "A038";
}
function getSearchPhrase() {
	var phrase = "";
	if(document.getElementById("searchResults_SearchPaging_lblTotalResults") != null) {
		if(url.match("searchvalue=")) {
			phrase += "SearchPhrase: " + $.query.get("SearchValue") + " | ";
		}
		if(url.match("an=")) {
			phrase += "AgeLow: " + $.query.get("AN") + " | ";
		}
		if(url.match("ax=")) {
			phrase += "AgeHigh: " + $.query.get("AX") + " | ";
		}
		if(url.match("at=")) {
			phrase += "Author: " + $.query.get("AT") + " | ";
		}
		if(url.match("cy=")) {
			phrase += "ChurchYearEvents: " + $.query.get("CY") + " | ";
		}
		if(url.match("cz=")) {
			phrase += "CoverSize: " + $.query.get("CZ") + " | ";
		}
		if(url.match("co=")) {
			phrase += "Collection: " + $.query.get("CO") + " | ";
		}
		if(url.match("fe=")) {
			phrase += "Features: " + $.query.get("FE") + " | ";
		}
		if(url.match("pf=")) {
			phrase += "Format: " + $.query.get("PF") + " | ";
		}
		if(url.match("gn=")) {
			phrase += "Gender: " + $.query.get("GN") + " | ";
		}
		if(url.match("cd=")) {
			phrase += "IncludesCD: " + $.query.get("CD") + " | ";
		}
		if(url.match("vi=")) {
			phrase += "IncludesVideo: " + $.query.get("VI") + " | ";
		}
		if(url.match("ln=")) {
			phrase += "Language: " + $.query.get("LN") + " | ";
		}
		if(url.match("mi=")) {
			phrase += "MasterIsbn: " + $.query.get("MI") + " | ";
		}
		if(url.match("oc=")) {
			phrase += "Occasions: " + $.query.get("OC") + " | ";
		}
		if(url.match("mp=")) {
			phrase += "Platform: " + $.query.get("MP") + " | ";
		}
		if(url.match("pp=")) {
			phrase += "PoweredByPradis: " + $.query.get("PP") + " | ";
		}
		if(url.match("pt=")) {
			phrase += "ProductType: " + $.query.get("PT") + " | ";
		}
		if(url.match("pu=")) {
			phrase += "ProductUse: " + $.query.get("PU") + " | ";
		}
		if(url.match("su=")) {
			phrase += "SecondaryProductUse: " + $.query.get("SU") + " | ";
		}
		if(url.match("sc=")) {
			phrase += "SeriesAndCollections: " + $.query.get("SC") + " | ";
		}
		if(url.match("sa=")) {
			phrase += "StrategicAlliances: " + $.query.get("SA") + " | ";
		}
		if(url.match("tt=")) {
			phrase += "TopicsTypes: " + $.query.get("TT") + " | ";
		}
		if(url.match("tr=")) {
			phrase += "Translation: " + $.query.get("TR") + " | ";
		}
	}
	return phrase;
}
function getSearchResultsNumber(cat) {
	
	if(document.getElementById("searchResults_SearchPaging_lblTotalResults") != null) {
		return document.getElementById("searchResults_SearchPaging_lblTotalResults").innerHTML;
	}
	else {
		if(cat == "A024") {
			return "0";
		}
		else {
			return "";
		}
	}
	//note this is not ideal for search results pages with no results, will return blank instead of 0.
}
function createRow(iurl, iid, ititle) {
	var row = new Array();
	//url
	row[0] = iurl;
	//id
	row[1] = iid;
	//title
	row[2] = ititle;
	
	return row;
}

var categories = new Array();
categories[0] = createRow("cultures/en-us/authors", "A002", "Author");
categories[1] = createRow("cultures/en-us/authors/author.htm", "A010", "Author > Generic Author");
categories[2] = createRow("cultures/en-us/authors/index.htm", "A008", "Author > Author Landing");
categories[3] = createRow("cultures/en-us/care", "A014", "A012 > Customer Service");
categories[4] = createRow("cultures/en-us/checkout", "A032", "Checkout");
categories[5] = createRow("cultures/en-us/company", "A016", "A012 > Corporate");
categories[6] = createRow("cultures/en-us/company/faq.htm", "A014", "A012 > Customer Service");
categories[7] = createRow("cultures/en-us/company/policy/rights", "A014", "A012 > Customer Service");
categories[8] = createRow("cultures/en-us/company/policy/submission.htm", "A014", "A012 > Customer Service");
categories[9] = createRow("cultures/en-us/home", "A001", "Home");
categories[10] = createRow("cultures/en-us/ministry", "B001", "Ministry");
categories[11] = createRow("cultures/en-us/nationalconvention", "A035", "Events");
categories[12] = createRow("cultures/en-us/newsroom", "A033", "News");
categories[13] = createRow("cultures/en-us/product", "A005", "Product > Product Category General");
categories[14] = createRow("cultures/en-us/product/audio", "B002", "Product > Audio");
categories[15] = createRow("cultures/en-us/product/bible", "B003", "Product > Bible");
categories[16] = createRow("cultures/en-us/product/book", "B004", "Product > Book");
categories[17] = createRow("cultures/en-us/product/curriculum", "B010", "Product > Curriculum");
categories[18] = createRow("cultures/en-us/product/ebook", "B005", "Product > eBook");
categories[19] = createRow("cultures/en-us/product/kidz", "B011", "Product > Kidz");
categories[20] = createRow("cultures/en-us/product/software", "B006", "Product > Software");
categories[21] = createRow("cultures/en-us/product/video", "B007", "Product > Video");
categories[22] = createRow("cultures/en-us/product/youth", "B013", "Product > Teen");
categories[23] = createRow("cultures/en-us/product/index.htm", "A004", "Product > Product Category Landing");  //special cases required for vida and ebook handled later
categories[24] = createRow("cultures/en-us/product/kidz/zonderkidz+home", "A004", "Product > Product Category Landing");
categories[25] = createRow("cultures/en-us/product/productdetail", "A006", "Product > Product Detail");
categories[26] = createRow("cultures/en-us/registration", "A029", "Membership");
categories[27] = createRow("cultures/en-us/search/search.htm", "A024", "Search Result");
categories[28] = createRow("cultures/en-us/search/search.htm?searchmode=advance", "A023", "Search");
categories[29] = createRow("cultures/en-us/use/lead", "B008", "Product > Academic");
categories[30] = createRow("cultures/en-us/use/learn", "B009", "Product > ChurchMinistry");
categories[31] = createRow("cultures/en-us/use/spirit", "B012", "Product > StudyReference");
categories[32] = createRow("zondervan/templates/searchadvancetemplate", "A024", "Search Result");
categories[33] = createRow("/cultures/en-us/events", "A035", "Events");
categories[34] = createRow("/cultures/en-us/events/greek", "B008", "Product > Academic");
categories[35] = createRow("/cultures/en-us/events/hebrew", "B008", "Product > Academic");

var url = location.href.toLowerCase();
var domain = location.host.toLowerCase();
var pathname = location.pathname.toLowerCase();

if(isZTestServer()) {
cmSetClientID("90298644");
}
else {
cmSetClientID("90298644", false, "analytics.zondervan.com");
cmSetProduction();
}
var cmCatId = getCategoryId();
cmCreatePageviewTag(getPageId(),cmCatId,getSearchPhrase(),getSearchResultsNumber(cmCatId),createExploreAttributes(getAuthorsExplore(),getFormatExplore(),domain,"",getAuthorNamesOnly(),"","","","","",getLanguageExplore(),"","","",""));

//if on Z order confirmation page create order tag
if(pathname == "/cultures/en-us/checkout/orderconfirmation.htm") {
	cmCreateOrderTag($.query.get("orderid"), $("#OrderConfirmation1_subtotalLabel").text(), $("#OrderConfirmation1_shippingCostLabel").text(), $("#OrderConfirmation1_orderedBy").text(), $(".city:first").text(), $(".state:first").text(), $(".zip:first").text());
}
