// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d)
{
	var s=1.70158;
	return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
};

jQuery(function( $ )
{
	var inc_exec = 'http://www.mixvirtual.com.br/source/include';
	var ajax_exec = 'http://www.mixvirtual.com.br/source/ajax';
	
	
	//################ SCROLLABLE ################
	// =================> slider home
	$(".vitrine-slide").tabs(".vitrine-tabs > div", {
		effect: 'slide',
		fadeOutSpeed: "slow",
		rotate: true
	}).slideshow({autoplay:true, interval:10000, autopause:true, clickable:false});
	// slider home <=================
	
	// =================> slider galeria img
	//thumb clicado
	$('.folio-slide a').bind('click',function()
	{
		$('.folio-slide a').removeClass('active');
		$(this).addClass('active');
	});
	//galeria carregada
	$('a.showgaleria').live('click',function()
	{
		var bum = $(this).attr('rel').split('@@');
		$.fancybox({
			'scrolling'		: 'no',
			'href'			: inc_exec+stringSEG(bum[0])+bum[1],
			'width'			: 620,
			'height'		: 430,
			'type'			: 'iframe',
			'autoDimensions': false,
			'overlayShow'	: true,
			'centerOnScroll': true,
			'enableEscapeButton': true,
			'showCloseButton': false
		});	
	});
	// slider galeria img <=================
	
	// =================> slider web
	$(".web-slide").scrollable({circular:true,easing:"swing",speed:800}).navigator(".web-nav");
	//rola para aba desejada 
	$('.web-slide').trigger('load', rolar());
	function rolar()
	{
		var rolar = $('#rolar').val();
		if(rolar != '')
		{
			switch(rolar)
			{
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
					var api = $(".web-slide").data("scrollable");
					api.seekTo(rolar, 800);
				break;
			}
		}
	}
	// slider web <=================
	
	// =================> slider fólio
	// custom easing
	$.easing.custom = function (x, t, b, c, d) {
		var s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	}
	$(".folio-slide").scrollable({circular:false,easing:'custom',speed:300}).navigator();
	// slider fólio <=================
	
	// =================> slider pacote
	$(".pack-slide").scrollable({circular:false,easing:"swing",speed:800}).navigator(".pack-nav");
	// slider pacote <=================
	//################ fim SCROLLABLE ################
	
	
	//################ FANCYBOX ################
	$('a#showOpiniao').fancybox({
		'scrolling'		: 'no',
		'autoDimensions': true,
		'overlayShow'	: true,
		'centerOnScroll': true,
		'showCloseButton': false, //colocar link para fechar nas janelas
		'onClosed'		: function(){
			//reseta form
			$('#getOpNome').val('');
			$('#getOpEmail').val('');
			$('#getOpClass').val('');
			$('#getOpiniao').val('');
			$('#loading_opiniao').html('');
		}
	});
	$('a#showLembrar').fancybox({
		'scrolling'		: 'no',
		'autoDimensions': true,
		'overlayShow'	: true,
		'centerOnScroll': true,
		'showCloseButton': false, //colocar link para fechar nas janelas
		'onClosed'		: function(){
			//reseta form
			$('#getLembrarEmail').val('');
			$('#load_lembrar').html('');
		}
	});
	// imprimir contas
	$('a#showChat').fancybox({
		'scrolling'		: 'no',
		'width'			: 500,
		'height'		: 500,
		'type'			: 'iframe',
		'autoDimensions': true,
		'overlayShow'	: true,
		'centerOnScroll': true,
		'enableEscapeButton': true,
		'showCloseButton': true
	});
	$('a#infodom').fancybox({
		'scrolling'		: 'no',
		'autoDimensions': true,
		'overlayShow'	: true,
		'centerOnScroll': true,
		'showCloseButton': false //colocar link para fechar nas janelas
	});
	// inserir endereço
	$('a#addEndereco').fancybox({
		'autoDimensions': true,
		'overlayShow'	: true,
		'centerOnScroll': true,
		'showCloseButton': false, //colocar link para fechar nas janelas
		'onClosed'		: function(){
			//reseta form endereço
			$('#endereco_pk').val('');
			$('#getTipoEnd').val('');
			$('#getCEP').val('');
			$('#getEstado').val('');
			$('#getCidade').val('');
			$('#getEndereco').val('');
			$('#getNumero').val('');
			$('#getBairro').val('');
			$('#getComplemento').val('');
		}
	});
	// inserir depoimento
	$('a#addDepoimento').fancybox({
		'autoDimensions': true,
		'overlayShow'	: true,
		'centerOnScroll': true,
		'showCloseButton': false, //colocar link para fechar nas janelas
		'onClosed'		: function(){
			//reseta form endereço
			$('#endereco_pk').val('');
			$('#getTipoEnd').val('');
			$('#getCEP').val('');
			$('#getEstado').val('');
			$('#getCidade').val('');
			$('#getEndereco').val('');
			$('#getNumero').val('');
			$('#getBairro').val('');
			$('#getComplemento').val('');
		}
	});
	// criar ticket
	$('a#addTicket').fancybox({
		'autoDimensions': true,
		'overlayShow'	: true,
		'centerOnScroll': true,
		'enableEscapeButton': true,
		'showCloseButton': false, //colocar link para fechar nas janelas
		'onComplete'	: function(){
			$('#ticket_action').val('add');
		},
		'onClosed'		: function(){
			//reseta form
			$('#getAssunto').val('');
			$('#getMensagem').val('');
			$('#ticket_action').val('');
			$('#load_ticket').html('');
		}
	});
	// imprimir contas
	$('a#printContas').fancybox({
		'scrolling'		: 'no',
		'width'			: 500,
		'height'		: 430,
		'type'			: 'iframe',
		'autoDimensions': true,
		'overlayShow'	: true,
		'centerOnScroll': true,
		'enableEscapeButton': true,
		'showCloseButton': true
	});
	// mudar plano (minhaconta)
	$('div#c_show_servico a.hide').fancybox({
		'autoDimensions': true,
		'overlayShow'	: true,
		'centerOnScroll': true,
		'showCloseButton': true,
		'onComplete'	: function(){
			
			$('#getPlan option').each(function()
			{
				//seleciona select
				if($(this).val() == $('#plano_pk').val())
				{
					$(this).hide();
				}
		  	});
		},
		'onClosed'		: function(){
			//reseta campos
			$('#getPlan option').removeAttr('style');
			$('#getPlan').val('');
			$('#plano_pk').val('');
			$('#dominio_pk').val('');
			$('#load_plano').html('');
			$('#domurl').html('');
		}
	});
	$('div#c_show_servico a.btn_acao').bind('click', function ()
	{
		var change = stringSEG($(this).attr('rel'));
		var bum = change.split('@@');
		$('#dominio_pk').val(bum[0]);
		$('#plano_pk').val(bum[1]);
		$('#domurl').html(bum[2]);
		$('div#c_show_servico a.hide').click();
	});
	// fim mudar plano
	
	// exec upload fancybox
	$('.setupload').bind('click', function ()
	{
		var elem,uri,bum;
		elem = $(this).attr('id');
		uri = $(this).attr('rel');
		bum = uri.split('@@');
		$.fancybox({
			'href'			: inc_exec+'/'+stringSEG(bum[0])+'?s='+bum[1],
			'scrolling'		: 'auto',
			'width'			: '55%',
			'height'		: '100%',
			'type'			: 'iframe',
			'autoDimensions': true,
			'overlayShow'	: true,
			'centerOnScroll': true,
			'enableEscapeButton': true,
			'showCloseButton': true
		});
	});
	//links alterar logo/foto (minhaconta)	
	$('#change_avatar').bind('mouseenter mouseleave', function()
	{
		if( $("#menu_avatar").hasClass("hide") )
		{
			$("#menu_avatar").attr("class","show");
			$('#menu_avatar span').bind('mouseleave', function()
			{
				$("#menu_avatar").attr("class","hide");
			});
		}
	});
	
	//colocar classe nos itens p/ fechar
	$('div.close_box a').click($.fancybox.close);
	//################ fim FANCYBOX ################
	
	
	//################ TOOLTIP ################
	$('.tip').tooltip({
		track: true,
		showURL: false,
		delay: 1
	});
	$('.foliotip').tooltip({
		track: true,
		showURL: false,
		delay: 1
	});
	$('.deptip').tooltip({
		track: true,
		showURL: false,
		delay: 1
	});
	$('.sqtip').tooltip({
		track: true,
		showURL: false,
		delay: 1
	});
	$('.socialtip').tooltip({
		track: true,
		showURL: false,
		delay: 0
	});
	$('.thumbtip').tooltip({
		delay: 0, 
	    track: true, 
	    showURL: false, 
	    bodyHandler: function(){ 
	        return $("<img/>").attr("src", this.src); 
	    } 
	});
	$('.ideatip').tooltip({
		delay: 50,
		fade: 250, 
	    track: true, 
	    showURL: false, 
	    bodyHandler: function(){ 
			return $(this).children('span').html(); 
	    } 
	});
	//################ fim TOOLTIP ################
	
	
	//################ DIVERSOS ################
	$('#leu').trigger('load', showDepoimentos() );
	//mostra depoimento completo
	$("a.showDep").fancybox({
		'autoDimensions': true,
		'overlayShow'	: true,
		'centerOnScroll': true,
		'showCloseButton': true,
		ajax : {
		    type	: "POST",
		    data	: $(this).attr('href')
		}
	});
	function slideDepoimentos()
	{
		//se terminou
		if(n_ini == n_itens)
		{
			n_ini = 0;
		}
		
		$('#d_thumbs li').attr('class','hide');
		
		$('#d_thumbs li:eq('+ n_ini +')').attr('class','show');
		$('#d_thumbs li:eq('+ n_ini +') img').css({opacity: 0.0}).animate({opacity: 1.0}, 1000);
		$('#d_thumbs li:eq('+ n_ini +') cite').css({opacity: 0.0}).animate({opacity: 1.0}, 2000);	
		
		n_ini++;
	}
	//muda em 5 segundos
	function showDepoimentos()
	{
		//qtd itens
		n_itens = $('#d_thumbs li').size();
		n_ini = 1;
		setInterval( slideDepoimentos, 12000 );
	}
	
	// alterna classe <a> expansíveis (minhaconta)
	$('a').bind('click', function ()
	{
		if( $(this).attr('class') == 'subnav' )
		{
			var muda = $(this).attr('id');
			$(".subnav_open").attr('class', 'subnav');
			$(".subnav").attr('title', 'visualizar este item');
			$(".subnav_open").attr('title', '');
			
			$('#'+muda).attr('class', 'subnav_open');
			$('#'+muda).attr('title', '');
		}
    });
	
	// funções específicas 
	// p/ qdo carregar páginas 
	$(document).ready( function()
	{
		$('a.subnav').attr('title','visualizar este item');
		
		//se está logado
		if( $('ul').hasClass('conectou') ) 
		{
			var showtab = $('input#tab_secao').val();  
			if(showtab == 'end')
			{
				$_('c_show_cadastro').style.display = 'none';
				$_('c_show_pic').style.display = 'none';
				$_('c_show_dep').style.display = 'none';
				aparece('c_show_endereco');
				$('#tab_secao').val('end');
			}
			if(showtab == 'pic')
			{
				$_('c_show_cadastro').style.display = 'none';
				$_('c_show_endereco').style.display = 'none';
				$_('c_show_dep').style.display = 'none';
				aparece('c_show_pic');
				$('#tab_secao').val('pic');
			}
			if(showtab == 'dep')
			{
				$_('c_show_cadastro').style.display = 'none';
				$_('c_show_endereco').style.display = 'none';
				$_('c_show_pic').style.display = 'none';
				aparece('c_show_dep');
				$('#tab_secao').val('dep');
			}
			if(showtab == 'fin')
			{
				$_('c_show_conta').style.display = 'none';
				$_('c_show_fatura_h').style.display = 'none';
				aparece('c_show_fatura');
				$('#tab_secao').val('fin');
			}
			if(showtab == 'pag')
			{
				$_('c_show_fatura').style.display = 'none';
				$_('c_show_fatura_h').style.display = 'none';
				aparece('c_show_conta');
				$('#tab_secao').val('pag');
			}
			if(showtab == 'fin_h')
			{
				$_('c_show_conta').style.display = 'none';
				$_('c_show_fatura').style.display = 'none';
				aparece('c_show_fatura_h');
				$('#tab_secao').val('fin_h');
			}
			if(showtab == 'spO')
			{
				$_('c_show_close').style.display = 'none';
				aparece('c_show_open');
				$('#tab_secao').val('spO');
			}
			if(showtab == 'spC')
			{
				$_('c_show_open').style.display = 'none';
				aparece('c_show_close');
				$('#tab_secao').val('spC');
			}
		}
		
		//se ainda não cadastrou endereço
		if ($('span').hasClass('addEnd'))
		{
			$('.close_box').hide();
			$('#msg_no_end').attr('class','font_bold_red_medium');
			$('#msg_no_end').html('<img src="'+pathImagens()+'/icon_alert.gif" /> Por favor, é necessário que você cadastre um <strong>ENDEREÇO</strong> em sua conta.');
			
			$('a#addEndereco').fancybox({
				'scrolling'		: 'no',
				'autoDimensions': true,
				'overlayShow'	: true,
				'centerOnScroll': true,
				'modal'			: true,
				'hideOnOverlayClick': true,
				'enableEscapeButton': false,
				'showCloseButton': false //colocar link para fechar nas janelas
			});
			$('a#addEndereco').click();
		}
		
		//se passou id do item p/ contratar
		if( $('div').hasClass('presel') )
		{
			var item = $('div.presel').attr('id');
			switch(item)
			{
				case 'service':
					$('#getTipo_selecionados').val( 'web' );
					$('#getPlan option:first').val('nocheck');
					$('#getPlan option:first').attr('selected', 'selected');
					$('#getPack option:first').val('nocheck');
					$('#getPack option:first').attr('selected', 'selected');		
				break;
				case 'plan':
					$('#getTipo_selecionados').val( 'host' );
					$('#getService option:first').val('nocheck');
					$('#getService option:first').attr('selected', 'selected');
					$('#getPack option:first').val('nocheck');
					$('#getPack option:first').attr('selected', 'selected');		
				break;
				case 'pack':
					$('#getTipo_selecionados').val( 'pack' );
					$('#getService option:first').val('nocheck');
					$('#getService option:first').attr('selected', 'selected');
					$('#getPlan option:first').val('nocheck');
					$('#getPlan option:first').attr('selected', 'selected');		
				break;
			}
			$('#c_options').show();
			$('#'+item).show();	
		}
		
		// logout
		if( $('p').hasClass('logout') )
		{
			var item,exec;
			item = $('p.logout').attr('id');
			exec = stringSEG( $('#'+item).attr('rel') );
			$.post(exec,function(data)
			{
				if(data == 'ok')
				{
					$('ul.box_account').hide();
				}
			});
		}
		
		// logout adm
		$('a.logout').bind('click', function ()
		{
			var exec = stringSEG( $(this).attr('rel') );
			$.post(exec,function(data)
			{
				if(data == 'ok')
				{
					pageRedirect();
				}else{
					alert("Não foi possível desconectar!\n\nPor favor, tente novamente.");
				}
			});
		});
		
		//funções fatura
		$('a.sendInvoice').bind('click', function() 
		{
			var f = $(this).attr('rel').split('@@');
			
			$('a#sendFatura').fancybox({
				'scrolling'		: 'no',
				'autoDimensions': true,
				'overlayShow'	: true,
				'centerOnScroll': true,
				'hideOnOverlayClick': true,
				'enableEscapeButton': true,
				'showCloseButton': false, //colocar link para fechar nas janelas
				'onComplete'		: function(){
					$('#send_fatura').val(f[0]);
					$('#send_dominio').val(f[1]);
				},
				'onClosed'		: function(){
					//reseta form
					$('#send_fatura').val('');
					$('#send_dominio').val('');
					$('#send_msg').val('');
				}
			});
			
			$('a#sendFatura').click();
		});
		
		$('a.acaoInvoice').bind('click', function() 
		{
			var exec = $(this).attr('rel');
			//mostra mensagem para confirmar 
			var acao = confirm("Será executada a ação para esta fatura. Deseja continuar?")
			
			//confirma
			if(acao == true)
			{
				$('#load_fatura').html('AGUARDE...');
				
				$.post(exec, function(data)
				{
					if (data == 'ok_c') 
					{
						alert("Fatura confirmada com sucesso!");
						pageRedirect();
					}
					if (data == 'notok_c') 
					{
						alert("Não foi possível confirmar a fatura.\nPor favor, tente novamente");
					}
					if (data == 'ok_d') 
					{
						alert("Fatura cancelada com sucesso!");
						pageRedirect();
					}
					if (data == 'notok_d') 
					{
						alert("Não foi possível cancelar a fatura.\nPor favor, tente novamente");
					}
					
					$('#load_fatura').html('');
				});
			}
		});
		
		$('a.acaoTicket').bind('click', function() 
		{
			var bum = $(this).attr('rel').split('@@');
			
			//responder
			if(bum[2] == 'r')
			{
				//se não preencheu resposta
				if( $('#getR_'+bum[1]) == '' )
				{
					alert("Digite a resposta para o ticket.");
					campoNOTOK( $_('getR_'+bum[1]) );
					return false;
				}else{
					var rtxt = $('#getR_'+bum[1]).val();
					var exec = bum[0] +'?t='+ bum[1] +'&rtxt='+ rtxt +'&acao='+ bum[2];
					var msg = 'Enviando resposta, aguarde...';
				}
			}
			//encerrar
			if(bum[2] == 'e')
			{
				$('#getR_'+bum[1]).val('');
				desmostra('ticket_'+bum[1]);
				
				var exec = bum[0] +'?t='+ bum[1] +'&acao='+ bum[2];
				var msg = 'Encerrando ticket, aguarde...';
			}
						
			//mostra mensagem para confirmar 
			var acao = confirm("Executar esta ação para o ticket?")
			//confirma
			if(acao == true)
			{
				$('#load_'+bum[1]).html(msg);
				
				$.post(exec, function(data)
				{
					if (data == 'ok_r') 
					{
						alert("Ticket respondido com sucesso!");
						pageRedirect();
					}
					if (data == 'notok_r') 
					{
						alert("Não foi possível responder o ticket.\nPor favor, tente novamente");
					}
					if (data == 'ok_e') 
					{
						alert("Ticket encerrado com sucesso!");
						pageRedirect();
					}
					if (data == 'notok_e') 
					{
						alert("Não foi possível encerrar o ticket.\nPor favor, tente novamente");
					}
					
					$('#load_ticket').html('');
				});
			}
		});
	});
	
	// contratar serviços
	$('.checkServico').bind('click', function() 
	{
		resetDominio();
		$('#c_options').fadeIn();
		
		switch( $(this).val() )
		{
			case 'web':
				$('#plan').hide();
				$('#getPlan option:first').val('nocheck');
				$('#getPlan option:first').attr('selected', 'selected');

				$('#pack').hide();
				$('#getPack option:first').val('nocheck');
				$('#getPack option:first').attr('selected', 'selected');
				
				$('#other').hide();
				$('#info_dominio').show();
				$('.btn_form').show();
								
				//mostra opções
				$('#service').fadeIn();
				$('#getService option:first').val('');
			break;
			
			case 'host':
				$('#service').hide();
				$('#getService option:first').val('nocheck');
				$('#getService option:first').attr('selected', 'selected');

				$('#pack').hide();
				$('#getPack option:first').val('nocheck');
				$('#getPack option:first').attr('selected', 'selected');
				
				$('#other').hide();
				$('#info_dominio').show();
				$('.btn_form').show();
				
				//mostra opções
				$('#plan').fadeIn();
				$('#getPlan option:first').val('');
			break;
			
			case 'pack':
				$('#service').hide();
				$('#getService option:first').val('nocheck');
				$('#getService option:first').attr('selected', 'selected');

				$('#plan').hide();
				$('#getPlan option:first').val('nocheck');
				$('#getPlan option:first').attr('selected', 'selected');
				
				$('#other').hide();
				$('#info_dominio').show();
				$('.btn_form').show();
				
				//mostra opções
				$('#pack').fadeIn();
				$('#getPack option:first').val('');
			break;
			
			case 'other':
				$('#service').hide();
				$('#getService option:first').val('nocheck');
				$('#getService option:first').attr('selected', 'selected');

				$('#plan').hide();
				$('#getPlan option:first').val('nocheck');
				$('#getPlan option:first').attr('selected', 'selected');
				
				$('#pack').hide();
				$('#getPack option:first').val('nocheck');
				$('#getPack option:first').attr('selected', 'selected');
				
				$('#info_dominio').hide();
				$('.btn_form').hide();
				
				//mostra opções
				$('#other').fadeIn();
			break;
		}
    });
	
	// alteração de endereços (minhaconta)
	// verificar em minha_conta.ctrl
	$('a#endChange').live('click', function() 
	{
		var end = stringSEG( $(this).attr('rel') );
		bum = end.split('@@');
		
		// informa id p/ alterar
		$('#endereco_pk').val(bum[0]);
		// pré-seleciona opções
		$('#getTipoEnd').val(bum[1]);
		$('#getCEP').val(bum[2]);
		$('#getNumero').val(bum[3]);
		//se tem complemento
		if(bum[4] != 'vazio')
		{
			$('#getComplemento').val(bum[4]);
		}
		
		// oculta btn add
		$('#btn_addEnd').hide();
		// mostra btn change
		$('#btn_changeEnd').show();
		// zera load
		$('#load_endereco').html('');
		
		$('a#addEndereco').fancybox({
			'scrolling'		: 'no',
			'autoDimensions': true,
			'overlayShow'	: true,
			'centerOnScroll': true,
			'enableEscapeButton': false,
			'showCloseButton': false, //colocar link para fechar nas janelas
			'onClosed'		: function(){
				
				// mostra btn add
				$('#btn_addEnd').show();
				// oculta btn change
				$('#btn_changeEnd').hide();
		
				//reseta form endereço
				$('#endereco_pk').val('');
				$('#getTipoEnd').val('');
				$('#getCEP').val('');
				$('#getEstado').val('');
				$('#getCidade').val('');
				$('#getEndereco').val('');
				$('#getNumero').val('');
				$('#getBairro').val('');
				$('#getComplemento').val('');
			}
		});
		$('a#addEndereco').click();
		$('#getCEP').trigger('focus');
		$('#getCEP').trigger('blur');
    });
	// exclusão de endereço (minhaconta)
	// verificar em minha_conta.ctrl
	$('a.enddel').bind('click', function() 
	{
		var end = stringSEG( $(this).attr('rel') );
		bum = end.split('@@');
		// load
		setLoading('load_del_endereco','Excluindo endereço');
		
		$.post(bum[1], { endereco_pk: bum[0] },function(data)
		{
			if(data == 'ok')
			{
				alert("Endereço excluído com sucesso!");
				$('load_del_endereco').html('');
				redir = pageAtual()+'&v=end';
				pageRedirect(redir);
			}
			if(data == 'notok')
			{
				setMessage('load_del_endereco', 'Não foi possível EXCLUIR o endereço. Por favor, tente novamente.', 'notok', 'alert', '');
			}
			if(data == 'erro')
			{
				setMessage('load_del_endereco', 'ENDEREÇO não encontrado. Não é possível excluir.', 'notok', 'alert', '');
			}
			if(data == 'unico')
			{
				setMessage('load_del_endereco', 'Sua conta precisa de pelo menos um ENDEREÇO cadastrado. Não é possível excluir.', 'notok', 'alert', '');
			}
			if(data == '')
			{
				setMessage('load_del_endereco', 'Não foi possível executar a ação. Por favor, tente novamente.', 'notok', 'alert', '');
			}
		});
    });
	//################ fim DIVERSOS ################
});
