function mCustomScrollbars(){
	$("#mcs_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",5); 
	$("#mcs2_container").mCustomScrollbar("vertical",400,"easeOutCirc",1.05,"auto","yes","yes",5); 
}

function addCommas(nStr)
{
	nStr = nStr.replace('.', ',');
	nStr += '';
	x = nStr.split(',');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

$(window).load( function() {
	mCustomScrollbars();
	
	$('.slideshow').cycle({
		fx: 'fade'
	});
	
	$('.slideshow_texto').cycle({
		fx: 'fade'
	});
	
	$('#slideshow_menu').cycle({
		fx:      'fade',
		timeout:  3000,
		pager:   '#nav'
	});
	
	$('#black-content').cycle({
		fx: 'fade',
		timeout: 0
	});
	
	$('.leia_tambem_slide').cycle({
		fx: 'fade'
	});
});

$(document).ready(function() {
	
	var data;
	function getStuff() {
		$.getJSON("jsonStock.php", function(datax) {
			function update(_data) {
				$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20Name%2CPercentChange%2CLastTradePriceOnly%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(" + _data.stockList + ")%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=?", 
				function(json) {
					if (json.hasOwnProperty("query") && json.query.hasOwnProperty("results") && json.query.results.hasOwnProperty("quote"))
					{
						var i = 0;
						for (i = 0; i < 6; i++)
						{
							if (json.query.results.quote.length > i)
							{
								if (json.query.results.quote[i].hasOwnProperty("Name") && json.query.results.quote[i].hasOwnProperty("PercentChange") && json.query.results.quote[i].hasOwnProperty("LastTradePriceOnly"))
								{
									//$('#finance'+i).text(json.query.results.quote[i].Name + "   " + json.query.results.quote[i].PercentChange + "   " + json.query.results.quote[i].LastTradePriceOnly);
									
									//$('#finance'+i+' > span.nome').text(json.query.results.quote[i].Name);
									$('#finance'+i+' > span.nome').text(_data.nameStockList[i]);
									$('#finance'+i+' > span.logo_variacao').removeClass('pos');
									$('#finance'+i+' > span.logo_variacao').removeClass('neg');
									if (json.query.results.quote[i].PercentChange.substring(0,1) == "+")
									{
										$('#finance'+i+' > span.logo_variacao').addClass('pos');
										$('#finance'+i+' > span.logo_variacao').text(json.query.results.quote[i].PercentChange.substring(0,1));
									}
									if (json.query.results.quote[i].PercentChange.substring(0,1) == "-")
									{
										$('#finance'+i+' > span.logo_variacao').addClass('neg');
										$('#finance'+i+' > span.logo_variacao').text(json.query.results.quote[i].PercentChange.substring(0,1));
									}
									$('#finance'+i+' > span.variacao').text(addCommas(json.query.results.quote[i].PercentChange.substring(1)));
									$('#finance'+i+' > span.ultimopreco').text(addCommas(json.query.results.quote[i].LastTradePriceOnly));
								}
							}
						}
						/*$('#finance0').text(json.query.results.quote[0].Name + "   " + json.query.results.quote[0].Change);
						$('#finance1').text(json.query.results.quote[1].Name + "   " + json.query.results.quote[1].Change);
						$('#finance2').text(json.query.results.quote[2].Name + "   " + json.query.results.quote[2].Change);
						$('#finance3').text(json.query.results.quote[3].Name + "   " + json.query.results.quote[3].Change);
						$('#finance4').text(json.query.results.quote[4].Name + "   " + json.query.results.quote[4].Change);
						$('#finance5').text(json.query.results.quote[5].Name + "   " + json.query.results.quote[5].Change);*/
					}
				});
			}
			update(datax);	
		});
	}
	setInterval(getStuff, 10000);
	
	//padrao inicial
	$('#container_produtos').hide();
	$('#barra_produtos').hide();
	
	function inicialSelect() {
		menuSelector("inicial");
		$('#black-content').cycle(0);
		encolheRabiola();
	}
	
	$("#menu-inicial").click(function(){
		inicialSelect();
		return false;
	});
	
	$("#footer-inicial").click(function() {
		inicialSelect();
		return false;
	});
	
	function produtosSelect(produto) {
		menuSelector("produtos");
		$('#black-content').cycle(0);
		esticaRabiola(produto);
		
		if (produto === "amc") $('#amc_mais').click();
		if (produto === "lfrating") $('#lfrating_mais').click();
		if (produto === "ondeinvestir") $('#ondeinvestir_mais').click();
		if (produto === "riskbank") $('#riskbank_mais').click();
	}
	
	$("#menu-produtos").click(function(){
		produtosSelect("amc");
		topFocoProduto("produto");
		return false;
	});
	
	$("#footer-produtos").click(function() {
		produtosSelect("amc");
		topFocoProduto("produto");
		return false;
	});
	
	$("#menu-amc").click(function() {
		produtosSelect("amc");
		topFocoProduto("amc");
		return false;
	});
	
	$("#menu-lfrating").click(function() {
		produtosSelect("lfrating");
		topFocoProduto("lfrating");
		return false;	
	});
	
	$("#menu-ondeinvestir").click(function() {
		produtosSelect("ondeinvestir");
		topFocoProduto("ondeinvestir");
		return false;
	});
	
	$("#menu-riskbank").click(function() {
		produtosSelect("riskbank");
		topFocoProduto("riskbank");
		return false;
	});
	
	function quemsomosSelect() {
		menuSelector("quemsomos");
		$('#black-content').cycle(1);
		encolheRabiola();
	}
	
	$("#menu-quemsomos").click(function(){
		quemsomosSelect();
		return false;
	});
	
	$("#footer-quemsomos").click(function() {
		quemsomosSelect();
		return false;
	});
	
	function codigoeticaSelect() {
		menuSelector("codigoetica");
		$('#black-content').cycle(2);
		encolheRabiola();
	}
	
	$("#menu-codigoetica").click(function(){
		codigoeticaSelect();
		return false;
	});
	
	$("#footer-codigoetica").click(function() {
		codigoeticaSelect();
		return false;
	});
	
	function contatosSelect() {
		menuSelector("contatos");
		$('#black-content').cycle(3);
		encolheRabiola();
	}
	
	$("#menu-contatos").click(function(){
		contatosSelect();
		return false;
	});
	
	$("#footer-contatos").click(function() {
		contatosSelect();
		return false;
	});
	
	
	function menuSelector(selected) {
		$("#menu-inicial").removeClass("sprite-mi_inicial_sel");
		$("#menu-produtos").removeClass("sprite-mi_produtos_sel");
		$("#menu-quemsomos").removeClass("sprite-mi_quemsomos_sel");
		$("#menu-codigoetica").removeClass("sprite-mi_codigoetica_sel");
		$("#menu-contatos").removeClass("sprite-mi_contatos_sel");
		if (selected === "inicial") $("#menu-inicial").addClass("sprite-mi_inicial_sel");
		if (selected === "produtos") $("#menu-produtos").addClass("sprite-mi_produtos_sel");
		if (selected === "quemsomos") $("#menu-quemsomos").addClass("sprite-mi_quemsomos_sel");
		if (selected === "codigoetica") $("#menu-codigoetica").addClass("sprite-mi_codigoetica_sel");
		if (selected === "contatos") $("#menu-contatos").addClass("sprite-mi_contatos_sel");
		if (selected != "inicial") $("#menu-inicial").addClass("sprite-mi_inicial");
		if (selected != "produtos") $("#menu-produtos").addClass("sprite-mi_produtos");
		if (selected != "quemsomos") $("#menu-quemsomos").addClass("sprite-mi_quemsomos");
		if (selected != "codigoetica") $("#menu-codigoetica").addClass("sprite-mi_codigoetica");
		if (selected != "contatos") $("#menu-contatos").addClass("sprite-mi_contatos");
	}
	
	function esticaRabiola() {
		$('#main').animate({height: "1825px"}, "slow");
		$('#footer').animate({top: "1730px"}, "slow");
		$('#logos').animate({top: "1778px"}, "slow");
		$('#container_produtos').show("slow");
		$('#barra_produtos').show("slow");
		$('html,body').animate({scrollTop: $("#container_produtos").offset().top}, "slow");
	}
	/*
	function esticaRabiola() {
		$('#main').animate({height: "1825px"}, "slow");
		$('#footer').animate({top: "1730px"}, "slow");
		$('#logos').animate({top: "1778px"}, "slow");
		$('#container_produtos').show("slow");
		$('#barra_produtos').show("slow");
		$('html,body').animate({scrollTop: $("#container_produtos").offset().top}, "slow");
	}*/
	
	function topFocoProduto(produto) {
		if (produto === "produto") $('html,body').animate({scrollTop: $("#barra_produtos").offset().top}, "slow");
		if (produto === "amc") $('html,body').animate({scrollTop: $("#produto_amc").offset().top}, "slow");
		if (produto === "lfrating") $('html,body').animate({scrollTop: $("#produto_lfrating").offset().top}, "slow");
		if (produto === "ondeinvestir") $('html,body').animate({scrollTop: $("#produto_ondeinvestir").offset().top}, "slow");
		if (produto === "riskbank") $('html,body').animate({scrollTop: $("#produto_riskbank").offset().top}, "slow");
	}
	
	function encolheRabiola() {
		$('#barra_produtos').hide("slow");
		$('#container_produtos').hide("slow");
		$('#main').animate({height: "800px"}, "slow");
		$('#footer').animate({top: "701px"}, "slow");
		$('#logos').animate({top: "749px"}, "slow");
	}
	
	$("#lbemail").focusin(function() { 		if($("#lbemail").val()=="e-mail") { $("#lbemail").val(""); } 	});
	$("#lbemail").focusout(function() { 		if($("#lbemail").val()=="") { $("#lbemail").val("e-mail"); } 	});
	
	$("#lemail").focusin(function() { 		if($("#lemail").val()=="e-mail") { $("#lemail").val(""); } 	});
	$("#lemail").focusout(function() { 		if($("#lemail").val()=="") { $("#lemail").val("e-mail"); } 	});
	
	
	//$("#lbsenha").focusin(function() { 		if($("#lbsenha").val()=="!@#$%54321*") { $("#lbsenha").val(""); } 	});
	//$("#lbsenha").focusout(function() { 		if($("#lbsenha").val()=="") { $("#lbsenha").val("!@#$%54321*"); } 	});
	
	$("#lbsenha").focusin(function() { 		if($("#lbsenha").val()=="") { $("#lbsenha").val(""); } 	});
	$("#lbsenha").focusout(function() { 		if($("#lbsenha").val()=="") { $("#lbsenha").val(""); } 	});
	
	//$("#lsenha").focusin(function() { 		if($("#lsenha").val()=="!@#$%54321*") { $("#lsenha").val(""); } 	});
	//$("#lsenha").focusout(function() { 		if($("#lsenha").val()=="") { $("#lsenha").val("!@#$%54321*"); } 	});
	
	$("#lsenha").focusin(function() { 		if($("#lsenha").val()=="") { $("#lsenha").val(""); } 	});
	$("#lsenha").focusout(function() { 		if($("#lsenha").val()=="") { $("#lsenha").val(""); } 	});
	
	$("#A0").click(function() {
		inicialSelect();
		showContent("0");
		hideContent("1");
		hideContent("2");
		hideContent("3");
		hideContent("4");
		reloadScrollbar();
	});
	
	$("#A1").click(function() {
		inicialSelect();
		showContent("1");
		hideContent("0");
		hideContent("2");
		hideContent("3");
		hideContent("4");
		reloadScrollbar();
	});
	
	$("#A2").click(function() {
		showContent("2");
		hideContent("0");
		hideContent("1");
		hideContent("3");
		hideContent("4");
		reloadScrollbar();
	});
	
	$("#A3").click(function() {
		inicialSelect();
		showContent("3");
		hideContent("0");
		hideContent("1");
		hideContent("2");
		hideContent("4");
		reloadScrollbar();
	});
	
	$("#A4").click(function() {
		inicialSelect();
		showContent("4");
		hideContent("0");
		hideContent("1");
		hideContent("2");
		hideContent("3");
		reloadScrollbar();
	});
	
	
	$("#lfnews0").click(function() {
		inicialSelect();
		showContent("0");
		hideContent("1");
		hideContent("2");
		hideContent("3");
		hideContent("4");
		reloadScrollbar();
	});
	
	$("#lfnews1").click(function() {
		inicialSelect();
		showContent("1");
		hideContent("0");
		hideContent("2");
		hideContent("3");
		hideContent("4");
		reloadScrollbar();
	});
	
	$("#lfnews2").click(function() {
		inicialSelect();
		showContent("2");
		hideContent("0");
		hideContent("1");
		hideContent("3");
		hideContent("4");
		reloadScrollbar();
	});
	
	$("#lfnews3").click(function() {
		inicialSelect();
		showContent("3");
		hideContent("0");
		hideContent("1");
		hideContent("2");
		hideContent("4");
		reloadScrollbar();
	});
	
});

function reloadScrollbar()
{
	$(".dragger").css("top","0px");
	$(".container").css("top","0px");
}

function showContent(index)
{
	$("#H"+index).show();
	$("#noticia"+index).show();
	$("#IMG"+index).show();
	$("#H2"+index).show();
}

function hideContent(index)
{
	$("#H"+index).hide();
	$("#noticia"+index).hide();
	$("#IMG"+index).hide();
	$("#H2"+index).hide();
}


function hideProdutos()
{
	$('#main').css("height","800px");
	$('#footer').css("top","701px");
	$('#logos').css("top","749px");
	$('#container_produtos').html("");     
	$('#container_produtos').fadeOut('slow');  
	$('#barra_produtos').hide();
}

/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function(){
	if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
		return this.elem[ this.prop ];
	}
	var r = parseFloat( jQuery.css( this.elem, this.prop ) );
	return typeof r == 'undefined' ? 0 : r;
}

