/*
Brightcove Helper v0.4
Code for helping GalleryView cope with Brightcove

v0.1 - Initial version
v0.2 - Added Google Analytics events to keep track of when videos are played
      (only on non-homepage pages)
v0.3 - IE will overwrite the document.title for a page if you have Flash objects on the
       page and hashes in the URL.

       http://www.pcreview.co.uk/forums/ie-document-title-anchors-and-flash-bug-report-t683661.html
       http://pastebin.com/xwzkXrEu
       https://jayahoojc.wordpress.com/2009/11/20/adobe-flash-plyer-bug-url-hash-added-window-title-in-ie/

       To fix this, we need to save the document.title to a local variable first and refer to it
       later.
v0.4 - Added new bc_startVideo method in order to fix the stupid IE8 bug

*/

// Array of Brightcove videos on the page; helps determine if they're loaded and available
// for JS API manipulation
var bc_ready = [];

// See 0.3 notes above; saving the original document title into a local var so we can use it
// after Flash has loaded and IE has overwritten it's page title.
var ie_title = document.title;

// Called when BC player template is loaded
function onTemplateLoaded(experienceID) {
	if (document.location.search == "?debug") {
		alert("Loaded: " + experienceID);
	}
	// Store the player's ID and make it "ready"
	bc_ready[experienceID] = true;
	
	// Google Analytics Events
	if (window._gaq !== undefined) {
		if (experienceID!== undefined) {
			_gaq.push(['_trackEvent', 'Videos', document.location.pathname + document.location.search + document.location.hash, 'Videos: ' + ie_title + ': ' + experienceID]);
		}
	}
	document.title = ie_title;
 
	// For future use
	// var bcExp = brightcove.getExperience(experienceID);
	// var modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
}
// Called from within the gallery view code and elsewhere, used to stop a particular BC player
function bc_stopVideo(experienceID) {
	// Check to make sure it's ready for manipulation by looking at our array
	if (bc_ready[experienceID]) {
		// Stop the video
		var bcExp = brightcove.getExperience(experienceID);
		if (document.location.search == "?debug") {
			alert("Stopping: " + experienceID);
			alert("bcExp: " + bcExp);
		}
		var modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
		if (document.location.search == "?debug") {
			alert("modVP: " + modVP);
		}

		modVP.stop();
	}
}
function bc_startVideo(experienceID) {
	// Check to make sure it's ready for manipulation by looking at our array
	if (bc_ready[experienceID]) {
		var bcExp = brightcove.getExperience(experienceID);
		var modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
		modVP.play();
	}
}
