
jQuery.noConflict();
(function($){

	$('html').addClass('js-on'); // html class

	function callOnDomReady() {
		var callbackId = 0;

		$('html').addClass('js-on'); // html class

		//initialize flowplayer
		window.contextPath = window.contextPath || '';

		$("div.video-onair").each( function () {

			var jElem = $(this),
				classes = this.className,
				linkSrc = $('a', this),
				src = linkSrc.attr('href').split('?'),
				params = strToObj(src[1]),
				width = params.width ||
					jElem.width(),
				height = params.height ||
					jElem.height(),
				objectId = src[0].substring(src[0].lastIndexOf("/") +1);

				var metafileUrl = src[0] + '/variable/config.js?' + src[1];
				$.getScript(metafileUrl, function() {

					// Find maximum width of the videos in the playlist
					var maxWidth = 0;
					if (typeof(config.playlist[0].metaData) != "undefined" && typeof(config.playlist[0].metaData.width) != "undefined") {
					   maxWidth =  config.playlist[0].metaData.width;
					}

					// Find maximum height of the videos in the playlist
					var maxHeight = 0;
					if (typeof(config.playlist[0].metaData) != "undefined" && typeof(config.playlist[0].metaData.height) != "undefined") {
					   maxHeight = config.playlist[0].metaData.height;
					}

					// Calculate optimal height according to maximum width/height of the videos in the playlist
					if (typeof(maxWidth) != "undefined" && maxWidth > 0 && typeof(maxHeight) != "undefined" && maxHeight > 0) {
					  height = 24 + Math.round((maxHeight * width) / maxWidth);
					}
					jElem.height(height);
					var reached = false;
		            linkSrc.flowplayer({
		            	src: contextPath + "/.resources/templating-kit/swf/flowplayer.swf",
						wmode:     "opaque",
						width:width,
						height:height
						},
						{
							clip:{
								autoPlay:      true,
								autoBuffering: true,
								url: config.playlist[0].url,
								onCuepoint: [[250], function() {
								        if (! reached) {
								            reached = true;
								            this.pause();
								        }
								    }
								]

							},

		        			playlist:      config.playlist,
							plugins:       {
				       			rtmp: {
				       	        	url: contextPath + "/.resources/templating-kit/swf/flowplayer.rtmp.swf"
				        		}
			        		}
						}) //ends the flowplayer command


		         });  //end the getScript command


         }); //ends the div

         function strToObj(str){
			var obj  = {};
			if(str){
				str = str.replace(/^\?/,'').replace(/&amp;/g, '&').split(/&/);
				$.each(str, function(i, param){
					queryPair = param.split(/\=/);
					obj[decodeURIComponent(queryPair[0])] = (queryPair[1]) ?
						decodeURIComponent(queryPair[1]) :
						'';
				});
			}
			return obj;
		}

}
	$(callOnDomReady);

}) (jQuery);

