/*
 * define customCallback on the page after this file is included to change the functionality.
 * 
 * example:
 * <script type="text/javascript">
 * <!--
 *  var imgTarget = '_self'; // I don't want the link to open in a new window
 *  
 *  // I want to do something else when the video finishes
 * 	(function(){ 
 *		window.onload = function() {
 *			customCallback= function(){
 *				console.log('video done');
 *			}
 *		}
 *  })();
 * //-->
 * </script> 
 */

// in case they want to customize it on the page
if (typeof imgTarget == 'undefined' || !imgTarget) var imgTarget = '_blank';

/* build our object */
if (typeof a4bnet == 'undefined' || !a4bnet)  var a4bnet = {}; // define our namespace
a4bnet.bcOverlay = {
	'default' : {
		ovrImg : {
			src    : 'http://www.beliefnet.com/media/home_header_logo.jpg',
			height : '',
			width  : '',
			click  : 'http://www.beliefnet.com',
			target : imgTarget	
		}
	}
};

/*
 * brightcove callback
 */
function onTemplateLoaded(experienceID) {
	
	a4bnet.bcOverlay[experienceID] = {
	    ovrImg : {},
		loadCb : false,
		hasClk : false
	};
	a4bnet.bcOverlay[experienceID].bcExp = brightcove.getExperience(experienceID);
	a4bnet.bcOverlay[experienceID].modVP = a4bnet.bcOverlay[experienceID].bcExp.getModule(APIModules.VIDEO_PLAYER); 	
    a4bnet.bcOverlay[experienceID].modExp = a4bnet.bcOverlay[experienceID].bcExp.getModule(APIModules.EXPERIENCE);
    
    a4bnet.bcOverlay[experienceID].ovrImg = {
		src    : a4bnet.bcOverlay['default'].src,
		height : a4bnet.bcOverlay['default'].height,
		width  : a4bnet.bcOverlay['default'].width,
		click  : a4bnet.bcOverlay['default'].click,
		target : a4bnet.bcOverlay['default'].target	    		
    };
    
    //a4bnet.bcOverlay[experienceID].modCon = a4bnet.bcOverlay[experienceID].bcExp.getModule(APIModules.CONTENT);

    a4bnet.bcOverlay[experienceID].modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, function(evt){
    	/*
    	 * onContentLoad
    	 * Callback for content load
    	 */
    	var video = a4bnet.bcOverlay[experienceID].modVP.getCurrentVideo();
    	if (typeof video.customFields != 'undefined' && video.customFields) {
    		if (typeof video.customFields.overlayimg != 'undefined' && video.customFields.overlayimg) {
    			a4bnet.bcOverlay[experienceID].ovrImg.src = video.customFields.overlayimg;
    			a4bnet.bcOverlay[experienceID].loadCb = true;
    		}
    	}
    	
    	if (typeof video.customFields != 'undefined' && video.customFields) {
    		if (typeof video.customFields.overlayimageclickthru != 'undefined' && video.customFields.overlayimageclickthru) {						
    			a4bnet.bcOverlay[experienceID].ovrImg.click = video.customFields.overlayimageclickthru;
    			a4bnet.bcOverlay[experienceID].hasClk = true;
    		}
    	}

    	a4bnet.bcOverlay[experienceID].ovrImg.height=a4bnet.bcOverlay[experienceID].modExp.getHeight();
    	a4bnet.bcOverlay[experienceID].ovrImg.width=a4bnet.bcOverlay[experienceID].modExp.getWidth();    	    	
    });
    
	if (typeof customCallback == 'undefined' || !customCallback) {
	    a4bnet.bcOverlay[experienceID].modVP.addEventListener(BCVideoEvent.VIDEO_COMPLETE, function(evt){
	    	/*
	    	 * onVideoEnd
	    	 * Callback for end of video
	    	 */
	    	if (a4bnet.bcOverlay[experienceID].loadCb != true) {return true;}
	    	
	    	// get the player container
	    	var playerContainer = document.getElementById(experienceID).parentNode;
	    	//console.log(playerContainer);
	    	
	    	// build the image node
	    	var imgNode = document.createElement('img');
	    	imgNode.src = a4bnet.bcOverlay[experienceID].ovrImg.src;
	    	imgNode.height = a4bnet.bcOverlay[experienceID].ovrImg.height;
	    	imgNode.width = a4bnet.bcOverlay[experienceID].ovrImg.width;
	    	imgNode.border = '0';
	    	imgNode.style.border = 'none';
	    	
	    	// build the image container
	    	var imgContainer = document.createElement('div');
	    	imgContainer.className = 'overlay-container';
	    	
	    	// build the anchor tag
	    	if (a4bnet.bcOverlay[experienceID].hasClk == true) {
	    		var imgClick = document.createElement('a');
	    		imgClick.href = a4bnet.bcOverlay[experienceID].ovrImg.click;
	    		imgClick.target = imgTarget;
	    		imgClick.style.textDecoration = 'none';
	    		imgClick.style.border = 'none';
	    	}
	    	
	    	// append the child nodes	
	    	imgContainer.appendChild(imgClick);
	    	imgClick.appendChild(imgNode);
	    	
	    	// hide the player
	    	playerContainer.style.display='none';
	    	
	    	// append the new node to the DOM
	    	var playerParent = playerContainer.parentNode;
	    	playerParent.insertBefore(imgContainer,playerContainer);
	    	
	    	
	    });  
	} else {
		a4bnet.bcOverlay[experienceID].modVP.addEventListener(BCVideoEvent.VIDEO_COMPLETE, function(evt){
			if (a4bnet.bcOverlay[experienceID].loadCb != true) {return true;}
			customCallback(evt);
		});
	}
}
