if(typeof(P6)!='object') { P6 = {}; }
if(typeof(P6.Manager)!='object') { P6.Manager = {}; }
/*----------------------------------------
	BRIGHTCOVE API MANAGER
 	(c) 2009 PEAK6 
	For questions contact Nate Racklyeft 
	
	Version:	1.0.1 - changed namespace 
	
	Requires:	jQuery 1.3+
----------------------------------------*/
P6.Manager.Brightcove = {};
P6.Manager.Brightcove.fetchCurrentPlayer = function(callback,delegate) {
	if(P6.Manager.Brightcove.__currentPlayer) {
		if(delegate) {
			P6.Manager.Brightcove.__currentPlayer.setDelegate(delegate);
		}
		callback(P6.Manager.Brightcove.__currentPlayer);
	} else {
		P6.Manager.Brightcove.init(callback,delegate);
	}
};
P6.Manager.Brightcove.init = function(callback,delegate) {
	onTemplateLoaded = function(experienceID) {
		P6.Manager.Brightcove.__currentPlayer = new P6.Manager.Brightcove.Player(experienceID,delegate);
		callback(P6.Manager.Brightcove.__currentPlayer);
	};
	if(typeof(brightcove)!='object') {
		$.get("http://admin.brightcove.com/js/BrightcoveExperiences.js",null,null,"script");
	}
	if(typeof(APIModules)!='object') {
		$.get("http://admin.brightcove.com/js/APIModules_all.js",null,null,"script");
	}
};

P6.Manager.Brightcove.Player = function(experienceID,delegate) {
	this.bcExp = brightcove.getExperience(experienceID);
	this.modExp = this.bcExp.getModule(APIModules.EXPERIENCE);
	this.modCon = this.bcExp.getModule(APIModules.CONTENT);
	this.modMenu = this.bcExp.getModule(APIModules.MENU);
	this.modVP = this.bcExp.getModule(APIModules.VIDEO_PLAYER);
	this.modSocial = this.bcExp.getModule(APIModules.SOCIAL);
	
	this.delegate = delegate || {};
	
	var ref = this;
	this.modExp	.addEventListener(BCExperienceEvent.TEMPLATE_READY, 	function(evt) { ref.onTemplateReady(evt); });
	this.modExp	.addEventListener(BCExperienceEvent.CONTENT_LOAD, 		function(evt) { ref.onContentLoad(evt); });
    this.modCon	.addEventListener(BCContentEvent.VIDEO_LOAD, 			function(evt) { ref.onVideoLoad(evt); }); 
	this.modMenu.addEventListener(BCMenuEvent.SEND_EMAIL_CLICK, 		function(evt) { ref.onMenuEvent(evt); });
	this.modVP	.addEventListener(BCMediaEvent.PLAY, 					function(evt) { ref.onMediaEvent(evt); });
	this.modVP	.addEventListener(BCMediaEvent.BEGIN, 					function(evt) { ref.onMediaEvent(evt); });
	this.modVP	.addEventListener(BCMediaEvent.COMPLETE, 				function(evt) { ref.onMediaEvent(evt); });
	this.modVP	.addEventListener(BCMediaEvent.STOP, 					function(evt) { ref.onMediaEvent(evt); });
	this.modVP	.addEventListener(BCMediaEvent.BUFFER_BEGIN, 			function(evt) { ref.onMediaEvent(evt); });
};

P6.Manager.Brightcove.Player.prototype = {
	"closeMenuPage" :function() {
		return this.modMenu.closeMenuPage();
	},
	"cueVideo" : function(vid, property) {
		var property = property || "id";
		return this.modVP.cueVideo(vid, property);
	},
	"isPlaying" : function() {
		return this.modVP.isPlaying();
	},
	"loadVideo" : function(vid, property) {
		var property = property || "id";
		return this.modVP.loadVideo(vid, property);
	},
	"playVideo" : function(vid, property) {
		vid = vid || false;
		this.closeMenuPage();
		if(!vid && !this.modVP.isPlaying()) {
			return this.modVP.play();
		}
		if(vid && !this.modVP.getCurrentVideo()) {
			return this.loadVideo(vid, property);
		}
		if(vid && this.modVP.getCurrentVideo().id == vid && !this.modVP.isPlaying()) {
			return this.modVP.play();
		}
		if(vid && this.modVP.getCurrentVideo().id != vid) {
			return this.loadVideo(vid, property);
		}
	},
	"setVideoLink" : function(link) {
		return this.modSocial.setLink(link);
	},
	"stopVideo" : function() {
		return this.modVP.stop();
	},
	// delegate notifications
	"onTemplateReady" : function (evt){
		if($.isFunction(this.delegate.onTemplateReady)) {
			this.delegate.onTemplateReady(evt);
		}
		this.templateReady = true;
	},
	"onContentLoad" : function (evt){
		if($.isFunction(this.delegate.onContentLoad)) {
			this.delegate.onContentLoad(evt);
		}
		this.contentReady = true;
	},
	"onVideoLoad" : function (evt){
		if($.isFunction(this.delegate.onVideoLoad)) {
			this.delegate.onVideoLoad(evt);
		}
		this.videoLoad = true;
	},
	"onMenuEvent" : function (evt) {
		if($.isFunction(this.delegate.onMenuEvent)) {
			this.delegate.onMenuEvent(evt);
		}
	},
	"onMediaEvent" : function (evt) {
		if($.isFunction(this.delegate.onMediaEvent)) {
			this.delegate.onMediaEvent(evt);
		}
	},
	// getters & setters
	"setDelegate" : function(delegate) {
		this.delegate = delegate || {};
	},
	"getCurrentVideo" : function() {
		return this.modVP.getCurrentVideo();
	}
};