/* CO BRANDED [ header ] - [ pre ] */

/* ::::: ............................................................. global functions
- if (cookie exists OR no cookie but a query string) write out iframe
	- getCookie(), setCookie(), deleteCookie() comes from [cookies.js] loaded earlier in the page
*/
function lookForCookie(updatedCookieValue, cookieTimeToLive, theSourceValue, baseURL, iFramePageSRC, iFrameHeight, iFrameId) {
	var theSourceCookie = getCookie("iMediaHeaders");
	
	if (theSourceCookie) { writeIFrameHeader(iFrameId, baseURL, iFramePageSRC, iFrameHeight); }
	if ((!theSourceCookie) && (theSourceValue)) {
		setCookie("iMediaHeaders", updatedCookieValue, cookieTimeToLive, "/", ".discovery.com");
		writeIFrameHeader(iFrameId, baseURL, iFramePageSRC, iFrameHeight);
	}
}

function writeIFrameHeader (iFrameID, baseURL, iFramePageSRC, iFrameHeight) {
	document.write("<iframe id=\"" + iFrameID + "\" name=\"" + iFrameID + "\" src=\"" + baseURL + iFramePageSRC + "\" width=\"810\" height=\"" + iFrameHeight + "\" style=\"margin: 0px; border: 0px;\" frameborder=\"0\" scrolling=\"no\"></iframe>");
}


/* ::::: ............................................................. set global variables
- get param from query string, set basic vars, establish a date object, look for cookie [iMediaHeaders]
	- getParam() comes from [getParameters.js] loaded earlier in the page
	- msn = [ ?source=msn_channel_uniqueIdentifier ] || [ ?dcitc=w01-000-xx-0000 ]
	- comcast = [ ?dcitc=w04-001-af-0001 ]
*/
var theSourceValue = false;
if (getParam("source")) { theSourceValue = getParam("source"); var splitCharacter = "_"; var theSourceValueSplit = theSourceValue.split(splitCharacter); var primarySplitValue = theSourceValueSplit[0]; }
if (getParam("dcitc")) { theSourceValue = getParam("dcitc"); var splitCharacter = "-"; var theSourceValueSplit = theSourceValue.split(splitCharacter); var primarySplitValue = theSourceValueSplit[0]; }

var iFramePageSRC = "";
var iFrameHeight = "";

var thisDay = new Date();

var theSourceCookie = getCookie("iMediaHeaders");


/* ::::: ............................................................. set MSN variables
- set vars for MSN
	- [ ?source=msn_channel_uniqueIdentifier ] || [ ?dcitc=w01-000-xx-0000 ]
	- array values ["source=[0] value","dcitc=[0] value","page url","header height"]
	- sourse=[1] :: { news, dsc, travel } cml, encarta, kids, women, family, houseohome, entertainment, homepage, outsmart
	- dcitc=[1] :: { 101 - news, 102 - dsc, 103 - travel } content modules, 104 - encarta, 105 - lifestyle, 106 - kids, 107 - entertainment, 108 - travel, 109 - homepage, 110 - outsmart
*/
var baseMSNURL = "http://msn.discovery.com/includes/v9/";
var msnIFrameId = "msntophat";

if ((theSourceValue) && ((primarySplitValue == "msn") || (primarySplitValue == "w01"))) {
	if (primarySplitValue == "msn") { var arrayFocusKey = 0; }
	else { var arrayFocusKey = 1; }
	
	var msnChannels = new Array(["cml",101,"default.html",65],["",102,"default.html",65],["",103,"default.html",65],["encarta",104,"default.html",65],["",105,"default.html",65],["women",000,"default.html",65],["family",000,"default.html",65],["househome",000,"default.html",65],["kids",106,"kidz.html",35],["entertainment",107,"default.html",65],["",108,"default.html",65],["homepage",109,"default.html",65],["outsmart",110,"default.html",65]);
	for (tsv = 0; tsv < msnChannels.length; tsv++) {
		if (theSourceValueSplit[1] == msnChannels[tsv][arrayFocusKey]) { iFramePageSRC = msnChannels[tsv][2]; iFrameHeight = msnChannels[tsv][3]; break; }
	}
	
	// init time to live for cookie [ 1000(milliseconds) * 60(seconds) * 60(minutes) * 24(hours) * 1(days) ]
	var msnCookieDie = new Date();
	msnCookieDie.setTime(thisDay.getTime() + 1000*60*4);
	
	// init cookie value
	if (splitCharacter == "_") { var msnCookieValue = iFrameHeight + "_" + iFramePageSRC + "_" + theSourceValue; }
	else { var msnCookieValue = iFrameHeight + "-" + iFramePageSRC + "-" + theSourceValue; }
	
	/* do not show tophat for kids [iFrameHeight = 35] or travel [theSourceValueSplit[1] = ?dcitc=(w01-108-...), ?source=(msn_travel_...)] */
	if ((iFrameHeight != 35) || ((theSourceValueSplit[1] != 108) || (theSourceValueSplit[1] != "travel"))) {
	if ((theSourceValueSplit[1] != 101) || (theSourceValueSplit[1] != 102) || (theSourceValueSplit[1] != 103)) {
		lookForCookie(msnCookieValue, msnCookieDie, theSourceValue, baseMSNURL, iFramePageSRC, iFrameHeight, msnIFrameId);
	}
	}
}


/* ::::: ............................................................. set COMCAST variables
- set vars for COMCAST
	- comcast = [ ?dcitc=w04-001-af-0001 ]
*/
var baseComcastURL = "http://www.discovery.com/ads/comcast/";
var comcastIFrameID = "comcasttophat";

if (((theSourceValue) && (primarySplitValue == "w04")) || (theSourceCookie)) {
	iFramePageSRC = "default.html"; iFrameHeight = 40;
	
	// init time to live for cookie [ 1000(milliseconds) * 60(seconds) * 60(minutes) * 24(hours) * 1(days) ]
	var comcastCookieDie = new Date();
	comcastCookieDie.setTime(thisDay.getTime() + 1000*60*60*24*1);
	
	// init cookie value
	var comcastCookieValue = iFrameHeight + "-" + iFramePageSRC + "-" + theSourceValue + "-" + 1;
	
	// [1 x through] if (no cookie) set cookie, call global write function
	if (!theSourceCookie) {
		setCookie("iMediaHeaders", comcastCookieValue, comcastCookieDie, "/", ".discovery.com");
		lookForCookie(comcastCookieValue, comcastCookieDie, theSourceValue, baseComcastURL, iFramePageSRC, iFrameHeight, comcastIFrameID);
	}
	
	// [2-3 x through] if cookie [height-src-w04-001-af-0001-count]
	if ((theSourceCookie) && (theSourceCookie.indexOf("w04") != -1)) {
		var theSourceCookieSplit = theSourceCookie.split("-");
		var pagesViewedCount = parseInt(theSourceCookieSplit[6]);
		
		// if (cookie & (count <= 2)) increase count, set updated cookie, call global write function
		if (pagesViewedCount <= 2) {
			pagesViewedCount = pagesViewedCount + 1;
			comcastCookieValue = theSourceCookie.substr(0, 31) + "-" + pagesViewedCount;
			setCookie("iMediaHeaders", comcastCookieValue, comcastCookieDie, "/", ".discovery.com");
			lookForCookie(comcastCookieValue, comcastCookieDie, theSourceValue, baseComcastURL, iFramePageSRC, iFrameHeight, comcastIFrameID);
		} else {
			deleteCookie("iMediaHeaders");
		}
	}
}

/* END CO BRANDED */