var timeout;
var elem;
$('#projects').hide();
$(document).ready(function() {
	
	//E-mail:
	$('.mail_info').safe_mail('info', 'bibtic', 'com');
	$('#projects').show();
		$('#projects').jcarousel({
			auto: 3,//3,
	        wrap: 'last',
			scroll: 1,
			buttonNextHTML: '<div><span>&gt;</span></div>',
			buttonPrevHTML: '<div><span>&lt;</span></div>',
	       initCallback: mycarousel_initCallback
		});
	
		
	//Efectes Div Tots els Projectes
	$('.bubble').hide();
	$("#projects .proj").hover(
		function()	{
			$(this).children('.bubble').stop(true).animate({opacity: 100, height: 260}, 700, "easeInOutCirc");
			elem=this;
			callTimer();
		},
		function(){
			clearTimeout(timeout);
			$(this).children('.bubble').stop(true).animate({opacity: 0, height: 0}, 400, "easeInSine");
		}
	);
	
	$( "#projects .proj" ).each(function( intIndex ){
		var bgX = -170;
		var bgX_inc = 170;
		$( this ).bind( "blur", function()	{
			$(this).children('.portrait').stop(true).animate({ backgroundPosition: '0 ' + bgX}, 700);
			bgX = bgX - bgX_inc;
			timeout=setTimeout("callTimer()",1700);
		});
	});
	
	
	//Efectes de footer
	$('#footer .bubble').show();
	
	/*fancybox*/
	$("a#inline").fancybox({ 
		'hideOnContentClick': false,
		'padding': 0,
		'opacity': true
	});
	$('.text_field').blur(function() { validateForm();});
	$('.text_area').blur(function() { validateForm();});
	
	//Goggle Map Functions:
	var loaded_maps=false;
	$('#ajax_googlemaps').show();
	
	$('#google_bubble').hover(
		function (){
			$.post('view/ajax/googlemaps.php', { eti : etiqueta_maps } , function(data) {
				$('#google_bubble .bubble').show();
				if (loaded_maps==false) {
					$('#ajax_googlemaps').html(data);
					}
				$('#ajax_googlemaps').stop(true).animate({opacity: 100}, 700, "easeInOutCirc");
				loaded_maps=true;
				});
			},
		function(){
			$('#ajax_googlemaps').stop(true).animate({opacity: 0}, 700, "easeInOutCirc");
			$('#google_bubble .bubble').fadeOut();
			}
		);
	
});

var carousel_global;
var guard_autoStart=true;	//start amb el auto-scroll del carousel

function mycarousel_initCallback(carousel)
{
	carousel_global=carousel;
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
       	carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
		if (guard_autoStart)	carousel.stopAuto();
    }, function() {
        if (guard_autoStart)	carousel.startAuto();
    });
};

function callTimer()
	{
	clearTimeout(timeout);
	$(elem).children('.portrait').blur();
	}
	
	
//Contact Form functions:
var data_global;
var guarda_form=false;
function restartMail()
	{
	$('#nom').removeClass('falta');
	$('#email').removeClass('falta');
	$('#sms').removeClass('falta');
	}

function validateForm()
	{
	if (guarda_form){
		restartMail();
		var estatus=true;
		form_c=document.getElementById('BibticcontactForm');
		if ((form_c.email.value.length<5)||(form_c.email.value.indexOf("@")<0)||(form_c.email.value.indexOf(".")<0))	
			{
			$('#email').addClass('falta');
			estatus=false;
			}
		if (form_c.nom.value.length<2)
			{
			$('#nom').addClass('falta');
			estatus=false;
			}
		if (form_c.sms.value.length<2)
			{
			$('#sms').addClass('falta');
			estatus=false;
			}
	return estatus;
		}
	}

function sendFormMail()
	{
	guarda_form=true;
	var estatus=validateForm();
	if (estatus)
		{
		$('#ajax_formButton').html('<img src="view/css/img/loader.gif" alt="sending" title="enviant formulari"/>');
		$.post('view/ajax/send_form.php', { lang : form_c.langg.value , nom : form_c.nom.value , mail : form_c.email.value , sms : form_c.sms.value }, function(data) {
			data_global=data;
			$('#ajax_formContent').fadeOut("slow", function(){
				//Fade out complete!
				$('#ajax_formContent').html(data_global);
				$('#ajax_formContent').fadeIn("slow");
				});
			});
		}
	return false;
	}
