/* (C) 2008 - 2011 coincident.tv - don't remove the below lines*/
	var winWidth = $(window).width()+"px";
	var winHeight = $(window).height()+"px";
	var centeredLeft = "0px";
	var baseBckgndURL = "defaultSkin.html";
	var urlQString = location.search;
	var jReady = false;
	var webPointURL = "";
	var reloadFF = false;
	var adRequested = false;
	var inPIP = false;
	var visibilityTimer;
	var tempMSIEFix;
	var resizeTimerCTV;
	var CTV = {
		constructors: {},
		vars: {},
		funcs: {}
		};
	
    function onCTVPlayerLoad() {
		jReady = true;
		postDebugMsg("browser info :\t"+ whichBrowserIs());
		showCTVswf(false);
		showCompanionAd();
		clearHTMLDiv();
		readyHTMLDiv();
		centerLoader();
		visibilityTimer = setInterval("checkAdVisibility()",1000);
	}
	
	function setWindowMargin(wmT, wmR, wmB, wmL) {
		if(varIsSet(wmT) && (!varIsSet(wmR) && !varIsSet(wmB) && !varIsSet(wmL))) {
			CTV.vars.windowMarginTop = parseInt(wmT);
			CTV.vars.windowMarginRight = parseInt(wmT);
			CTV.vars.windowMarginBottom = parseInt(wmT);
			CTV.vars.windowMarginLeft = parseInt(wmT);
		}
		
		if(varIsSet(wmT) && varIsSet(wmR) && (!varIsSet(wmB) && !varIsSet(wmL))) {
			CTV.vars.windowMarginTop = parseInt(wmT);
			CTV.vars.windowMarginRight = parseInt(wmR);
			CTV.vars.windowMarginBottom = parseInt(wmT);
			CTV.vars.windowMarginLeft = parseInt(wmR);
		}
		
		if(varIsSet(wmT) && varIsSet(wmR) && varIsSet(wmB) && varIsSet(wmL)) {
			CTV.vars.windowMarginTop = parseInt(wmT);
			CTV.vars.windowMarginRight = parseInt(wmR);
			CTV.vars.windowMarginBottom = parseInt(wmB);
			CTV.vars.windowMarginLeft = parseInt(wmL);
		}
	}
	
	function addEvent(elem, type, eventHandle) {
	    if (elem == null || elem == undefined) {
	    	alert(elem+" doesn't exist");
	    	return;
	    }
 		if ( elem.addEventListener ) {
    		elem.addEventListener( type, eventHandle, false );
    	} else if ( elem.attachEvent ) {
        	elem.attachEvent( "on" + type, eventHandle );
    	}
	}
	
    //-------------------------------------------------------------------------
    function isIE7()
    {
    	return navigator.userAgent.indexOf("MSIE 7.0") != -1;
    }
    //-------------------------------------------------------------------------
	// called from ExternalInterface in VideoViaBrightcove
	function setHash(hash) {
		window.location.hash = hash;
	}
	function getAdVisible() {
		return adRequested;
	}
	function getAdHTML() {
		var html = document.getElementById('adCompanionDiv').innerHTML;
		postDebugMsg("getting ad of: ", html);
		return html;
	}
	function setAdHTML(html, showAd) {
		postDebugMsg("setting ad to:", html);
		document.getElementById('adCompanionDiv').innerHTML = html;
		adRequested = showAd;
		showCompanionAd();
	}	
	
	function checkAdVisibility() {
		if((!adRequested) && $("#adBlock").css("visibility") == "visible" ) {
			postDebugMsg("*** ad visible ***");
			adRequested = true;
			setCompanionAdVisible();
		}
	}
	
	function setCompanionAdVisible () {
		postDebugMsg("companionAd visible");
		$("#adCompanionDiv").css("visibility", "visible");
		$("#playerAdBg").css("visibility", "visible");
		$("#adBlock").css("visibility", "visible");
		$("#ad").css("visibility", "visible");
		positionCompanionAd();
	}

	function onNewCTV() {
		adRequested = false;
		showCompanionAd();
	}
	
	function setCompanionAdHidden () {
		postDebugMsg("companionAd hidden");
		$("#adCompanionDiv").css("visibility", "hidden");
		$("#playerAdBg").css("visibility", "hidden");
		$("#adBlock").css("visibility", "hidden");
		$("#ad").css("visibility", "hidden");
		positionCompanionAd();
	}
	
	function showCompanionAd() {
		if (adRequested && !inPIP) {
			postDebugMsg("showing companionAd");
			setCompanionAdVisible();
		} else {
			postDebugMsg("hidding companionAd");
			setCompanionAdHidden();
		}
	}
	
	function onAdVisibilityChange(divChanged) {
		alert("visibility changed at "+divChanged);
		postDebugMsg("companioAd values: \nadCompanionDiv: "+$("#adCompanionDiv").css("visibility")+"\nplayerAdBg: "+$("#playerAdBg").css("visibility")+"\nadBlock: "+$("#adBlock").css("visibility")+"\nad: "+$("#ad").css("visibility"));	
		if ($(divChanged).css("visibility") == "visible") {
			adRequested = true;
			clearInterval(visibilityTimer);
		}
		postDebugMsg("==== companionAd visibility changed to => " + adRequested);
	}
	
	function whichBrowserIs() {
		var browserCodeName = navigator.appCodeName;
		var browserName = navigator.appName;
		var browserVersion = navigator.appVersion;
		var browserCookieEnabled = navigator.cookieEnabled;
		var browserPlatform = navigator.platform;
		var browserUserAgent = navigator.userAgent;
		
		var runningBrowser;
		postDebugMsg(browserUserAgent);
		if(browserUserAgent.indexOf("Firefox") != -1) {
			return "FireFox";
		}
		else
			return "Other";
	}

	function thisMovie(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	}
	 
	function sendToActionScript(value) {
	thisMovie("CTVswfCont").sendToActionScript(value);
	}
	
	function resizeStart() {
		window.clearTimeout(resizeTimerCTV);
		resizeTimerCTV = window.setTimeout("onWinResize()", 150);
	}
     
	function onWinResize() {
		try {
		thisMovie("CTVPlayer").onWinResize();
		} catch(e) {
//			
		}
		if(document.getElementById('CTVbckgndiFrame')) {
			layoutBackground();
			changeBackground($("#CTVbckgndiFrame").attr("src"), true);
		}
		window.clearTimeout(resizeTimerCTV);
	}
		 
	 function isReady() {
		return jReady;
	 }
	 
	 function stageWidth() {
	 	var windowWidth;
	 	
		if($(window).width() < window.innerWidth)
			windowWidth = window.innerWidth;
		else
	 		windowWidth =  $(window).width();
	 	windowWidth -= (CTV.vars.windowMarginLeft + CTV.vars.windowMarginRight);
	 	return windowWidth;
	 }
	 
	 function stageHeight() {
	 	var windowHeight;
	 	
		if($(window).height() < window.innerHeight)
			windowHeight = window.innerHeight;
		else
	 		windowHeight = $(window).height();
	 	windowHeight -= (CTV.vars.windowMarginTop + CTV.vars.windowMarginBottom);
	 	return windowHeight;
	 }
	 
	function showCTVswf(showSWF) {
		if(showSWF) {
			killLoader();
			$("#CTVswfCont").animate({opacity:1},500);
			$("#CTVswfArea").animate({opacity:1},500);
		}
		else {
			$("#CTVswfCont").css("opacity","0");
			$("#CTVswfArea").css("opacity","0");
		}
	}
	
	function centerLoader() {
		centeredLeft = ((stageWidth() - $("#CTVLoaderCont").width())/2)+ CTV.vars.windowMarginLeft +"px";
		centeredTop = ((stageHeight() - $("#CTVLoaderCont").height())/2)+ CTV.vars.windowMarginTop +"px";
		$("#CTVLoaderCont").css("left",centeredLeft);
		$("#CTVLoaderCont").css("top", centeredTop);
	}
	
	function killLoader() {
		var preLoadDiv = document.getElementById('CTVLoaderCont');
		preLoadDiv.parentNode.removeChild(preLoadDiv);
	}

	function pipStateDiv() {
		var relHeight;
		if(($("#CTVrenderHTML").css("height") == $(window).height()+"px") && ($("#CTVrenderHTML").css("height").search(/px/i))) {
			relHeight = ($(window).height() - ($("#CTVrenderHTML").css("top").substr(0,$("#CTVrenderHTML").css("top").search(/px/i)))) + "px";
			$("#CTVrenderHTML").css("height",relHeight);								   
		}
	}

	function webPointStateDiv(strURL) {
		readyHTMLDiv();
		
		if (!isIE7()) // IE7 doesn't work well with opacity; won't use it.
		{
			$("#CTVrenderHTML").css("opacity", "0");
		}
		$("#CTVrenderHTML").attr("src","");
		webPointURL = strURL;
	}
	
	function showCTVWebPoint() {
		if(webPointURL != "") {
			$("#CTVrenderHTML").attr("src",webPointURL);
			postDebugMsg("redndering web point : "+webPointURL);
			postDebugMsg("history stack count: "+history.length);
			webPointURL = "";
			if (!isIE7())  // IE7 doesn't work well with opacity; won't use it.
			{
				$("#CTVrenderHTML").animate({opacity:1},1200);
			}
		}
	}
			
	function changeSWFDim(arrSWFChange) {
		postDebugMsg("transition SWF from: \n - width: "+ $("#CTVswfCont").css("width") +"\n- height: " + $("#CTVswfCont").css("height") + "\n- left: " + $("#CTVswfCont").css("left") + "\n- top: " + $("#CTVswfCont").css("top") + "\n- state: " + arrSWFChange['state'] + "\n- video scale: "+ arrSWFChange['vidScale'] +"\nto: \nCTV position("+arrSWFChange['x']+", "+arrSWFChange['y']+")\nCTVsize("+arrSWFChange['width']+"px - "+arrSWFChange['height']+"px)");
		
		if($("#CTVswfCont").css("width") == arrSWFChange['width']+"px" && $("#CTVswfCont").css("height") == arrSWFChange['height']+"px" && $("#CTVswfCont").css("left") == arrSWFChange['x'] && $("#CTVswfCont").css("top") == arrSWFChange['y'] && arrSWFChange['state'] == "web") {
			postDebugMsg("already in PIP mode");
			showCTVWebPoint();
		}
		if($("#CTVswfCont").css("width") != arrSWFChange['width']+"px" || $("#CTVswfCont").css("height") != arrSWFChange['height']+"px" || $("#CTVswfCont").css("left") != arrSWFChange['x'] || $("#CTVswfCont").css("top") != arrSWFChange['y']) {
			if(arrSWFChange['x'] == 0 && arrSWFChange['y'] == 0)
			{
				centeredLeft = (($(window).width() - arrSWFChange['width'])/2)+CTV.vars.windowMarginLeft+"px";
				centeredTop = (($(window).height() - arrSWFChange['height'])/2)+CTV.vars.windowMarginTop+"px";
				postDebugMsg("transitioning SWF to: \nCTV position("+centeredLeft+", "+centeredTop+")\nCTVsize("+arrSWFChange['width']+"px - "+arrSWFChange['height']+"px)");
				$("#CTVswfCont").animate({width:arrSWFChange['width']+"px", height:arrSWFChange['height']+"px",top:centeredTop, left:centeredLeft}, { queue:false, duration:500});			
			}
			else
			{
				postDebugMsg("current state:"+arrSWFChange['state']);
				$("#CTVswfCont").css("width", arrSWFChange['width']+"px")
								.css("height", arrSWFChange['height']+"px")
								.css("top", arrSWFChange['y']+CTV.vars.windowMarginTop+"px")
								.css("left", arrSWFChange['x']+CTV.vars.windowMarginLeft+"px");
				showCTVWebPoint();
				CTV.vars.width = arrSWFChange['width']+"px";
				CTV.vars.height = arrSWFChange['height']+"px";
				CTV.vars.top = arrSWFChange['y']+CTV.vars.windowMarginTop+"px";
				CTV.vars.left = arrSWFChange['x']+CTV.vars.windowMarginLeft+"px";
				CTV.vars.videoScale = arrSWFChange['vidScale'];
				if(userAgentMatches("Windows","MSIE")) {
					tempMSIEFix = setTimeout("msIESizeFix()",1000);
				}
			}
			positionCompanionAd();
		}
	}
	
	function positionCompanionAd() {
		var currentCTVWidth = $("#CTVswfCont").css("width");
		var currentCTVHeight = $("#CTVswfCont").css("height");
		var currentCTVX = $("#CTVswfCont").css("left");
		var currentCTVY = $("#CTVswfCont").css("top");
		var currentCompanionAdWidth = $("#adCompanionDiv").css("width");
		var currentCompanionAdHeight = $("#adCompanionDiv").css("height");
		var currentCompanionAdX = $("#adCompanionDiv").css("top");
		var currentCompanionAdY = $("#adCompanionDiv").css("left");
		var newCompanionAdX = parseInt(currentCTVX) + parseInt(currentCTVWidth) + 10;
		var videoScale = CTV.vars.videoScale || '0';
		
		postDebugMsg("[inside posCompAd] video scale: " + videoScale);
		inPIP = (parseInt(videoScale) < .75);
		if (inPIP || !adRequested) {
			$("#adCompanionDiv").css("left", "-10000px");
			$("#adCompanionDiv").css("top", currentCTVY);
			$("#ad").css("left", "-10000px");
			$("#adBlock").css("left", "-10000px");
			postDebugMsg("companionAd moved to: "+$("#adCompanionDiv").css("left")+", "+$("#adCompanionDiv").css("top"));
		} else {
			$("#adCompanionDiv").css("left", newCompanionAdX+"px");
			$("#adCompanionDiv").css("top", currentCTVY);
			postDebugMsg("companionAd moved to: "+$("#adCompanionDiv").css("left")+", "+$("#adCompanionDiv").css("top"));
		}
	}
	
	function newDimensionsHTML(arrHTMLDim) {
		postDebugMsg("resize HTML to: "+arrHTMLDim['width']+"px"+" x "+arrHTMLDim['height']+"px");
		if($("#CTVHTMLDiv").css("width") != arrHTMLDim['width']+"px" || $("#CTVHTMLDiv").css("height") != arrHTMLDim['height']+"px") {
			$("#CTVHTMLDiv").animate({width:arrHTMLDim['width']+"px"},{queue:false, duration: 200})
		 					.animate({height:arrHTMLDim['height']+"px"},200);
		 }
	 }
	
	function newPositionHTML(arrHTMLPos) {
		postDebugMsg("reposition HTML to: "+arrHTMLPos['x']+", "+arrHTMLPos['y']);
		if ($("#CTVHTMLDiv").css("left") != arrHTMLPos['x'] || $("#CTVHTMLDiv").css("top") != arrHTMLPos['y']) {
			$("#CTVHTMLDiv").css("left",(parseInt(arrHTMLPos['x'])+CTV.vars.windowMarginLeft)+"px");
			$("#CTVHTMLDiv").css("top",(parseInt(arrHTMLPos['y'])+CTV.vars.windowMarginTop)+"px");
		}
	}

	function startHTMLDiv() {
		$("#CTVrenderHTML").removeClass("CTVwebPointRenderV");
		$("#CTVrenderHTML").addClass("CTVwebPointRenderH");
	}
	
	function clearHTMLDiv() {
		deleteCTVCloseButton();
		var iFrame2Delete = document.getElementById('CTVrenderHTML');
		if (iFrame2Delete != null)
			iFrame2Delete.parentNode.removeChild(iFrame2Delete);
		
		var div4iFrame = document.getElementById('CTVHTMLDiv');
		var iFrameHeader = document.createElement('IFRAME');
		
		iFrameHeader.id = "CTVrenderHTML";
		iFrameHeader.className = "CTVwebPointRenderH";
		iFrameHeader.frameBorder = "0";
		iFrameHeader.allowTransparency = "true";
		div4iFrame.style.width = "0px";
		div4iFrame.style.height = "0px";
		div4iFrame.appendChild(iFrameHeader);
	}
	
	function removeHTMLDiv() {
		var iFrame2Delete = document.getElementById('CTVrenderHTML');
		if (iFrame2Delete != null) {
			iFrame2Delete.parentNode.removeChild(iFrame2Delete);
		var iFrameDeleted = document.getElementById('CTVrenderHTML');
		}
	}
	
	function readyHTMLDiv() {
		$("#CTVrenderHTML").attr("src","");
		$("#CTVrenderHTML").removeClass("CTVwebPointRenderH");
		$("#CTVrenderHTML").addClass("CTVwebPointRenderV");
	}
	
	function centerSWF() {
		centeredLeft = (($(window).width() - $("#CTVswfCont").width())/2)+CTV.vars.windowMarginLeft+"px";
		centeredTop = (($(window).height() - $("#CTVswfCont").height())/2)+CTV.vars.windowMarginTop+"px";
		$("#CTVswfCont").animate({left:centeredLeft, top:centeredTop},{queue:false, duration:500});
	}
	
	function changeBackground(strBckgnd, winResize) {
		var strURL = "";
		
		if(typeof(strBckgnd) == "undefined") return;
		postDebugMsg("+ request = change background to: "+strBckgnd);
		if ($("#CTVbckgndiFrame").attr("src") == strBckgnd && (!winResize)) {
			postDebugMsg("\t- background already set to "+strBckgnd);
		} else {
			postDebugMsg("CTVbckgndiFrame div class: "+document.getElementById("CTVbckgndiFrame"));
			if(document.getElementById('CTVbckgndiFrame') != null) {
				postDebugMsg("\t- iFrame exists, deleting");
				var iFrame2Delete = document.getElementById('CTVbckgndiFrame');
				postDebugMsg("\t- iFrame2Delete = "+iFrame2Delete.id);
				iFrame2Delete.parentNode.removeChild(iFrame2Delete);
				postDebugMsg("\t- looking for the iFrame: "+document.getElementById('CTVbckgndiFrame'));
				if(document.getElementById('CTVbckgndiFrame') == null) {
					postDebugMsg("\t- deleted ...");
				} else {
					postDebugMsg("\t- still here ...");
				}
			}
			postDebugMsg("	- iFrame doesn't exists ... creating");
			layoutBackground();
			var div4iFrame = document.getElementById('CTVbckgndArea');
			var iFrameHeader = document.createElement('IFRAME');
			iFrameHeader.id = "CTVbckgndiFrame";
			iFrameHeader.className = "CTVemptyBckgnd";
			iFrameHeader.scrolling = "no";
			iFrameHeader.frameBorder = "0";
			iFrameHeader.opacity = 0;
			iFrameHeader.src = strBckgnd;
			div4iFrame.appendChild(iFrameHeader);
			$("#CTVbckgndiFrame").animate({opacity:1}, 500);
			postDebugMsg("\t- iFrame created, loading bckgnd: "+strBckgnd);
		}
	}
	
	function createCTVCloseButton(closeButtonTop, closeButtonRight) {
		postDebugMsg("creating close button DIV");
		deleteCTVCloseButton();
		var closeButtonDiv = document.createElement('DIV');
		
		closeButtonDiv.id = "CTVCloseButtonDiv";
		closeButtonDiv.className = "CTVCloseButtonDiv";
		closeButtonDiv.style.top = "1px";
		closeButtonDiv.style.right = "1px";
		closeButtonDiv.title = "Click here to close";
		document.getElementById('CTVHTMLDiv').appendChild(closeButtonDiv);
		
		postDebugMsg("creating close IMG");
		var closeButtonImage = document.createElement('IMG');
		
		closeButtonImage.id = "CTVCloseButton";
		closeButtonImage.className = "CTVCloseButton";
		closeButtonImage.src = ('ui/CTVCloseButton.png');
		document.getElementById('CTVCloseButtonDiv').appendChild(closeButtonImage);
		closeButtonImage.onclick = companionCTVWebPoint;
		closeButtonImage.onmouseover = onMouseOverCTVCloseButton;
		closeButtonImage.onmouseout = onMouseOutCTVCloseButton;
	}
	
	function deleteCTVCloseButton() {
		var closeButtoniFrame = document.getElementById('CTVCloseButtonDiv');
		if(closeButtoniFrame != null)
			closeButtoniFrame.parentNode.removeChild(closeButtoniFrame);
		
		var closeButtonImage = document.getElementById('CTVCloseButton');
		if(closeButtonImage != null) {
			closeButtonImage.parentNode.removeChild(closeButtonImage);
			alert("button deleted");
		}
	}
	 
	function companionCTVWebPoint() {
		thisMovie("CTVPlayer").companionCTVWebPoint();
	}
	
	function onMouseOverCTVCloseButton() {
		var CTVCloseButton = document.getElementById('CTVCloseButton');
		if(CTVCloseButton) {
			CTVCloseButton.src = 'ui/CTVCloseButton-over.png';
			CTVCloseButton.style.cursor = "pointer";
		}
	}
	
	function onMouseOutCTVCloseButton() {
		var CTVCloseButton = document.getElementById('CTVCloseButton');
		if(CTVCloseButton) {
			CTVCloseButton.src = 'ui/CTVCloseButton.png';
			CTVCloseButton.style.pointer = "default";
		}
	}
	
	function getPageTitle() {
		return document.title;
	}
	
	function setPageTitle(newPageTitle) {
		document.title = ""+newPageTitle+"";
	}
		
	function getParam(arg) {
		if (urlQString.indexOf(arg) >= 0) {
		var pntr = urlQString.indexOf(arg) + arg.length + 1;
		if (urlQString.indexOf("&", pntr) >= 0) {
			return urlQString.substring(pntr, urlQString.indexOf("&", pntr));
			} else {
				return urlQString.substring(pntr, urlQString.length);
			}
			} else {
				return null;
			}
	}
		
	function postDebugMsg(strDebug) {
		try {
			console.log(strDebug);
		} catch(err) {}
	}
			
	function varIsSet(varName) {
		return (typeof(varName)!='undefined');
	}

	function userAgentMatches(osName, browserName) {
		var browserUserAgent = navigator.userAgent;
		if((browserUserAgent.indexOf(osName) >= 0) && (browserUserAgent.indexOf(browserName) >= 0)) {
			return true;
		}
		return false;
	}

	function msIESizeFix(swfWidth, swfHeight) {
		document.getElementById('CTVswfCont').style.height = parseInt(CTV.vars.height) + 1 + "px";
		clearTimeout(tempMSIEFix);
	}

	function layoutBackground() {
		$("#CTVbckgndiFrame").animate({opacity:0},200);
		$("#CTVbckgndArea").css("width", stageWidth());
		$("#CTVbckgndArea").css("height", stageHeight());
		$("#CTVbckgndArea").css("top", CTV.vars.windowMarginTop+"px");
		$("#CTVbckgndArea").css("left", CTV.vars.windowMarginLeft+"px");
		$("#CTVbckgndiFrame").animate({opacity:1},250);
	}

/* end of required code lines */

