﻿// JavaScript Document
function gtvFollowtoggle(num, tabId, int) {
	var i = 1; while (i <= int) {
		if (num != 1) {
			document.getElementById(tabId + '1').className = 'null';
		}
		if (num == i) {
			document.getElementById(tabId + i).className = 'selected';
			document.getElementById("div_" + tabId + i).style.display = "block";
		} else {
			document.getElementById(tabId + i).className = 'null';
			document.getElementById("div_" + tabId + i).style.display = "none";
		}
		i++;
	}
}

function submitFormActionFooter() {
	var keywords = document.getElementById("searchbox-footer").value;
	searchlink = searchlink + "" + keywords;
	top.location.href = searchlink;
	return false;
}	

//Player companion banner code
var adShown = false;

function ShowAd() {
	$('.companionad').show("slide", { direction: "left" }, 640);
	adShown = true;
}

function HideAd() {
	if (adShown == true) {
		$('.companionad').hide();
		adShown = false;
	}
}

//Dynamically loading a brightcove player
function GTVLoadPlayer(name, videoID, playerID, width, height, subsection) {
	var params = {};
	params.playerID = playerID;
	params.videoId = videoID;
	params.autoStart = "true";
	params.bgcolor = "#000000";
	params.width = width;
	params.height = height;
	params.isVid = 1;
	params.isUI = 1;
	params.dynamicStreaming = true;
	params.publisherID = BCSetting.publisherID;
	params.omnitureAccountID = BCSetting.omnitureAccountID;
	params.marketName = BCSetting.marketName;
	params.division = "Broadcast";
	params.pageContentCategory = "video";
	params.adServerURL = BCSetting.adServerURL;
	params.pageContentSubcategory = subsection;
	params.SSTSCode = BCSetting.SSTS;

	var player = brightcove.createElement("object");
	player.id = params.videoId;
	var parameter;
	for (var i in params) {
		parameter = brightcove.createElement("param");
		parameter.name = i;
		parameter.value = params[i];
		player.appendChild(parameter);
	}
	//following is needed to make html5 player work
	parameter = brightcove.createElement("param");
	parameter.name = "@videoPlayer";
	parameter.value = params.videoId;
	player.appendChild(parameter);

	var playerContainer = document.getElementById(name);
	if (playerContainer != null) {
		while (playerContainer.hasChildNodes()) {
			playerContainer.removeChild(playerContainer.lastChild);
		}
		brightcove.createExperience(player, playerContainer, false);
		document.getElementById(name).style.backgroundImage = "none";
		document.getElementById(name).style.width = "0";
		document.getElementById(name).style.height = "0";
	}
}

//Video functions
function LoadNextVideo() {
	BCGannett.videoPlayerModule.loadVideo(NextUpVideos[curindex]);
	nextVideo(NextUpVideos[curindex]);
	curindex = curindex + 1;
}

function PlayerBC() { };

PlayerBC.prototype = {
	onTemplateReady: function () {
		BCGannett.advertisingModule.enableExternalAds();
	},
	onMediaChange: function (pEvent) {
		var immersiveUrl = [
			BCSetting.immersiveBaseURL,
			BCGannett.videoPlayerModule.getCurrentVideo().id
		].join("");
		BCGannett.socialModule.getEmbedCode();
		BCGannett.socialModule.setLink(immersiveUrl);
	},
	getSSTSCode: function () {
		return BCGannett.experienceModule.getPlayerParameter("SSTSCode");
	},

	playerAPIInit: function (pExperienceID) {
		this.bcPlayer;
		this.experienceModule;
		this.videoPlayerModule;
		this.advertisingModule;
		this.contentModule;
		this.socialModule;
		this.cuePointsModule;

		this.bcPlayer = brightcove.getExperience(pExperienceID);
		this.experienceModule = this.bcPlayer.getModule(APIModules.EXPERIENCE);
		this.experienceModule.addEventListener(BCExperienceEvent.TEMPLATE_READY, this.onTemplateReady);

		this.videoPlayerModule = this.bcPlayer.getModule(APIModules.VIDEO_PLAYER);
		this.videoPlayerModule.addEventListener(BCMediaEvent.CHANGE, BCGannett.onMediaChange);
		this.videoPlayerModule.addEventListener(BCMediaEvent.COMPLETE, BCGannett.onMediaComplete);

		this.contentModule = this.bcPlayer.getModule(APIModules.CONTENT);
		BCGannett.contentModule.addEventListener(BCContentEvent.MEDIA_LOAD, BCGannett.onGetMedia);

		this.socialModule = this.bcPlayer.getModule(APIModules.SOCIAL);
		this.socialModule.addEventListener(BCSocialEvent.EMBED_CODE_RETRIEVED, BCGannett.onEmbedCodeRetrieved);

		this.advertisingModule = this.bcPlayer.getModule(APIModules.ADVERTISING);
		this.advertisingModule.addEventListener(BCAdvertisingEvent.EXTERNAL_AD, BCGannett.onExternalAd);
		this.advertisingModule.addEventListener(BCAdvertisingEvent.AD_COMPLETE, BCGannett.onAdComplete);

		if (BCSetting.overlayson == true) {
			this.cuePointsModule = this.bcPlayer.getModule(APIModules.CUE_POINTS);
			this.cuePointsModule.addEventListener(BCCuePointEvent.CUE, this.onOverlayAd);
			this.videoPlayerModule.addEventListener(BCMediaEvent.BEGIN, this.onMediaBegin);
		}
	},

	onMediaComplete: function (pEvent) {
		if (BCSetting.isImmersive != null) {
			setTimeout("LoadNextVideo();", 8000);
		}
	},

	onMediaBegin: function (pEvent) {
		if (!BCGannett.firstVideoPlayed) BCGannett.firstVideoPlayed = true;

		var _adPolicy = {};
		_adPolicy = BCGannett.advertisingModule.getAdPolicy();

		if (_adPolicy) {
			BCGannett._adsEnabled = true;
		} else {
			BCGannett._adsEnabled = false;
		}


		var _vid = BCGannett.videoPlayerModule.getCurrentVideo();
		var _cue = BCGannett.cuePointsModule.getCuePoints(_vid.id);

		if (BCGannett._adsEnabled == true && _vid.economics != 0) {
			if (_cue) {
				BCGannett.cuePointsModule.clearCodeCuePoints(_vid.id);
				BCGannett.cuePointsModule.clearAdCuePoints(_vid.id)
			}

			BCGannett.cuePointsModule.addCuePoints(_vid.id,
					BCGannett._overlay.createArray({
						name: "Pre-roll",
						time: 0,
						type: 0
					})
				);

			BCGannett.cuePointsModule.addCuePoints(_vid.id,
					BCGannett._overlay.createArray({
						name: "overlay",
						time: BCGannett._overlay.getOverlayBegin(),
						type: 1
					})
				);

			BCGannett.cuePointsModule.addCuePoints(_vid.id,
					BCGannett._overlay.createArray({
						name: "Post-roll",
						time: _vid.length,
						type: 0
					})
				);


			if (BCGannett._madeAdRequest == false) {

				BCGannett._overlay.setJustOverlays();

				var adPolicy = new Object();
				adPolicy.adServerURL = BCGannett.experienceModule.getPlayerParameter("adServerURL");
				BCGannett.advertisingModule.setAdPolicy(adPolicy);
				BCGannett.advertisingModule.requestAd(adPolicy.adServerURL);
			}
		}
	},

	onOverlayAd: function (pData) {
		if (typeof pData.cuePoint == "undefined") { return; }

		if (BCGannett._adsEnabled == false) { return; }

		if (pData.cuePoint.type != 1) { return; }

		if (BCGannett._overlay.isRoadBlock() == false && BCGannett._madeAdRequest == false) {
			var adPolicy = new Object();
			adPolicy.adServerURL = BCGannett.experienceModule.getPlayerParameter("adServerURL");
			BCGannett.advertisingModule.setAdPolicy(adPolicy);
			BCGannett.advertisingModule.requestAd(adPolicy.adServerURL);
		} else {
			BCGannett.advertisingModule.showAd(BCGannett._overlay.getOverlayAdData());
		}

	},

	onExternalAd: function (pAdString, callback) {
		if (typeof ExternalAd == "function") {
			var _opt = {
				expandedId: "adframe",
				companionId: "companionad",
				adUrl: BCSetting.adServerURL,
				alias: BCSetting.companionAlias,
				iframeId: "iframe_ad"
			};
			new ExternalAd(pAdString, _opt);
		} else {
			BCGannett.advertisingModule.resumeAfterExternalAd();
		}
	},

	onAdComplete: function (evt) {
		if (BCSetting.isImmersive == null) {
			HideAd();
		}
	},

	onEmbedCodeRetrieved: function (pEvent) {
		var snippet = pEvent.snippet;

		if (!BCGannett.originalFlashVars) {
			BCGannett.originalFlashVars = snippet.split('flashVars="')[1].split('"')[0];
		}

		var newFlashVars = 'omnitureAccountID=' + BCGannett.experienceModule.getPlayerParameter("omnitureAccountID") + '&' +
			'pageContentCategory=' + BCGannett.experienceModule.getPlayerParameter("pageContentCategory") + '&' +
			'pageContentSubcategory=' + BCGannett.experienceModule.getPlayerParameter("pageContentSubcategory") + '&' +
			'marketName=' + BCGannett.experienceModule.getPlayerParameter("marketName") + '&' +
			'division=' + BCGannett.experienceModule.getPlayerParameter("division") + '&' +
			'SSTSCode=' + BCGannett.experienceModule.getPlayerParameter("SSTSCode") + '&' +
			BCGannett.originalFlashVars + '\"';

		var objectFlashVars = '<param name="flashVars" value="' + newFlashVars;
		var embedFlashVars = 'flashVars="' + newFlashVars;

		snippet = snippet.replace(new RegExp('<param name="flashVars" value="[^ \t\v\n\r\f]*"'), objectFlashVars);
		snippet = snippet.replace(new RegExp('flashVars="[^ \t\v\n\r\f]*"'), embedFlashVars);

		BCGannett.socialModule.setEmbedCode(snippet);
	}
};


//Article functions
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (1) {
			curleft += obj.offsetLeft;
			if (!obj.offsetParent) {
				break;
			}
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (1) {
			curtop += obj.offsetTop;
			if (!obj.offsetParent) {
				break;
			}
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}


var timeout = 500;
var closeTimer = 0;

function displaySharingLayer() {

	cancelCloseTime();
	if (document.getElementById("sharinglayer").style.display == "none") {
		document.getElementById("sharinglayer").style.display = "block";
	} else {
		closeTime()
	}
}

function collapseLayer() {
	document.getElementById("sharinglayer").style.display = "none";
}

function closeTime() {
	closeTimer = window.setTimeout(collapseLayer, timeout);
}

function cancelCloseTime() {
	if (closeTimer) {
		window.clearTimeout(closeTimer);
		closeTimer = null;
	}
}

function setSharingLayer() {
	document.getElementById("sharinglayer").style.left = findPosX(document.getElementById("gtv_toolsArticle")) - 0;
	document.getElementById("sharinglayer").style.top = findPosY(document.getElementById("gtv_toolsArticle")) + 0;
}

var max = 18;
var min = 12;
var size = 14;

function article_IncreaseFontsize() {
	article_getObj();
	if (size != max) {
		size = size + 2;
		parasInDiv = stObj.getElementsByTagName("p");
		for (var i = 0; i < parasInDiv.length; i++) {
			parasInDiv[i].style.fontSize = size + "px";
		}
	} else {
		alert("You are already at the maximum size!");
	}
}

function article_DecreaseFontsize() {
	article_getObj();
	if (size != min) {
		size = size - 2;
		parasInDiv = stObj.getElementsByTagName("p");
		for (var i = 0; i < parasInDiv.length; i++) {
			parasInDiv[i].style.fontSize = size + "px";
		}
	} else {
		alert("You are already at the minimum size!");
	}
}

function article_getObj() {
	stObj = (document.getElementById) ? document.getElementById('gtv_article_container') : document.all('gtv_article_container');
}
