﻿/* modifica easing per un movimento più fluido */
jQuery.easing['jswing'] = jQuery.easing['swing'];		
jQuery.extend( jQuery.easing,{
	easeCool: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	}
});

jQuery.fn.outerScrollHeight = function(includeMargin) {
	var element = this[0];
	var jElement = $(element);
	var totalHeight = element.scrollHeight;
	totalHeight += jElement.outerHeight(includeMargin) - jElement.innerHeight();
	return totalHeight;
};

var AnBi = {
	init : function(){	
		Cufon.replace('h1')
		Cufon.replace('h2')
		Cufon.replace('h3', {hover: 'true'});
		Cufon.replace('h4');
		Cufon.replace('label');
		Cufon.replace('.block > ul > li');
		$('input[type!="hidden"], textarea').each(function(){ cont = $('<div></div>'); $(this).wrap(cont); });
		$('form button').click(function(){
			$('.loader').addClass('load');	
			$.ajax({
				type: "POST",
				url: "lib/sendmail.php",
				data: $('form').serialize(),
				success: function(msg){				
					if(msg=='sent'){
						alert("Mail sent! Thank you.")
						$('form input[type!="hidden"]').val("")
						$('form textarea').val("");
						$('form input[type!="hidden"]').parent('div').css({'background':'#ffffff'});
						$('form textarea').parent('div').css({'background':'#ffffff'});
						$('.loader').removeClass('load');
					}
					if(msg=='no-sent'){
						AnBi.showMsg("Server Error: Mail not sent!")
						$('.loader').removeClass('load');
					}
					if(msg=='no-secure'){
						$('#secure').parent('div').css({'background':'red'});
						AnBi.showMsg("Wrong answer!");
						$('.loader').removeClass('load');
					}
					if(msg=='no-name'){
						$('#name').parent('div').css({'background':'red'});
						AnBi.showMsg("Missing name!");
						$('.loader').removeClass('load');
					}
					if(msg=='no-text'){
						$('#text').parent('div').css({'background':'red'});
						AnBi.showMsg("Missing text!");
						$('.loader').removeClass('load');
					}
					if(msg=='no-mail'){
						$('#mail').parent('div').css({'background':'red'});
						AnBi.showMsg("Missing mail!")
						$('.loader').removeClass('load');
					}
				}
			});
		});		
		$('h1 > a').click(function(){
			$('.hidden-menu h3').animate({'top':'100px'}).attr('class','off');
			$('.cont-block').animate({'left':'0%'},'slow','easeCool');
			location.hash = '#';			
		});
		gap = '15px';
		$('.hidden-menu h3').each(function(i,e){
			$(e).hover(function(){
				$(this).animate({'top':'0px'},'fast','easeCool');
			},function(){
				if($(this).hasClass('on')){
					$(this).animate({'top':'0px'},'slow','easeCool')
				}else{
					$(this).animate({'top':gap},'fast','easeCool')
				};
			})
			if($(e).attr('target')!='_blank'){
				$(e).click(function(){
					n = (i+1)*100;
					$('.cont-block').animate({'left':'-'+n+'%'},'slow','easeCool');
					$('.hidden-menu h3:not('+i+')').animate({'top':gap}).attr('class','off');
					$('.hidden-menu h3:eq('+i+')').animate({'top':'0px'}).attr('class','on');
				})
			}
		});
		$('.block > h3 > a').each(function(i,e){
			if($(e).attr('target')!='_blank'){
				$(e).click(function(){
					n = (i+1)*100;
					$('.cont-block').animate({'left':'-'+n+'%'},'slow','easeCool');
					$('.hidden-menu h3:not('+i+')').animate({'top':gap}).attr('class','off');
					$('.hidden-menu h3:eq('+i+')').animate({'top':'0px'}).attr('class','on');
				})
			}
		});	
		
		if(location.hash!=''){
			if(location.hash=='#twitter'){ 
				$('.block > h3:eq(0) > a:eq(0)').click();
			}
			if(location.hash=='#photos'){ 
				$('.block > h3:eq(1) > a:eq(0)').click();
			}
			if(location.hash=='#contacts'){ 
				$('.block > h3:eq(3) > a:eq(0)').click();
			}
			if(location.hash=='#projects'){ 
				$('.block > h3:eq(2) > a:eq(0)').click();
			}
			if(location.hash=='#linkedin'){ 
				$('.block > h3:eq(4) > a:eq(0)').click();
			}
		}


		$('#tw > ul > li').click(function(){window.open("http://twitter.com/anbi/status/"+$(this).attr('rel'));});	
		$("#flickr a").fancybox({ 'type':'image', 'titleFormat':AnBi.formatTitle, 'overlayColor':'#000'});
		$("#prj a").fancybox({ 'type':'iframe', 'overlayColor':'#000', 'width':'640', 'height':'480'});
		$('textarea').bind('keypress', function(e) {
			if($(this).outerScrollHeight()-2>$(this).height()){ $(this).height($(this).outerScrollHeight()+10); }		
		});
		$('html').css({ 'overflow':'auto', 'overflow-x':'hidden'});	
		maxH = 0;	
		$(window).resize(function() {
			$('.block').each(function(ind,el){ if($(el).height()>maxH) maxH = $(el).height(); });
			$('.block').css({'width':$(window).width()-20+'px','min-height':maxH+'px','margin':'0 10px'});
		});	
		$('.block').each(function(ind,el){ if($(el).height()>maxH){ maxH = $(el).height(); } });
		$('.block').css({'width':$(window).width()-20+'px','min-height':maxH+'px','margin':'0 10px'});	
		$('#loader').fadeOut();
	},
	showMsg : function(msg){
		if(msg){
			bgMsg = $('<div class="msg"></div>').css({ 'height':$('body').height()+'px', 'width':$('body').width()+'px', 'z-index':'3000', 'opacity':'.5', 'background':'#000'})
			contMsg = $('<div class="msg"></div>').css({ 'height':$('body').height()+'px', 'width':$('body').width()+'px', 'z-index':'3001' }).click(function(){$('.msg').fadeOut(function(){$('.msg').remove();});	})
			ttt = Math.round($(document).scrollTop()+(($(window).height()-120)/2));
			textMsg = $('<div><p>'+msg+'</p><br /><p class="small">Please try again.</p><br /><br /><a href="javascript:;" onclick="AnBi.showMsg()">close</a></div>').css({'margin':ttt+'px auto 0'})
			$(window).resize(function() {
				$(bgMsg).height($('body').height())
				$(bgMsg).width($('body').width())
				$(contMsg).height($('body').height())
				$(contMsg).width($('body').width())
				$(textMsg).css({'margin-top':ttt+'px'})
			});			
			$('body').append(bgMsg);
			$(contMsg).append(textMsg);
			$('body').append(contMsg);			
			Cufon.replace('.msg > div > p');
			Cufon.replace('.msg > div > a');
			$('.msg').fadeIn();
		}else{
			$('.msg').fadeOut(function(){$('.msg').remove();});			
		}
	},
	formatTitle : function(title){
		return '<table id="fancybox-title-float-wrap" cellpadding="0" cellspacing="0"><tbody><tr><td id="fancybox-title-float-left"></td><td id="fancybox-title-float-main"><b>' + title + '</b> - <a href="http://www.flickr.com/photos/andreabianchin/'+this.href.split('id=')[1]+'/" target="_blank">Show on Flickr</a></td><td id="fancybox-title-float-right"></td></tr></tbody></table>';
	}
}

$(function() {
	AnBi.init();
});
