var cnnPipeLineModulePlayerPref = false;	

if(cnnDetectCNNPipeLine()) {
	if((navigator.userAgent.indexOf('Opera')==-1) && document.getElementById('cnnPipelineLauncher') && document.getElementById('cnnPipelineLauncher').canLaunch("web")) {
		cnnPipeLineModulePlayerPref = true;
	}
}

if(WM_readCookie('CNN_Player_Pref')) {
	cnnPipeLineModulePlayerPref = true;
}

var cnnPipelineHeaderLinks = new Array(['/pipeline/signin/','Sign in'],['/help/pipeline/','Help']);
var cnnPipelineUpsellSmWoolText = "Want to Watch LIVE Video?";
var cnnPipelineUpsellLrgText = "Live news, no commercials. Learn More";
var cnnPipelineUpsellLrgTextLink = "/pipeline/";
var cnnPipelineOmnitureText = "Mainpage: Pipeline Marketing Watch Box Non-subscriber";
	
if (cnnPipeLineModulePlayerPref) {
	cnnPipelineHeaderLinks = new Array(['/pr/pipeline/download.html','Prefs'],['/help/pipeline/','Help']);
	cnnPipelineUpsellSmWoolText = "Want to learn about Pipeline's features?";	
	cnnPipelineUpsellLrgText = "Demo them now";
	cnnPipelineUpsellLrgTextLink = "/pipeline/guide/index.html";
	cnnPipelineOmnitureText = "Mainpage: Pipeline Marketing Watch Box Subscriber";
}

var cnnPipelineModuleCacheBuster = (Math.round(Math.random()*(10000000-1))+1);
var cnnAnimationValue = 10;
var cnnAnimationInterval = 40;

		function cnnPipeContainer()
		{
			this.cnnCollapseNodeID = 0;		
			this.cnnExpandNodeID = 0;
			this.cnnPipeLineModuleMaxRowHeight = 78;
			this.cnnPipeLineModuleMinRowHeight = 24;
			this.cnnCollapsedRowHeight = 34;
			this.cnnExpandedRowHeight = 68;
			this.tzImageObjbArr = new Array();		
			this.heightAdjustObjbArr = new Array();
			this.containerArr = new Array();
			this.idArr = new Array();
			this.idExpansionStatus = new Array();
			this.numIds = 0;
			this.dataUrl = '';
			this.refreshRate = 30000;
			this.objName = '';
			this.xmlHttpReq = false;
			this.iframeObj = false;
			this.domParserAX = false;
			this.parseHandler = new Function();
			
		}
		function cnnPipeContainer_setObjName(objName)
		{
			this.objName = objName;
		}
		cnnPipeContainer.prototype.setObjName = cnnPipeContainer_setObjName;
		
		function cnnPipeContainer_setParsingFunction(func)
		{
			this.parseHandler = func;
		}
		cnnPipeContainer.prototype.setParsingFunction = cnnPipeContainer_setParsingFunction;
		
		function cnnPipeContainer_refresh()
		{
			if (!this.xmlHttpReq && window.ActiveXObject)
			{
				try
				{
					this.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e)
				{
					try
					{
						this.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
						// do nothing
					}
				}
				try
				{
					this.domParserAX = new ActiveXObject("Microsoft.XMLDOM");
	        		xmlDoc.async = false;
				}catch(e) {
				// do nothing
				}			
			} else if (!this.xmlHttpReq && window.XMLHttpRequest && (navigator.userAgent.indexOf('Mozilla/5.0')==-1) && (navigator.userAgent.indexOf('Opera')==-1))
			{
				this.xmlHttpReq = new XMLHttpRequest();
				if(this.xmlHttpReq.overrideMimeType) {
					this.xmlHttpReq.overrideMimeType("text/xml");
				}

			}
			if(this.xmlHttpReq)
			{
				this.xmlHttpReq.open("GET", this.dataUrl+"?cacheID="+(cnnPipelineModuleCacheBuster%30),false);
		        this.xmlHttpReq.send("");
				this.handleResponse();
			}
			else
			{
				this.iframeObj=document.getElementById(this.objName+'Frame');
		window.frames[this.objName+'Frame'].location.replace(this.dataUrl+"?cacheID="+(cnnPipelineModuleCacheBuster%30));
			}
			window.setTimeout(this.objName+'.refresh()',this.refreshRate);
			cnnPipelineModuleCacheBuster++;
		}
		cnnPipeContainer.prototype.refresh = cnnPipeContainer_refresh;
		
		function cnnPipeContainer_setDataUrl(url)
		{
			this.dataUrl = url;
		}
		cnnPipeContainer.prototype.setDataUrl = cnnPipeContainer_setDataUrl;

		function cnnPipeContainer_setRefreshRate(time) // in seconds
		{
			this.refreshRate = time*1000;		
		}
		cnnPipeContainer.prototype.setRefreshRate = cnnPipeContainer_setRefreshRate;
		
		function cnnPipeContainer_addContainer(id,height)
		{
			if (document.getElementById(id)) {
				document.getElementById(id).style.height = "24px";
			}
			this.idArr[this.idArr.length] = id;
			this.containerArr[this.containerArr.length] = document.getElementById(id);
			this.heightAdjustObjbArr[this.heightAdjustObjbArr.length] = document.getElementById( id );

			this.tzImageObjbArr[this.tzImageObjbArr.length] = document.getElementById(id +'Image');			
			
			this.idExpansionStatus[this.idExpansionStatus.length] = false;
			this.numIds++;
			if (height) {
				this.cnnPipeLineModuleMaxRowHeight = height;
				this.cnnExpandedRowHeight = this.cnnPipeLineModuleMaxRowHeight;				
			}
		}
		cnnPipeContainer.prototype.addContainer = cnnPipeContainer_addContainer;

		function cnnPipeContainer_zipNode(id)
		{
			if (!document.getElementById(id)) { return;}
			for(var i=0;i<this.numIds;i++)
			{

				if(id==this.idArr[i])
				{
					if(this.idExpansionStatus[i])
					{
						this.privateCollapseNode( i );
					}
					else
					{
						this.privateExpandNode( i );
					}
				}else
				{
					if(this.idExpansionStatus[i])
					{
						this.privateCollapseNode( i );
					}
				}
			}
		}
		cnnPipeContainer.prototype.zipNode = cnnPipeContainer_zipNode;
			var cnnClassValue = '';

		function cnnPipeContainer_cnnCollapseNode(which) {
			if (this.cnnExpandedRowHeight > this.cnnPipeLineModuleMinRowHeight ) {
				document.getElementById(which.id).style.height = this.cnnExpandedRowHeight;	
			} else {
				document.getElementById(which.id).style.height = this.cnnPipeLineModuleMinRowHeight;			
				window.clearInterval(this.cnnCollapseNodeID);
				this.cnnCollapseNodeID = 0;
				this.cnnExpandedRowHeight = this.cnnPipeLineModuleMaxRowHeight;
				if(document.getElementById(which.id+'Image') && document.getElementById(which.id+'Image').lowsrc) {
					document.getElementById(which.id+'Image').src = 'http://i.cnn.net/cnn/images/1.gif';
				}
			}
				this.cnnExpandedRowHeight = this.cnnExpandedRowHeight-cnnAnimationValue;

		}
cnnPipeContainer.prototype.cnnCollapseNode = cnnPipeContainer_cnnCollapseNode;	
			
		function cnnPipeContainer_privateCollapseNode(item)
		{
		
		if (this.cnnCollapseNodeID == 0) {
			this.cnnCollapseNodeID = window.setInterval(this.objName+".cnnCollapseNode("+this.objName+".heightAdjustObjbArr["+item+"])",cnnAnimationInterval);

			this.idExpansionStatus[item]=false;
		
			cnnClassValue=eval(this.objName+".containerArr[ "+item+" ].className.replace(new RegExp(\"cnnPipelineExpanded\"), \"cnnPipelineCollapsed\")");
			
			cnnClassValue=cnnClassValue.replace(new RegExp(" cnnOver"), "");			
			
			window.setTimeout(this.objName+".containerArr[ "+item+" ].className='"+cnnClassValue+"';",500);
			
//			window.setTimeout(this.objName+".idExpansionStatus["+item+"]=false;",500);
		}
		}	
		cnnPipeContainer.prototype.privateCollapseNode = cnnPipeContainer_privateCollapseNode;			

function cnnPipeContainer_cnnExpandNode(which) {
	if (this.cnnCollapsedRowHeight <= this.cnnPipeLineModuleMaxRowHeight) {
		document.getElementById(which.id).style.height = this.cnnCollapsedRowHeight;
	} else {
		document.getElementById(which.id).style.height = this.cnnPipeLineModuleMaxRowHeight;
		window.clearInterval(this.cnnExpandNodeID);
		this.cnnExpandNodeID = 0;
		this.cnnCollapsedRowHeight = this.cnnPipeLineModuleMinRowHeight;
	}

	this.cnnCollapsedRowHeight = this.cnnCollapsedRowHeight+cnnAnimationValue;
}
cnnPipeContainer.prototype.cnnExpandNode = cnnPipeContainer_cnnExpandNode;

		function cnnPipeContainer_privateExpandNode(item)
		{
			var realId = this.idArr[item]+'Image';
			if(document.getElementById(realId))
			{
				if (document.getElementById(realId).lowsrc){
					document.getElementById(realId).src = document.getElementById(realId).lowsrc;
				}
			}
			if (this.cnnExpandNodeID == 0) {
			this.cnnExpandNodeID = window.setInterval(this.objName+".cnnExpandNode("+this.objName+".heightAdjustObjbArr["+item+"])",cnnAnimationInterval);
			this.idExpansionStatus[item] = true;


			this.containerArr[ item ].className =  this.containerArr[ item ].className.replace(new RegExp(" cnnOver"), "");
			this.containerArr[ item ].className =  this.containerArr[ item ].className.replace(new RegExp("cnnPipelineCollapsed"), "cnnPipelineExpanded");
//			window.setTimeout(this.objName+".idExpansionStatus["+item+"]=true;",500);
			}
		}	
		cnnPipeContainer.prototype.privateExpandNode = cnnPipeContainer_privateExpandNode;			


		function cnnPipeContainer_handleResponse(node)
		{
			if((this.xmlHttpReq && this.xmlHttpReq.readyState==4) || (this.iframeObj && this.iframeObj.readyState && this.iframeObj.readyState=='complete')|| (this.iframeObj))
			{
				var docObj= '';
				if(this.iframeObj)
				{
					if(this.iframeObj.contentDocument)
					{
						docObj = this.iframeObj.contentDocument;
					}
					else
					{
						docObj = document.frames(this.iframeObj.name).document;
					}
				}
				else
				{
					if(this.domParserAX )
					{
						this.domParserAX.async = false;
		        		this.domParserAX.loadXML(this.xmlHttpReq.responseText);
		        		docObj = this.domParserAX.documentElement;			
					}
					else
					{
						docObj = this.xmlHttpReq.responseXML.documentElement;
					}
				}
				var valueNLs = docObj.getElementsByTagName("value");
				var callObjs = new Array();
				for(var i=0;i<valueNLs.length;i++)
				{
					var callObj = new Object();
					var valueNode = valueNLs.item(i);
					callObj.key = valueNode.getAttribute('key');
					callObj.action = valueNode.getAttribute('type');
					callObj.data = valueNode.getAttribute('contents');
				callObjs[callObjs.length] = callObj;
				}
				this.parseHandler(callObjs,this);
			}
		}
		cnnPipeContainer.prototype.handleResponse = cnnPipeContainer_handleResponse;


		function updateContents(objects, objPtr)
		{
			for(var i=0;i<objects.length;i++)
			{
				var dataObj = objects[i];
				//First the non-dom items.
				if(dataObj.key=='refreshRate') { objPtr.setRefreshRate(dataObj.data);}
				else
				{
					var domObj = document.getElementById(dataObj.key);
					if(domObj)
					{
						if(dataObj.action=='html') { 
							if (domObj.id.substr(0,19) == "cnnPipelineHeadline") {
								if (dataObj.data == "LIVE BREAKING NEWS" || dataObj.data == "NEWS ALERT (Live)" || dataObj.data == "DEVELOPING STORY (Live)" || dataObj.data == "- Stay tuned -") { 
									domObj.innerHTML = dataObj.data;
								} else {
									domObj.innerHTML = '<img src="http://i.a.cnn.net/cnn/.element/img/1.5/main/video/icon.pipe.module.gif" width="43" height="10" border="0" alt="LIVE" title="LIVE">  '+dataObj.data;						
								}
							} else {
								domObj.innerHTML = "<p>"+dataObj.data+"<\/p>";
							}
						}

						if(dataObj.action=='image') {
							if (dataObj.data != "") {
								if(document.getElementById(dataObj.key).src!='http://i.cnn.net/cnn/images/1.gif')
								{
									document.getElementById(dataObj.key).src = dataObj.data+"?cacheID="+cnnPipelineModuleCacheBuster;
									document.getElementById(dataObj.key).style.display = "block";
								}
								if (document.getElementById(dataObj.key).lowsrc) {
									document.getElementById(dataObj.key).lowsrc = dataObj.data+"?cacheID="+cnnPipelineModuleCacheBuster;
								}
							} else {
								document.getElementById(dataObj.key).style.display = "none";
							}
						}
						if(dataObj.action=='style') { 
							domObj.className = (domObj.className.indexOf('cnnPipelineExpanded') > -1) ? "cnnPipelineExpanded "+dataObj.data : "cnnPipelineCollapsed "+dataObj.data;
						}
					}
				}
			}
		
		}

function CNN_changeBackground(which, val) {
	switch (val) {
		case 1:
			which.className += " cnnOver";
			break;
		case 0:
			which.className=which.className.replace(new RegExp(" cnnOver\\b"), "");
			break;
	}

}
		
function CNN_initVideoModules(){
	if (!document.getElementsByTagName){ return; }
	var pipelineContainers = document.getElementById("cnnPipelineModule").getElementsByTagName("div");


	for (var i=0; i<pipelineContainers.length; i++){
		var pipelineContainer = pipelineContainers[i];
			if(pipelineContainer.id.substr(0,9) == "plineCntr") {
				//alert(pipelineContainer.id);
				pipelineContainer.onmouseover = function () {CNN_changeBackground(this,1); return false;}
				pipelineContainer.onmouseout = function () {CNN_changeBackground(this,0); return false;}	
			}
			
			if(pipelineContainer.id.substr(0,22) == "cnnPipelineHeadlineBar") {
				pipelineContainer.id = pipelineContainer.id.replace(new RegExp("cnnPipelineHeadlineBar"), "");
				//alert(pipelineContainer.id);


				pipelineContainer.onclick = function () {pipeCtrl.zipNode("plineCntr"+this.id); return false;}
				
				
				
			}	
			
	}

	var freeVidContainers = document.getElementById("cnnFreevideoModule").getElementsByTagName("div");
	for (var i=0; i<freeVidContainers.length; i++){
		var freeVidContainer = freeVidContainers[i];
			if(freeVidContainer.id.substr(0,11) == "freeVidCntr") {
				//alert(pipelineContainer.id);
				freeVidContainer.onmouseover = function () {CNN_changeBackground(this,1); return false;}
				freeVidContainer.onmouseout = function () {CNN_changeBackground(this,0); return false;}	
			}
			
			if(freeVidContainer.id.substr(0,21) == "cnnfreeVidHeadlineBar") {
				freeVidContainer.id = freeVidContainer.id.replace(new RegExp("cnnfreeVidHeadlineBar"), "");
				//alert(pipelineContainer.id);


				freeVidContainer.onclick = function () {freeVidCntr.zipNode("freeVidCntr"+this.id); return false;}
				
				
				
			}	
			
	}


	var freeVidMPContainers = document.getElementById("cnnFreevideoMPModule").getElementsByTagName("div");
	for (var i=0; i<freeVidMPContainers.length; i++){
		var freeVidMPContainer = freeVidMPContainers[i];
			if(freeVidMPContainer.id.substr(0,13) == "freeVidMPCntr") {
				//alert(pipelineContainer.id);
				freeVidMPContainer.onmouseover = function () {CNN_changeBackground(this,1); return false;}
				freeVidMPContainer.onmouseout = function () {CNN_changeBackground(this,0); return false;}	
			}
			
			if(freeVidMPContainer.id.substr(0,23) == "cnnfreeVidMPHeadlineBar") {
				freeVidMPContainer.id = freeVidMPContainer.id.replace(new RegExp("cnnfreeVidMPHeadlineBar"), "");
				//alert(pipelineContainer.id);


				freeVidMPContainer.onclick = function () {freeVidMPCntr.zipNode("freeVidMPCntr"+this.id); return false;}
				
				
				
			}	
			
	}
	
	if (!cnnPipeLineModulePlayerPref) {
		document.getElementById('cnnPipelineModule').className = "";
	}

}		

function CNN_flipModule(show,hide) {
	if (document.getElementById) {
		document.getElementById(hide).style.display = "none";
		document.getElementById(show).style.display = "block";
	}
}
