/*
	Developed by Vu Xuan Cuong (Sky)
	Email: skyeverything@gmail.com
*/

(function(jQuery) {
	jQuery.fn.newVideo = function(options){
		// default configuration properties
		var defaults = {	
			videoId: 0,
			autoPlay: false
		}; 
		
		var o = jQuery.extend(defaults, options); //o is options
		
		this.each(function() {
			var obj = jQuery(this),
				videoPlayer = obj.find("video").get(0),
				videoList = new Array(),
				curVideoId = o.videoId,
				getItem = obj.find(".Enable"),
				totalItem = getItem.length;
			
			// play item
			function _playThis(id) {
				obj.find(".TimePlay").text("00:00");
				obj.find(".TimeRemain").text("00:00");
				obj.find(".LoadBar").fadeIn("slow");
				var curFile = videoList[id];
				curVideoId = id;
				videoPlayer.src = curFile;
				videoPlayer.autoplay = "true";
				obj.find(".PauseButton").show();
				obj.find(".PlayButton").hide();
				videoPlayer.load();
				videoPlayer.play();
			}
			
			function _realScreen() {
				obj.addClass("VideoBox").removeClass("Full");
				obj.find(".FullScreenButton").show();
				obj.find(".ResizeButton").hide();
			}
			
			function _hideVideoControl() {
				obj.find(".VideoControl").fadeOut("slow");
			}
			
			function _start() {
				// load item into the list
				getItem.each(function(e) {
					videoList[e] = jQuery(this).attr("href");
					jQuery(this).bind("click",function(){
						_playThis(e);
						return false;
					});
				});
				
				// remove the controls attribute
				videoPlayer.removeAttribute('controls');
				
				// listen for timeupdate and update the time display in the controller
				videoPlayer.addEventListener('timeupdate',function(e) {
					// split currentTime (seconds) into separate hour/minute/second strings
					var s = e.target.currentTime,
						d = e.target.duration,
						b = e.target.buffered,
						percent = Math.floor(s/d*100),
						h=Math.floor(s/3600),
						dh=Math.floor(d/3600),
						ds=d%3600,
						m=Math.floor(s/60),
						dm=Math.floor(d/60);
					
					for (var i=0; i < b.length; i++) {
						var buffPx = Math.floor(obj.find(".ProgressBar").width()/d * b.end(i));
						obj.find(".BufferedBar").css({ "width" : buffPx + "px" });
					}
					
					s=s%3600;
					s=Math.floor(s%60);
					ds=Math.floor(ds%60);
					// pad the minute and second strings to two digits
					if (s.toString().length < 2) s="0"+s;
					if (m.toString().length < 2) m="0"+m;
					if (ds.toString().length < 2) ds="0"+ds;
					if (dm.toString().length < 2) dm="0"+dm;
					
					obj.find(".TimePlay").text(m+":"+s);
					obj.find(".TimeRemain").text(dm+":"+ds);
					
					sliderBar.slider("value", percent);
					
					if(d > 0) {
						obj.find(".LoadBar").fadeOut("slow");
					}
				}, true);
				
				videoPlayer.addEventListener('ended',function(e) {
					jQuery(".AudioControl").show();
					videoPlayer.currentTime = 0;
					sliderBar.slider("value", 0);
					obj.find(".PlayButton").show();
					obj.find(".PauseButton").hide();
					videoPlayer.pause();
				}, true);
				
				var sliderBar = obj.find(".SliderRange").slider({
					range: "min",
					value: 0,
					min: 0,
					max: 100,
					slide: function(event, ui) {
						var d2 = videoPlayer.duration/100;
						videoPlayer.currentTime = ui.value*d2;
					}
				});
				
				if(o.autoPlay == true) {
					_playThis(0);
				}
			}
			
			// init
			_start();
			
			obj.find(".NextButton").bind("click", function() {
				if((curVideoId + 1) >= totalItem) {
					curVideoId = 0;
				} else {
					curVideoId++;
				}
				_playThis(curVideoId);
			});
			
			obj.find(".PrevButton").bind("click", function() {
				if(curVideoId - 1 < 0) {
					curVideoId = totalItem - 1;
				} else {
					curVideoId--;
				}
				_playThis(curVideoId);
			});
			
			obj.find(".PlayButton").bind('click',function() {
				videoPlayer.play();
				obj.find(".PauseButton").show();
				jQuery(this).hide();
			});
			
			obj.find(".PauseButton").bind('click',function() {
				videoPlayer.pause();
				obj.find(".PlayButton").show();
				jQuery(this).hide();
			});
			
			obj.find(".FullScreenButton").bind('click',function() {		
				obj.addClass("Full").removeClass("VideoBox");
				obj.find(".ResizeButton").show();
				jQuery(this).hide();
			});
			
			obj.find(".ResizeButton").bind('click',function() {
				_realScreen();
			});
			
			if(!global.BD.iPad && !global.BD.iPhone) {
				var soundBar = obj.find(".SoundRange").slider({
					range: "min",
					value: 50,
					min: 0,
					max: 100,
					slide: function(event, ui) {
						videoPlayer.volume = ui.value/100;
					}
				});
				obj.find(".SoundMin").bind('click',function() {
					videoPlayer.volume = 0;
					soundBar.slider( "option", "value", 0 );
				});
				obj.find(".SoundMax").bind('click',function() {
					videoPlayer.volume = 1;
					soundBar.slider( "option", "value", 100 );
				});
			} else {
				jQuery(".SoundBox").hide();
			}
		});
	};
})(jQuery);
