/**
 * Merged HTML5 video and HTML5 viewer classes for v2
 */

(function(){ eval(Ustream.namespace);
	
	//create missing namespaces
	if ( ! Ustream.classes ) {
		Ustream.classes = {};
	}
	
	/**
	 * Global register for the HTML5 Video applications
	 * @namespace HTML5 video tags
	 */
	Ustream.html5video = {};
	
	/**
	* baseclass for HTML5 video applications
	*/
	Ustream.classes.Html5Video = new Class({
		
		/**
		 * default options
		 */
		options: {
			autoplay: false,
			poster: null,
			preload: false
		},
		
		supported: false,
		supportedAgents: [],
		supportedPages: [],
		video: null,
		agent: "",
		html: "",

		/**
		 *
		 * @class baseclass for HTML5 video applications <br />
		 * creates a flash video replacement <br />
		 *
		 * @constructs
		 * @param {String} id - target Id
		 * @param {Object} options - Parameters to set up the class instance
		 */
		initialize: function(id, options ){
			this.id = id;
			this.options = merge(this.options, options || {});
			this.checkSupport();
		},
		
		create: function() {
			$("#" + this.id).html( this.html );
			
			if ( $("#" + this.id + " video").length ) {
				this.video = $("#" + this.id + " video");
			}
			this.callback();
		},

		/**
		 * Check for the support of the HTML5 video replacement on a particular page
		 */
		checkSupport: function() {
			if ( this.checkAgent() && $.inArray( Ustream.Vars.pageId, this.supportedPages ) != -1 ) {
				this.supported = true;
			} else {
				this.supported = false;
			}
		},

		/**
		 * Check user agent support
		 */
		checkAgent: function() {
			var supAgentsLength = this.supportedAgents.length;
			for (var i = 0; i < supAgentsLength; i++) {
				if ( navigator.userAgent.toLowerCase().indexOf( this.supportedAgents[i] ) != -1 ) {
					this.agent = this.supportedAgents[i];
					return true;
				}
			}
			return false;
		},
		
		callback: function() {}
	});
	
	Ustream.classes.Html5Viewer = Ustream.classes.Html5Video.extend({
		
		supportedAgents : ["iphone", "ipad", "ipod"],
		supportedPages : ["Channel_Details", "Channel_DetailsV3", "Video_Video", "Moment_Moment"],
		
		initialize: function( id, options ) {
			this.parent( id, options );
			
			//register to global ustream.html5video namespace
			if ( ! Ustream.html5video.viewer ) {
				Ustream.html5video.viewer = {};
			}
			Ustream.html5video.viewer[id] = this;
			
			if ( Ustream.Vars.videoPictureUrl ) {
				this.options.poster = Ustream.Vars.videoPictureUrl;
			}
			
			if ( Ustream.Vars.liveThumbnailUrl ) {
				this.options.poster = Ustream.Vars.liveThumbnailUrl
			}
			
			this.buildHtml();
		},
		
		/**
		 * We can override the default support logic here.. ie. No flash etc.
		 */
		/*
		checkSupport: function() {
			this.parent();
		},*/

		buildHtml: function() {
			
			/* if ( ustream.vars.isLive == "false" && (ustream.vars.pageId == "Channel_Details" || ustream.vars.pageId == "Channel_DetailsV3") ) {
				this.html = '<div class="offair">Off Air</div>';
			} else */ if ( ! Ustream.Vars.liveHttpUrl ) {
				this.html = '<div class="justflash">Currently this video is only viewable in Flash :( - We\'are working hard to make all content viewable on both the iPad and iPhone, please check back later!</div>';
			} else {
				
				this.html += '<video controls';
				if ( this.options.width ) {
					this.html += ' width="' + this.options.width + '"';
				}
				if ( this.options.height ) {
					this.html += ' height="' + this.options.height + '"';
				}
				if ( this.options.poster ) {
					this.html += ' poster="' + this.options.poster + '"';
				}
				if ( this.options.autoplay ) {
					this.html += ' autoplay';
				}
				/* buggy
				if ( this.options.preload ) {
					this.html += ' preload="auto"';
				} else {
					this.html += ' preload="metadata"';
				}*/
				this.html += ' src="' + Ustream.Vars.liveHttpUrl + '">';
				this.html += "</video>";
				
				// ustream logo on the video tag
				if (navigator.userAgent.toLowerCase().indexOf('iphone') == -1 && navigator.userAgent.toLowerCase().indexOf('ipod') == -1) {
					// always specify the pageId..
					if ( $.inArray( Ustream.Vars.pageId, [ "Channel_Details", "Channel_DetailsV3" ]) != -1 ) {
						$("#" + this.id).css({ position: "relative" });
						this.html += '<div class="watermark_live"></div>';
					} else if ($.inArray( Ustream.Vars.pageId, [ "Moment_Moment", "Video_Video" ]) != -1) {
						$("#" + this.id).css({ position: "relative" });
						this.html += '<div class="watermark_recorded"></div>';
					}
				}
			}
		},
		
		callback: function() {
			
			// remove black frame
			$("#" + this.id).css({ overflow: "hidden", height: this.options.height });
					
			if ( Ustream.Vars.pageId == "Channel_Details" ) {
				$("#header").css({minWidth: 0});
			}
							
			// seek to a moment
			// this needs to be done in the v3 version too
			if ( Ustream.Vars.pageId == "Video_Video" && this.video && Ustream.Vars.momentSeekTo ) {
				
			}
		}
				
	});

})();
