jQuery(function(){
	$("#contact_box").css('opacity','0');
	$("#contact").toggle(function(){
		$("#contact_box").animate({
							opacity: 1,
							left: '27',
							height: 'toggle'
							}, 1000, function() {
							// Animation complete.
						  });
	}, close_contact);
	$("#contact_close").click(function(){
		$("#contact").trigger('click');
	});	

	
	$("#contact_form").submit(function() {
		$(this).find("input[type=submit]").hide();
		$(this).find("#ajax_loader").css('display','block');
		$.post("/index/mail", $(this).serialize(),function(data){
			$("#contact_form input[type=submit]").show();
			$("#ajax_loader").hide();
				if(data == "false"){
					alert("אירעה שגיאה בשליחת ההודעה. אנא נסו שנית מאוחר יותר.");
				}
				else
				{
					$("#contact_form").animate({
						opacity: 0
						}, 1000, function() {
						// Animation complete.
					  });
					$('#contact_main').html('<img src="/style/contact-box/bird.png"/>');
					$("#contact_main img").css('opacity','0');
					$('#contact_main img').animate({
						opacity: 1
						}, 1000, function() {
						// Animation complete.
					  });
			
				}
		});
		
		return false;
	});
	//var cloudMove = setTimeout(function(){moveCloud(1)},100);
	
	var picAnimation = setTimeout(function(){animatePic()},3000);
});

var movePic = Math.ceil( Math.random() * 3 );

function moveCloud(num){
	$('body').css('background-position',num + "px 50%");
	num++;
	setTimeout(function(){moveCloud(num)},100);
}

function animatePic(){
	
	if(movePic == 1)
	{
		
		$("#fish").css( {backgroundPosition: "100px 0"} ).stop().animate(
				{backgroundPosition:"(0 0)"}, 
				1200,'linear',function(){
					$(this).animate(
							{right:($(window).width()-100)}, 
							20000,'linear',function(){
								$(this).animate({backgroundPosition: "-100px 0"},1200,'linear',function(){
									$(this).css( {right: "0"} );
								});
					});
				});

	}
	else if(movePic == 2)
	{
		$("#tzniha").css( {backgroundPosition: "0 -100px"} ).stop().animate(
				{backgroundPosition:"(0 0)"}, 
				1200,'linear',function(){
					$(this).animate(
							{top:($(window).height()-100)}, 
							20000,'linear',function(){
								$(this).animate({backgroundPosition: "0 100px",opacity:0},1200,'linear',function(){
									$(this).css( {top: '0'} );
								});
					});
				});
	}
	else if(movePic == 3)
	{
		$("#flyingBird").css( {backgroundPosition: "-100px 0"} ).stop().animate(
				{backgroundPosition:"(0 0)"}, 
				1200,'linear',function(){
					$(this).animate(
							{left:($(window).width()-100)}, 
							20000,'linear',function(){
								$(this).animate({backgroundPosition: "100px 0"},1200,'linear',function(){
									$(this).css( {left: "0"} );
								});
					});
				});
	}
	//$("body").append('');
	
	
	if(movePic==3)
		movePic = 1;
	else
	{
		movePic++;
	}
	setTimeout(function(){animatePic();},40000);
}

function close_contact(){
	$("#contact_box").animate({
							opacity: 0,
							left: '27',
							height: 'toggle'
						    }, 1000, function() {
							// Animation complete.
						  });
}



