/* +--------------------------------------------------
   | canoeBcPlayer
   |   canoe brightcove player
   |
   | version : 0.3c
   | release : 2009-04-22
   |
   | author : jean-baptiste landry
   | email  : jean-baptiste.landry@canoe.ca
   |
   | copyrights : 2009 canoë inc.
   +-------------------------------------------------- */


canoeBcPlayer = function() {
	
	/* +--------------------------------------------------
	   | _executeFunction()
	   +-------------------------------------------------- */
	var _executeFunction = function(func,obj) {
		if (typeof(func) == 'function') {
			func(obj);
		} else {
			eval(func);
		}
		return false;
	};


	/* +--------------------------------------------------
	   | _inArray()
	   +-------------------------------------------------- */
	var _inArray = function(needle,haystack) {
		for (var i in haystack) {
			if (haystack[i] == needle) {
				return true;
			}
		}
		return false;
	};


	/* +--------------------------------------------------
	   | _triggerBrightcoveExperience()
	   +-------------------------------------------------- */
	var _triggerBrightcoveExperience = function() {
		brightcove.createExperiences(null,_uid);
		return false;
	};


	/* +--------------------------------------------------
	   | _generate()
	   +-------------------------------------------------- */
	var _generate = function() {
		
		if (canoeBcPlayer.templates[_player]) {
			
			// template
			// -----------------------
			var t = canoeBcPlayer.templates[_player];


			// playlists
			// -----------------------
			var playlists = [];

			// playlist sets
			for (var i in _playlistsets) {
				
				// if set exists
				var list = canoeBcPlayer.playlistsets[_playlistsets[i]];
				if (list.length) {
				
					// foreach playlist
					for (var j in list) {
						
						// if playlist exists
						var pid = canoeBcPlayer.playlists[list[j]];
						if (pid) {

							// if not already in the final list
							if (!_inArray(pid,playlists)) {
								playlists.push(pid);
							}
						}
					}
				}
			}

			// playlists
			for (var i in _playlists) {
				
				// if playlist exists
				var pid = canoeBcPlayer.playlists[_playlists[i]];
				if (pid) {

					// if not already in the final list
					if (!_inArray(pid,playlists)) {
						playlists.push(pid);
					}
				}
			}

		/* +--------------------------------------------------------------------------
			| do something for these
			+--------------------------------------------------------------------------
			|	<param name="additionalAdTargetingParams" value="str" />
			|	<param name="adServerURL" value="url" />
			|	<param name="overrideAds" value="bool" />
			|	<param name="externalAds" value="bool" />
			|	<param name="playlistTargeting" value="str" />
			+--------------------------------------------------------------------------
			| https://help.brightcove.com/developer/docs/advertising/config-params.cfm
			+-------------------------------------------------------------------------- */

			return ''+
				'<object id="'+_uid+'" class="BrightcoveExperience">'+
				'	<param name="bgcolor" value="#ffffff" />'+
				'	<param name="width" value="'+t['width']+'" />'+
				'	<param name="height" value="'+t['height']+'" />'+
				'	<param name="playerID" value="'+t['player']+'" />'+
				'	<param name="publisherID" value="'+t['publisher']+'" />'+
				'	<param name="isVid" value="true" />'+
				'	<param name="isUI" value="true" />'+
				'	<param name="wmode" value="transparent" />'+ // default as window (but changed to give more flexibility)
				'	<param name="autoStart" value="'+((_autostart) ? 'true' : 'false')+'" />'+
				'	<param name="@videoPlayer" value="'+_video+'" />'+
				'	<param name="@playlistTabs" value="'+playlists.join(',')+'" />'+
				'	<param name="@playlistTabs.featured" value="'+_playlist+'" />'+
				'	<param name="@videoList" value="'+playlists.join(',')+'" />'+  // should be videoId but only playlistId works ... weird
				'	<param name="@videoList.featured" value="'+_video+'" />'+
				'</object>'+
			'';
		} else {
			return 'player template \''+_player+'\' not found';
		}
	};




	/* +--------------------------------------------------
	   | _registerApi()
	   +-------------------------------------------------- */
	this._registerApi = function(controller) {
		this.api._controller = controller;
		this.api.videoplayer = this.api._controller.getModule(APIModules.VIDEO_PLAYER);
		this.api.content     = this.api._controller.getModule(APIModules.CONTENT);
		this.api.experience  = this.api._controller.getModule(APIModules.EXPERIENCE);
		this.api.menu        = this.api._controller.getModule(APIModules.MENU);
		this.api.social      = this.api._controller.getModule(APIModules.SOCIAL);
		this.api.advertising = this.api._controller.getModule(APIModules.ADVERTISING);
		this.api.cuepoints   = this.api._controller.getModule(APIModules.CUE_POINTS);
		this.api.search      = this.api._controller.getModule(APIModules.SEARCH);
		
		_readyInterval = setInterval('if (canoeBcPlayer.instances['+_index+'].api.videoplayer.getCurrentVideo() != null) { canoeBcPlayer.instances['+_index+']._executeReadyFunctions(); }', 100);

		return false;
	};

	/* +--------------------------------------------------
	   | _executeReadyFunctions()
	   +-------------------------------------------------- */
	this._executeReadyFunctions = function() {
		clearInterval(_readyInterval);
		
		this.api._loaded = true;

		if (_ready.length) {
			for (var i in _ready) {
				_executeFunction(_ready[i],this);
			}
		}
		
		return false;
	};


	/* +--------------------------------------------------
	   | addPlaylist(id)
	   +-------------------------------------------------- */
	this.addPlaylist = function(id) {
		if (!_inArray(id,_playlists)) {
			_playlists.push(id);
		}	

		return this;
	};

	/* +--------------------------------------------------
	   | addPlaylistSet(id)
	   +-------------------------------------------------- */
	this.addPlaylistSet = function(id) {
		if (!_inArray(id,_playlistsets)) {
			_playlistsets.push(id);
		}	

		return this;
	};

	/* +--------------------------------------------------
	   | setAutostart(autostart)
	   +-------------------------------------------------- */
	this.setAutostart = function(autostart) {
		_autostart = (autostart) ? true : false;
		return this;
	};

	/* +--------------------------------------------------
	   | setTemplate(id)
	   +-------------------------------------------------- */
	this.setTemplate = function(id) {
		_player = id;
		return this;
	};

	/* +--------------------------------------------------
	   | setPlaylist(id)
	   +-------------------------------------------------- */
	this.setPlaylist = function(id) {
		_playlist = (canoeBcPlayer.playlists[id]) ? canoeBcPlayer.playlists[id] : 0;
		return this;
	};

	/* +--------------------------------------------------
	   | setPlaylistById(id)
	   +-------------------------------------------------- */
	this.setPlaylistById = function(id) {
		_playlist = (Number(id)) ? id : 0;
		return this;
	};

	/* +--------------------------------------------------
	   | setVideo(id)
	   +-------------------------------------------------- */
	this.setVideo = function(id) {
		_video = (Number(id)) ? id : 0;
		return this;
	};




	/* +--------------------------------------------------
	   | append(id)
	   +-------------------------------------------------- */
	this.append = function(id) {
		document.getElementById(id).innerHTML = _generate();
		_triggerBrightcoveExperience();
		return this;
	};

	/* +--------------------------------------------------
	   | write()
	   +-------------------------------------------------- */
	this.write = function() {
		document.write(_generate());
		_triggerBrightcoveExperience();
		return this;
	};

	/* +--------------------------------------------------
	   | ready(function)
	   +-------------------------------------------------- */
	this.ready = function(data) {
		if (this.api._loaded) {
			_executeFunction(data);
		} else {
			_ready.push(data);
		}
		return this;
	};



	/* +--------------------------------------------------
	   | toString()
	   +-------------------------------------------------- */
	this.toString = function() {
		return '[object canoeBcPlayer v'+canoeBcPlayer.version+'] - '+((_player) ? _player : 'null')+':'+((_video) ? _video : 'null');
	};



	// private properties
	// -----------------------
	canoeBcPlayer.instances.push(this);
	var _index         = canoeBcPlayer.instances.length-1;
	var _uid           = '_canoebcplayer_'+_index;
	var _player        = '';
	var _playlists     = [];
	var _playlistsets  = [];
	var _playlist      = 0;
	var _video         = 0;
	var _autostart     = false;
	var _ready         = [];
	var _readyInterval = 0;




	/* +--------------------------------------------------
	   | api
	   +-------------------------------------------------- */
	this.api = {};
	this.api._controller = null;
	this.api._loaded     = false;
	this.api.videoplayer = null;
	this.api.content     = null;
	this.api.experience  = null;
	this.api.menu        = null;
	this.api.social      = null;
	this.api.advertising = null;
	this.api.cuepoints   = null;
	this.api.search      = null;
};





/* +--------------------------------------------------
   |
   | STATIC METHODS
   |
   +-------------------------------------------------- */
canoeBcPlayer.version      = '0.3c';
canoeBcPlayer.templates    = {};
canoeBcPlayer.playlists    = {};
canoeBcPlayer.playlistsets = {};
canoeBcPlayer.instances    = [];

/* +--------------------------------------------------
   | addTemplate(id,data)
   +-------------------------------------------------- */
canoeBcPlayer.addTemplate = function(id,data) {
	canoeBcPlayer.templates[id] = data;
	return canoeBcPlayer;
};

/* +--------------------------------------------------
   | addPlaylist(id,data)
   +-------------------------------------------------- */
canoeBcPlayer.addPlaylist = function(id,pid) {
	canoeBcPlayer.playlists[id] = pid;
	return canoeBcPlayer;
};

/* +--------------------------------------------------
   | addPlaylistSet(id,data)
   +-------------------------------------------------- */
canoeBcPlayer.addPlaylistSet = function(id,pset) {
	canoeBcPlayer.playlistsets[id] = pset;
	return canoeBcPlayer;
};

/* +--------------------------------------------------
   | toString()
   +-------------------------------------------------- */
canoeBcPlayer.toString = function() {
	return '[object canoeBcPlayer v'+canoeBcPlayer.version+']';
};








/* +--------------------------------------------------
   |
   | Brightcove register function
   |
   +-------------------------------------------------- */
function onTemplateLoaded(experienceID) {
	if (typeof(APIModules) != 'undefined') {
		var bcexp    = brightcove.getExperience(experienceID);
		var elements = experienceID.split('_');
		var id       = elements[elements.length-1];

		canoeBcPlayer.instances[id]._registerApi(bcexp);
	}
	return false;
}
