/* Ajax functions */
function showBusy(div){
	$(div).block({
		overlayCSS: {backgroundColor: '#fff'},
		message:'<img src="'+base_url+'img/ajax_loader.gif" />',
		css:{border:'0', background:'none'}
	})
}

function updatePage(html,div){
	window.setTimeout(function(){
		$(div).html(html);
	},900)
}

function form_ok(){
	var ok = {
		'it':'<strong>Messaggio inviato.</strong> Grazie per averci contattato. Risponderemo al pi&ugrave; presto!', 
		'en':'<strong>Message has been submitted.</strong>Thank you for contacting us. We will be in touch with you soon!'
	};
	
	//ok
	$('#form_message').html(ok[lang])
		.css({'border': '1px solid #89aa5b', 'background' : '#f9f4df url('+base_url+'img/icon_ok.gif) 10px center no-repeat'})
		.fadeIn('slow');
	
	//disable send
	$('#form button').removeClass('orange').addClass('gray')
		.live('click', function(eve){
			eve.preventDefault();
		});
}



$(document).ready(function(){ 

//Jcarousel portfolio

$('#gallery').jcarousel({
	scroll: 3	
});


//Ajax form submit
$("#form").live('submit', function(eve){
	eve.preventDefault();
	
	//Reset message
	$('#form_message').hide();
	
	//Ajax post
	$.ajax({
		url: base_url+lang+'/home/send',
		type: "POST",
		dataType: "html",
		data: $("#form").serialize(), //Collect data
		beforeSend: function(){
			showBusy('#form');
		},
		success: function(html){
			
			window.setTimeout(function(){
				
				//Remove block
				$('#form').unblock();
				
				//Check response
				if (parseFloat(html)){ //If 1 -> OK!
					form_ok();
				} else {
					//Error
					$('#form_message').html(html).fadeIn('slow');
				}
			
			},1000)
			
		}
	});
	
});

//Page scroll
	$(".anchor").click(function(eve){
		eve.preventDefault();
		var target = $(this).attr("href");
		
		var target_offset = $(target).offset();
		var target_top = target_offset.top;
				
		$('html, body').animate({scrollTop:target_top}, 500);
	});

//Back to top
	$('a[href=#top]').click(function(eve){
		eve.preventDefault();
		$('html, body').animate({scrollTop:0}, 500);
		return false;
	});


}); /*End of $(document).ready(function())*/
