/*
* $Id: dniBrightcove.js 24688 2009-06-02 11:14:06Z jhickford $
*/
//parses the XML string into DOM
function parseXML(xml) {
	if (window.ActiveXObject) {
		
		dom = new ActiveXObject("Microsoft.XMLDOM");
		dom.async = false;
		dom.loadXML(xml);
	} else if (window.XMLHttpRequest) {
		dom = (new DOMParser()).parseFromString(xml, "text/xml"); 
	}
	return dom;
}

// Convert ad server XML into a Brightcove video ad object
function getVideoAd(adXML){
	var videoAd = new Object();
	videoAd.type = "videoAd";

	var nodeItems = adXML.firstChild.childNodes.length;
	var currentNode = adXML.firstChild.firstChild;
	
	//get the root node attributes
	videoAd.duration = (adXML.firstChild.getAttribute("duration")) ? adXML.firstChild.getAttribute("duration") : 15;
	if(adXML.firstChild.getAttribute("trackStartURLs")) videoAd.trackStartURLs = adXML.firstChild.getAttribute("trackStartURLs").split(",");
	if(adXML.firstChild.getAttribute("trackMidURLs") ) videoAd.trackMidURLs = adXML.firstChild.getAttribute("trackMidURLs").split(",");
	if(adXML.firstChild.getAttribute("trackEndURLs") ) videoAd.trackEndURLs = adXML.firstChild.getAttribute("trackEndURLs").split(",");
	if(adXML.firstChild.getAttribute("trackPointURLs") ) videoAd.trackPointURLs = adXML.firstChild.getAttribute("trackPointURLs").split(",");
	videoAd.trackPointTime = (adXML.firstChild.getAttribute("trackPointTime") ) ? adXML.firstChild.getAttribute("trackPointTime") : 0;

	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) videoAd.videoURL = currentNode.firstChild.nodeValue; 
		if(currentNode.nodeName == "videoClickURL" && currentNode.firstChild) videoAd.videoClickURL = currentNode.firstChild.nodeValue;			
		currentNode = currentNode.nextSibling;
	}
	return videoAd;
}

// Convert ad server XML into an image and url - based on matching a div name against adserver XML child nodes
function getCustomAd(adId,adXML){
	var ad = new Object();
	ad.linkUrl = null;
	ad.imageUrl = null;

	var nodeItems = adXML.firstChild.childNodes.length;
	var currentNode = adXML.firstChild.firstChild;
	
	for(var i = 0; i < nodeItems; i++) {
		if(currentNode.nodeName == adId + "URL" && currentNode.firstChild) ad.imageUrl = currentNode.firstChild.nodeValue; 
		if(currentNode.nodeName == adId + "ClickURL" && currentNode.firstChild) ad.linkUrl = currentNode.firstChild.nodeValue;			
		currentNode = currentNode.nextSibling;
	}
	return ad;
}


// Class to handle the event handlers to send reporting infomration to Brightcove
function everyZingReportingObject() {
	var _playerName = null;
	var _titleName = null;
	
	var _experienceID = null;
	var _experience = null;
	var _myURL = null;
	var _reportSuiteId = null;
	var _typeOfInteractive=null;
	var _siteId = null;
	
	this.initalize = _initalize;
	this.contentLoad = _contentLoad;
	this.videoComplete = _videoComplete;
	this.videoProgress = _videoProgress;
	this.adComplete = _adComplete;
	this.adStart = _adStart;
	
	function _initalize(experienceID,playerName,titleName, reportSuiteId,siteId) {
		_experienceID = experienceID;
		_experience = document.getElementById(_experienceID);
		_reportSuiteId = reportSuiteId;
		_typeOfInteractive = "VIDEO:";
		_playerName = playerName;
		_titleName = "SF:"+ titleName;
		_myURL = location.href;
		_siteId = siteId;
	}
		
	function _contentLoad(event) {}
	
	function _adStart(event) {
		s_linkType = 'o';
		s_linkTrackVars = "events";
		s_linkTrackEvents = "event1";
		s_events = "event1";
		s_linkName = _typeOfInteractive + "Ad Started";
		s_lnk=s_co(_experience);
		void(s_gs(_reportSuiteId));
		s_events = "";
		//$("#ClipInfo").append("Ad Start ");			
	}
	
	function _adComplete(event) {
		s_linkType = 'o';
		s_linkTrackVars = "events";
		s_linkTrackEvents = "event9";
		s_events = "event9";
		s_linkName = _typeOfInteractive + "Ad Completed";
		s_lnk=s_co(_experience);
		void(s_gs(_reportSuiteId));
		s_events = "";
		//$("#ClipInfo").append("Ad Complete ");	
	}
	
	// only send each of the %completeness events once.
	function _videoProgress(percentage) {
		if(percentage == 3) {
			s_linkType = 'o';
			s_linkTrackVars = "prop15,eVar15,prop18,eVar18,prop19,eVar19,prop17,eVar17,events";
			s_linkTrackEvents = "event2";
			s_prop17 = _siteId + ":" + _titleName;
			s_eVar17 = _siteId + ":" + _titleName;
			s_prop15 = _typeOfInteractive + _myURL.replace(/http:\/\//,"");
			s_eVar15 = _typeOfInteractive + _myURL.replace(/http:\/\//,"");
			s_prop18 = _siteId;
			s_eVar18 = _siteId;
			s_prop19 = "DNI:" + _playerName;
			s_eVar19 = "DNI:" + _playerName;
			s_events = "event2";
			s_linkName = _typeOfInteractive + "Clip Started";
			s_lnk=s_co(_experience);
			void(s_gs(_reportSuiteId));
			s_prop15 = "";
			s_eVar15 = "";
			s_prop17 = "";
			s_eVar17 = "";
			s_prop18 = "";
			s_eVar18 = "";
			s_prop19 = "";
			s_eVar19 = "";
			s_events = "";
			//$("#ClipInfo").append("Clip Start " );
		}
	}

	function _videoComplete(event) {
		s_linkType = 'o';
		s_linkTrackVars = "events";
		s_linkTrackEvents = "event3";
		s_events = "event3";
		s_linkName = _typeOfInteractive + "Clip Completed";
		s_lnk=s_co(_experience);
		void(s_gs(_reportSuiteId));
		s_events = "";
		//$("#ClipInfo").append("Video Complete ");
	}
	
	
}




// Class to handle the event handlers to send reporting infomration to Brightcove
function dniBrightcoveReporting() {
	var _completeness = null;
	var _playerName = null;
	var _titleLength = null;
	var _titleName = null;
	var _formIndicator = null;
	
	var _experienceID = null;
	var _experience = null;
	var _myURL = null;
	var _reportSuiteId = null;
	var _typeOfInternactive=null;
	var _siteId = null;
	var _specialEventArr = new Array();
	
	var _BCModExp = null;
	var _BCModMenu = null;
	var _BCModVideo = null;
	var _BCModAd = null;
	
	this.initalize = _initalize;
	this.contentLoad = _contentLoad;
	this.videoComplete = _videoComplete;
	this.videoProgress = _videoProgress;
	this.videoLoad  = _videoLoad;
	this.adComplete = _adComplete;
	this.adStart = _adStart;
	
	function _initalize(isLongForm,experienceID,playerName,reportSuiteId,siteId,BCModExp,BCModMenu,BCModVideo,BCModAd) {
		_completeness = -1;
		
		_experienceID = experienceID;
		_experience = document.getElementById(_experienceID);
		_reportSuiteId = reportSuiteId;
		_typeOfInteractive = "VIDEO:";
		_playerName = playerName;
		_myURL = location.href;
		_siteId = siteId;
		
		if (isLongForm) _formIndicator = "LF:";
		else _formIndicator = "SF:";
		
		_BCModExp = BCModExp;
		_BCModMenu = BCModMenu;
		_BCModVideo = BCModVideo;
		_BCModAd = BCModAd;
		
		// Brightcove can get confused about binding multiple event listeners to the same event if the event handlers live in a different scope (e.g. the ad handler and reporting objects)
		// therefore we can't seem to use the same events in both handlers.  see the ad progress event below
		_BCModExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, this.contentLoad);
		_BCModVideo.addEventListener(BCVideoEvent.VIDEO_COMPLETE, this.videoComplete);
		_BCModVideo.addEventListener(BCVideoEvent.VIDEO_PROGRESS, this.videoProgress);
		
		
		
		_BCModAd.addEventListener(BCAdvertisingEvent.AD_COMPLETE, this.adComplete); 
		// Note - have used the ad progress event to avoid clashing with the ad handlers ad start event.  This event handler is unbound once first triggered.  Cunning huh.
		_BCModAd.addEventListener(BCAdvertisingEvent.AD_PROGRESS, this.adStart);
	}
	
	function _adStart(event) {
		if(! _titleName) {_contentLoad();}
		s_linkType = 'o';
		s_linkTrackVars = "events";
		s_linkTrackEvents = "event1";
		s_events = "event1";
		s_linkName = _typeOfInteractive + "Ad Started";
		s_lnk=s_co(_experience);
		void(s_gs(_reportSuiteId));
		s_events = "";
		_BCModAd.removeEventListener(BCAdvertisingEvent.AD_PROGRESS, _adStart);	
		//$("#ClipInfo").append("Ad Start ");			
	}
	function _adComplete(event) {
		s_linkType = 'o';
		s_linkTrackVars = "events";
		s_linkTrackEvents = "event9";
		s_events = "event9";
		s_linkName = _typeOfInteractive + "Ad Completed";
		s_lnk=s_co(_experience);
		void(s_gs(_reportSuiteId));
		s_events = "";
		//$("#ClipInfo").append("Ad Complete ");	
	}
	
	function _videoLoad(event) {
		var currentVideo = _BCModVideo.getCurrentVideo();
		if(currentVideo && (_titleName != _formIndicator + ": " + currentVideo.displayName)) {
			_contentLoad();
		}
	}
	
	function _contentLoad(event) {
		_BCModVideo.addEventListener(BCVideoEvent.VIDEO_LOAD, _videoLoad);
		var currentVideo = _BCModVideo.getCurrentVideo();
		if(currentVideo) {
			_titleName = _formIndicator + ": " + currentVideo.displayName;
			_titleLength = currentVideo.length / 1000;
			_completeness = -1;
		}
	}
	function _videoComplete(event) {
		_completeness = 1;
		s_linkType = 'o';
		s_linkTrackVars = "events";
		s_linkTrackEvents = "event3";
		s_events = "event3";
		s_linkName = _typeOfInteractive + "Clip Completed";
		s_lnk=s_co(_experience);
		void(s_gs(_reportSuiteId));
		s_events = "";
		_BCModVideo.removeEventListener(BCVideoEvent.VIDEO_COMPLETE, _videoComplete);	
		//$("#ClipInfo").append("Video Complete ");
	}
	
	// only send each of the %completeness events once.
	function _videoProgress(event) {
		if(_completeness < 0 && event.position / event.duration > 0) {
			if(! _titleName) {_contentLoad();}
			_completeness = 0;
			s_linkType = 'o';
			s_linkTrackVars = "prop15,eVar15,prop18,eVar18,prop19,eVar19,prop17,eVar17,events";
			s_linkTrackEvents = "event2";
			s_prop15 = _typeOfInteractive + _myURL.replace(/http:\/\//,"");
			s_prop18 = _siteId;
			s_prop19 = "DNI:" + _playerName;
			s_eVar15 = _typeOfInteractive + _myURL.replace(/http:\/\//,"");
			s_eVar18 = _siteId;
			s_eVar19 = "DNI:" + _playerName;
			s_prop17 = _siteId + ":" + _titleName;
			s_eVar17 = _siteId + ":" + _titleName;
			s_events = "event2";
			s_linkName = _typeOfInteractive + "Clip Started";
			s_lnk=s_co(_experience);
			void(s_gs(_reportSuiteId));
			s_prop15 = "";
			s_eVar15 = "";
			s_prop17 = "";
			s_eVar17 = "";
			s_prop18 = "";
			s_eVar18 = "";
			s_prop19 = "";
			s_eVar19 = "";
			s_events = "";
			//$("#ClipInfo").append("Clip Start );
		}
	}
}


// Handler to mange the Brightcove takeover of in page adverts
function dniBrightcoveAdvertisingHandler() {
	this.adSpots = new Array();
	var _BCModAd = null;
	var _BCModVideo = null;
	var _adPolicy = null;
	
	this.onExternalAd = _onExternalAd;
	this.onAdStart = _onAdStart;
	
	// Keep and array of adverts that are Video compatible on the page	
	this.createAdSpot = function(advert) {
		this.adSpots.push(advert);
	}
	
	this.bindBrightcove = function(BCModAd,BCModVideo) {
		_BCModAd = BCModAd;
		_BCModVideo = BCModVideo;

		// ad event handlers for whenever an unknown ad call is received
		_BCModAd.addEventListener(BCAdvertisingEvent.EXTERNAL_AD, this.onExternalAd);
		
		// event handlers that show the normal page dispaly ad when no companion ad is served through BC.  These events are unbound if a companion ad is served
		_BCModAd.addEventListener(BCAdvertisingEvent.AD_START, this.onAdStart);
		_BCModVideo.addEventListener(BCVideoEvent.STREAM_START, this.onAdStart);
	}
	
	function _onExternalAd(event) {
		var pXML = event.ad;
		var adXML = parseXML(pXML);
		var videoAd = getVideoAd(adXML);
		
		// todo remove hardcoding of advertsing?
		$.each(advertising.adSpots, function() {
			var ad = getCustomAd(this.id,adXML);
			// if ad is valid display the ad other wise show the display ad
			if(ad.linkUrl && ad.imageUrl) {
				$("#" + this.id).html("<a target = '_blank' href='" + ad.linkUrl + "'><img src='" + ad.imageUrl + "' /></a>")
			} else {
				_onAdStart();
			}
		});
		// remove the event listeners from showing the display ad on the start of the vide oad or the title
		_BCModAd.removeEventListener(BCAdvertisingEvent.AD_START, _onAdStart);
		_BCModVideo.removeEventListener(BCVideoEvent.STREAM_START, _onAdStart);
		// send the video ad object back to the BC player to show
		_BCModAd.showAd(videoAd);
	}
	
	//todo - remove hardcoded advertsing?
	function _onAdStart(event) {
		$.each(advertising.adSpots, function() {
			this.showDisplayAd();
		});
	}
}

// Handles a Brightcove overridable ad spot.  The id must match the id of the element where the ad will be inserted, and the start of the XML in the ad response.
function dniAdvert(id,adCall,width,height) {
	this.id = id;
	this.adCall = adCall;
	this.width = width;
	this.height = height;
	
	this.showDisplayAd = function() {
		$("#" + this.id).empty();
		// create iframe and use the ad request path as the src attribute
		var $frame = $(document.createElement('iframe')).attr({ 
          id: "adFrame_" + id,
		  name: "adFrame_" + id,
		  width: width,
          height: height,
          frameBorder: "0",
		  scrolling : "no",
		  src : this.adCall
        });
		$frame.appendTo('#' + id);
	}
}
