wxtools.lastTitleInLineup = false;
wxtools.currentTitleId;
wxtools.currentTitleName;
wxtools.encodedFLVURL = "";
wxtools.titleIds;
wxtools.counter = 0;

wxtools.newsChannelID = "823425597";
wxtools.forecastsChannelID = "824514779";
wxtools.lifestyleChannelID = "824720679";
wxtools.severeChannelID = "824250742";
wxtools.ontwcChannelID = "823503751";
wxtools.safetyChannelID = "1526070510";

/*test playerids
wxtools.newsChannelID = "1591583385";
wxtools.forecastsChannelID = "1591583387";
wxtools.lifestyleChannelID = "1591592407";
wxtools.severeChannelID = "1591583388";
wxtools.ontwcChannelID = "1578108777";
wxtools.safetyChannelID = "1578108775";
*/
            
        	
            
            
            
            
       

function isNumeric(num)
{
	if (num >=0 || num < 0)
	return true;
	return false;
}

function onTemplateLoaded(message) 
{

	wxtools.log("The template has loaded.");
    callFlash("addEventListener", "contentLoad", "onContentLoad");
	callFlash("addEventListener", "adPlay", "adStartEvent");
	callFlash("addEventListener", "adComplete", "adStopEvent");
	callFlash("addEventListener", "mediaStart", "mediaStartEvent");
	callFlash("addEventListener", "mediaStop", "mediaStopEvent");
	callFlash("addEventListener", "mediaComplete", "onMediaComplete");

	callFlash("addEventListener", "searchResult", "SearchResultReturned");
	callFlash("addEventListener", "searchError", "SearchResultReturnedWithError");

	adIsDonePlayingState();
}

function onMediaComplete()
{
	wxtools.log("Media Complete Event");
	wxtools.log("wxtools.lastTitleInLineup "+ wxtools.lastTitleInLineup);
	wxtools.log("haltAutoPlay " + wxtools.haltAutoPlay );

	wxtools.mediaInitiallyPlaying = false;		
	wxtools.log("OnMediaComplete - setting wxtools.mediaInitiallyPlaying to " + wxtools.mediaInitiallyPlaying);
	
	if(wxtools.lastTitleInLineup == false )
	{
		if(wxtools.haltAutoPlay == false)
		{
			wxtools.log("continue playing lineup" );
			wxtools.counter++;
			callFlash("selectTitle", wxtools.titleIds[wxtools.counter]);
			
		}
	}

	if (wxtools.lastTitleInLineup == true)
	{
		wxtools.lastTitleInLineup = false;
	}

	if (wxtools.haltAutoPlay == true)
	{
		wxtools.haltAutoPlay = false;
	}

	

}


function getFLVFileName(inUrl)
{
	var hashURL = inUrl.split('/');
	return hashURL[hashURL.length-1];
}

function getCurrentTitle_Result(titleDTO)
{
	wxtools.sameClipPlaying = false;

	wxtools.currentTitleId = titleDTO.id;
	wxtools.currentTitleName = titleDTO.displayName;
	wxtools.encodedFLVURL = getFLVFileName(titleDTO.FLVFullLengthURL);
	
	wxtools.clipID = Number(titleDTO.referenceId);
	wxtools.previousClipID = Number(wxtools.previousClipID);

	if( wxtools.mediaPlayEventFiring == true )
	{
		//wxtools.log("previous clip id length " +  wxtools.previousClipID.length);
		//wxtools.log(" clip id length "+ wxtools.clipID.length);
		wxtools.log ("comparing ***" + wxtools.clipID + "*** vs ***" + wxtools.previousClipID + "***");

		if (wxtools.clipID == wxtools.previousClipID)
		{
			wxtools.sameClipPlaying = true;
		}

		wxtools.log ("wxtools.mediaInitiallyPlaying " + wxtools.mediaInitiallyPlaying);
		wxtools.log ("wxtools.sameClipPlaying " + wxtools.sameClipPlaying);



		if (wxtools.mediaInitiallyPlaying == false || wxtools.sameClipPlaying == false)
		{
			callFlash("getLineupById", titleDTO.lineupId);
			wxtools.mediaInitiallyPlaying = true;
			wxtools.log ("wxtools.mediaInitiallyPlaying now is  " + wxtools.mediaInitiallyPlaying);
			wxtools.previousClipID = titleDTO.referenceId;
		}

		wxtools.mediaPlayEventFiring = false ;

	}
	else if (wxtools.adPlayEventFiring == true)
	{
		callFlash("getLineupById", titleDTO.lineupId);
	}




}

function getLineupById_Result(lineupDTO)
{
	wxtools.lastTitleInLineup = false;
	wxtools.titleIds = lineupDTO.videoIds;
	wxtools.collectionID = lineupDTO.referenceId;

	for(var i = 0; i < wxtools.titleIds.length; i++)
	{
		if(Number(wxtools.currentTitleId) == Number(wxtools.titleIds[i])) wxtools.counter = i;
	}

	if(Number(wxtools.currentTitleId) == Number(wxtools.titleIds[wxtools.titleIds.length - 1]) )
	  // || wxtools.playerID == '824514779') //BZ13158
	{
		wxtools.log("marking lastTitleInLineup to true" );
		wxtools.lastTitleInLineup = true;
	}
	
	if (wxtools.mediaPlayEventFiring == true)
	{
		wxtools.MMAS_Beacon.logBeacon(config["lineupRefName"],wxtools.collectionID, wxtools.currentTitleName, wxtools.clipID, wxtools.encodedFLVURL);			
		wxtools.mediaPlayEventFiring = false;
	}
	
	if (wxtools.mediaPlayEventFiring == true)
	{
		wxtools.mediaPlayEventFiring = false;
	}


		if (wxtools.adPlayEventFiring == true)
	{
		wxtools.playerInfo = getPlayerInfoByCol(wxtools.collectionID);
		wxtools.Analyticus_Beacon.logAdIsPlaying(wxtools.playerInfo[0], lineupDTO.id, wxtools.currentTitleId, wxtools.playerInfo[1]);			
		wxtools.adPlayEventFiring = false;
	}
}


function getPlayerLineupIds_Result(lineupIds)
{
	//wxtools.log("getPlayerLineupIds_Result looping through " +  lineupIds.length + " lineup ids" );
	for(var i = 0; i < lineupIds.length; i++)
	{
		callFlash("fetchLineupById", lineupIds[i]);
	}

	if (wxtools.foundClipFromAllLineupsCall == true)
	{
		wxtools.foundClipFromAllLineupsCall = false;
	}
}

function fetchLineupById_Result(lineupDTO)
{
	if (wxtools.foundClipFromAllLineupsCall == false)
	{
		for(var k = 0; k < lineupDTO.videoIds.length; k++)
		{
			//wxtools.log("fetchLineupById_Result comparing " + lineupDTO.videoIds[k] + " vs "  + wxtools.bcTitleDTO_ID );
			if(Number(lineupDTO.videoIds[k]) == Number(wxtools.bcTitleDTO_ID))
			{
				//wxtools.log("Found the clip, play title ");
				callFlash("selectTitle", wxtools.bcTitleDTO_ID, lineupDTO.id);
				wxtools.foundClipFromAllLineupsCall = true;
				break;
			}
		}
		return;
	}

}

function mediaStartEvent() 
{
    wxtools.log("mediaStartEvent.");
	wxtools.mediaPlayEventFiring = true;
	callFlash("getCurrentTitle");
}

function mediaStopEvent()
{
	wxtools.mediaStopEventFiring = true;
}

function adStartEvent() 
{
    wxtools.log("adPlayEvent.");
	wxtools.adPlayEventFiring = true;
	wxtools.adIsPlaying = true;
	callFlash("getCurrentTitle");
	adIsPlayingState();
	resetContainer(true);
}

function adStopEvent() 
{
    wxtools.log("adCompleteEvent.");
	adIsDonePlayingState();
	wxtools.adIsPlaying = false;
}

/*	
function adDurationStatus() 
{
    wxtools.log("adDuration Fires.");
}
*/	
function onContentLoad() 
{
    wxtools.log("The content has loaded.");

	if (wxtools.samePlayer == false || wxtools.tryingToPlayClip == true)
	{
		if ( wxtools.isSwappingPlayer == null || wxtools.isSwappingPlayer == false )
		{
			callFlash("getTitleByReferenceId", config["videoRef"]);
			wxtools.isSwappingPlayer = false;
			wxtools.samePlayer = false;
			wxtools.tryingToPlayClip = false;
		}
		
		if ( wxtools.isSwappingPlayer == true )
		{
			wxtools.isSwappingPlayer = false;
		}
			
	}
}

function processInboundEmailedLink()
{
	wxtools.playerInfo = getPlayerInfoByPid(wxtools.bcPlayerID);
	wxtools.playerID = wxtools.playerInfo[0];
	wxtools.adServerURL = wxtools.playerInfo[1];
	wxtools.preloadBackColor = wxtools.playerInfo[2];
	wxtools.lineupRefName = wxtools.playerInfo[4];

	config["playerId"] = wxtools.playerID;
	config["adServerURL"] = wxtools.adServerURL;
	config["preloadBackColor"] = wxtools.preloadBackColor;
	config["videoId"] = wxtools.bcClipID;
	
	try{
		if(wxtools.ABtesting.on){
			var configValues = wxtools.ABtesting.switchUser(config["playerId"]);
			config["playerId"] = configValues.playerId;
			config["adServerURL"] = configValues.adServerURL;
		}
	}catch(e){
	}
	createExperience(config, 8);

	processUIDisplay();	
	changeChannelSelect(wxtools.playerID);
}

function processUIDisplay()
{
   var z = document.styleSheets.length;
	if (!saf) 
	{
		for (var i = 1; i < z; i++) 
		{
			if (document.styleSheets[i].title != wxtools.playerID) 
			{
				document.styleSheets[i].disabled = true;
			} 
			else 
			{
				document.styleSheets[i].disabled = false;
			}
		}
	}
	
	//fix for safari css calls
	if(saf)
	{
		
		//document.getElementById("searchBox").style.display = "none";
		//document.getElementById("searchBoxSearching").style.display = "block";
		//document.getElementById("safarisearching").style.display="none";
	
		var newCSS = "";
		if ( wxtools.playerID == "823425597")
		{
			newCSS = 'http://ima.weather.com/common/stylesheet/verticals/bb_news.css';
		}
		else if ( wxtools.playerID == "824514779")
		{
			newCSS = 'http://ima.weather.com/common/stylesheet/verticals/bb_forecasts.css';
		}	
		else if ( wxtools.playerID == "824720679")
		{
			newCSS = 'http://ima.weather.com/common/stylesheet/verticals/bb_lifestyle.css';
		}
		else if ( wxtools.playerID == "824250742")
		{
			newCSS = 'http://ima.weather.com/common/stylesheet/verticals/bb_severe.css';
		}
		else if ( wxtools.playerID == "823503751")
		{
			newCSS = 'http://ima.weather.com/common/stylesheet/verticals/bb_ontwc.css';
		} 
		
		var head = document.getElementsByTagName('head')[0];
	    var newElement = document.createElement('link');
	 	newElement.rel = 'stylesheet';
	 	newElement.href = newCSS;
	 	head.appendChild(newElement);
	}	
	else
	{
		//document.getElementById("search").style.display = "block";
		//document.getElementById("searchBoxSearching").style.display = "none";
		//document.getElementById("safarisearching").style.display="none";
	}

    wxtools.MMAS_Beacon = new VideoMMASLogger(window.location.referer,GetCookie("RMID"),wxtools.from);
	wxtools.Analyticus_Beacon = new AnalyticusLogger();
    wxtools.ChannelSwap_Beacon = new ChannelSwapLogger();
}



function initializePage()
{
	
	localSearchMgr = new WxVideoSearch();
	localSearchMgr.addListener(wxSearchListener);

	config["wmode"] = "transparent";
	config["suppressNoContentMessage"] = true;
	config["width"] = 988;
	config["height"] = 550;
	config["autoStart"] = true;
	config["continuousPlay"] = "false";
	
	if (wxtools.bcClipID != null &&  wxtools.bcPlayerID != null )
	{
		//inbound from emailed link.
		processInboundEmailedLink();
		return;
	}

	if ( wxtools.clipID != null && wxtools.clipID !="" && wxtools.clipID !="unknown")
	{
	    //wxtools.log("Inbound link detected, playClip Collection:" + wxtools.collectionID + " clip:" +  wxtools.clipID);
		if (wxtools.collectionID != null && wxtools.collectionID !="" && wxtools.collectionID !="unknown")
		{
			if (isLocalRegCollection(wxtools.collectionID))
			{
				var tmpCid = getVideo2LocalCollectionCode(wxtools.clipID);
				if(tmpCid)
				{
					wxtools.playerInfo = getPlayerInfoByCol(tmpCid);
				}
			}
			else
     		 	{
				//if ( isNumeric()wxtools.collectionID == "topstory" )
				if ( isNumeric(wxtools.collectionID) == false )
				{
					var tmpCid = getCollectionIdUsingTitleID(wxtools.collectionID);
					if(tmpCid)
					{
						wxtools.playerInfo = getPlayerInfoByCol(tmpCid);
						wxtools.collectionID = wxtools.playerInfo[3];
					}
				}
				else
				{
					wxtools.playerInfo = getPlayerInfoByCol(wxtools.collectionID );
				}
			}
		}
		else
		{
			wxtools.playerInfo = getPlayerInfoByCol(wxtools.clip2CollHash[wxtools.clipID] )
		}
		if (wxtools.playerInfo == null )
		{
			setDefaultPlayer();
		}
		else
		{
			if ( wxtools.playerInfo == null)
			{
				alert("Sorry, clip is not available.  Please try your search again.");
				return;
			}

			wxtools.playerID = wxtools.playerInfo[0];
			wxtools.adServerURL = wxtools.playerInfo[1];
			wxtools.preloadBackColor = wxtools.playerInfo[2];
			wxtools.lineupRefName = wxtools.playerInfo[4];
		}

	}
	else if (wxtools.collectionID != null && wxtools.collectionID !="" && wxtools.collectionID !="unknown")
	{
		if (isLocalRegCollection(wxtools.collectionID))
		{
			var tmpCid = getVideo2LocalCollectionCode(wxtools.clipID);
			if(tmpCid)
			{
				wxtools.playerInfo = getPlayerInfoByCol(tmpCid);
			}
		}
		else if ( isNumeric(wxtools.collectionID) == false )
		
		{
			var tmpCid = getCollectionIdUsingTitleID(wxtools.collectionID);
			if(tmpCid)
			{
				wxtools.playerInfo = getPlayerInfoByCol(tmpCid);
				wxtools.collectionID = wxtools.playerInfo[3];
			}

		}
		else
     		{				
			wxtools.playerInfo = getPlayerInfoByCol(wxtools.collectionID );
			if (wxtools.playerInfo == null )
			{
				wxtools.collectionID = getCollectionIdUsingTitleID(wxtools.collectionID );
				wxtools.playerInfo = getPlayerInfoByCol(wxtools.collectionID );
			}
		}

		if (wxtools.playerInfo == null )
		{
			setDefaultPlayer();
		}
		else
		{
			if ( wxtools.playerInfo == null)
			{
				alert("Sorry, clip is not available.  Please try your search again.");
				return;
			}
			wxtools.clipID = wxtools.coll2ClipHash[wxtools.playerInfo[3][0]];
			wxtools.playerID = wxtools.playerInfo[0];
			wxtools.adServerURL = wxtools.playerInfo[1];
			wxtools.preloadBackColor = wxtools.playerInfo[2];
			wxtools.lineupRefName = wxtools.playerInfo[4];
		}
	}
	else
	{
		setDefaultPlayer();
		//no params, set autostart to false;
		config["autoStart"] = false;	
	}
	
	
	//wxtools.previousClipID = wxtools.clipID;
	wxtools.previousClipID = null;

	config["playerId"] = wxtools.playerID;
	config["adServerURL"] = wxtools.adServerURL;
	config["preloadBackColor"] = wxtools.preloadBackColor;
	config["lineupRef"] = wxtools.collectionID;
	config["lineupRefName"] = wxtools.lineupRefName;
	config["videoRef"] = wxtools.clipID;

	try{
		if(wxtools.ABtesting.on){
			var configValues = wxtools.ABtesting.switchUser(config["playerId"]);
			config["playerId"] = configValues.playerId;
			config["adServerURL"] = configValues.adServerURL;
		}
	}catch(e){
	}
	createExperience(config, 8);

	processUIDisplay();
    changeChannelSelect(wxtools.playerID);

}

function setDefaultPlayer()
{
	//no clip or collection..play default
	wxtools.playerID = wxtools.defaultPlayerID;
	wxtools.collectionID = null;
	wxtools.lineupRefName = null;
	wxtools.clipID = null;

	if (wxtools.dataHash) 
	{
		//wxtools.log("Found the dataHash.");
		for (var i=0;i<wxtools.dataHash.length;i++) 
		{
			if (wxtools.dataHash[i]["pid"] == wxtools.playerID) {
				//wxtools.log("Found the playerID.");
				if(wxtools.dataHash[i].psc != ""){
					// Set collectionID to primary_subcategory if it exists
				  wxtools.collectionID = wxtools.dataHash[i].psc;
					for(var z=0;z<wxtools.dataHash[i].c.length;z++){
						// Find the tid associated with the psc
						if(wxtools.dataHash[i].c[z].cid == wxtools.dataHash[i].psc){
				      wxtools.lineupRefName = wxtools.dataHash[i].c[z].tid;
						}
						break;
					}
				  wxtools.log("Setting the collectionId to "+wxtools.dataHash[i].psc+".");
				}else{
					// Otherwise set collectionID to the first one in collections array, which is ordered by subcatid
				  wxtools.collectionID = wxtools.dataHash[i]["c"][0]["cid"];
				  wxtools.lineupRefName = wxtools.dataHash[i]["c"][0]["tid"];
				  wxtools.log("Setting the collectionId to "+wxtools.dataHash[i]["c"][0]["cid"]+".");
				}

				wxtools.preloadBackColor = wxtools.dataHash[i]["bg"];
				wxtools.adServerURL = wxtools.dataHash[i]["as"];
				//wxtools.log("Setting the collectionId to "+wxtools.dataHash[i]["c"][0]["cid"]+".");
				break;
			}
		}
	}
	
	if (wxtools.coll2ClipHash) 
	{
		if (null != wxtools.collectionID) 
		{
			//wxtools.log("Found the coll2ClipHash.");
			wxtools.clipID = wxtools.coll2ClipHash[wxtools.collectionID][0];
			//wxtools.log("Setting the clipId to " + wxtools.clipID);
		}
	}
}

function playClip(aCollectionID, aClipID)
{

	//get Playerinfo
	wxtools.log("playClip start clip " + aClipID + " collection " + aCollectionID)
	if (wxtools.adIsPlaying == true)
	{
		return;
	}
	
	try{
		if(wxtools.ABtesting.on){
			if(config["playerId"]==wxtools.ABtesting.playerId){
				config["playerId"] = wxtools.newsChannelID;
			}
		}
	}catch(e){
	}
	var currentPlayerID = config["playerId"];
	var currentLineupID = config["lineupRef"];

	wxtools.previousClipID = wxtools.clipID;

	playingClipState();
	wxtools.haltAutoPlay = false;

	if (isLocalRegCollection(aCollectionID))
	{
		var tmpCid = getVideo2LocalCollectionCode(aClipID);
		if(tmpCid)
		{
			wxtools.playerInfo = getPlayerInfoByCol(tmpCid);
		}
	}
	else if ( isNumeric(aCollectionID) == false )
	
	{
		var tmpCid = getCollectionIdUsingTitleID(aCollectionID);
		if(tmpCid)
		{
			wxtools.playerInfo = getPlayerInfoByCol(tmpCid);
		}

	}
	else
      	{
		wxtools.playerInfo = getPlayerInfoByCol(aCollectionID);
	}

	if ( wxtools.playerInfo == null)
	{
		alert("Sorry, clip is not available.  Please try your search again.");
		return;
	}

	config["continuousPlay"] = "false";
	config["playerId"] = wxtools.playerInfo[0];
	config["adServerURL"] = wxtools.playerInfo[1];
	config["preloadBackColor"] = wxtools.playerInfo[2];
	config["lineupRef"] = wxtools.playerInfo[3];
	config["lineupRefName"] = wxtools.playerInfo[4];

	config["videoRef"] = aClipID;
	config["autoStart"] = false;
	config["width"] = 988;
	config["height"] = 550;

	//get player ID
	activePlayerID = wxtools.playerInfo[0];
	wxtools.playerID = activePlayerID;
	wxtools.collectionID = wxtools.playerInfo[3];
	wxtools.clipID = aClipID;

	if(currentPlayerID)
	{
		if (currentPlayerID == activePlayerID)
		{
			wxtools.samePlayer = true;
		}
	}

	if(currentLineupID)
	{
		wxtools.log( "currentLineupID"   + currentLineupID);
		wxtools.log( "wxtools.collectionID"   + wxtools.collectionID);
		if (currentLineupID == wxtools.collectionID)
		{
			wxtools.log("same collection id, force play")
			wxtools.sameLineup = true;
		}
		else
		{
			wxtools.log("different collection id, force play")
			wxtools.sameLineup = false;
		}
	}

	//set up style
	
	var z = document.styleSheets.length;
	if (!saf) {
		for (var i = 1; i < z; i++) {
			if (document.styleSheets[i].title != activePlayerID) {
				document.styleSheets[i].disabled = true;
			} else {
				document.styleSheets[i].disabled = false;
			}
		}
	}
	
	//fix for safari css calls
	if(saf)
	{
		var newCSS = "";
		if (activePlayerID == "823425597")
		{
			newCSS = 'http://c.imwx.com/common/stylesheet/verticals/bb_news.css';
		}
		else if (activePlayerID == "824514779")
		{
			newCSS = 'http://c.imwx.com/common/stylesheet/verticals/bb_forecasts.css';
		}	
		else if (activePlayerID == "824720679")
		{
			newCSS = 'http://c.imwx.com/common/stylesheet/verticals/bb_lifestyle.css';
		}
		else if (activePlayerID == "824250742")
		{
			newCSS = 'http://c.imwx.com/common/stylesheet/verticals/bb_severe.css';
		}
		else if (activePlayerID == "823503751")
		{
			newCSS = 'http://c.imwx.com/common/stylesheet/verticals/bb_ontwc.css';
		} 
		
		var head = document.getElementsByTagName('head')[0];
	    var newElement = document.createElement('link');
	 	newElement.rel = 'stylesheet';
	 	newElement.href = newCSS;
	 	head.appendChild(newElement);
	}	


	wxtools.log("wxtools.sameLineup "+ wxtools.sameLineup);
	wxtools.log("wxtools.samePlayer "+ wxtools.samePlayer);
	wxtools.tryingToPlayClip = true;

	if (wxtools.samePlayer == true)
	{
		callFlash("fetchTitleByReferenceId", aClipID);
	}
	else
	{
		try{
			if(wxtools.ABtesting.on){
				var configValues = wxtools.ABtesting.switchUser(config["playerId"]);
				config["playerId"] = configValues.playerId;
				config["adServerURL"] = configValues.adServerURL;
			}
		}catch(e){
		}
		callFlash("loadPlayer",config);
		//wxtools.tryingToPlayClip = false;
	}
   changeChannelSelect(wxtools.playerID);
   
}

function swapPlayer(playerID)
{
	if (wxtools.adIsPlaying == true)
	{
		//document.channels.reset();
		return;
	}
    if (wxtools.playerID == playerID)
    {
        return;
    }
	wxtools.playerID = playerID;
	wxtools.searchingFor = document.getElementById('searchText').value;
	wxtools.isSwappingPlayer = true;
	resetContainer(true);
	adIsPlayingState();
	wxtools.userInitiated = true;
	wxtools.haltAutoPlay = false;
	wxtools.previousClipID = wxtools.clipID;
	
	//get Player Information
	wxtools.playerInfo = getPlayerInfoByPid(playerID);
	activePlayerID = wxtools.playerInfo[0];
	wxtools.adServerURL = wxtools.playerInfo[1];
	
	if (wxtools.dataHash) 
	{
		for (var i=0;i<wxtools.dataHash.length;i++) 
		{
			if (wxtools.dataHash[i]["pid"] == activePlayerID) 
			{
				wxtools.log("Found the playerID.");
				if(wxtools.dataHash[i].psc != ""){
					// Set collectionID to primary_subcategory if it exists
				  wxtools.collectionID = wxtools.dataHash[i].psc;
					for(var z=0;z<wxtools.dataHash[i].c.length;z++){
						if(wxtools.dataHash[i].c[z].cid == wxtools.dataHash[i].psc){
				      wxtools.lineupRefName = wxtools.dataHash[i].c[z].tid;
						}
						break;
					}
				  wxtools.log("Setting the collectionId to "+wxtools.dataHash[i].psc+".");
				}else{
				  wxtools.collectionID = wxtools.dataHash[i]["c"][0]["cid"];
				  wxtools.lineupRefName = wxtools.dataHash[i]["c"][0]["tid"];
				  wxtools.log("Setting the collectionId to "+wxtools.dataHash[i]["c"][0]["cid"]+".");
				}
				break;
			}
		}
	}
	if (wxtools.coll2ClipHash) 
	{
		if (null != wxtools.collectionID) 
		{
			wxtools.clipID = wxtools.coll2ClipHash[wxtools.collectionID][0];
			wxtools.log("Setting the clipId to " + wxtools.clipID);
		}
	}
	
	
	config["videoRef"] = wxtools.clipID;
	config["lineupRef"] = wxtools.collectionID;
	config["lineupRefName"] = wxtools.lineupRefName;		
	config["autoStart"] = false; //tells the player to start playing video on load
	config["preloadBackColor"] = wxtools.playerInfo[2];
	config["continuousPlay"] = "false";
	config["playerId"] = activePlayerID;
	config["width"] = 988;
	config["height"] = 550;
	config["adServerURL"] = wxtools.playerInfo[1];
	
	
	//set the correct CSS
	var z = document.styleSheets.length;
	if(!saf)
	{
		for(var i=1;i<z;i++)
		{
			if(document.styleSheets[i].title != activePlayerID )
			{
				document.styleSheets[i].disabled=true;
			}
			else
			{
				document.styleSheets[i].disabled=false; 							
			}
		}
	}
	//fix for safari css calls
	if(saf)
	{
		var newCSS = "";
		if (activePlayerID == "823425597")
		{
			newCSS = 'http://c.imwx.com/common/stylesheet/verticals/bb_news.css';
		}
		else if (activePlayerID == "824514779")
		{
			newCSS = 'http://c.imwx.com/common/stylesheet/verticals/bb_forecasts.css';
		}	
		else if (activePlayerID == "824720679")
		{
			newCSS = 'http://c.imwx.com/common/stylesheet/verticals/bb_lifestyle.css';
		}
		else if (activePlayerID == "824250742")
		{
			newCSS = 'http://c.imwx.com/common/stylesheet/verticals/bb_severe.css';
		}
		else if (activePlayerID == "823503751")
		{
			newCSS = 'http://c.imwx.com/common/stylesheet/verticals/bb_ontwc.css';
		} 
		
		var head = document.getElementsByTagName('head')[0];
	    var newElement = document.createElement('link');
	 	newElement.rel = 'stylesheet';
	 	newElement.href = newCSS;
	 	head.appendChild(newElement);
	}	

	//keep whatever was in search window.
	document.getElementById('searchText').value = wxtools.searchingFor;
	
	try{
		if(wxtools.ABtesting.on){
			var configValues = wxtools.ABtesting.switchUser(config["playerId"]);
			config["playerId"] = configValues.playerId;
			config["adServerURL"] = configValues.adServerURL;
		}
	}catch(e){
	}
	callFlash("loadPlayer", config);
	
    //log beacon for channelswap
	wxtools.ChannelSwap_Beacon.logChannelSwapBeacon(getChannelActionID(wxtools.playerID));
    changeChannelSelect(activePlayerID);

}

function getChannelActionID( aPlayerID )
{
	var actionID = 0;
	if (aPlayerID == wxtools.newsChannelID)
	{
		actionID = 45;
	}
	else if (aPlayerID == wxtools.forecastsChannelID)
	{
		actionID = 46;
	}
	else if (aPlayerID == wxtools.lifestyleChannelID)
	{
		actionID = 47;
	}
	else if (aPlayerID == wxtools.severeChannelID)
	{
		actionID = 48;
	}
	else if (aPlayerID == wxtools.ontwcChannelID)
	{
		actionID = 49;
	}
	else if (aPlayerID == wxtools.safetyChannelID)
	{
		actionID = 50;
	}
	
	return actionID;
}


function isLocalRegCollection(collection)
{	
		return collection == "regwxforecast" || collection == "localwxforecast" ? true : false;
}
	
	
function getPlayerInfoByPid(pid)
{
	for(var pidIndex = 0; pidIndex < wxtools.dataHash.length; pidIndex++)
	{			
		if(wxtools.dataHash[pidIndex]['pid'] == pid)
		{
			var theCid = "";
			if(wxtools.dataHash[pidIndex].psc != ""){
				// Set theCID = primary_subcategory if it exists
				theCid = wxtools.dataHash[pidIndex].psc;
			}else{
				theCid = wxtools.dataHash[pidIndex].c[0].cid;
			}
			return [wxtools.dataHash[pidIndex]['pid'],wxtools.dataHash[pidIndex]['as'],wxtools.dataHash[pidIndex]['bg'],theCid];
		}
	}
	return null;
}

function getPlayerInfoByCol(col)
{
	for(var pidIndex = 0; pidIndex < wxtools.dataHash.length; pidIndex++)
	{			
		for(var cidIndex = 0; cidIndex < wxtools.dataHash[pidIndex]['c'].length; cidIndex++)
		{				
			if(wxtools.dataHash[pidIndex]['c'][cidIndex]['cid']== col)
			{					
				return [wxtools.dataHash[pidIndex]['pid'],wxtools.dataHash[pidIndex]['as'],wxtools.dataHash[pidIndex]['bg'],wxtools.dataHash[pidIndex]['c'][cidIndex]['cid'],wxtools.dataHash[pidIndex]['c'][cidIndex]['t']];
			}
		}
	}
	return null;
}

function getCollectionIdUsingTitleID(atid)
{
	for(var pidIndex = 0; pidIndex <  wxtools.dataHash.length; pidIndex++){			
		for(var cidIndex = 0; cidIndex <  wxtools.dataHash[pidIndex]['c'].length; cidIndex++){				
			if( wxtools.dataHash[pidIndex]['c'][cidIndex]['tid']== atid){					
				return  wxtools.dataHash[pidIndex]['c'][cidIndex]['cid'];
			}
		}
	}
	return null;
}

function getVideo2LocalCollectionCode(clipID)
{
	var collection_id =  wxtools.clip2CollHash[clipID];
	if (collection_id) 
	{
		return collection_id;
	}
	return null;
}




function initialState()
{
	//currently not using but may need to swap channel swapper image
	adIsPlayingState();	
}

function adIsPlayingState()
{	
	divList["msg_data"].style.display = "block";	
}

function adIsDonePlayingState()
{
	divList["msg_data"].style.display = "none";	
}



function resetPromoItems(){
	//turns off promo elements
    return;
    /*
	document.getElementById("news_promo").style.display = "none";
	document.getElementById("forecasts_promo").style.display = "none";
	document.getElementById("lifestyle_promo").style.display = "none";
	document.getElementById("severe_promo").style.display = "none";
	document.getElementById("ontwc_promo").style.display = "none";
    */
}

function changeChannelSelect(activePlayerID)
{
            
                var arVal = Array();
                        
                        arVal[wxtools.newsChannelID] = 'news';
                        arVal[wxtools.forecastsChannelID] = 'forecast';
                        arVal[wxtools.lifestyleChannelID] = 'living'
                        arVal[wxtools.severeChannelID] = 'storm';
                        arVal[wxtools.ontwcChannelID] =  'tv';
                        arVal[wxtools.safetyChannelID] = 'safety';
                        
                        var chanName = arVal[activePlayerID];
                        
       document.getElementById(chanName).className = arVal[activePlayerID];
       fixPipes(activePlayerID);

}


function fixPipes(activePlayerID)
{
            
             var arVal = Array();
                        
                        arVal[wxtools.newsChannelID] = 'news';
                        arVal[wxtools.forecastsChannelID] = 'forecast';
                        arVal[wxtools.lifestyleChannelID] = 'living'
                        arVal[wxtools.severeChannelID] = 'storm';
                        arVal[wxtools.ontwcChannelID] =  'tv';
                        arVal[wxtools.safetyChannelID] = 'safety';
       
               var chanName = arVal[activePlayerID];
               
                arp = Array();
                        arp['news'] = 'p1';
                        arp['forecast'] = 'p1,p2';
                        arp['safety'] = 'p2,p3';
                        arp['living'] = 'p3,p4';
                        arp['storm'] = 'p4,p5';
                        arp['tv'] = 'p5';
                        
                        document.getElementById('p1').className = 'p_on_'+chanName ;
                        document.getElementById('p2').className = 'p_on_'+chanName ;
                        document.getElementById('p3').className = 'p_on_'+chanName ;
                        document.getElementById('p4').className = 'p_on_'+chanName ;
                        document.getElementById('p5').className = 'p_on_'+chanName ;
            
                        var arPipe = arp[chanName].split(',');
                        for(var x=0;x<arPipe.length; ++x )
                        {
                                    document.getElementById(arPipe[x]).className = 'p_off_'+chanName;
                                    
                        }
                        
                        
}

function show(val)
            {
            document.getElementById(val).className = val+'_on_'+wxtools.playerID;
            fixPipe(val, 0);
            
            }
                        
function hide(val)
        {
        document.getElementById(val).className = val;
        fixPipe(val, 1);
        }

function fixPipe(val , num)
{
            
        arChan = Array();
        arChan[wxtools.newsChannelID] = 'news';
        arChan[wxtools.forecastsChannelID] = 'forecast';
        arChan[wxtools.lifestyleChannelID] = 'living'
        arChan[wxtools.severeChannelID] = 'storm';
        arChan[wxtools.ontwcChannelID] =  'tv';
        arChan[wxtools.safetyChannelID] = 'safety';
        
        var chanName = arChan[wxtools.playerID];
        
        
        arVal = Array();
        arVal['news'] = 'p1';
        arVal['forecast'] = 'p1,p2';
        arVal['safety'] = 'p2,p3';
        arVal['living'] = 'p3,p4';
        arVal['storm'] = 'p4,p5';
        arVal['tv'] = 'p5';
        
        arNum = Array();
        arNum[0] = 'p_off_'+chanName;
        arNum[1] = 'p_on_'+chanName;
        
       // alert(arNum[num]);
        
        var arPipe = arVal[val].split(',');
        for(var x=0;x<arPipe.length; ++x )
        {
            document.getElementById(arPipe[x]).className = arNum[num];
        }    
            

}


