(function($){
	$(function(){
		jQuery(".youtubeMediaList").append('<span class="playMask"></span>');
		jQuery("span.playMask").css({
			top      : 0,
			left     : 0,
			width    : 100 + "%",
			height   : 100 + "%",
			position : "absolute",
			display  : "block",
			zIndex   : 3,
			cursor   : "pointer"
		});

		jQuery("span.playMask").hover(function(){
			jQuery(this).removeClass("playMask");
			jQuery(this).addClass("playMaskHover");
		}, function(){
			jQuery(this).addClass("playMask");
			jQuery(this).removeClass("playMaskHover");
		});

		jQuery(".youtubeMedia").each(function(index , value){
			var obj = jQuery(this).tubeplayer({
				width: 100 + "%", // the width of the player
				height: 100 + "%", // the height of the player
				allowFullScreen: "true", // true by default, allow user to go full screen
				initialVideo: VideoIds[index], // the video that is loaded into the player
				preferredQuality: "default",// preferred quality: default, small, medium, large, hd720
				onPlay: function(id){}, // after the play method is called
				onPause: function(){}, // after the pause method is called
				onStop: function(){}, // after the player is stopped
				onSeek: function(time){}, // after the video has been seeked to a defined point
				onMute: function(){}, // after the player is muted
				onUnMute: function(){}, // after the player is unmuted
				autoPlay:false,
				// iframed:false,
				onPlayerEnded:function(){
					jQuery(".youtubeMedia").eq(index).hide();
					jQuery("span.playMask").eq(index).show();
				},						
			});

			jQuery(this).parents(".youtubeMediaList").find("span.playMask").click(function(){
				jQuery(".youtubeMedia").eq(index).show();
				jQuery(".youtubeMedia").eq(index).tubeplayer('play');
				// jQuery(this).hide();
				jQuery("span.playMask").show();
				//jQuery(".youtubeMedia").tubeplayer('pause');

				for (var i = 0; i < VideoIds.length; i++) {
					
					if (i != index) {
						jQuery(".ContInfoData").hide();
						jQuery(".mediaVideo").tubeplayer('pause');
						jQuery(".youtubeMedia").eq(i).tubeplayer('pause');
						jQuery(".youtubeMedia").eq(i).hide();
						// jQuery(".youtubeMedia").eq(i).tubeplayer(VideoIds[i]);
					} else {
						
						// jQuery(".youtubeMedia").eq(i).show();
						// jQuery("span.playMask").eq(index).hide();
						// console.log(index);
						// console.log(i);
					}
				}	
				// console.log(index);


				jQuery(this).hide();

			});

			jQuery("span.mediaHref , ul.OBUMedia li.media , .subNav .subNavUL li , .ContentFlow .flow div.item , .Exterior .photoList , .mediaList .mediaNav .mediaNavList , .Characteristic .ContInfoDataNavList").click(function(){
				jQuery(".youtubeMedia").each(function(index , value){
					jQuery(".youtubeMedia").eq(index).tubeplayer('pause');
					// console.log(index);
				});
				jQuery(".youtubeMedia").hide();
				jQuery("span.playMask").show();
			})
		});


		//	initial stop all youtube
		// jQuery(".youtubeMedia").each(function(index , value){
		// 	console.log(index);
		// 	jQuery(".youtubeMedia").eq(index).tubeplayer('pause');
		// });

		//Scroll
		function ScrollMovie() {
			var args = location.hash;
			if (jQuery(args) != undefined) {
				jQuery(args).click();
			}
		}

		var MrTime = window.setInterval(function(){
			ScrollMovie();;
			window.clearInterval(MrTime);
		}, 300);
	})
})(jQuery)