// JavaScript Document
var TabbedContent = {
	init: function() {	
		$(".tab_item").mouseover(function() {
		
			var background = $(this).parent().find(".moving_bg");
			
			$(background).stop().animate({
				left: $(this).position()['left']
			}, {
				duration: 300
			});
			
			TabbedContent.slideContent($(this));
			
		});
	},
	
	slideContent: function(obj) {
		
		var margin = $(obj).parent().parent().find(".slide_content").width();
		margin = margin * ($(obj).prevAll().size() - 1);
		margin = margin * -1;
		
		$(obj).parent().parent().find(".tabslider").stop().animate({
			marginLeft: margin + "px"
		}, {
			duration: 300
		});
	}
}

$(document).ready(function() {
	
	
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = $.curCSS;
		$.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			fx.start = [start[0],start[2]];
			var end = toArray(fx.end);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};	
	
	
	
	
	 TabbedContent.init();
     $("#reveal").airport([ 'emoções à flor da pele.','suspiros de felicidade.','um brilho no olhar.','sorrisos sinceros.','adrenalina no sangue.','corações saindo pela boca.','borboletas no estômago.', 'corações palpitando.', 'gargalhadas descontroladas.', 'friozinho na barriga.', 'arrepios na coluna.'  ]);
	 //$("a").tipTip({maxWidth: "90px",delay:100,edgeOffset:10});
	 $('a.navigation').bind('click',function(event){
		var $anchor = $(this);
		$('html, body').stop().animate({
			scrollLeft: $($anchor.attr('href')).offset().left
		}, 2000,'easeOutBounce');
		event.preventDefault();
	 }).bt({width:90,strokeWidth:0,positions:'bottom',fill: 'rgba(255, 194, 14, .95)'});
	 $("a.portifolio").fancybox({
				'overlayShow'	: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
			}); 		
						
	$("a.videos").click(function() {
		$.fancybox({
			'autoScale'		: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'width'			: 640,
			'height'		: 385,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true'
			}
		});
		return false;
	});
			
	$("a.team-item").draggable({
			start: function(event,ui){$(this).addClass('drag'); $('a.team-item').attr('bt-xtitle','');},
			stop:  function(event,ui){$(this).removeClass('drag');},
			stack: "a.team-item"
			}).click(function(){return false}).bt({strokeWidth:0,width:90,positions:'top',centerPointX:.4,fill: 'rgba(255, 255, 255, .95)',});
	

	//$("a.team-item").bt({trigger:'none',width:80,positions:'top',centerPointX:.4,overlap:40});

			
	$("#team-details").droppable({
			over: function( event, ui ) {
					if($(ui.draggable).attr('detail') != ''){
						$('#more-detail').text('Veja mais');				
						$('#more-detail').attr('href',$(ui.draggable).attr('detail'));
					}else{
						$('#more-detail').text('');				
					}
					$(this).css("background-color","#444");
					y = $(ui.draggable).attr('position')*40;
					s1 = $(ui.draggable).attr('scoretitle1');
					s2 = $(ui.draggable).attr('scoretitle2');
					s3 = $(ui.draggable).attr('scoretitle3');
					b1 = $(ui.draggable).attr('score1');
					b2 = $(ui.draggable).attr('score2');
					b3 = $(ui.draggable).attr('score3');
					$('#team-name').animate({"background-position":"30px -"+y+"px"},1000,'easeOutBounce'); 					
					$('#score1').text(s1);
					$('#score2').text(s2);
					$('#score3').text(s3);
					$('#bar1').animate({"width":b1+"px"},200,'easeOutBounce');
					$('#bar2').animate({"width":b2+"px"},600,'easeOutBounce');
					$('#bar3').animate({"width":b3+"px"},1000,'easeOutBounce');
				},
			out:  function ( event, ui) {
					$('#team-name').animate({"background-position":"30px 0px"},500);				
					$(this).css("background-color","black"); 
					$('#score1').text('');
					$('#score2').text('');
					$('#score3').text('');
					$('#bar1').animate({"width":"1px"},200);
					$('#bar2').animate({"width":"1px"},200);
					$('#bar3').animate({"width":"1px"},200);					
					$("a.team-item").attr('bt-xtitle','');		
					$('#more-detail').text('');									
				},
			drop: function ( event, ui ) {
				if($('a.berlinda').attr('id') != $(ui.draggable).attr('id')){
					$('a.berlinda').animate({top:'0px',left:'0px'});
					$('a.berlinda').removeClass('berlinda');
					$(ui.draggable).addClass('berlinda');
					$('a.team-item').each(function(index){
					  $(this).attr('bt-xtitle',$(this).attr(ui.draggable.attr('id')));					
					});
				}
			}
			}); 

	$(document).mousemove(function(e){
			$('a.team-item.over').each(function(d){if ($(this).hasClass('drag')){$(this).css('background-position', '-1080px');} else {$(this).css('background-position', '-960px');}});
			$('a.team-item.move').each(function(d){
					$(this).mouseover(function(){$(this).removeClass('move').addClass('over');});
					$(this).mouseout(function(){$(this).removeClass('over').addClass('move');}); 
					var offset = $(this).offset();
					var xPos = offset.left + 60;
					var yPos = offset.top + 60;
					var angle = Math.atan2((yPos - e.pageY), (xPos - e.pageX)) * (180/Math.PI);
					if(angle >= 67.5 && angle < 112.5) { $(this).css('background-position', '-0px'); }
					if(angle >= 112.5 && angle < 157.5) { $(this).css('background-position', '-120px'); }
					if(angle >= 157.5 || angle < -157.5) { $(this).css('background-position', '-240px'); }
					if(angle >= -157.5 && angle < -112.5) { $(this).css('background-position', '-360px'); }
					if(angle >= -112.5 && angle < -67.5) { $(this).css('background-position', '-480px'); }
					if(angle >= -67.5 && angle < -22.5) { $(this).css('background-position', '-600px'); }
					if(angle >= -22.5 && angle < 22.5) { $(this).css('background-position', '-720px'); }
					if(angle >= 22.5 && angle < 67.5) { $(this).css('background-position', '-840px'); }
				});
	}); 
	 $('#form-contato').validate({
        rules: {
            nome: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            nome: "EPA! Esqueceu de colocar seu nome.",
            email: "EPA! Esqueceu de colocar seu email."
        }
    });
	
	
	
	$("#form-contato").submit(function() {
		var nome = $("#nome").val();
		var email = $("#email").val();
		var empresa = $("#empresa").val();
		var textarea = $("#textarea").val();
		var dataString = 'nome='+ nome + '&email=' + email + '&empresa=' + empresa + '&mensagem=' + textarea;
	  if(nome=='' || email==''){
		  	  return false;
	  }else{	 
		$.ajax({
		type: "POST",
		url: "envia_mensagem.php",
		data: dataString,
		success: function(){
		  $("#nome").val('');
		  $("#email").val('');
		  $("#empresa").val('');
		  $("#textarea").val('');
		  alert('Pronto! Que tal ver nosso portifólio agora?!');
		}
		});
	  }
	  return false;
   });

	
});

