var bc_config = new Object();
	bc_config["videoRef"] 	= '<?= $configarray['videoRef'] ?>';
	bc_config['videoGuid']	= '<?= $ArticleID; ?>'
	bc_config["site"] 		= '<?= $site_domain; ?>';
	bc_config["videoLink"] 	= document.URL;


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_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(bc_videoPlayer.getVideoDuration());
	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);
	}
}

function onVideoStart(evt){
	var pos = Math.round(evt.position);
	if(pos > 0) gaMedia(bc_config["videoRef"], pos, vDuration, 'play');
}

function onVideoStop(evt){
	var pos = Math.round(evt.position);
	if(pos > 0 && pos != vDuration) gaMedia(bc_config["videoRef"], pos, vDuration, 'stop');
}


function onCuePoint(evt){
	gaMedia(bc_config["videoRef"], evt.cuePoint.time, vDuration, 'playp');
}


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 gaMedia(vFileName,vCurrentPos,vEndPos,vAction){
	pageTracker._trackEvent('Flash Player', vAction, vFileName, vCurrentPos*1000);
}

function onExternalAd(evt){

}

function playAd(pAdString){
	new ExternalAd(pAdString);
}

function ExternalAd(pXML, pOptions){
	this.buildAd = function(pXML){
		if (pXML.indexOf("<a ") !== -1 || pXML.indexOf("<script ") !== -1 || pXML.indexOf("<Script ") !== -1 ){
			callFlash("endExternalAd");
			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 ){
			callFlash("endExternalAd");
			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;
		return ad;
	}

	var ad = this.buildAd(pXML);
	if(ad.videoURL == 'undefined'){
		callFlash("endExternalAd");
	}else{
		callFlash("playAd", ad);
	}
}

