	/*********************************************************************

		A modified library for brightcove in javascript
		
		I modified the kudos code to essentially just be a URL generator
		for brightcove api queries, removing the existing callbacks, which
		we'll use jQuery for...

		-- Joe Hohertz

		BASED ON:

		Kudos-js, Brightcove Javascript SDK
		Version 0.1
	
		Official Website - http://www.kudos-js.com
		Code Repository	- http://code.google.com/p/kudos-js
	
		Authors:
			Brian Franklin, Services Engineer, Brightcove, Inc
			Matthew Congrove, Services Engineer, Brightcove, Inc

		Copyright 2009 Brian Franklin, Matthew Congrove
	
		Permission is hereby granted, free of charge, to any person obtaining a copy
		of this software and associated documentation files (the "Software"), to deal
		in the Software without restriction, including without limitation the rights
		to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
		copies of the Software, and to permit persons to whom the Software is
		furnished to do so, subject to the following conditions:

		THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
		IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
		FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
		AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
		LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
		OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
		THE SOFTWARE.

	*********************************************************************/

	var judos = new function () {
	
		// Enter your Brightcove Media Read API token below
		// Or don't, it's better to set this in your own code if you can
		this.token = "";
		
		// Callback is always ? for jQuery, it subs something here...
		this.cb = "?";
		
		// Don't change below this line
		this.api = "http://api.brightcove.com/services/library";
		this.calls = [
			{ "s":"find_all_videos", "o":false },
			{ "s":"find_playlists_for_player_id", "o":"player_id" },
			{ "s":"find_all_playlists", "o":false },
			{ "s":"find_playlist_by_id", "o":"playlist_id" },
			{ "s":"find_related_videos", "o":"video_id" },
			{ "s":"find_video_by_id", "o":"video_id" },
			{ "s":"find_videos_by_ids", "o":"video_ids" },
			{ "s":"find_videos_by_tags", "o":"or_tags" },
			{ "s":"find_video_by_reference_id", "o":"reference_id" },
			{ "s":"find_video_by_reference_ids", "o":"reference_ids" },
			{ "s":"find_videos_by_user_id", "o":"user_id" },
			{ "s":"find_videos_by_campaign_id", "o":"campaign_id" },
			{ "s":"find_videos_by_text", "o":"text" },
			{ "s":"find_playlists_by_ids", "o":"playlist_ids" },
			{ "s":"find_playlist_by_reference_id", "o":"reference_id" },
			{ "s":"find_playlists_by_reference_ids", "o":"reference_ids" }
		];

		// Construct the API call...
		// s = api call to call
		// v = various name/value pairs or simple string
		this.getURL = function (s, v) {
			v = v || null;
			var o = null;
			var q = "";
			//for whatever reason, comparison is made on substring, here and while checking table
			s = s.toLowerCase().replace(/(find_)|(_)|(get_)/g, "");
			// find our entry
			for (var z = 0 ; z < this.calls.length ; z++) {
				if (s == this.calls[z].s.toLowerCase().replace(/(find_)|(_)|(get_)/g, "")) {
					// reset s to the api call proper
					s = this.calls[z].s;
					// o = ... looks to be the query parameter relevant to this query call
					o = this.calls[z].o;
				}
			}
			// append the command to query
			q = this.api+"?command="+s
			// we break into various methods of handling these parameters
			if ((typeof v == "object") && v) {
				// we have a object of vars pairs
				// iterative for each of them and...
				for (var x in v) {
					// append the query params
					if (x == "selector") {
						// selector is special, and subs for the relevant param for the key to this query
						q += "&"+o+"="+v[x];
					} else {
						// generic case, pass the name and value through verbatim
						q += "&"+x+"="+v[x];
					}
				}
				// append default callback if none specified.
				if (typeof v.callback != "string") {
					q += "&callback="+this.cb;
				}
				// append default token if none specified.
				if (typeof v.token != "string") {
					q += "&token="+this.token;
				}
			} else if (v) {
				// value is assumed to be scalar here.
				// append the token, callback, and selector
				q += "&"+o+"="+v+"&callback="+this.cb;
				q += "&token="+this.token;
			} else {
				// no value, valid for some cases like find_all_videos
				// append the token, callback
				q += "&token="+this.token;
				q += "&callback="+this.cb;
			}			
			
			return q;
		};
		
		this.getPlayerMarkup = function (playerId, videoIds, params, additional) {

			var values = { "id":"myExperience", "bgcolor":"FFFFFF", "width":486, "height":412 };

                        if(typeof(params) != "undefined") {
			  for (var key in values) {
                            if(key in params) {
                              values[key] = params[key];
                            }
                          }
			}

                        var additionalCode = "";

                        if(additional instanceof Array) {
                          for (var key in additional) {
                            additionalCode += '<param name="' + key + '" value="' + additional[key] + '" />';
                          }
                        }

                        var videoCode = "";

                        if(typeof(videoIds) != "undefined") {
                          if(videoIds instanceof Array) {
                            videoCode = '<param name="@videoPlayer" value="';

                            for (var videoId in videoIds) {
                              videoCode += videoId + ',';
                            }

                            videoCode.replace(/,$/, '" />');
                          } else {
                            videoCode = '<param name="@videoPlayer" value="' + videoIds + '" />';
                          }
                        }

                        var code =
                          '<object id="' + values['id'] + '" class="BrightcoveExperience">' +
                          '<param name="bgcolor" value="#' + values['bgcolor'] + '" />' +
                          '<param name="width" value="' + values['width'] + '" />' +
                          '<param name="height" value="' + values['height'] + '" />' +
                          '<param name="playerID" value="' + playerId + '" />' +
                          videoCode +
                          additionalCode +
                          '<param name="isVid" value="true" />' +
                          '<param name="isUI" value="true" />' +
                          '</object>';

                        return code;
		
		}

	}();


