var bcExp;
var bc_videoPlayer;
var bc_experience;
var bc_content;
var bc_menu;
var bc_social;
var bc_cue;
var bc_currentVideo;
var bc_currentID;
var bc_isReady = false;
var bc_playCount = 0;
var vDuration;

function onTemplateLoaded(experienceID) {
	bcExp = brightcove.getExperience(experienceID);
	
	// EXPERIENCE MODULE API //
	bc_experience = bcExp.getModule(APIModules.EXPERIENCE);
	bc_experience.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
	
	// CONTENT MODULE API //
	bc_content = bcExp.getModule(APIModules.CONTENT);
	bc_content.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);
	
	// PLAYER MODULE API //		
	bc_videoPlayer = bcExp.getModule(APIModules.VIDEO_PLAYER);
	bc_videoPlayer.addEventListener(BCVideoEvent.VIDEO_CONNECT, onConnect);
	bc_videoPlayer.addEventListener(BCVideoEvent.STREAM_START, onStreamStart);
	bc_videoPlayer.addEventListener(BCVideoEvent.VIDEO_START, onVideoStart);
	bc_videoPlayer.addEventListener(BCVideoEvent.VIDEO_STOP, onVideoStop);
	bc_videoPlayer.addEventListener(BCVideoEvent.VIDEO_COMPLETE, onVideoComplete);
	
	bc_advertising = bcExp.getModule(APIModules.ADVERTISING);
	bc_advertising.enableExternalAds(true);
	bc_advertising.addEventListener(BCAdvertisingEvent.EXTERNAL_AD, onExternalAd);
	
	// MENU MODULE //
	bc_menu = bcExp.getModule(APIModules.MENU);
	bc_menu.addEventListener(BCMenuEvent.VIDEO_REQUEST, onRequest);

	// CUE POINTS MODULE //
	bc_cue = bcExp.getModule(APIModules.CUE_POINTS);
	bc_cue.addEventListener(BCCuePointEvent.CUE, onCuePoint);

}

function onConnect(evt){
	bc_menu.closeMenuPage();
}

function onContentLoad(evt) {
	
	callCounter();
	bc_currentVideo = bc_videoPlayer.getCurrentVideo();
	bc_social = bcExp.getModule(APIModules.SOCIAL);
	bc_social.setLink('http://www.'+bc_config['site']+'.com/common/media/video/player.php?videoRef='+bc_config["videoRef"]);
}


function onVideoLoad(evt) {
	bc_currentID = evt.video.id;
	bc_videoPlayer.loadVideo(evt.video.id);
}

function onStreamStart(evt) {
	vDuration = Math.round(evt.duration);
	if(vDuration > 0){
		bc_cue.clearCodeCuePoints(bc_currentVideo.id);	
		var vCue = new Array(Math.round(vDuration * .25), Math.round(vDuration * .50),Math.round(vDuration * .75), vDuration);
		var pass = bc_cue.addCuePoints(bc_currentVideo.id, vCue);
		hbxMedia(bc_config["videoRef"], 0, vDuration, 'play');
	}
}

function onVideoStart(evt){
	var pos = Math.round(evt.position);
	if(pos > 0 && vDuration > 0) hbxMedia(bc_config["videoRef"], pos, vDuration, 'playp');
}

function onVideoStop(evt){
	var pos = Math.round(evt.position);
	if(pos > 0 && pos != vDuration) hbxMedia(bc_config["videoRef"], pos, vDuration, 'pause');
}


function onCuePoint(evt){
	if(evt.cuePoint.time > 0 && vDuration > 0)hbxMedia(bc_config["videoRef"], evt.cuePoint.time, vDuration, 'playp');
}

function onVideoComplete(evt){
	hbxMedia(bc_config["videoRef"], vDuration, vDuration, 'stop');
}

function onRequest(evt){
	config['videoRef'] = evt.args.referenceId;
	bc_social.setLink('http://www.'+bc_config['site']+'.com/common/media/video/player.php?videoRef='+bc_config["videoRef"]);
}


function callCounter(){
	var url = 'http://www.'+bc_config['site']+'.com/common/brightcove/bc_flash_functions.php';
	var re = new Ajax.Request(url, { parameters:{mode:"incrementCounter", guid:bc_config['videoGuid']}});
}

function hbxMedia(vFileName,vCurrentPos,vEndPos,vAction){

	_hbSet('m.s',vAction);
	_hbSet('m.f',vFileName);
	_hbSet('m.cp',vCurrentPos*1000);
	_hbSet('m.ep',vEndPos*1000);
	_hbSet('m.cl','Flash Player');
	_hbSet('m.cv','9.0');
	_hbSet('m.tt','e');
	_hbSend();
}

function onExternalAd(evt){
	new ExternalAd(evt["ad"]); 
}


function ExternalAd(pXML){
	this.buildAd = function(pXML){
		if (pXML.indexOf("<a ") !== -1 || pXML.indexOf("<script ") !== -1 || pXML.indexOf("<Script ") !== -1  || pXML.indexOf("CDATA ") !== -1){
			bc_advertising.resumeAfterExternalAd();
			return;
		}
		if (window.ActiveXObject){
			//parses the XML for IE browsers
			var adXML = new ActiveXObject("Microsoft.XMLDOM");
			adXML.async = false;
			adXML.loadXML(pXML);
		} else if (window.XMLHttpRequest) {
			var adXML = (new DOMParser()).parseFromString(pXML, "text/xml"); //parses the XML for Mozilla browsers
		} 
		if(adXML.firstChild.getAttribute("duration") == null ){
			bc_advertising.resumeAfterExternalAd();
			return;
		}
		
		var ad = new Object();
			ad.type = "videoAd";
		var nodeItems = adXML.firstChild.childNodes.length;
		var currentNode = adXML.firstChild.firstChild;
		for(var i = 0; i < nodeItems; i++){
			//checks to see if the current nodes are in our Rich Media Templates and assigns them if they exist
			if( currentNode.nodeName == "videoURL" && currentNode.firstChild ){
				ad.videoURL = currentNode.firstChild.nodeValue;
			}
			if( currentNode.nodeName == "videoClickURL" && currentNode.firstChild ){ 
				ad.videoClickURL = currentNode.firstChild.nodeValue;
			}
			currentNode = currentNode.nextSibling;
		}
		ad.duration = (adXML.firstChild.getAttribute("duration") !== "") ? adXML.firstChild.getAttribute("duration") : 15;
		
		if(adXML.firstChild.getAttribute("trackStartURLs") !== ""){
			ad.trackStartURLs = adXML.firstChild.getAttribute("trackStartURLs").split(",");
		}
			
		return ad;
	}
	var ad = this.buildAd(pXML);
	if(ad.videoURL == 'undefined'){
		bc_advertising.resumeAfterExternalAd();
	}else{
		bc_advertising.showAd(ad)
		//alert(ad.videoURL);
		
		//callFlash("playAd", ad);
	}
}
