/**
 * 
 */

var ReportingManager = {
	REQUEST_PARTS : null,
	STATIC_CONTENT_CONTENT_SECTION : "guides",
	SEPARATOR : ":",
	STATIC_PATHS : {
		"animalplanet" : 1,
		"realtime" : 2,
		"peopleandarts" : 3,
		"travelandliving" : 4
	},
	REGION_PATHS : {
		"nl" : 1, "be" : 2, "se" : 3, "no" : 4, "dk" : 5, "au" : 6, "sea" : 7, "enespanol" : 8
	},
	BRANDS : {
		1 : "Animal Planet",
		2 : "Real Time",
		3 : "People & Arts",
		4 : "Travel & Living"
	},

	DEFAULT_BRAND : "Discovery Channel",

	init : function() {
		ReportingManager.request.parseRequest();
		if (!ReportingManager.logic.startManually()) {
			ReportingManager.start(true);
		}
	},
	start : function(inline) {
		if (!inline) {
			ReportingManager.manual.prepare();
		}
		ReportingManager.reporting.init();
		ReportingManager.omniture.start();
		if (!inline) {
			ReportingManager.manual.cleanup();
		}
	},
	manual : {
		safe : false,
		standardWrite : document.write,
		safeWrite : function(o) {
			document.body.appendChild((function() {
				var e = document.createElement("div");
				e.innerHTML = o;
				e.className = "dniManualReporting";
				return e;
			})());
		},
		prepare : function() {
			(function() {
				if (!this.safe) {
					document.write = this.safeWrite;
					this.safe = true;
				}
			}).apply(ReportingManager.manual);
		},
		cleanup : function() {
			(function() {
				if (this.safe) {
					document.write = this.standardWrite;
					this.safe = false;
				}
			}).apply(ReportingManager.manual);
		}
	},
	reporting : {
		init : function() {
		
			if (window.location.href.match(/^.*www?\.tudiscoverykids\.com/) || window.location.href.match(/^.*www?\.discoverykidsbrasil\.com/) || window.location.href.match(/^.*discoverykidsbrasil\.uol\.com\.br/) || window.location.href.match(/^.*discoverykidsbrasil-staging\.uol\.com\.br/) || window.location.href.indexOf("tudiscoverykids-com.discoveryisispreview2") > -1 || window.location.href.indexOf("discoverykidsbrasil-com.discoveryisispreview2") > -1) {
				s_prop3 = s_eVar3 = ReportingManager.reporting.getProp3Kids()
			} else {
				s_prop3 = s_eVar3 = s_prop2 + ReportingManager.SEPARATOR
						+ ReportingManager.reporting.getProp3();
			}
			
			s_prop7 = s_eVar7 = s_prop1 + ReportingManager.SEPARATOR
					+ ReportingManager.reporting.getProp7();
			s_prop22 = s_eVar22 = ReportingManager.reporting.getProp22();
			s_prop23 = s_eVar23 = ReportingManager.reporting.getProp23();

			if (document.domain.match(/^.*yourdiscovery\.com$/)) {
				s_prop24 = s_eVar24 = ReportingManager.reporting.getProp24();
			}
			
			s_prop35 = s_eVar35 = ReportingManager.reporting.getProp35();

			s_prop33 = s_eVar33 = ReportingManager.reporting.getProp33();

			s_channel = s_prop1 + ReportingManager.SEPARATOR
					+ ReportingManager.reporting.getSChannel();
			s_pageName = ReportingManager.request.getPageName();
			s_campaign = ReportingManager.reporting.getCampaign();
		},
		getProp3 : function() {
			if (ReportingManager.logic.isStaticContent()) {
				return ReportingManager.logic.mapStaticPath();
			} else {
				return ReportingManager.logic.getBrand();
			}
		},
		getProp3Kids : function() {
			if (ReportingManager.logic.isStaticContent()) {
				return "";
			} else {
				return dniReporting.contentType || "";
			}		
		},
		getProp7 : function() {
			if (ReportingManager.logic.isStaticContent()) {
				if (ReportingManager.logic.isBrandedContent() && ReportingManager.logic.isRegionalisedContent()) {
					return ReportingManager.REQUEST_PARTS[3]
				} else if (ReportingManager.logic.isBrandedContent() || ReportingManager.logic.isRegionalisedContent()){
					return ReportingManager.REQUEST_PARTS[2];
				} else {
					return ReportingManager.REQUEST_PARTS[1];
				}
			} else {
				return ReportingManager.logic.getContentArea();
			}
		},
		getProp22 : function() {
			// JAVASCRIPT FILE /t0/javascript/regionmap.js NEEDS TO BE INCLUDED
			// IT CONTAINS THE OBJECT DniRegionMap
			// VAR s_prop21 NEEDS TO BE ON THE PAGE
			return DniRegionMap.getRegion(s_prop21);
		},
		getProp23 : function() {

			var returnValue, userInfo;

			if ((typeof ($TORA) == "undefined")
				|| (typeof (userInfo = $TORA("USER")) == "undefined")) {
				returnValue = "UNKNOWN";
			} else {

				var suffix = " ";
				
				// ADD CHECKS TO SEE IF THE METHODS EXIST TO BATTLE RACE CONDITIONS IN IE
				if (userInfo.isLoggedInViaFB && userInfo.isLoggedInViaFB())
					suffix += "[FBL]";
				
				if(!userInfo.isLoggedIn){
					returnValue = "UNKNOWN";
				} else {
					switch (userInfo.isLoggedIn()) {
					case false:
						returnValue = "USER NOT LOGGED IN";
						break;
					case true:
						returnValue = "USER LOGGED IN" + suffix;
						break;
					default:
						returnValue = "UNKNOWN";
					}
				}
			}

			return returnValue;
		},
		getProp24 : function() {
			return ReportingManager.logic.getPartnerName();
		},
		getProp33 : function() {
			if (ReportingManager.logic.isStaticContent()) {
				return "";
			} else {
				return ReportingManager.logic.getPageType();
			}
		},
		getProp35 : function() {
			if(typeof s_prop35 != "undefined"){
				return s_prop35;
			}
			
			if (typeof dniReporting == "undefined" || typeof dniReporting.localisedSite == "undefined") 
			{
				if(document.location.href.match(/^.*yourdiscovery[-\.]com.*$/)) 
				{
					var pattern = /^http\:\/\/.*?\/(nl|se|no)(\/.*$|$)/;
					var matches = pattern.exec(window.location.href);
					var sitePath = matches ? matches[1] : 'no-match';
	
					var localisationMap = {"nl": "Netherlands", "se": "Sweden", "no": "Norway", "no-match" : "International"};
					return localisationMap[sitePath];
				}	
				else if (document.location.href.match(/^.*tudiscovery[-\.]com.*$/)) 
				{
					var pattern = /^http\:\/\/.*?\/(enespanol|homeandhealth)(\/.*$|$)/;
					var matches = pattern.exec(window.location.href);
					var sitePath = matches ? matches[1] : 'no-match';
	
					var localisationMap = {"enespanol": "EnEspanol", "no-match" : "Latam", "homeandhealth" : "H&H Latam"};
					
					if ((s_prop21=="US") && (sitePath != "homeandhealth")){
						return "EnEspanol";
					} else {
						return localisationMap[sitePath];
					}
					
				}
				else if (document.location.href.match(/^.*discoverybrasil[-\.]com.*$/)) 
				{
					var pattern = /^http\:\/\/.*?\/(homeandhealth)(\/.*$|$)/;
					var matches = pattern.exec(window.location.href);
					var sitePath = matches ? matches[1] : 'no-match';
	
					var localisationMap = {"no-match" : "Brazil", "homeandhealth" : "H&H Brazil"};
					return localisationMap[sitePath];
				}
				else if (document.location.href.match(/^.*discoverybrasil[-\.]uol\.com\.br.*$/)) 
				{
					var pattern = /^http\:\/\/.*?\/(homeandhealth)(\/.*$|$)/;
					var matches = pattern.exec(window.location.href);
					var sitePath = matches ? matches[1] : 'no-match';
	
					var localisationMap = {"no-match" : "Brazil", "homeandhealth" : "H&H Brazil"};
					return localisationMap[sitePath];
				}
				else if (document.location.href.match(/^.*livtv[-\.]com.*$/))
				{
					return "LIV";
				}
				else if (document.location.href.match(/^.*tlcpolska[-\.]com.*$/)) 
				{
					return "TLCPoland";
				}
				else if (document.location.href.match(/^.*discoverymujer[-\.]com.*$/)) 
				{
					return "H&H Latam";
				} 
				else if (document.location.href.match(/^.*discoverymulher[-\.]com.*$/)) 
				{
					return "H&H Brazil";
				} 
				else if (document.location.href.match(/^.*discoverymulher[-\.]uol\.com\.br.*$/)) 
				{
					return "H&H Brazil";
				} 
				else if (document.location.href.match(/^.*tlc-tv[-\.]ru.*$/)) 
				{
					return "TLCRussia";
				} 				
				else  
				{
					return "";
				}
			} 
			else 
			{
				return dniReporting.localisedSite;
			}
		},
		getSChannel : function() {
			if (ReportingManager.logic.isStaticContent()) {
				return ReportingManager.STATIC_CONTENT_CONTENT_SECTION;
			} else {
				return ReportingManager.logic.getContentSection();
			}
		},
		getCampaign : function() {
			return ReportingManager.request.getRequestParameter("campaign");
		}
	},
	logic : {
		getBrand : function() {
			return dniReporting.brand;
		},
		getContentArea : function() {
			return dniReporting.contentArea;
		},
		getContentSection : function() {
			return dniReporting.contentSection;
		},
		getPageType : function() {
			return dniReporting.pageType;
		},
		isPageNameNoQueryString : function() {
			if (typeof dniReporting.flags != "undefined") {
				if (dniReporting.flags.pageNameNoQueryString) {
					return dniReporting.flags.pageNameNoQueryString;
				} else {
					return false;
				}
			}
			return false;
		},
		mapStaticPath : function() {
			var staticPath = ReportingManager.logic.getStaticPath();
			return ReportingManager.logic.mapValue(staticPath,
					ReportingManager.STATIC_PATHS, ReportingManager.BRANDS,
					ReportingManager.DEFAULT_BRAND);
		},
		mapValue : function(key, mapId, mapValue, defaultValue) {
			var itemId = mapId[key];
			var item = mapValue[itemId];
			if (item != null) {
				return item;
			} else {
				return defaultValue;
			}
		},
		isStaticContent : function() {
			if (typeof dniReporting == "undefined") {
				return true;
			} else if ((ReportingManager.logic.getBrand() == "undefined" && dniReporting.contentType == "undefined")
					|| ReportingManager.logic.getContentArea() == "undefined"
					|| ReportingManager.logic.getContentSection() == "undefined") {
				return true;
			} else {
				return false;
			}
		},
		// TO CHECK STATIC CONTENT SITTING BEHIND BRAND URL
		isBrandedContent : function() {
			var staticPath = ReportingManager.logic.getStaticPath();
			var brandId = ReportingManager.STATIC_PATHS[staticPath];
			if (brandId != null) {
				return true;
			} else {
				return false;
			}
		},
		isRegionalisedContent : function() {
			var regionPath = ReportingManager.REQUEST_PARTS[1];
			var region = ReportingManager.REGION_PATHS[regionPath];
			
			return region != null ? true : false;
		},
		startManually : function() {
			if (typeof dniReporting != "undefined"
					&& typeof dniReporting.startManually != "undefined") {
				return true;
			} else {
				return false;
			}
		},
		getStaticPath : function() {
			if(this.isRegionalisedContent()) 
				return ReportingManager.REQUEST_PARTS[2]
			
			return ReportingManager.REQUEST_PARTS[1];
		},
		getStaticContentArea : function() {
			if(this.isRegionalisedContent()) 
				return ReportingManager.REQUEST_PARTS[3]
			return ReportingManager.REQUEST_PARTS[2];
		},
		cleanPageName : function(url) {
			var queryReplacements = ["cc", "campaign"],
				exp, re, newPageName = url;

			for(var i = 0, qr; (qr = queryReplacements[i]); i++ ){
				exp = "&*"+ qr +"=([^&]*)";
				re = new RegExp(exp,"i");
				newPageName = newPageName.replace(re, "");
			}
			newPageName = newPageName.replace(/\?$|\?(?=#)/, "");
			newPageName = newPageName.replace(/\?&/, "?");
			return newPageName;
		},
		getPartnerName : function() {
			var returnValue = null;
			var dHREF = location.href.toLowerCase();
			if (dHREF.indexOf("partner=") != -1) {
				var startchar = (dHREF.indexOf("partner") + 8);
				var querystringend = dHREF.substring(startchar);
				if (querystringend.indexOf("&") != -1) {
					var sep_pos = (querystringend.indexOf("&") + startchar);
					var partner = dHREF.substring(startchar, sep_pos);
				} else {
					var partner = dHREF.substring(startchar);
				}
				returnValue = partner;
			}
			return returnValue;
		}
	},
	request : {
		parseRequest : function() {
			ReportingManager.REQUEST_PARTS = window.location.pathname
					.split("/");
		},
		getPageName : function() {
			// THIS STATEMENT STRIPS THE PROTOCOL AND FRAGMENT IDENTIFIER FROM
			// THE CURRENT URL
		var defaultPageName = window.location.href.replace(/^(?:http\:\/\/|)(.*?)(?:#.*$|$)/, "$1");
		if (typeof dniReporting != "undefined") {
			if (ReportingManager.logic.isPageNameNoQueryString()) {
				var urlParts = defaultPageName.split("?");
				return urlParts[0];
			} else {
				return ReportingManager.logic.cleanPageName(defaultPageName);
			}
		}
		return defaultPageName;
	},
	getRequestParameter : function(name) {
		var regexS = "[\\?&]" + name + "=([^&#]*)";
		var regex = new RegExp(regexS);
		var results = regex.exec(window.location.href);
		if (results != null) {
			return results[1];
		}
		return "";
	}
	},
	omniture : {
		start : function() {
			// THIS FUNCTION IS INSIDE "omniture.js"
	omnitureStart();
}
}
}
ReportingManager.init();

