
$(document).ready(function() {
	
	 if($('#moviePlayer').length > 0) initMoviePlayer();
   if($('#animCont').length > 0){
		  initAnim();
	 }else{
			$('#video_skip').hide();
			$('#video_more').show(); 
	 }
		
});

var imgLoadArr = {img1: false, img2: false, img3: false}

function initAnim(){
	
	//Load in the required images first
	
	var img1 = new Image();
	var img2 = new Image();
	var img3 = new Image();
	
  $(img1).load(function () {
		 $('#leftDoor').css({'background-image': 'url('+$(this).attr('src')+')'});
		 $('#leftMask').width(220);
     imgLoadArr['img1'] = true;
		 startAnim();
   }) .attr('src', 'images/template/header/door_left.png');
	
	 $(img2).load(function () {
		 $('#rightDoor').css({'background-image': 'url('+$(this).attr('src')+')'});
		 $('#rightMask').width(220);
     imgLoadArr['img2'] = true;
		 startAnim();
   }) .attr('src', 'images/template/header/door_right.png');
	
	 $(img3).load(function () {
		 $('#animText').css({'background-image': 'url('+$(this).attr('src')+')'});
     imgLoadArr['img3'] = true;
		 startAnim();
   }) .attr('src', 'images/template/header/text.png');
	
}

function startAnim(){
	
	var startAnim = true;
	for(x in imgLoadArr){
		if(imgLoadArr[x] == false) startAnim = false;
	}
	
	if(startAnim){
		
		//Delay it from starting for a half a second
		setTimeout(function(){
		
			var cont = $('#animCont');
			
			var leftDoor = $('#leftDoor');
			var rightDoor = $('#rightDoor');
			
			var animText = $('#animText');
			
			//Animate Text
			animText.animate({
				right: 300
			}, 1000, 'easeOutQuad', function(){
				
				$(this).animate({
					right: 350
				}, 2000, 'linear', function(){
					
						$(this).animate({
							right: 800
						}, 500, 'easeInQuad');
					
					});
			});
			
			//Animate Doors
			setTimeout(function(){
				leftDoor.animate({
					left: 50
				}, 1000, 'easeInQuad');
				
				rightDoor.animate({
					right: 50
				}, 1000, 'easeInQuad');
				
			}, 3250);
			
			//Fade it all out
			setTimeout(function(){
				cont.fadeOut(750);
			}, 4000);
			
			setTimeout(function(){
				if($('#playMovie').length > 0 && !isMobile){
					$('#jplayer').jPlayer("play");
					$('#video_more').hide();
					$('#video_skip').delay(300).fadeIn(500);
				}else{
					$('#video_skip').hide();
					$('#video_more').show();
				}
				
			}, 4000);
		
		}, 500);
		
	}
	
}

var movFiles = {'breads' : 'Breads',
								'cakes' : 'Cakes',
								'natural' : 'Ingredients',
								'viennoiseries' : 'Viennoiseries'};

function initMoviePlayer(){

	//build the movie cont videos
	var movieList = $('<ul/>', {
											'id': 'movieList'
										});
	
	var totalMovs = 0;				
	for(x in movFiles){
		
		if(x.length > 0){
			var movItem = $('<li/>').append($('<a/>').addClass('item_'+x).html('<div>'+movFiles[x]+'</div>').bind('click', {movId:x}, function(event){ setMovie(event.data.movId); $('#movieCont').hide(); $('#moviePlayer').show(); $('#video_more').val('More Videos'); $('#jplayer').jPlayer("play"); }));
			movieList.append(movItem);
			totalMovs++;
		}
		
	}
	
	$('#movieCont').append(movieList);

	//Get random video to start:
	var rKey = ranProp(movFiles);

	//Create Movie Player
	$("#jplayer").jPlayer({
			size: {width: 790, height: 420},
			ready: function () {
				setMovie(rKey);
			},
			swfPath: "js",
			supplied: "m4v, webmv"
  });

	if(!isMobile){
		
		$('#jplayer').bind($.jPlayer.event.play, function(){
			$('.jp-video-play').hide();
			$('#video_more').hide();
			$('#video_skip').show();
		});
	
	
		$('#jplayer').bind($.jPlayer.event.pause, function(){
			$('.jp-video-play').show();
		});
		
		$("#jplayer").bind($.jPlayer.event.ended, function(event) {
			
			if(!isMobile){
				$('#moviePlayer').hide();
			}
			
			$('#video_skip').hide();
			$('#video_more').show();
			
			
		});
	
	}
	
	$('#video_skip').bind('click', function(){
		$('#jplayer').jPlayer("stop");
		$(this).hide();
		$('#video_more').show();
		$('#moviePlayer').hide();
	});
	
	$('#video_more').bind('click', function(){
		toggleMovieCont();
	});
	
	if(isMobile){
		
		$('#moviePlayer').show();
		/*
		var myNav = $("#nav ul li");

		myNav.toggle(function(){
			 var videos = $("video");
			 videos.removeAttr("controls");
		},
		function(){
			 var videos = $("video");
			 videos.attr("controls","true");
		})
		*/
	}

}

function setMovie(selMov){
	
	var mobMov = (isMobile)? '_mobile' : '';
	
	$('#jplayer').jPlayer("setMedia", {
				  m4v: "/video/"+selMov+mobMov+".mp4",
					webmv: "/video/"+selMov+".webm",
					poster: "/video/"+selMov+".jpg"
				});
	
}

function toggleMovieCont(){
	
	var movieCont = $('#movieCont');
	var movieList = $('#movieList');
	
	if(movieCont.is(':hidden')){
		
		setTimeout(function(){ $('#video_more').val('Cancel'); }, 500);
		
		movieCont.fadeIn(500);
		movieList.delay(500).fadeIn(300);
		
	}else{
		
		movieList.fadeOut(300);
		movieCont.delay(300).fadeOut(500);
		
		setTimeout(function(){ $('#video_more').val('More Videos'); }, 500);
		
	}
	
}

function ranProp(obj) {
    var result;
    var count = 0;
    for (var prop in obj)
        if (Math.random() < 1/++count)
           result = prop;
    return result;
}

