// JavaScript Document
(function($) {
	// plugin definition
	$.fn.gVideo = function(options) {		
		// build main options before element iteration		
		// iterate and reformat each matched element
		return this.each(function() {
			var $gVideo = $(this);
			
			//create html structure
			//main wrapper
			var $video_wrap = $('<div></div>').addClass('video-player');
			//$gVideo.wrap($video_wrap); //---------- This breaks iOS! We manually wrap all the videos now
						
			//controls wraper
			//var $video_controls = $('<div class="video-controls"><a class="video-play button" title="Play/Pause">Play Video</a><div class="video-seek"></div><div class="video-timer">00:00</div><div class="volume-box"><div class="volume-slider"></div><a class="volume-button button" title="Mute/Unmute">Mute Sound</a></div></div>');
			if (options == "skip") {
				var $video_controls = $('<div class="video-controls"><a class="replay-button videobutton" style="float:left;">Restart</a><a class="video-play videobutton" title="Play/Pause">Play Video</a><span>|</span><a class="volume-button videobutton" title="Mute/Unmute">Mute</a><span>|</span><a class="skip-button videobutton" title="Skip Video">Skip</a></div>');
			} else {
				var $video_controls = $('<div class="video-controls"><a class="replay-button videobutton" style="float:left;">Restart</a><a class="video-play videobutton" title="Play/Pause">Play Video</a><span>|</span><a class="volume-button videobutton" title="Mute/Unmute">Mute</a><span>|</span><a class="enlarge-button videobutton" title="Enlarge">Enlarge</a></div>');
			}
			
			$(window).load(function() {
				var videoWidth = $gVideo.width();
				$gVideo.closest(".video-player").find(".video-controls").css({"width":videoWidth+"px","marginLeft":"auto","marginRight":"auto"});
			});
			
			if (!$gVideo.find("#flvPlayer:visible").length) { //---- only show controls for non-Flash
				$gVideo.after($video_controls);
			}
			
			//get new elements
			var $video_container = $gVideo.parent('.video-player');
			var $video_controls = $('.video-controls', $video_container);
			var $play_btn = $('.video-play', $video_container);
			var $video_seek = $('.video-seek', $video_container);
			var $video_timer = $('.video-timer', $video_container);
			var $volume = $('.volume-slider', $video_container);
			var $volume_btn = $('.volume-button', $video_container);
			var $enlarge_btn = $('.enlarge-button', $video_container);
			var $skip_btn = $('.skip-button', $video_container);
			var $replay_btn = $('.replay-button', $video_container);
			
			// $video_controls.hide(); // keep the controls hidden //---- no need to hide them
			
			var gPlay = function() {
				if($gVideo.prop('paused') == false) {
					$gVideo[0].pause();	
				} else if($gVideo.prop('ended') == true) {
					$gVideo.prop("currentTime",0);
					$gVideo[0].play();
				} else {					
					$gVideo[0].play();				
				}
			};			
			
			$play_btn.click(gPlay);
			$gVideo.click(gPlay);
			
			$gVideo.bind('play', function() {
				$play_btn.addClass('paused-button').text("Pause Video"); //---- Play
			});
			
			$gVideo.bind('pause', function() {
				$play_btn.removeClass('paused-button').text("Play Video"); //---- Pause
			});
			
			$gVideo.bind('ended', function() {
				//$play_btn.removeClass('paused-button');
				$play_btn.addClass('paused-button').text("Play Again"); //---- End
			});
			
			//------ enlarge
			var gEnlarge = function() {
				if($gVideo.prop('paused') == false) { $gVideo[0].pause(); } //---- pause video if it's playing
				
				var thisVideoHTML = $gVideo.closest(".video-player").find("video").outerHTML(); //---- get video player code
				$(".popup").html("").popUp("<div class='video-player'>"+thisVideoHTML+"</div>",true,function() {
					$(this).css({"width":"1000px","marginLeft":"-527px"}).find("video").css("width","100%").gVideo(); //---- adjust sizing
					$(this).find(".enlarge-button").hide().prev("span").hide(); //---- hide enlarge button
					//---- vertically center
					var enlargeHeight = $(this).height();
					var browserHeight = $(window).height();
					if (browserHeight > enlargeHeight) {
						var marginTop = (browserHeight-enlargeHeight)*.5;
						$(".popup").css("marginTop",marginTop+"px");
					}
				});
				
				/***** - THIS WORKS, BUT IT COMPLICATES THE FLASH
				var videoID = 7; //$(this).closest(".module").attr("id");
				$.ajax({
					url:"videoPopup.php",
					data:{videoID:videoID},
					success:function(data) {
						$(".popup").popUp(data,false,function() {
							$(this).css({"width":"1000px","marginLeft":"-520px"}).find("video").css("width","100%").gVideo(); //---- adjust sizing
							$(this).find(".enlarge-button").hide().prev("span").hide(); //---- hide enlarge button
						});
					}
				});
				*/
			};
			$enlarge_btn.click(gEnlarge);
			
			//---- restart
			var gReplay = function() {
				$gVideo.prop("currentTime",0);
				$gVideo[0].play();
			};			
			$replay_btn.click(gReplay);
			
			//------ skip
			var gSkip = function() {
				if($gVideo.prop('paused') == false) { $gVideo[0].pause(); } //---- pause video if it's playing
				$(".video-player").fadeOut(function() {
					
					$(".content").animate({"backgroundColor":"#fff"},{duration:300}); // content fadein
					$('.homepageBanner').fadeIn(400, function() {
								$("div#bannerNavigation div:first").css("backgroundColor","#efefef");
								$(".modules").fadeIn(function() {
											$(".contentSpacer").css("height","0"); //---- adjust footer position
											footerToBottom();
										});
								bannerRotateInt = setInterval(function() {
									bannerRotate();
								},6000);
					  });//homepagebanner fadein
					  
				});
				
			};
			$skip_btn.click(gSkip);
			
			/* 
			var seeksliding;			
			var createSeek = function() {
				if($gVideo.prop('readyState')) {
					var video_duration = $gVideo.prop('duration');
					$video_seek.slider({
						value: 0,
						step: 0.01,
						orientation: "horizontal",
						range: "min",
						max: video_duration,
						animate: true,					
						slide: function(){							
							seeksliding = true;
						},
						stop:function(e,ui){
							seeksliding = false;						
							$gVideo.prop("currentTime",ui.value);
						}
					});
					$video_controls.show();					
				} else {
					setTimeout(createSeek, 150);
				}
			};

			createSeek();
		
			var gTimeFormat=function(seconds){
				var m=Math.floor(seconds/60)<10?"0"+Math.floor(seconds/60):Math.floor(seconds/60);
				var s=Math.floor(seconds-(m*60))<10?"0"+Math.floor(seconds-(m*60)):Math.floor(seconds-(m*60));
				return m+":"+s;
			};
			
			var seekUpdate = function() {
				var currenttime = $gVideo.prop('currentTime');
				if(!seeksliding) $video_seek.slider('value', currenttime);
				$video_timer.text(gTimeFormat(currenttime));							
			};
			
			$gVideo.bind('timeupdate', seekUpdate);
			
			var video_volume = 1;
			$volume.slider({
				value: 1,
				orientation: "vertical",
				range: "min",
				max: 1,
				step: 0.05,
				animate: true,
				slide:function(e,ui){
						$gVideo.prop('muted',false);
						video_volume = ui.value;
						$gVideo.prop('volume',ui.value);
					}
			}); */
			
			var muteVolume = function() {
				if($gVideo.prop('muted')==true) {
					$gVideo.prop('muted', false);
					//$volume.slider('value', video_volume);
					
					$volume_btn.removeClass('volume-mute').text("Mute");					
				} else {
					$gVideo.prop('muted', true);
					//$volume.slider('value', '0');
					
					$volume_btn.addClass('volume-mute').text("Unmute");
				};
			};
			
			$volume_btn.click(muteVolume);
			
			//$gVideo.removeprop('controls');
			
		});
	};

	//
	// plugin defaults
	//
	$.fn.gVideo.defaults = {		
	};

	//-----/ function to get outer HTML for use in the 'enlarge' event above /--------//
	$.fn.outerHTML = function(s) {
		return s
			? this.before(s).remove()
			: jQuery("<p>").append(this.eq(0).clone()).html();
	};

})(jQuery);

/* function videoenlarge() {
	var $gVideo = $(this);
	if($gVideo.prop('paused') == false) { $gVideo[0].pause(); } //---- pause video if it's playing
				
				var thisVideoHTML = $(this).closest(".video-player").find("video").outerHTML(); //---- get video player code
				$(".popup").html("").popUp(thisVideoHTML,true,function() {
					$(this).css({"width":"1000px","marginLeft":"-520px"}).find("video").css("width","100%").gVideo(); //---- adjust sizing
					$(this).find(".enlarge-button").hide().prev("span").hide(); //---- hide enlarge button
				});
} */


