$(document).ready(function() {
    if ($.browser.msie) {
        $('body').addClass('ie');
    };

    $('a').click(function() {
		if(!($(this).hasClass('itemProduto')))
        $(this).blur()
    });

    $('a.disable').click(function() {
        return false;
    })

    $('h3.accor, .fix').ifixpng();

	/* Google Analytics */
//	$('.ga').click(function(){
//		if($(this).hasClass('cat') || $(this).hasClass('lang')){
//			pageTracker._trackPageview($(this).attr('rel') + $(this).html());
//		}
//		else {
//			pageTracker._trackPageview($(this).attr('rel'));
//		}
//	});
//	$('#navClose.pt a').click(function(){
//		pageTracker._trackPageview('/botao_ocultar_mostrar_menu');
	//});
	//$('#navClose.pt span').live("click", function(){
//		pageTracker._trackPageview('/botao_ocultar_mostrar_menu');
	//});

    // Deixa a div "left" com a mesma altura do conteúdo de "document"
    $('#left').css({ height: $(document).height() + 'px' });
    $(window).bind('resize', function() {
        $('#left').css({ height: $(document).height() + 'px' });
	});

    $('div#flashHome.pt').flash({ src: '/pt/swf/destaque.swf', width: 740, height: 330, wmode: 'transparent' }, { version: 8 });
    $('div#flashHome.es').flash({ src: '/es/swf/destaque.swf', width: 740, height: 330, wmode: 'transparent' }, { version: 8 });

    $('ul.navSection').superfish();

    $('a.itemProduto').click(function() {
        if ($(this).next().hasClass('visible')) {
            $(this).next().removeClass('visible').fadeOut();
        }
        else {
            $(this).next().addClass('show visible').hide().fadeIn('slow');
        }
    }).blur(function() {
       	$(this).next().removeClass('visible').fadeOut();
    })

    $('.boxPasso ul').jScrollPane({ scrollbarWidth: 15, scrollbarMargin: 0, showArrows: true });

    $('.scrollInner').jScrollPane({ scrollbarWidth: 15, scrollbarMargin: 0, showArrows: true }).mouseleave(function() {
        $(this).find('ul li').removeClass('selected');
    }).mouseover(function() {
        $(this).parents('.scrollInner').find('ul li').removeClass('selected');
        $(this).addClass('selected');
    }).find('a').click(function() {
        $(this).parents('.itemProduto').html($(this).html());
        $(this).parents('.boxProduto').removeClass('visible').fadeOut();
    });

    $('.hint').each(function() {
        $(this).val($(this).attr('title'));
    }).focus(function() {
        $title = $(this).attr('title');
        if ($(this).val() == $title) {
            $(this).val('');
        }
    }).blur(function() {
        $title = $(this).attr('title');
        if ($.trim($(this).val()) == '') {
            $(this).val($title);
        }
    }).parents('form').submit(function() {
        $title = $(this).attr('title');
        if ($.trim($(this).val()) == $title) {
            $(this).val('');
        }
    });

	$('.itemBusca:odd').addClass('white');

    $('.itemProduto').each(function() {
        $selected = $(this).next().find('.scrollInner ul li.selected a').html();

        if ($selected != null) {
            $(this).html($selected);
        }
    });

	$('#tableConsultor tr:last td').css({ borderBottom: 'none' });
	$('#tableConsultor tr:second td').css({ borderTop: 'none' });

	if($('#right').height() < $(window).height()){
		$dif = $(window).height() - $('#right').height();
		$('#footer').css({ marginTop: $dif + 'px' });
	};

    $('#selectTv, #selectDisplay, #selectCategorias').selectbox();

    $("#formDownloads select#selectCategorias").change(function() {
        $val = $(this).val();
        $("#formDownloads").submit();
    })

    // Galeria
    $('ul#navImage, .listNav ul').jcarousel({ scroll: 2 });

    carregaImagem($('ul#navImage li a:first').attr('href'), $('ul#navImage li a:first').find('img').attr('alt'));

    $('ul#navImage li a:first').addClass('hover').find('img').stop().fadeTo(0, 1.0);

    $('ul#navImage li a').hover(function() {
        $(this).addClass('over').find('img').stop().fadeTo(1000, 1.0);
        $('ul#navImage li a').each(function() {
            if (!($(this).hasClass('hover')) && !($(this).hasClass('over'))) {
                $(this).find('img').stop().fadeTo(1000, 0.7);
            }
        });
    }, function() {
        $('ul#navImage li a').removeClass('over');
        $('ul#navImage li a').each(function() {
            if (!($(this).hasClass('hover'))) {
                $(this).find('img').stop().fadeTo(1000, 0.7);
            }
        });
    });

    $('ul#navImage li a').click(function() {
        $('ul#navImage li a').removeClass('hover');
        $(this).addClass('hover').find('img').stop().fadeTo(1000, 1.0);
        $('ul#navImage li a').each(function() {
            if (!($(this).hasClass('hover'))) {
                $(this).find('img').stop().fadeTo(1000, 0.7);
            }
        });
        carregaImagem($(this).attr('href'), $(this).find('img').attr('alt'));
        return false;
    });
    // Fim da galeria

    // Animação do menu Lateral
    $('ul.navSection:eq(0) li a').mouseover(function() {
        if ($(this).parent().find('ul').length) {
            $('#panelNav').css({ overflow: 'visible' });
            $('ul.navSection:eq(1)').css({ display: 'none' });
            if (parseInt($('.panelNavInner').css('left')) < 0 && !($('#panelNav').hasClass('hover'))) {
                $('ul.navSection:eq(1)').css({ display: 'block' });
            }
        }
    });

    $('#btnNavPrev').mouseover(function() {
        $('#panelNav').css({ overflow: 'hidden' }).addClass('hover');
        $('.panelNavInner').stop();
        $(this).addClass('hover');
        $('.panelNavInner').animate({ left: '0' }, { duration: 600, easing: 'easeOutCirc' });
    });

    $('#btnNavHome').mouseover(function() {
        $('#panelNav').css({ overflow: 'hidden' }).removeClass('hover'); ;
        $('ul.navSection:eq(1)').css({ display: 'block' });
        $('.panelNavInner').stop();
        $('#btnNavPrev').removeClass('hover');
        $('.panelNavInner').animate({ left: '-193px' }, { duration: 600, easing: 'easeOutCirc' });
    });

    $('#nav').mouseleave(function() {
        $('#panelNav').css({ overflow: 'hidden' }).removeClass('hover'); ;
        $('ul.navSection:eq(1)').css({ display: 'block' });
        $('.panelNavInner').stop();
        $('#btnNavPrev').removeClass('hover');
        $('.panelNavInner').animate({ left: '-193px' }, { duration: 600, easing: 'easeOutCirc' });
    });
    // Fim da animação do menu Lateral

/*
    // Animação da barra lateral
    $('#navClose a').mouseover(function() {
        if ($(this).hasClass('close')) {
            $('#left').stop();
            $('#navClose').prepend('<span class="close"></span>').css({ zIndex: '40' });
            $('#left').animate({ left: '-180' }, { duration: 300, easing: 'easeInExpo' });
        }
    });

    $('#right').live('mouseover', function() {
        if ($('#navClose span').length) {
            $('#left').stop();
            $('#left').animate({ left: '-208' }, { duration: 200, easing: 'easeInExpo' });
            $('#navClose span').remove();
        }
    });

    $('#navClose span').live('click', function() {
        $('#left').animate({ left: '0' }, { duration: 1000, easing: 'easeOutExpo' });
        $(this).parent().find('a').removeClass('close');
        $('#navClose').css({ zIndex: '10' });
        $('#navClose span').remove();
    });

    $('#navClose a').click(function() {
        if ($(this).hasClass('close')) {
            $('#left').animate({ left: '0' }, { duration: 1000, easing: 'easeOutExpo' });
            $('#navClose').css({ zIndex: '10' });
            $('#navClose span').remove();
            $(this).removeClass('close');
        }
        else {
            $('#left').animate({ left: '-208px' }, { duration: 1000, easing: 'easeOutCirc' });
            $(this).addClass('close');
        }
        return false;
    });

    $('ul#navSection li a').mouseover(function() {
        $(this).parent().addClass('hover');
    });
    // Fim da animação da barra lateral

*/

    $('div#accordion h3').click(function() {
        $('div#accordion h3').removeClass('hover');
        $(this).parent().find('div.accor').slideUp('slow');
        if ($(this).next().is(':visible')) {
            $(this).removeClass('hover');
            $(this).next().slideUp('slow');
        } else {
            $(this).addClass('hover');
            $(this).next().slideDown('slow');
        }
    });

    $('#accordion h3:first').addClass('hover').next().addClass('show');

    $('#btnLanguage').click(function() {
        if ($('#boxFooter').is(':visible')) {
            $('#boxFooter').fadeOut();
        }
        else {
            $('#boxFooter').hide().fadeIn();
        }
        return false;
    });

    $('#footer').mouseleave(function() {
        $('#boxFooter').fadeOut();
    })

    $('.box144 ul li a').click(function() {
        $(this).addClass('hover');
    });

    if ($('.box602')){
        $tamBox = $('.box602').height();
        $tamTxt = $('.box602 p').height();
        $padding = parseInt(($tamBox - $tamTxt) / 2) + 'px';
        $('.box602 p').css({ paddingTop: $padding });
    };

	$('.box521.func').each(function(){
		$tamBox = $(this).height();
        $tamTxt = $(this).find('.txtFunc').height();
        $padding = parseInt(($tamBox - $tamTxt) / 2) + 'px';
        $(this).find('.txtFunc').css({ paddingTop: $padding });
	});

    // Landing page
    $('#selectConsultores').selectbox();
    $('#tblConsult tr, .imgTblBot').hide();
    $('.selectbox-wrapper ul li').click(function() {
        $selec = $('#selectConsultores option:selected').val();
        if ($selec == "") {
            $('#tblConsult tr, .imgTblBot').hide();
        } else {
            $('#tblConsult thead tr, .imgTblBot').show();
            $('#tblConsult tbody tr').hide();
            $('#tblConsult tbody tr.tr' + $selec).show();
            $('#tblConsult tbody tr.tr' + $selec + ' td span').hide().fadeIn();
        };
    });
});

function carregaImagem($imagemUrl, $imagemAlt){
	$('#imageAlvo').html('<img id="imgLoading" src="/img/loading.gif" alt="Carregando" />').animate({opacity: 1.0}, 1000, function(){
		$img = new Image();
		$($img).load(function(){
			$('#imageAlvo').html(this);
	   		$(this).css('display', 'none').fadeIn('slow');
	    }).error(function(){
	      	$('#imageAlvo').append('<p>Ocorreu um erro durante o carregamento da imagem.</p>');
	    }).attr('src', $imagemUrl).attr('alt', $imagemAlt).width('550px').height('400px');
	});
}