/**
 * Copyright (C) 2008 Brightcove, Inc.  All Rights Reserved.  No
 * use, copying or distribution of this work may be made except in
 * accordance with a valid license agreement from Brightcove, Inc.
 * This notice must be included on all copies, modifications and
 * derivatives of this work.
 *  
 * Brightcove, Inc MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT
 * THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. BRIGHTCOVE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
 * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 *
 * "Brightcove" is a trademark of Brightcove, Inc.
 * 
 * @description JS Example on how to play external Ads
 **/

    var player;
	var expMod;
	
	var externalBannerOrig = ""; // store contents of banner that may be temporarily replaced by companion ads

    // called when template loads, we use this to store a reference to the player and modules
    // and add event listeners for external Ads
    function onTemplateLoaded(experienceID) {

		// 'experienceID' is equivalent to the html object id (ie: "myExperience")
		player = brightcove.getExperience(experienceID);
		
		// mainly for debug messages
		expMod = player.getModule(APIModules.EXPERIENCE);
		 expMod.debug("exp module is loaded in externalad.js");

		// For Rendition selection, to force player to use highest or lowest bitrates
		var videoPlayer = player.getModule("videoPlayer");
		if (videoPlayer) {
			// set callback function for rendition selection
			videoPlayer.setRenditionSelectionCallback(selectRendition);
		}

	}

	// Forces player to use lowest quality video rendition
	// This is the new policy for players that aren't hi-def or hi res 
	/**
	* The callback invoked whenever the player reaches a point when a new selection can be selected. This will occur on initial playback
	* as well, for streaming videos, when there are multiple buffering events or when the screen size changed, as when going full screen.
	* This method must take an object as an argument and return an int value that represents the index of the rendition to play.
	*
	* @param  context  The context that the player uses to select a new rendition. This object includes the following properties:
	*           video  The video currently playing to which the renditions belong.
	*           currentRendition  The currently selected rendition for the video.
	*           renditions  An Array of renditions for the video to choose from.
	*           detectedBandwidth  The last detected bandwidth value.
	*           screenWidth  The pixel width of the video screen in which the rendition will play.
	*           screenHeight  The pixel height of the video screen in which the rendition will play.
	*
	* @returns  The index of the rendition in the renditions list for the video player to play.
	*/
	function selectRendition(context) {
		var renditions = context.renditions;
		var renditionIndex = -1;
		var size = Number.MAX_VALUE;
		for (var i = 0; i < renditions.length; i++) {
			// set the rendition index for the rendition with the smallest size
			if (renditions[i].size < size) {
				size = renditions[i].size;
				renditionIndex = i;
			}
		}
		// describeRendition(context.renditions[renditionIndex]);
		return renditionIndex;
	}
	
	/**
	* Traces out the values of rendition for testing.
	*
	* @param  rendition  The rendition to traces values for.
	* @param  index  The index in the renditions array where this rendition is found.
	*/
	function describeRendition(rendition) {
		var message = ("size: " + rendition.size);
		message += ("\nframeWidth: " + rendition.frameWidth);
		message += ("\nframeHeight: " + rendition.frameHeight);
		message += ("\nencodingRate: " + rendition.encodingRate);
		alert(message);
	}
	
	
