/*
* Script para animar as fotos da equipe
*
*/

var fotosInfo = [
	{name: 'Otílio Paulo',img: 'otilio', cargo: 'Diretor', twitter: 'netsoftpi', multifoto: false },
	{name: 'Dalla Cristiane',img: 'dalla', cargo: 'Diretora', twitter: 'netsoftpi', multifoto: false },
	{name: 'Lucas Simão',img: 'lucas', cargo: 'Coordenador de Projetos', twitter: 'LucasS2Adna', multifoto: false },
//	{name: 'Diolene Medina',img: 'anonimo', cargo: 'Atendente', twitter: '', multifoto: false },
	{name: 'Thiago Feitosa',img: 'thiago', cargo: 'Suporte', twitter: '', multifoto: false },
	{name: 'Gracielly Costa',img: 'gracy', cargo: 'Desenvolvedora', twitter: 'gracycf', multifoto: false },
	{name: 'Lucas Aquiles',img: 'aquiles', cargo: 'Desenvolvedor', twitter: 'lucas_aquiles', multifoto: false },
	{name: 'José Victor',img: 'victor', cargo: 'Desenvolvedor', twitter: 'josevictorvs', multifoto: false },
	{name: 'Wandresson Souza',img: 'wan', cargo: 'Desenvolvedor', twitter: 'wansoul', multifoto: false },
];

var imagesDir = "wp-content/themes/netsoft/images";
var mouseOver = false;

//var cont = 0;
var origem = {x: 0, y: 0};

var duracaoHover = 1200;
var duracaoLeave = 100;

var tamanhoInicial = 80;
var tamanhoFinal = 160;
var difTamanhos = (tamanhoFinal - tamanhoInicial)/2;

var easeHover = 'easeOutElastic';
var easeLeave = 'easeOutCubic';

//array de fotos
var fotos = new Array(fotosInfo.length);

//array de z-index e z-index inicial
var zIndexInicial = 900;
var zIndexMax = zIndexInicial + fotosInfo.length - 1;

//angulo inicial que define a posição da 1ª foto no topo
var PI = Math.PI;
var anguloInicial = 0;//- PI / 2 ;
var intervaloRotacao = 60; //define a velocidade da rotação
var anguloIncremento = PI / 280;
var raio = 190;
var sentidoRotacao = 1;

var animar = true;

$(document).ready( function () {
	
	if( $(".entry-title").text() == "Equipe"){
	
		$("div.entry-content").append("<div id='equipeWrapper' ></div>");
		
		//Origem do sistema de coordenadas a partir da imagem do branding
		origem.x = Math.round( $("#equipeWrapper").offset().left + $("#equipeWrapper").width() /2 ) ;
		origem.y = Math.round( $("#equipeWrapper").offset().top + $("#equipeWrapper").height() /2 ) ;
		
		$("#equipeWrapper").append("<div id='legendaFoto'>Sistema de Vendas</div>");	

		//posicionar fotos
		for(i = 0; i < fotos.length; i++){
			
			$("#equipeWrapper").append("<img id='foto"+i+"' class='foto'/>");
			fotos[i] = $("#foto"+i);
			
			fotos[i].attr({
				src: imagesDir+'/equipe/'+ fotosInfo[i].img +'.jpg',
				alt: fotosInfo[i].name,
			});
			
			fotos[i].css({
				width: tamanhoInicial,
				height: tamanhoInicial,
				cursor: "pointer",
				'z-index': zIndexInicial + i
			});
		
		}
		
		var cont = 0;
		$(".foto").each( function(){
		
			var legenda = "<span>"+fotosInfo[cont].name+"</span><br/>"+fotosInfo[cont].cargo;
			var twitter = fotosInfo[cont].twitter;
			var numFoto = cont;
		
			$(this).hover( function(){
				$(this).attr({
					src: imagesDir+'/equipe/'+ fotosInfo[numFoto].img+'.jpg',
				});
				mouseOver = true;
				zIndexSwitch( $(this) );
				animar = false;
				maximizar( $(this) );
				$("#legendaFoto").html(legenda).show();
				
			}).mouseleave( function(){
				mouseOver = false;
				minimizar( $(this) );
			}).click( function(){
				if(twitter != '')
					window.open("http://www.twitter.com/"+twitter);
			});
			
			cont = cont >= fotosInfo.length ? 0 : ++cont ;
			
		});
		
		atualizarPosicoesIcones();
	
	}

});

function maximizar(foto){
	var xIni = foto.offset().left;
	var yIni = foto.offset().top;

	var xFinal = xIni - difTamanhos;
	var yFinal = yIni - difTamanhos;
	
	foto.stop().animate({width: tamanhoFinal, height:tamanhoFinal, left: xFinal, top: yFinal }, {duration: duracaoHover, easing: easeHover, queue: false});
	
	$("#legendaFoto").css({ left: xFinal, top: yFinal+tamanhoFinal+2})
}

function minimizar(foto){
	var xFinal = foto.offset().left;
	var yFinal = foto.offset().top;

	var xIni = xFinal + difTamanhos;
	var yIni = yFinal + difTamanhos;
	
	foto.stop().animate({width: tamanhoInicial, height: tamanhoInicial, left: xIni, top: yIni }, {duration: duracaoLeave, easing: easeLeave, queue: false});
	
	$("#legendaFoto").hide();
	
	animar = true;
	atualizarPosicoesIcones();
	
}

function atualizarPosicoesIcones(){
	var angulo = 2 * PI / fotos.length;
	var x, y;
	var correcaoPosicao = tamanhoInicial/2;
	anguloInicial += anguloIncremento * sentidoRotacao;
	var ang;
	
	//Origem do sistema de coordenadas a partir da imagem do branding
	origem.x = Math.round( $("#equipeWrapper").offset().left + $("#equipeWrapper").width() /2 ) ;
	origem.y = Math.round( $("#equipeWrapper").offset().top + $("#equipeWrapper").height() /2 ) ;
	
	for(i = 0; i < fotos.length; i++){
		ang = anguloInicial + angulo*i ;
		
		x = Math.round( origem.x + raio * Math.cos( ang ) );
		y = Math.round( origem.y + raio * Math.sin( ang ) );
		
		fotos[i].css({
			left: x - correcaoPosicao,
			top: y - correcaoPosicao
		});
		
		if(!mouseOver){
			if(fotosInfo[i].multifoto){
				var angIncr = PI/8;
				var angAtual = ang%(2*PI);
				var numFoto;
				
				if( angAtual > 2*PI-angIncr/2 || angAtual < angIncr ){
					numFoto = 0;
				} else if( angAtual > angIncr && angAtual < 3*angIncr ){
					numFoto = 1;
				} else if( angAtual > 3*angIncr && angAtual < 5*angIncr ){
					numFoto = 2;
				} else if( angAtual > 5*angIncr && angAtual < 7*angIncr ){
					numFoto = 3;
				} else if( angAtual > 7*angIncr+angIncr/2 && angAtual < 9*angIncr ){
					numFoto = 4;
				} else if( angAtual > 9*angIncr && angAtual < 11*angIncr ){
					numFoto = 5;
				} else if( angAtual > 11*angIncr && angAtual < 13*angIncr ){
					numFoto = 6;
				} else if( angAtual > 13*angIncr && angAtual < 15*angIncr ){
					numFoto = 7;
				}
				
				fotos[i].attr({
					src: imagesDir+'/equipe/'+ fotosInfo[i].img+numFoto+'.jpg',
				});
			}
		}
	
	}
	
	if(animar){
		setTimeout(atualizarPosicoesIcones, intervaloRotacao);
	}

}

function zIndexSwitch(iconAtivo){
	var temp = iconAtivo.css("z-index");
	for(i=0; i < fotos.length; i++){
		if( fotos[i].css("z-index") == zIndexMax ){
			fotos[i].css({'z-index': temp });
		}
	}
	iconAtivo.css({'z-index': zIndexMax});
}

