/* bc3_api_class.js
*
*	BC3Api -- a simple class for making Brightcove3 API requests
*
*	Author: Ronan Rafferty
*	Date: 09/10/2008
* 
* 	*Adapted from Jason Levitt's JSONscriptRequest class
* 
* 	A SECURITY WARNING FROM DOUGLAS CROCKFORD:
* 	"The dynamic <script> tag hack suffers from a problem. It allows a page 
* 	to access data from any server in the web, which is really useful. 
* 	Unfortunately, the data is returned in the form of a script. That script 
* 	can deliver the data, but it runs with the same authority as scripts on 
* 	the base page, so it is able to steal cookies or misuse the authorization 
* 	of the user with the server. A rogue script can do destructive things to 
* 	the relationship between the user and the base server."
* 
* 	Sample Usage: TODO add an example
* 
*/
	function BC3Api() {
		// Instance Properties
		
		
	    // REST request proprties
	    this.fullUrl = ""; 
	    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
	    this.headLoc = document.getElementsByTagName("head").item(0);
	}

/*	Static Properties
 */
	BC3Api.prototype.SERVER_URL = "http://api.brightcove.com/services/library";
	BC3Api.prototype.VIDEOS_TAGS_COMMAND = "command=find_videos_by_tags";
	BC3Api.prototype.VIDEOS_TEXT_COMMAND = "command=find_videos_by_text";
	BC3Api.prototype.VIDEOS_BY_IDS_COMMAND = "command=find_videos_by_ids";
	BC3Api.prototype.VIDEOS_BY_LINEUP = "command=find_playlist_by_id";
	BC3Api.prototype.CALLBACK_PRE_COMMAND = "callback=";
	BC3Api.prototype.TOKEN = 'token=sO41DP-z_d48omAJKISz-CnQGAbMOvC8-n4iQo3vwXs.';
	
// TODO: Make the Token Safe

/*	Methods
 */
 	/* createRequest method
	*/
	BC3Api.prototype.createRequest = function (id, command, args, callback) 
	{
		this.fullUrl = this.SERVER_URL + '?' + command + args + '&' + this.CALLBACK_PRE_COMMAND + callback + '&' + this.TOKEN;
				
		this.buildScriptTag(id); 
		this.addScriptTag();
	}
	
 	/* findVideosByTags method
 	 *
 	 * @param	args	An object collection of parameteres for the function call
 	 * 					{
 	 * 						id: 					The name for the script which is added on the fly
 	 * 						and_tags: 				A string of tags comma delimeted tags to search the library with eg 'Mountain,Lion,Attack'
 	 * 						or_tags:				A string for the search type. Available options are 'and_tags' or 'or_tags'
 	 * 						callback:				A string with the JavaScript function name which will be called on completion
 	 * 						page_size: (opt)  		An Integer  of items returned per page.
 	 * 						page_number: (opt) 		An Integer (zero-indexed) of the page to return.
 	 * 						sort_by: (opt) 			The field by which to sort the results. In this method, results can be sorted only by MODIFIED_DATE and PLAYS_TRAILING_WEEK.
 	 * 						sort_order: (opt) 		How to order the results: ascending (ASC) or descending (DESC).
 	 * 						get_item_count: (opt) 	If true, also return how many total results there are.
 	 * 						fields: (opt) 			A comma-separated list of the fields you wish returned in the object. 						
 	 * 					} 
 	 *  * see https://help.brightcove.com/developer/docs/mediaapi/objects.cfm for object properties
	 */
	BC3Api.prototype.findVideosByTags = function (args) 
	{
		// Test for required paramaters
		if(args.id == undefined || (args.and_tags == undefined && args.or_tags == undefined) || args.callback == undefined ) return false;
		
		// Create the paramaters string
		var params = "&";
		
		for(name in args){
			if(name != "id" && name != "callback") params += "&" + name + "=" + args[name];
		}
		
		// Make the request & return true
		this.createRequest(	args.id, 
							this.VIDEOS_TAGS_COMMAND,
							params,
							args.callback
						);
	
		 return true;
	}
	
 	/* findVideosByIds method
 	 *
 	 * @param	args	An object collection of parameteres for the function call
 	 * 					{
 	 * 						id: 					The name for the script which is added on the fly
 	 * 						video_ids: 				A list of the ids for the videos we'd like to retrive
 	 * 						callback:				A string with the JavaScript function name which will be called on completion
 	 * 						fields: (opt) 			A comma-separated list of the fields you wish returned in the object. 						
 	 * 					} 
 	 *  * see https://help.brightcove.com/developer/docs/mediaapi/objects.cfm for object properties
	 */
	BC3Api.prototype.findVideosByIds = function (args) 
	{
		// Test for required paramaters
		if(args.id == undefined || args.video_ids == undefined || args.callback == undefined ) return false;
		
		// Create the paramaters string
		var params = "&";
		
		for(name in args){
			if(name != "id" && name != "callback") params += "&" + name + "=" + args[name];
		}
		
		// Make the request & return true
		this.createRequest(	args.id, 
							this.VIDEOS_BY_IDS_COMMAND,
							params,
							args.callback
						);
	
		 return true;
	}
	
 	/* findVideosByLineupId method
 	 *
 	 * @param	args	An object collection of parameteres for the function call
 	 * 					{
 	 * 						id: 					The name for the script which is added on the fly
 	 * 						playlist_id: 			A list of the ids for the videos we'd like to retrive
 	 * 						callback:				A string with the JavaScript function name which will be called on completion
 	 * 						fields: (opt) 			A comma-separated list of the fields you wish returned in the object. 						
 	 * 					} 
 	 *  * see https://help.brightcove.com/developer/docs/mediaapi/objects.cfm for object properties
	 */
	BC3Api.prototype.findVideosByLineupId = function (args) 
	{
		// Test for required paramaters
		if(args.id == undefined || args.playlist_id == undefined || args.callback == undefined ) return false;
		
		// Create the paramaters string
		var params = "&";
		
		for(name in args){
			if(name != "id" && name != "callback") params += "&" + name + "=" + args[name];
		}
		
		// Make the request & return true
		this.createRequest(	args.id, 
							this.VIDEOS_BY_LINEUP,
							params,
							args.callback
						);
	
		 return true;
	}
	
 	/* findVideosByText method
 	 *
 	 * @param	args	An object collection of parameteres for the function call
 	 * 					{
 	 * 						id: 					The name for the script which is added on the fly
 	 * 						text: 					A string of the text we are looking for eg 'looking for this'
 	 * 						callback:				A string with the JavaScript function name which will be called on completion
 	 * 						page_size: (opt)  		An Integer  of items returned per page.
 	 * 						page_number: (opt) 		An Integer (zero-indexed) of the page to return.
 	 * 						get_item_count: (opt) 	If true, also return how many total results there are.
 	 * 						fields: (opt) 			A comma-separated list of the fields you wish returned in the object. 						
 	 * 					} 
 	 *  * see https://help.brightcove.com/developer/docs/mediaapi/objects.cfm for object properties
	 */
	BC3Api.prototype.findVideosByText = function (args) 
	{
		// Test for required paramaters
		if(args.id == undefined || args.text == undefined  || args.callback == undefined ) return false;
		
		// Create the paramaters string
		var params = "";
		
		for(name in args){
			if(name != "id" && name != "callback") params += "&" + name + "=" + args[name];
		}
		
		// Make the request & return true
		this.createRequest(	args.id, 
							this.VIDEOS_TEXT_COMMAND,
							params,
							args.callback
						);
	
		 return true;
	}

	/* buildScriptTag method
	*/
	BC3Api.prototype.buildScriptTag = function (id) 
	{	
	    // Create the script tag
	    this.scriptObj = document.createElement("script");
	    
	    // Add script object attributes
	    this.scriptObj.setAttribute("type", "text/javascript");
	    this.scriptObj.setAttribute("charset", "utf-8");
	    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
	    this.scriptObj.setAttribute("id", id);
	}
	 
	/* removeScriptTag method
	*/
	BC3Api.prototype.removeScriptTag = function () 
	{
	    // Destroy the script tag
	    this.headLoc.removeChild(this.scriptObj);  
	}
	
	/* addScriptTag method
	*/
	BC3Api.prototype.addScriptTag = function () 
	{
	    // Create the script tag
	    this.headLoc.appendChild(this.scriptObj);
	}
	