$(document).ready(function() {

	/* mobile detection */
	
	// based on screen size
	function is_mobile() {
		if(screen.width <= 1007) {
			return true;		
		} else {
			return false;
		}
	}
	
	

	/* player */

	var videos_dir = "http://michelintruck.com/assets/mrt_videos";	
//	var videos_dir = "http://www.theletteryellow.com/mrtvt/videos";
	var swf_path = "http://michelintruck.com/assets";
		
	// instance
	$("#mrtvt_jplayer").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				m4v: videos_dir + "/01_grazing.mp4",
				ogv: videos_dir + "/01_grazing.ogm",
				webmv: videos_dir + "/01_grazing.webm",
				flv: videos_dir + "/01_grazing.flv"				
//				poster: "http://www.theletteryellow.com/mrtvt/videos/01_grazing.png"
			});
			// do this to preload the swf player
			$("#mrtvt_jplayer").jPlayer("mute"); 			
			$("#mrtvt_jplayer").jPlayer("play");			
		},
		play: function() { // To avoid both jPlayers playing together.
			$(this).jPlayer("pauseOthers");
//			if(event.jPlayer.version.flash!=="n/a") { 
//				$("downloadDisclaimer").css("display","block");
//			}
		},
		swfPath: swf_path,
		supplied: "flv, m4v, webmv, ogv",
		solution: "flash, html",		
		size: {width: 420, height: 315},
		errorAlerts: false,
		warningAlerts: false
	});	
	
	// spot - click - play
	$(".spot").click(function() {
		
		// id	
		var id = $(this).attr("id").replace("spot","");

		// play
		goToAndPlay(id);
	
	});

	// link - click - play
	$(".link").live("click",function() {
		
		// id	
		var id = $(this).attr("id").replace("link","");

		// play
		goToAndPlay(id);
	
	});	

	// watch - click - play
/*	$("#tooltip .watch").live("click",function() {
		
			alert("");
	
		// id	
//		var id = $(this).parent().attr("id").replace("tt","");

		
		// play
		//goToAndPlay(id);
	
	});*/
			
	// launch video/slide	
	function goToAndPlay(id) {

		// do this to stop (if needed) and unmute the player from preload
		$("#mrtvt_jplayer").jPlayer("stop");			
		$("#mrtvt_jplayer").jPlayer("unmute");							

		// set
		var file_name = $("#video"+id+" .file_name").html();
		var title = $("#video"+id+" .title").html();		
		var content = $("#video"+id+" .content").html();				

		// show player
		$("#mrtvt #player").css("left","0px");
		
		// content or video?
		if(content=="") { // video:

			// hide content / show player
			$("#mrtvt #playerContent").css("display","none");			
			$("#mrtvt #playerVideo").css("width","420px");
									
			// change and play
			$("#mrtvt_jplayer").jPlayer("setMedia", {
				m4v: videos_dir + "/"+file_name+".mp4",
				ogv: videos_dir + "/"+file_name+".ogm",
				webmv: videos_dir + "/"+file_name+".webm",
				flv: videos_dir + "/"+file_name+".flv"				
	//				poster: "http://www.theletteryellow.com/mrtvt/videos/01_grazing.png"
			}).jPlayer("play");
		
		} else { // content:
		
			// show content / stop/hide player
			$("#mrtvt #playerContent").css("display","block");			
			$("#mrtvt #playerVideo").css("width","0px");
			$("#mrtvt_jplayer").jPlayer("stop");
		
		}
				
		// set title/content
		$("#mrtvt #playerTitle").html(title);	
		$("#mrtvt #playerContent").html(content);			
	
	}
	
	// close button
	$("#playerClose").click(function() {
	
		// stop player
		$("#mrtvt_jplayer").jPlayer("stop");
		
		// hide player - DO NOT USE display: none HTML player will fail
		$("#mrtvt #player").css("left","-1000px");
	
	});



	/* expand/shrink */

	// expand/shrink
	$(".spot").mouseover(function() {
		
		// set 
		var id = $(this).attr("id").replace("spot","");
		var as = 150; // animation speed
	
		// not mobile?
//		if(!is_mobile()) { 
			// change image to play button
			$("#label"+id).attr("src","http://michelintruck.com/img/mrtvt/play.png");	
//		}
				
		// animate spot
		$("#item"+id+",#label"+id).stop(true,true).animate({
				"top": "-=4px",
				"left": "-=4px",
				"height": '+=8px',
				"width": '+=8px'			
		}, as);
						
	});
	$(".spot").mouseout(function() {
				
		// set 
		var id = $(this).attr("id").replace("spot","");
		var as = 250; // animation speed

		// not mobile?
//		if(!is_mobile()) { 
			// change image back
			$("#label"+id).attr("src","http://michelintruck.com/img/mrtvt/label"+id+".png");	
//		}
		
		// animate spot
		$("#item"+id+",#label"+id).stop(true,true).animate({
				"top": "+=4px",
				"left": "+=4px",
				"height": '-=8px',
				"width": '-=8px'			
		}, as);
			
	});



	/* tooltip */
	
	if(!is_mobile()) { //non-mobile only
	
		// tooltip
		$(".ttRight").tooltip({ 
			delay: 0, 
			showURL: false, 
			track: true,
			fixPNG: true,
			extraClass: "ttRight",
			top: -30,
			left: 30,
			bodyHandler: function() { 
				var id = $(this).attr("id").replace("spot","");		
				var html = $("#tt"+id).html(); 
				return html; 		
			} 
		});
		$(".ttLeft").tooltip({ 
			delay: 0, 
			showURL: false, 
			track: true,
			fixPNG: true,
			top: -30,
			left: -340,		
			extraClass: "ttLeft",		
			bodyHandler: function() { 
				var id = $(this).attr("id").replace("spot","");		
				var html = $("#tt"+id).html(); 
				return html; 
			} 
		});	
		$(".ttBottom").tooltip({ 
			delay: 0, 
			showURL: false, 
			track: true,
			fixPNG: true,
			top: 25,
			left: -140,		
			extraClass: "ttBottom",		
			bodyHandler: function() { 
				var id = $(this).attr("id").replace("spot","");		
				var html = $("#tt"+id).html(); 
				return html; 
			} 
		});	
	
	}
	
	
	/* welcome */
	
	// click
	$("#welcome").click(function() {
	
		$("#welcome").css("display","none");
	
	});
				
});
