/* jQuery  || definisco una var per il $ per evitare il conflitto con Prototype+Scriptacolous */
var $ale_=jQuery.noConflict();

$ale_(document).ready(function() {
	/* nascondo i div del contenuto dello showcase */
	$ale_('.showcaseContents').hide();
	
	/* alternative al metodo toggle / show / hide */
	jQuery.fn.slideFadeToggle = function(speed, easing, callback) { return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback); };
	jQuery.fn.fadeToggle = function(speed, easing, callback) { return this.animate({opacity: 'toggle'}, speed, easing, callback); };
	/* /alternative */
	
	/* blend per il menu */
	$ale_("#menu li span").blend();
	/* /blend */

	/* menu, growl, etc */						   
	$ale_('span.goToSection').click(function() {						
		where=$ale_(this).attr('title');
		if(where!='') { $ale_('html,body').animate({scrollTop:$ale_("span#"+where).offset().top},1000); }
		return false;
	});
	$ale_('.back2top').click(function(){
		$ale_('html,body').animate({scrollTop:$ale_("span#topPage").offset().top},1000);
		return false;
	});
	/* /menu etc */
	
	/* frecce sezione successiva / top */
	$ale_('.nextSection img').click(function(){
		sezione=($ale_(this).attr('id').replace("next_",""));
		$ale_('html,body').animate({scrollTop:$ale_("span#"+sezione).offset().top},1000);
		return false;
	});
	/* /frecce */
	
	/* slider  services */
	var totWidth=0; var positions = new Array(); var _current=0;
	$ale_('#slides .slide').each(function(i){ positions[i]= totWidth; totWidth += $ale_(this).width(); });
	$ale_('#slides').width(totWidth);
	$ale_('#menuServices ul li').click(function(){
		$ale_('li.menuItem').removeClass('activeLi');
		$ale_(this).addClass('activeLi');
		var pos = $ale_(this).prevAll('.menuItem').length;
		_current=pos;
		$ale_('#slides').stop().animate({marginLeft:-positions[pos]+'px'},600);
		return false;
	});
	$ale_('#menuServices ul li.menuItem:first').addClass('activeLi');
	$ale_('#servicesSection h1').click(function(){ $ale_('li.menuItem').removeClass('activeLi'); $ale_('#slides').stop().animate({marginLeft:-positions[0]+'px'},600); });
	/* /slider services */
	
	/* popup spiegazione servizi */
	$ale_(".serviceSlideText ul li").click(function(){
		descServ=$ale_(this).attr('title');
		if(descServ!='') { $ale_('#pop').jGrowl(descServ); }
		return false;
	});
	/* /popup */
	
	/* animazione works */
	var sezioneAttuale='';
	$ale_('#menuWorks li').click(function() {
		sezioneAttuale = $ale_(this).attr('title');
		//alert(sezioneAttuale);
		$ale_('#swInner').animate({top:-($ale_("#anchor_"+sezioneAttuale).position().top)},1000);
		return false;
	});
	$ale_('#worksSection>h1').click(function(){ $ale_('#swInner').animate({top:0},1000); return false;
	});
	/* /animazione */
	
	/* single work slider */
	var _positions = new Array(); var _totWidth=0; var __current=0; var abc=''; var bcd='';
	$ale_('.singleWsliderInner div').each(function(i){ _positions[i]= totWidth; _totWidth += $ale_(this).width(); });
	$ale_('.singleWsliderInner').width(_totWidth);
	$ale_('.singleWmenu ul li').click(function(){
		xyz=$ale_('#menuW_'+sezioneAttuale+' ul li').parent().parent().attr('id');
		if(('menuW_'+sezioneAttuale)==(xyz)){
			abc=$ale_(this).parent().parent().parent().find('div').attr('id');
			bcd='slides_'+sezioneAttuale;
				if(abc==bcd) {
					$ale_('#menuW_'+sezioneAttuale+' ul li').removeClass("activeLiW");
					$ale_(this).addClass("activeLiW");
					var pos = $ale_(this).prevAll('li').length;
					$ale_('#slides_'+sezioneAttuale).stop().animate({marginLeft:-positions[pos]+'px'},600);
				}
		}
		return false;
	});
	$ale_('.singleWmenu ul').each(function(i){ $ale_(this).children(':first-child').addClass("activeLiW"); });
	/* /single work */	
	
	/* form contatti */
	$ale_("#contact_form").submit(function(){
		var err=1; var nome = $ale_('input[name=nome]'); var cognome = $ale_('input[name=cognome]'); var email = $ale_('input[name=email]'); var messaggio = $ale_('textarea[name=messaggio]');
		if (nome.val()=='') { $ale_('label[for=nome]').addClass('error'); }
		else {
			$ale_('label[for=nome]').addClass('correct');
			if (cognome.val()=='') { $ale_('label[for=cognome]').addClass('error'); }
			else {
				$ale_('label[for=cognome]').addClass('correct');
				if (email.val()=='') { $ale_('label[for=email]').addClass('error'); }					
				else {
					$ale_('label[for=email]').addClass('correct');
					if (messaggio.val()=='') { $ale_('label[for=messaggio]').addClass('error'); }
					else {
						$ale_('label[for=messaggio]').addClass('correct');
						err=0;
		} } } }
		if(err==0) {
			var data = 'nome=' + nome.val() + '&cognome='+ cognome.val() + '&email=' + email.val() +  '&messaggio=' + messaggio.val();  
			$ale_.ajax({ 
				url: "mail.php",    
				type: "POST",            
				data: data, 
				success: function(html) {
					$ale_('.sendingMessage').html(html);
				}      
			});
		} else { alert("Compilare tutti i campi!"); }
		return false;
	});
	/* /form contatti */
	
	/* ultimi tweets */
	$ale_("#twitter").tweet();
	$ale_('span#toggleTwitter').click(function(){
		if ($ale_('#twitter').css("display")=="block") { $ale_('span#toggleTwitter').html("show last tweet"); }
		else { $ale_('span#toggleTwitter').html("hide me"); }	
		$ale_('#twitter').fadeToggle('slow');
		return false;
	});
	/* /ultimi tweets */
});	/* /document.ready */

$ale_(function(){   
	/* preload immagini */
	$ale_("img.lazy").lazyload({effect:'fadeIn'});
	
	/* scroll del footer */
	function moveFooter() { var footerOffset = footerYloc.top + $ale_(this).scrollTop() + "px"; $ale_('#footer').animate({top:footerOffset},{duration:1000,queue:false}); }
	footerYloc = $ale_('#footer').offset(); $ale_(window).scroll(moveFooter); moveFooter();
	/* /scroll del footer */
});
