// AS3/Acudeo 
// 07/08/2009
	
	 var playerLoaded = false;

	 //custom
	 function receiveBCEventInfo(eventName, type)
	 {
		var typeToAd = "";
		if (type=="ad") {
			typeToAd=ad.videoURL;
		}
		// for debug purposes, only
		if (typeof debugdiv == 'undefined') {} else {
			if (debugdiv != "") {
				var currentText = document.getElementById( debugdiv ).innerHTML;
				currentText=currentText+'<br>' + eventName + ' ' + typeToAd;
				document.getElementById( debugdiv ).innerHTML=currentText;
			}
		}
	 }

	 //custom
	 function creatediv(id, left, top) {
   		var newdiv = document.createElement('div');
   		newdiv.setAttribute('id', id);
      		newdiv.style.width = 1;
       	newdiv.style.height = 1;
       	newdiv.style.position = "absolute";
		if ((left || top) || (left && top)) {
       		newdiv.style.position = "absolute";       
       		if (left) {
           			newdiv.style.left = left;
       		}       
       		if (top) {
           			newdiv.style.top = top;
       		}
   		}
   		newdiv.style.visibility="hidden";
		//newdiv.style.background = "#00C";
		//newdiv.style.border = "4px solid #000";
		document.body.appendChild(newdiv);
	 }

	 var cueDone;
	 var videoAdAsset;
	 var videoAdURL;
	 var videoTrack;
	 var expandedBannerClickURL;
	 var expandedBannerURL;
	 var expandedBannerTrack;
	 var collapsedBannerURL;
	 var collapsedBannerClickURL;
	 var collapsedBannerTrack;
	 var displayNameOfCurrentContent;
	 var duration;
	 var ad =
	 {
		// Setup some defaults.
		type:"videoAd", //the ad format ("videoAd")
 		trackStartURLs:[""], //the list of URLs to fire to track impressions when the video starts playing
 		videoURL:videoAdAsset, //the url to the video asset (FLV or SWF)
  		videoClickURL:"", //the url to launch when the video is clicked
  		duration:15 //the length of the video ad in seconds
	 };

	 if (typeof BCVideoTrackingPixelLocation == "undefined") {
		creatediv("BCVideoTrackingPixelLocation",1,1);
		var BCVideoTrackingPixelLocation = "BCVideoTrackingPixelLocation";
 	 }
 
 	 if (typeof BCBoxTrackingPixelLocation == "undefined") {
		creatediv("BCBoxTrackingPixelLocation",5,5);
		var BCBoxTrackingPixelLocation = "BCBoxTrackingPixelLocation";
 	 }
 
 	 if (typeof BCBannerTrackingPixelLocation == "undefined") {
		creatediv("BCBannerTrackingPixelLocation",10,10);
		var BCBannerTrackingPixelLocation = "BCBannerTrackingPixelLocation";
	 }	

	 // ------------------------------------------------------------------
	 // BC Player Code
	 // ------------------------------------------------------------------
	 // ------------------------------------------------------------------
	 // ------------------------------------------------------------------

	 //By use of this code snippet, I agree to the Brightcove Publisher T and C 
	 //found at http://corp.brightcove.com/legal/terms_publisher.cfm. 
	
	 var params = {};
	
	 params.publisherID = "1119379849";	  //FIXED
	 params.bgcolor = "#FFFFFF"; 	
	 params.isVid = "true";
	 params.isUI = "true";
	 params.useExternalAdControls = "true";
		
	 /* 
	 * feel free to edit these configurations
	 * to modify the player experience
	 */
	 
	 if (typeof acudeoProgramID == 'undefined') {} else {		
		 params.AcudeoProgramID = acudeoProgramID;  //if it's not being passed by the player itself( setup in the advertising setup tab of the BC console), then setting up by producer in DCR. -->
	 }

	 if (typeof bcVideoId == 'undefined') {} else {
		params.videoId = bcVideoId; //the default video loaded into the player. The ID or refid of a video to program into the player.
	 }	
	 
	 if (typeof bcPlayerTag == 'undefined') {
		params.playerTag= 'No playerName Set'; 
        } else {
	 	params.playerTag = bcPlayerTag; //player tag used for identifying this page in brightcove reporting.A name for the player that appears in player reports. This lets you use a more memorable and readable identifier for the player than its player ID.
	 }
	 
	 if (typeof autoStart == 'undefined') {
	 	params.autoStart = 'true';
	 } else {
	 	params.autoStart = autoStart; //tells the player to start playing video on load. If true, the video begins to play as soon as the player loads.
	 }
	 
	 if (typeof preloadBackColor == 'undefined') {
	 	params.wmode = 'transparent';
	 } else {
	 	params.preloadBackColor = preloadBackColor; //background color while loading the player
	 }

	 if (typeof externalAdControlsBkgd == 'undefined') {
	 } else {
	 	params.externalAdControlsBkgd  = externalAdControlsBkgd ; //ad bgcolor overlay
	 }

	 if (typeof adTechID == 'undefined') {} else {
		 if (adTechID != null) {
		 	params.adTechID = adTechID;	 	 		
		 }
	 }

	 /* 
	 * set the player's size using the parameters below
	 * to make this player dynamically resizable, set the width and height as a percentage
	 */

	 params.width = bcPlayerWidth;
	 params.height = bcPlayerHeight;
 
	 /* do not edit these config items */
	 params.playerID = bcPlayerID;
	 var player = brightcove.createElement("object");
	 player.id = "myExperience";
	 player.className="BrightcoveExperience";
	 var parameter;
	 for (var i in params) {
     		parameter = brightcove.createElement("param");
     		parameter.name = i;
     		parameter.value = params[i];
     		player.appendChild(parameter);
	 }

	 var playerContainer = document.getElementById("bcPlayer");
	 brightcove.createExperience(player, playerContainer, true);

	 var videoTitle;
	 var videoDescription;
	 var eType = "";
	
	 var bcExp;
	 var modVP;
	 var modExp;
	 var modCon;
	 var modCP;
	 var modMenu; 
 
	 // called when template loads, this function stores a reference to the player and modules.
	 // Then event listeners will be added for when the template is ready and when a user 
	 // clicks on a video.
	 function onTemplateLoaded(experienceID) {
    		//alert("EVENT: TEMPLATE_LOAD");   
    		bcExp = brightcove.getExperience(experienceID);     
    		
    		modExp = bcExp.getModule(APIModules.EXPERIENCE);
		modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
    		modCon = bcExp.getModule(APIModules.CONTENT);
		modCP = bcExp.getModule(APIModules.CUE_POINTS);
		modMenu = bcExp.getModule(APIModules.MENU);
  		
		// Event Listeners...
    		modExp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
    		modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
		modVP.addEventListener(BCMediaEvent.STOP, onPause);
		modVP.addEventListener(BCMediaEvent.PLAY, onMediaStart);
		modVP.addEventListener(BCMediaEvent.BEGIN,onStreamStart); //Dispatched when the media begins playing the first time, post buffer. This is the equivalent to the old "streamStart" and should be used for any tracking purposes. It will only be dispatched once for a media object, so not on replay (unless the media is loaded a second time after another piece of media is played in between).
		modVP.addEventListener(BCMediaEvent.COMPLETE, onMediaComplete);
		modMenu.addEventListener(BCMenuEvent.MENU_PAGE_OPEN, onMenuPageOpen);						
		// End Event Listeners....

		playerLoaded = true;
		// Advertisement debug messages
		receiveBCEventInfo("[AdTech] Video Ad Tracking Pixel = " + BCVideoTrackingPixelLocation, "");
		receiveBCEventInfo("[AdTech] Box Ad Tracking Pixel = " + BCBoxTrackingPixelLocation, "");
		receiveBCEventInfo("[AdTech] Banner Ad Tracking Pixel = " + BCBannerTrackingPixelLocation, "");	
	}

	// ------------------------------------------------------------
	// ------------------------------------------------------------
	// Event Listner functions.
	// ------------------------------------------------------------
	
	function onTemplateReady(evt) {
    		//alert("EVENT: TEMPLATE_READY"); 
    	}

	function onContentLoad(evt) {
		//alert("EVENT: CONTENT_LOAD"); 
		eType="contentLoad";   		 		 		
	}	

	function onPause() {
		//alert("EVENT: ON_PAUSE"); 
		receiveBCEventInfo("[Pause] ", "ad");
	}

	function onMediaStart(evt){	  
		//alert("EVENT: MEDIA_START"); 		
		currVid = modVP.getCurrentVideo();		
	}

	function onMediaComplete() {
		//alert("EVENT: MEDIA_COMPLETE"); 		
		receiveBCEventInfo("[Omniture:Finished] " + videoTitle, "");		
		if (modCP.removeEventListener(BCCuePointEvent.CUE,onCuePoint)) {		
			receiveBCEventInfo("[Removed eventListener cuePoint] " + videoTitle, "");
		}		
	}	

	function onMenuPageOpen() {	
		//alert("EVENT: MENU_PAGE_OPEN"); 
		receiveBCEventInfo("[Omniture:MenuOptionAccessed] " + titleId, "");			
	}
		
	function onCuePoint(evt) {			
		receiveBCEventInfo("[Received cuePoint] " + evt.cuePoint.name, "");
		if (evt.cuePoint.name == '50%') {
			//alert("EVENT: CUE_POINT 50%"); 
			receiveBCEventInfo("[Omniture:50%] " + evt.cuePoint.name, "");
		}
	}
	
	function onStreamStart(evt) {
		//alert("EVENT: STREAM_START"); 
		currVid = modVP.getCurrentVideo();
		getCurrentTitle_Result(currVid);
	}	
	
	function createCuePoint() {			 
    		setTimeout("setCuePoint(" + titleId  + ");", 40);    		    		 
	}

	function setCuePoint(videoId) {
		var midPointTime = totalVideoLength/1000;
		receiveBCEventInfo("[Set midPointTime] " + midPointTime, "");
    		var cuePoints = [{name:'50%',time:midPointTime,type:1}];
    		modCP.addCuePoints(videoId, cuePoints);    	
		modCP.addEventListener(BCCuePointEvent.CUE,onCuePoint);	
	}

	//------------------------------------------------------------
	//The below function must be included.  This is what tells the
	//Acudeo component where to put the companion banner (box ad.)
	//------------------------------------------------------------
	function displayCompanionAds(banners) { 
		//alert("calling ad");
		document.getElementById(BCSynchedBoxAdLocation).innerHTML=""; // Clear the existing ad from that spot
		tmDisplayBanner(banners, BCSynchedBoxAdLocation, 300, 250); 
	} 		 

	// ------------------------------------------------------------
	// ------------------------------------------------------------
	// Result functions.
	// ------------------------------------------------------------
	//custom
	function getCurrentTitle_Result(titleDTO) {
		//alert("EVENT: CURRENT_TITLE_RESULT");
		modCP.addEventListener(BCCuePointEvent.CUE,onCuePoint);  	
		receiveBCEventInfo("[Added eventListener: cuePoint] " + titleDTO.displayName, " ");

		receiveBCEventInfo("[Omniture:ShowTitle] " + titleDTO.displayName, " ");
		videoTitle = titleDTO.displayName;
		titleId = titleDTO.id;

		var tags = new Array();
	 	if (titleDTO.tags != null) {
			tags = titleDTO.tags;
		} else {
			tags[0] = "Untagged Video"
		}
		receiveBCEventInfo("[Omniture:Tag1] " + tags[0], " ");
		totalVideoLength = titleDTO.length;
		totalVideoLength = totalVideoLength/2;
		
		createCuePoint();		
		
		receiveBCEventInfo("[Omniture:Player] " + bcPlayerTag, " ");		
		receiveBCEventInfo("[Omniture:Sent] Event2 (Clip Start) ", "" );
		receiveBCEventInfo("[Omniture:Sent] ShowTitle= "+titleDTO.displayName+", Tag[0]= "+tags[0], "" );
	}
	
	// -----------------------------------------------------
	// -----------------------------------------------------
