









(function( $ ){
    var methods = {
        init : function(options) {
            // Default settings
			var settings = {
				"videoComponent"    : "Video Player Component", // Name of KA video player component (required)
				"videoUrl"          : false, // URL of video XML (required)
				"playlists"         : false, // Array of playlists (optional)
				"prerollUrl"        : false, // URL of RealMedia ad (optional)
				"omniture"          : false, // Name for Omniture tracking, friendly name (optional)
				"custom"            : false, // Optional objects for other components
				"debug"             : false // Turn on debug
			}
            
            // Override/combine variables with any that have been passed
			if (options) { $.extend(settings, options); }
	               
			// Debug console logging, local to plugin only
			debug = function(str) {
				if (settings.debug) {
					var s = new String(str); s = s+"<br />"; $("#video-plugin-debug").append(s);
				} 
			}
			
			isExternalUrl = function(str) {
				var isExternal    = str.indexOf("http");
				
				if (isExternal < 0) {
	            			isExternal = str.indexOf("rtmp");
	            		}
			
				debug("isExternalUrl: "+isExternal);
			
				return(isExternal);
			}	
		
			
            
            // Handling of targeted element
            return this.each(function() { 
            	var $this = $(this); // $this contains the element you called the plugin on and has all JQ functions

            	

				// Add debug DIV if turned on
				if (settings.debug) {
					$this.append('<pre id="video-plugin-debug" style="position:absolute;top:0;left:0;background-color:#ffc;z-index:99;"></pre>');
					$("#video-plugin-debug").click(function() { $("#video-plugin-debug").html(""); });
					debug("---- DEBUG CONSOLE IS ON");
				}
				
				// Get OBJECT element and get ID for later use
				var widgetId = $this.find("object").attr("id");
				debug("Widget ID: "+widgetId);
				
				// Check browser to handle doc object accordingly.
				var s_browserAgent = navigator.userAgent;
				if ((s_browserAgent.indexOf("Chrome") > 0) || (s_browserAgent.indexOf("Safari") > 0) || (s_browserAgent.indexOf("MSIE") > 0)) {
					o_player  = window[widgetId];
				} else {
					o_player  = document[widgetId];
				}				
						
				// Set video XML on player and playlist, overwrites any baked into object
				
				if (settings.videoUrl) {
					if (isExternalUrl(settings.videoUrl) < 0) {
						settings.videoUrl = "http://www.10tv.com"+settings.videoUrl;
					}
					
					o_player.setComponentProperty(settings.videoComponent, "mediaURL", settings.videoUrl);

					debug("videoComponent: "+settings.videoComponent);
					debug("videoURL: "+settings.videoUrl);					
					
					// Only process playlists if exists, overwrites any baked into object
					if (settings.playlists) {
						debug("Number of playlists: "+settings.playlists.length);
						for (i=0; i < settings.playlists.length; i++) {
							var s_playlistComponent =  settings.playlists[i]["componentName"];
							var s_playlistFeedUrl   =  settings.playlists[i]["feedUrl"];
							
							if (isExternalUrl(s_playlistFeedUrl) < 0) {
					        		s_playlistFeedUrl = "http://www.10tv.com"+s_playlistFeedUrl;
							}
					            	
							debug("playlistComponent "+i+": "+s_playlistComponent);
					        	debug("playlistUrl "+i+": "+s_playlistFeedUrl);					            	
					            	
					        	o_player.setComponentProperty(s_playlistComponent, "mediaURL", s_playlistFeedUrl);
						
						} // END playlist loop
					}
				}
				
				// Omniture settings, overwrites in-widget settings
				if (settings.omniture) {
					debug ("Omniture tracking is ON");
				
					for (var propertyName in settings.omniture) {
						if (settings.omniture.hasOwnProperty(propertyName)) {
							var omnitureProperty      = propertyName;
							var omniturePropertyValue = settings.omniture[propertyName];
							
							o_player.setComponentProperty("AppContainer", omnitureProperty, omniturePropertyValue);
							
							debug ("Setting: "+omnitureProperty+" = "+omniturePropertyValue);
						}
					}					
				} else {
					debug ("Omniture tracking is OFF");
				}			
					
				
				// Set preroll URL if exists, overwrites any baked into object
				if (settings.prerollUrl) {
					debug("prerollUrl: "+settings.prerollUrl);
					
					o_player.setComponentProperty(settings.videoComponent, "twentyFourSevenAdTag", settings.prerollUrl);
				}
				
				// Set custom component values if specified
				if (settings.custom) {
					debug ("Custom components found");
					
					for (i=0; i < settings.custom.length; i++) {
						var o_custom = settings.custom[i];
						
						if (o_custom["componentName"] && o_custom["propertyName"] && o_custom["value"]) {
							o_player.setComponentProperty(o_custom["componentName"], o_custom["propertyName"], o_custom["value"]);
							
							debug (i+": componentName: "+o_custom["componentName"]);
							debug (i+": propertyName: "+o_custom["propertyName"]);
							debug (i+": value: "+o_custom["value"]);							
						}
					}
				}				

            }); // End each()
            
        }   // END init()

    }; // END methods

    // Plugin namespace and attachment
    $.fn.kickappsVideo = function( method ) {
            if ( methods[method] ) {
                    return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
            } else if ( typeof method === 'object' || ! method ) {
                    return methods.init.apply( this, arguments );
            } else {
                    $.error( 'Method ' +  method + ' does not exist on jQuery.kickappsVideo ' );
            }    

    };
})(jQuery);
