var sx, mostrino, body;

$(document).ready(function(){
	mostrino = $("#mostrino");
	sx = mostrino.css("left");
	body = $("body");
	showBallon();
	showImpiccata();
	setInterval("showOcchi()",40000);
	//setInterval("cadutaMostrino()",10000);
	//cadutaMostrino();
	loadContent();
	showBio();
	contactFooter();
	//slide();
	gallery();
	intro();
});

function intro(){
	$("#intro_logo").click(function(){
		//$(this).fadeOut(900);
		$(this).animate(
			{
				opacity:0
			},
			{
				duration:900,
				complete: function(){
					//$(this).css({"display":"none"});
					$(this).remove();
					$("header, nav, section, footer").fadeIn(500);
					slide();
				}
			}
		);		
	});	
}

function showBallon(){
	$("footer li:first-child + li div").hover(function(){
		$("#ballon").fadeIn(300);
	},function(){
		$("#ballon").fadeOut(200);
	});
}

function showMostrino(){
	var img = mostrino.find("img");	
	mostrino.animate(
		{
			left:154,
			opacity:1
		},
		{
			duration:400,
			complete: function(){
				img.attr({"src":"img/mostrino2.jpg"}).animate(
					{
						opacity:1
					},
					{
						duration:500,
						complete: function(){
							img.attr({"src":"img/mostrino1.jpg"}).animate(
								{
									opacity:1
								},
								{
									duration:1000,
									complete: function(){
										img.attr({"src":"img/mostrino2.jpg"}).animate(
											{
												opacity:1
											},
											{
												duration:1000,
												complete: function(){
													mostrino.animate(
														{
															left:sx
														},
														400
													);
												}
											}
										);
									}
								}
							);
						}
					}
				);
			}
		}		
	);
}

function showOcchi(){
	$("#occhi").fadeIn(4000,function(){
		$(this).animate(
			{
				opacity:1
			},
			1000,
			function(){
				$(this).fadeOut(3000,function(){
					setTimeout("showMostrino()",20000);
				});			
			}
		);
	});
}

function showImpiccata(){ 
	var _this = $(this),
		corvo = $("#corvo"), 
		impiccata = $("#impiccata"),
		bimba = $("#bambina"),
		menu = $("nav ul");
	corvo.hover(function(){
		menu.stop(true,true).fadeOut(1000);
		bimba.stop(true,true).fadeOut(1000);
		impiccata.stop(true,true).fadeIn(1000);
		mostrino.animate({
			left:sx
		},300);
	},function(){
		menu.stop(true,true).fadeIn(1000);
		bimba.stop(true,true).fadeIn(1000);
		impiccata.stop(true,true).fadeOut(1000);
	});
}

function loadContent(){
	$("nav a").click(function(){
		if($(this).hasClass("on")) return;
		var id = $(this).attr("id");
		$("nav a").removeClass("on");
		$(this).addClass("on");
		$("section").load('other/'+id+'.html',function(){
			checkForm();
			slide();
			layerVideo();
			reel();
			gallery();
		});
		return false;
	});
}

function contactFooter(){
	$("footer .contacts").click(function(){
		var contacts = $("nav #contacts");
		if(contacts.hasClass("on")) return;
		var cl = $(this).attr("class");
		$("nav a").removeClass("on");
		contacts.addClass("on");
		$("section").load('other/'+cl+'.html',function(){
			checkForm();
		});
		return false;
	});
}

function checkForm(){
		$("#send").click(function(){
		var validEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;		
		var formActive=$('#contactform');
		var formOk=true;		
		var name=$("#name").val();
		var surname=$("#surname").val();
		var email=$("#email").val();
		if(name=='' || surname=='' || email==''){
			body.append('<div id="layer"><a href="#" id="close" title="close">&nbsp;</a><p>Please fill in all mandatory fields</p></div>');
			close();
			if(name==''){$("#name").addClass('error');}
			if(surname==''){$("#surname").addClass('error');}
			if(email==''){$("#email").addClass('error');}	
			formOk=false;
		}else if(!validEmail.exec(email)){
			body.append('<div id="layer"><a href="#" id="close" title="close">&nbsp;</a><p>Please insert a valid e-mail address</p></div>');
			close();
			$("#email").addClass('error');			
			formOk=false;
		} 
		if($('.error').is('div')){
			$('.error').click(function() {
				$(this).removeClass('error');
			});
		}else{
			$('.error').keyup(function() {
				$(this).removeClass('error');
			});
		}
		 	
		if(formOk){					
			formActive.serialize();
			var dati = formActive.serialize();
  			$.ajax({
				type: "POST",
				url: "actions/form_mail.php",
				data:dati,
				success:function(msg){
 					if(msg=='ok'){
						$('#contactform').html('<div class="response"><p>Your request was sent successfully. Thank you</p></div>');
					}
					if(msg=='error'){
						$('#contactform').html('<div class="response"><p>An error has occurred. Try again later.</p><p><a href="index.html">Back to the homepage</a></p></div>');
					}
				}
			});
		}	
		return false;
	});
}


function showBio(){
	$("#bambina").click(function(){
		body.append('<div id="layer"></div>');
		$("#layer").load("other/bio.html",function(){close();});
	});
}

function close(){
	$("#close").click(function(){
		$("#layer").remove();
		return false;
	});
}

function layerVideo(){
	$(".video").click(function(){
		var _this = $(this);
		var srcImg = _this.find("img").attr("src");
		var title = _this.attr("title");
		var name = _this.find("figcaption span").text();
		var srcVideo = _this.attr("href").split(".")[0];
		//body.append('<div id="layer"><div class="video_wrapper"><a href="#" id="close" title="close">&nbsp;</a><h2>'+title+'</h2><p>'+name+'</p><video width="512" height="288" poster="'+srcImg+'" controls autobuffer><source src="'+srcVideo+'.mp4" type="video/mp4" /><source src="'+srcVideo+'.ogv" type="video/ogg" /><object width="512" height="312" type="application/x-shockwave-flash" data="player/player.swf"><param name="movie" value="player/player.swf" /><param name="flashvars" file="'+srcVideo+'.mp4" image="'+srcImg+'"/></object></video></div></div>');
		body.append('<div id="layer"><div class="video_wrapper"><a href="#" id="close" title="close">&nbsp;</a><h2>'+title+'</h2><p>'+name+'</p><video width="512" height="288" controls autobuffer><source src="'+srcVideo+'.mp4" type="video/mp4" /><source src="'+srcVideo+'.ogv" type="video/ogg" /><object width="512" height="312" type="application/x-shockwave-flash" data="player/player.swf?image=../'+srcImg+'&file='+srcVideo+'.mp4"><param name="movie" value="player/player.swf" /><param name="flashvars" file="'+srcVideo+'.mp4" /></object></video></div></div>');
		close();
		return false;
	});
}

function reel(){
	var video = '<iframe src="http://player.vimeo.com/video/35971888?title=0&amp;byline=0&amp;portrait=0" width="400" height="319" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
	$(".video_reel").click(function(){
		body.append('<div id="layer"><div class="video_wrapper"><a href="#" id="close" title="close">&nbsp;</a><h2>My showreel</h2><div class="reel">'+video+'</div></div></div>');
		close();
		return false;
	});
}

/* function layerVideo(){
	var layer = $("#layer");
	$(".video").click(function(){
		var _this = $(this);
		var title = _this.attr("title");
		var name = _this.find("figcaption span").text();
		var srcVideo = _this.attr("href").split(".")[0];
		layer.fadeIn(300,function(){			
			$(".video_wrapper h2").text(title);
			$(".video_wrapper p").text(name);
			$("video source").eq(0).attr({"src":srcVideo+".mp4"});
			$("video source").eq(1).attr({"src":srcVideo+".ogv"});
			console.log(srcVideo+".mp4");
			$("object").attr({"data":"player/player.swf?image=../poster.jpg&file=../"+srcVideo+".mp4"});
			$("object param[name=flashvars]").attr({"file":"../"+srcVideo+".mp4"});
			$("#close").click(function(){
				layer.fadeOut();
				return false;
			});
		});
		return false;
	});
} */

function slide(){
	$(".carousel").jCarouselLite({
		btnNext: '#btn_dx',
		btnPrev: '#btn_sx',
		visible: 1,
		speed: 600,
		circular: false
	});
}

function gallery(){
	$(".illustration, .storyboard, .puppet, .animation").fancybox({
		autoScale: false,
		autoDimensions: false,
		width: 855,
		height: 650
	});
}
