/*
 * Desenvolvido por: Webee E-marketing e Eduardo Bertolucci
 * Data da Criação: 12/01/2010
 * Copyright (c) 2010 Webee E-marketing (http://www.webee.com.br)
 * Copyright (c) 2010 Eduardo Bertolucci (http://www.eduardobertolucci.com)
 */

// Cufon
// Fontes Personalizadas - Helvetica Neue
Cufon.replace("h2, .accordion-cabecalho", { fontFamily: "Helvetica Neue"})

// Fontes Personalizadas - Sun
Cufon.replace("h3,h4,h5,p,label,a span,.cufon,legend,.lista-contato li,.lista-formulario-1 strong,.lista-itens,.lista-faq,.results div", { fontFamily: "Sun"})


// Fontes Personalizadas - Sun - Com efeito hover
Cufon.replace(".link-contato,.lista-busque-tambem li a,.lista-busque-tambem-facas li a,.lista-busque-tambem-imagens li a,.lista-busque-tambem-2 li a,.quero-me-cadastrar,.esqueci-a-senha,#link-agenda,#link-newsletter,.lista-itens-agenda-newsletter li a,#logado p a,.lista-resultado-da-busca li a,.hover-link,.lista-software-ftp li span,.lista-tutoriais span,.lista-cidade-mapa li a,.lista-nome-cidades li a,.box-paginacao strong,.box-paginacao a", { fontFamily: "Sun" , hover: true })

// Fontes Personalizadas - Frutiger
Cufon.replace(".link-historio,#frase,.mapa-nome-ul,.mapa-titulo,ul.nivel-2 li strong,.lista-vendedores a, .historico-compras ul li div a", { fontFamily: "Frutiger" })

// Fontes Personalizadas - Frutiger - Com efeito hover
Cufon.replace("#links-cadastro li a,#menu-rodape,#links-atendimento li a,.o-menu li a,#menu-midiograf li ul a,#endereco a,#btn-atendimento span,#btn-cadastro span, #pag-comercio .produtos-personale li strong, #corpo .divNomeCliente span, #corpo .divNomeCliente strong, .box-dados strong, .frutiger", { fontFamily: "Frutiger" , hover: true })

// inicio jQuery
jQuery.noConflict()
jQuery(document).ready(function() {	
	if(jQuery.browser.version == 7.0){	
		jQuery(function() {
			var zIndexNumber = 1000;
			jQuery('div').each(function() {
				jQuery(this).css('zIndex', zIndexNumber)
				zIndexNumber -= 10
			})
		})
	}								
	//MENU ATENDIMENTO - OVER
	jQuery("#menu-atendimento").mouseover(function(){
		jQuery("#btn-atendimento").stop().animate({"top":"0"},{queue: false, duration: 200, easing: "easeOutQuart"})
		jQuery("#links-atendimento").stop().animate({"top":"0"},{queue: false, duration: 200, easing: "easeOutQuart"})
	})
	
	//MENU ATENDIMENTO - REMOVER
	jQuery("#menu-atendimento").mouseout(function(){
		jQuery("#btn-atendimento").stop().animate({"top":"-114px"},{queue: false, duration: 200, easing: "easeOutQuart"})
		jQuery("#links-atendimento").stop().animate({"top":"-140px"},{queue: false, duration: 200, easing: "easeOutQuart"})
	})
	
	//MENU CADASTRO - OVER
	jQuery("#menu-cadastro").mouseover(function(){
		jQuery("#btn-cadastro").stop().animate({"top":"0"},{queue: false, duration: 200, easing: "easeOutQuart"})
		jQuery("#links-cadastro").stop().animate({"top":"0"},{queue: false, duration: 200, easing: "easeOutQuart"})
	})
	
	//MENU CADASTRO - REMOVER
	jQuery("#menu-cadastro").mouseout(function(){
		jQuery("#btn-cadastro").stop().animate({"top":"-114px"},{queue: false, duration: 200, easing: "easeOutQuart"})
		jQuery("#links-cadastro").stop().animate({"top":"-140px"},{queue: false, duration: 200, easing: "easeOutQuart"})
	})
		
	//MENU ATENDIMENTO - btn-radar
	jQuery("#btn-radar").mouseover(function(){
			jQuery("div.imagem").addClass("btn-hover-radar")
		}
	).mouseout(function(){
			jQuery("div.imagem").removeClass("btn-hover-radar")
		}
	)

	//MENU ATENDIMENTO - btn-vendas
	jQuery("#btn-vendas").mouseover(function(){
			jQuery("div.imagem").addClass("btn-hover-vendas")
		}
	).mouseout(function(){
			jQuery("div.imagem").removeClass("btn-hover-vendas")
		}
	)

	//MENU ATENDIMENTO - btn-orcamento
	jQuery("#btn-orcamento").mouseover(function(){
			jQuery("div.imagem").addClass("btn-hover-orcamento")
		}
	).mouseout(function(){
			jQuery("div.imagem").removeClass("btn-hover-orcamento")
		}
	)
		
	//MENU ATENDIMENTO - btn-faq
	jQuery("#btn-faq").mouseover(function(){
			jQuery("div.imagem").addClass("btn-hover-faq")
		}
	).mouseout(function(){
			jQuery("div.imagem").removeClass("btn-hover-faq")
		}
	)
	
	//MENU ATENDIMENTO - btn-endereco
	jQuery("#btn-enderecos").mouseover(function(){
			jQuery("div.imagem").addClass("btn-hover-enderecos")
		}
	).mouseout(function(){
			jQuery("div.imagem").removeClass("btn-hover-enderecos")
		}
	)

	//MENU ATENDIMENTO - fale-conosco
	jQuery("#btn-fale-conosco").mouseover(function(){
			jQuery("div.imagem").addClass("btn-hover-fale-conosco")
		}
	).mouseout(function(){
			jQuery("div.imagem").removeClass("btn-hover-fale-conosco")
		}
	)
	
	//MENU ATENDIMENTO - fale-conosco
	jQuery("#btn-consultor-online").mouseover(function(){
			jQuery("div.imagem").addClass("btn-hover-consultor-online")
		}
	).mouseout(function(){
			jQuery("div.imagem").removeClass("btn-hover-consultor-online")
		}
	)
	
	//MENU CADASTRO - vantagens
	jQuery("#btn-vantagens").mouseover(function(){
			jQuery("div.imagem-2").addClass("btn-hover-vantagens")
		}
	).mouseout(function(){
			jQuery("div.imagem-2").removeClass("btn-hover-vantagens")
		}
	)
	
	//MENU CADASTRO - cadastre-se
	jQuery("#btn-cadastre-se").mouseover(function(){
			jQuery("div.imagem-2").addClass("btn-hover-cadastre-se")
		}
	).mouseout(function(){
		jQuery("div.imagem-2").removeClass("btn-hover-cadastre-se")
		}
	)

	// ACCORDION
	jQuery('.accordion').accordion({
			header: '.accordion-cabecalho',
			active: false,
			autoheight: false,
			alwaysOpen: false,
			animated: 'easeslide'
		}
	)
	
	// PÁGINA - PERSONALE - MINHA CONTA - LOGADO
	// Clica/Mostra
	jQuery("#link-meus-dados").click(function (){
		jQuery("#info-meus-dados").slideToggle("low")
		jQuery("#info-minhas-preferencias").hide("low")
		jQuery("#info-meu-historio").hide("low")
		jQuery("#info-meu-contato").hide("low")
	})
	jQuery("#link-minhas-preferencias").click(function (){
		jQuery("#info-meus-dados").hide("low")
		jQuery("#info-minhas-preferencias").slideToggle("low")
		jQuery("#info-meu-historio").hide("low")
		jQuery("#info-meu-contato").hide("low")
	})
	jQuery("#link-meu-historio").click(function (){
		jQuery("#info-meus-dados").hide("low")
		jQuery("#info-minhas-preferencias").hide("low")
		jQuery("#info-meu-historio").slideToggle("low")
		jQuery("#info-meu-contato").hide("low")
	})
	jQuery("#link-meu-contato").click(function (){
		jQuery("#info-meus-dados").hide("low")
		jQuery("#info-minhas-preferencias").hide("low")
		jQuery("#info-meu-historio").hide("low")
		jQuery("#info-meu-contato").slideToggle("low")
	})
	
	// PÁGINA - MIDIOGRAF - MENU
	jQuery(".o-menu li.submenu").each(function(){
		var el = jQuery("#" + jQuery(this).attr("id") + " ul:eq(0)")
		 
		jQuery(this).hover(function(){ 
			el.fadeIn(200)
		}, function(){ 
			el.fadeOut(100)
		})
	})
	
	// Enchimento top e Bottom para a primeira e última LI
	jQuery(".o-menu ul:eq(0) li:first").addClass("first")
	jQuery(".o-menu ul:eq(0) li:last").addClass("last")	
	jQuery(".o-menu ul:eq(1) li:first").addClass("first")
	jQuery(".o-menu ul:eq(1) li:last").addClass("last")
	jQuery(".o-menu ul:eq(2) li:first").addClass("first")
	jQuery(".o-menu ul:eq(2) li:last").addClass("last")	
	jQuery(".o-menu ul:eq(3) li:first").addClass("first")
	jQuery(".o-menu ul:eq(3) li:last").addClass("last")
	
	// PÁGINA - MIDIOGRAF - AGENDA/ACONTECE
	jQuery("#link-agenda").click(function (){
		jQuery("#conteudo-agenda").slideToggle("low")
		jQuery("#conteudo-newsletter").hide("low")
	})
	jQuery("#link-newsletter").click(function (){
		jQuery("#conteudo-newsletter").slideToggle("low")
		jQuery("#conteudo-agenda").hide("low")
		jQuery("#conteudo-newsletter").css("visibility","visible")
	})
	
	// jScrollPane / Barra de Rolagem
	jQuery("#conteudo-rolagem").jScrollPane({
		showArrows: true,
		scrollbarWidth: 17,
		dragMaxHeight : 20,
		reinitialiseOnImageLoad: true
	})
	
	// Campo File
	jQuery("input#txt_curriculo, input#txt_referencia").filestyle({
		image: "img/ico-btn-lupa.gif",
		imagewidth : 18,
		imageheight : 22,
		width : 264
	})
	
	// Link Externo
	jQuery('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	})
	
	// Banners Midio Store
	jQuery("#mycarousel").jcarousel({		
		scroll: 1
	})
	
	//Vertical-menu - Comercio
	makeScrollable("div.sc_menu_wrapper", "div.sc_menu");
})
// fim jQuery

// Thickbox e Flash
function showLink(src) {
	//criando um link falso, para o LightBox captar as informações
	var a_false = document.createElement("A")
	a_false.className = "thickbox"
	a_false.href = src + "?TB_iframe=true&amp;modal=true"
	
	//chamando a função do lightbox responsável pelo início das ações
	tb_show(a_false.className,a_false.href)
	
	//anulando o link
	a_false = null
}

function abrir(pagina, largura, altura) {
	// Definindo meio da tela
	var esquerda = (screen.width - largura)/2;
	var topo = (screen.height - altura)/2;
	
	// Abre a nova janela
	window.open(pagina,'','height=' + altura + ', width=' + largura + ', top=' + topo + ', left=' + esquerda);
}

//Vertical-menu - Comercio
function makeScrollable(wrapper, scrollable){

	// Get jQuery elements
	var wrapper = jQuery(wrapper), scrollable = jQuery(scrollable);

	// Hide images until they are not loaded
	scrollable.hide();
	var loading = jQuery('<div class="loading">Loading...</div>').appendTo(wrapper);

	// Set function that will check if all images are loaded
	var interval = setInterval(function(){
		var images = scrollable.find('.box-foto img');
		var completed = 0;

		// Counts number of images that are succesfully loaded
		images.each(function(){
			if (this.complete) completed++;	
		});

		if (completed == images.length){
			clearInterval(interval);

			// Timeout added to fix problem with Chrome
			setTimeout(function(){
				loading.hide();

				// Remove scrollbars
				wrapper.css({overflow: 'hidden'});
				scrollable.slideDown('slow', function(){
					enable()
				});
			}, 100);
		}
	}, 100);

	function enable(){
		// height of area at the top at bottom, that don't respond to mousemove
		var inactiveMargin = 99;					

		// Cache for performance
		var wrapperWidth = wrapper.width();
		var wrapperHeight = wrapper.height();

		// Using outer height to include padding too
		var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;

		// Do not cache wrapperOffset, because it can change when user resizes window

		// We could use onresize event, but it's just not worth doing that 

		// var wrapperOffset = wrapper.offset();

		// Create a invisible tooltip
		var tooltip = jQuery('<div class="sc_menu_tooltip"></div>')
			.css('opacity', 0)
			.appendTo(wrapper);

		// Save menu titles
		scrollable.find('a').each(function(){				
			jQuery(this).data('tooltipText', this.title);
		});

		// Remove default tooltip
		scrollable.find('a').removeAttr('title');		

		// Remove default tooltip in IE
		scrollable.find('img').removeAttr('alt');

		var lastTarget;

		//When user move mouse over menu
		wrapper.mousemove(function(e){
			// Save target
			lastTarget = e.target;
			var wrapperOffset = wrapper.offset();
			var tooltipLeft = e.pageX - wrapperOffset.left;

			// Do not let tooltip to move out of menu.

			// Because overflow is set to hidden, we will not be able too see it 

			tooltipLeft = Math.min(tooltipLeft, wrapperWidth - 75); //tooltip.outerWidth());
			var tooltipTop = e.pageY - wrapperOffset.top + wrapper.scrollTop() - 40;

			// Move tooltip under the mouse when we are in the higher part of the menu
			if (e.pageY - wrapperOffset.top < wrapperHeight/2){
				tooltipTop += 80;
			}

			tooltip.css({top: tooltipTop, left: tooltipLeft});				

			// Scroll menu
			var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;

			if (top < 0){
				top = 0;
			}			

			wrapper.scrollTop(top);
		});

		// Setting interval helps solving perfomance problems in IE
		var interval = setInterval(function(){
			if (!lastTarget) return;
			var currentText = tooltip.text();
			if (lastTarget.nodeName == 'IMG'){					

				// We've attached data to a link, not image
				var newText = jQuery(lastTarget).parent().data('tooltipText');

				// Show tooltip with the new text
				if (currentText != newText) {
					tooltip
						.stop(true)
						.css('opacity', 0)	
						.text(newText)
						.animate({opacity: 1}, 1000);
				}
			}
		}, 200);

		// Hide tooltip when leaving menu
		wrapper.mouseleave(function(){
			lastTarget = false;
			tooltip.stop(true).css('opacity', 0).text('');
		});
	}
}
