$(document).ready(function(){
	
	////// SMOKE ANIMATION //////////////////////////////////////////

	$('body').append('<div id="smoke1"></div><div id="smoke2"></div>');
	
	setInterval("bloooming_smoke('smoke1','smoke2')", 150);
	
		
	///// SHOW LOADING IMAGE ON AJAX ACTIVITY ////////////////
	$('.ajax')
	    .hide()  // hide it initially
	    .ajaxStart(function() {
	        $(this).show();
	    })
	    .ajaxStop(function() {
	        $(this).delay(500).fadeOut();
	    });
    

	///// SLIDER 1,2,3 ////////////////////////////////////////

	$('#slider1').anythingSlider({
		easing: 'swing',
		autoPlay: true,
		delay: 10000,
        animationTime: 500 	
	});
	
	$('#slider2').nivoSlider({
		directionNav:true, 
		directionNavHide:false 
	});

	$('#slider3').anythingSlider({
		easing: 'linear',
		autoPlay: true,
		delay: 6000,
        animationTime: 1000 	
	});
	
	
	/// wrap the #thumbNav div to create a flexible slider
	
	$('#thumbNav').wrap('<div id="thumbOuter" />');
	$('.nivo-controlNav').wrap('<div id="thumbOuter2" />');
	$('#thumbNav').show();
	
	$('#thumbOuter2').appendTo('#slider2wrapper');
	$('.nivo-directionNav').appendTo('#slider2wrapper');
	
	
	/// CURVY CORNERS ON IMAGES FOR FIREFOX //////////////////////////

 	   	$('#tabs img').bloooming_roundPic();
 	   	$('.content_left img').bloooming_roundPic();
 	   	$('.content_right img').bloooming_roundPic();



	/// TABS ////////////////////////////////////////////
	
	$(".tabcontent").hide(); 
	$("ul.tabs li:first").addClass("active").show(); 
	$(".tabcontent:first").show(); 


		//On Click Event
		$("ul.tabs li").click(function() {
	
			$("ul.tabs li").removeClass("active"); 
			$(this).addClass("active"); 
			$(".tabcontent").hide(); 
			var activeTab = $(this).find("a").attr("href"); 
			$(activeTab).show(); 
			return false;
		});

	
	$('.logo_small').click(function(){
   		 $('html, body').animate({scrollTop:0}, 'slow');
   		 return false;
	});
		
	
	/// CUFON ////////////////////////////////////////////
 
    Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('h3');
	Cufon.replace('.price');
	

	/// COLLAPSIBLE MENU //////////////////////////////////
	$(".productsmenu > li > a").click(function(){
	
		if($(this).children('ul').is(':visible') == false) {
			$(this).next().slideToggle(300);
		}
		else {
		
		}
	});
	
	$('.productsmenu > li ul:first').show();	

	$('.content_right .littlegal li').bloooming_alternate(2);
	
	/// SHOP FUNCTIONS //////////////////////////////////

	$('.products').bloooming_shop();	


	/// GALLERIES FUNCTIONS //////////////////////////////////

	$('.zoom').colorbox({
		opacity:0.6
	});	
	
	$(".wizard").colorbox({
		opacity:0.9, iframe:false, innerWidth:1100, innerHeight:750
	});
	

	$('.videozoom').colorbox({
		iframe:true, innerWidth:425, innerHeight:344
	});	
	
	$('.zoom').bloooming_picHover();
	$('.videozoom').bloooming_picHover();
	
	
	/// CONTACT FORM //////////////////////////////////

	$('.submit').click(function(){

		var name = $('#name').val();
		var email = $('#email').val();
		var phone = $('#phonenumber').val();
		var message = $('#message').val();
		var valid = true;
		
		if (name == '' || name == $('#name').attr('title')) {
			$('.name-error').show();
			valid = false;
		}else{
			$('.name-error').hide();
		}
		
		if (email == '' || email == $('#email').attr('title') || !pattern5.test(email)) {
			$('.email-error').show();
			if(!pattern5.test(email)){
			 $('.email-error').html("Email nel formato non corretto!");
			}
			if(email=='' || email == $('#email').attr('title')){
			 $('.email-error').html("campo obbligatorio!");
			}
			valid = false;
		}else{
			$('.email-error').hide();
		}
		
		if (message == '' || message == $('#message').attr('title')) {
			$('.message-error').show();
			valid = false;
		}else{
			$('.message-error').hide();
		}
		
		// send data with ajax
		
		var formData = 'name='+ name + '&email=' + email + '&phone=' + phone + '&message=' + message + '&auth=' + 'true';
		
		if (valid == true) {		
			
			
			
			// Send
					$.ajax({
						url: 'lib/sendmsg.php',
						dataType: 'json',
						type: 'POST',
						data: formData,
						success: function(data, textStatus, XMLHttpRequest)
						{
							if (data.valid)
							{
								$('.error').hide();
								$('#name').val('');
								$('#email').val('');
								$('#phonenumber').val('');
								$('#message').val('');
								$('.thx').show();
								$('.thx-err').hide();
							}
							else
							{
								// Message
								$('.thx-err').show();
								$('.thx-err').html(data.response);
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown)
						{
							// Message
							$('#message').val('');
							$('#name').val('');
							$('#email').val('');
							$('.thx-err').show();
							$('.thx-err').html("Errore di trasmissione, riprovate piu tardi");
						}
					});
					
					// Message
					
				
			
			
			
			
			
			
			
			
			
		} // valid

				
		return false;
	
	});


});
