function rotator() {
	// Tira opacidade de todas as fotos e mostra a primeira
	$('#butter_top_rot ul li').css({
		opacity : 0.0
	});
	$('#butter_top_rot ul li:first').css({
		opacity : 1.0
	});
	// Chama o rotate
	setInterval('rotate()', 10000);
}

function rotate() {
	// Pega primeira imagem
	var current = ($('#butter_top_rot ul li.show') ? $('#butter_top_rot ul li.show')
			: $('#butter_top_rot ul li:first'));
	if (current.length == 0)
		current = $('#butter_top_rot ul li:first');
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#butter_top_rot ul li:first')
			: current.next())
			: $('#butter_top_rot ul li:first'));
	next.css({
		opacity : 0.0
	}).addClass('show').animate({
		opacity : 1.0
	}, 2000);
	current.animate({
		opacity : 0.0
	}, 2000).removeClass('show');
};

function rotate_anterior() {
	// Pega anterior
	var current = ($('#butter_top_rot ul li.show') ? $('#butter_top_rot ul li.show')
			: $('#butter_top_rot ul li:last'));
	if (current.length == 0)
		current = $('#butter_top_rot ul li:last');
	var next = ((current.prev().length) ? ((current.prev().hasClass('show')) ? $('#butter_top_rot ul li:last')
			: current.prev())
			: $('#butter_top_rot ul li:last'));
	next.css({
		opacity : 0.0
	}).addClass('show').animate({
		opacity : 1.0
	}, 0);
	current.animate({
		opacity : 0.0
	}, 0).removeClass('show');
};
function rotate_proximo() {
	// Pega anterior
	var current = ($('#butter_top_rot ul li.show') ? $('#butter_top_rot ul li.show')
			: $('#butter_top_rot ul li:first'));
	if (current.length == 0)
		current = $('#butter_top_rot ul li:first');
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#butter_top_rot ul li:first')
			: current.next())
			: $('#butter_top_rot ul li:first'));
	next.css({
		opacity : 0.0
	}).addClass('show').animate({
		opacity : 1.0
	}, 0);
	current.animate({
		opacity : 0.0
	}, 0).removeClass('show');
};

$(document).ready(
		function() {
			$("a[rel^='prettyVideo']").prettyPhoto({
				animationSpeed : 'slow',
				hideflash: true,
			});
			$(".gallery a[rel^='prettyPhoto']").prettyPhoto({
				animationSpeed : 'slow',
				theme : 'light_square',
				slideshow : 2000,
				hideflash: true,
				autoplay_slideshow : true
			});
			$("a[rel^='prettyMovie']").prettyPhoto({
				animationSpeed : 'slow',
				hideflash: true
			});
			$(".gallery a[rel^='prettyPhoto']").prettyPhoto({
				animationSpeed : 'fast',
				hideflash: true,
				slideshow : 10000
			});
			$("a[rel^='prettyPhotoMenu']").prettyPhoto({
				animationSpeed : 'fast',
				hideflash: true,
				slideshow : 10000
			});
			
			// Search
			$("#search_query").wrap("<span id='header_search_wrap' />");
			$("#search_query").focusin(function() {
				$("#search_query").animate({
					color : '#ffffff'
				}, 200, function() {
					$("#search_query").val('').animate({
						color : '#777777'
					}, 0);
				});
			});
			// Car Tab
			$("#header_car_carros").children('a').corner();
			$("#logo_front_carbel").click(function() {
				location.href = "/";
			});
			var gor = 0;
			$("#header_car_right").mouseover(function() {
				$("#header_car_right").data('loop', true).loopright();
			});
			$("#header_car_right").mouseout(function() {
				$("#header_car_right").data('loop', false);
			});
			$("#header_car_left").mouseover(function() {
				$("#header_car_left").data('loop', true).loopleft();
			});
			$("#header_car_left").mouseout(function() {
				$("#header_car_left").data('loop', false);
			});
			while ($('#header_car_carros').height() > 25) {
				$('#header_car_carros').width(
						$('#header_car_carros').width() + 5);
			}

			var gor = 0;
			var gol = $('#header_car_carros').width() - 895;
			$('#header_car_carros').css('height', '25px');
			$('#header_car_carros').css('width', gol + 895 + 'px');
			$.fn.loopright = function() {
				if ($("#header_car_right").data('loop') == true) {
					if (gor > -(gol - 40)) {
						gor = (gor - 40);
						$('#header_car_carros')
								.animate(
										{
											left : gor
										},
										300,
										undefined,
										function() {
											if ($("#header_car_right").data(
													'loop') == true)
												$("#header_car_right").data(
														'loop', true)
														.loopright();
										});
					} else {
						$("#header_car_right").data('loop', false).stop();
					}
				}
			};

			$.fn.loopleft = function() {
				if ($("#header_car_left").data('loop') == true) {
					if (gor < 0) {
						gor = (gor + 40);
						$('#header_car_carros')
								.animate(
										{
											left : gor
										},
										300,
										undefined,
										function() {
											if ($("#header_car_left").data(
													'loop') == true)
												$("#header_car_left").data(
														'loop', true)
														.loopleft();
										});
					} else {
						$("#header_car_left").data('loop', false).stop();
					}
				}
			};
			// menu
			if ($.browser.msie) {
				if ($.browser.version == '8.0') {
					$('#butter_bot_menu ul li ul').css('top', '-200px');
				}
			}

			$("#butter_bot_menu > ul > li > ul").corner();
			$('#butter_top_menu > ul > li:last').css('border', 'none');
			$('#butter_bot_menu > ul > li > ul > li:last')
					.css('border', 'none');
			$('#butter_top_menu > ul > li').mouseenter(function() {
				$(this).clearQueue();
				$(this).animate({
					backgroundPosition : '8px 5px'
				}, 300);
				$(this).children('a').css("text-decoration", "underline");
			});
			$('#butter_top_menu > ul > li').mouseleave(function() {
				$(this).animate({
					backgroundPosition : '0px 5px'
				}, 200);
				$(this).children('a').css("text-decoration", "none");
			});

			$('#butter_bot_menu > ul > li:last').css('border', 'none');
			$('#butter_bot_menu > ul > li').mouseenter(function() {
				$(this).clearQueue();
				$(this).animate({
					backgroundPosition : '8px 0px'
				}, 300);
				$(this).children('a').css("text-decoration", "underline");
				if ($(this).children('ul').length != 0) {
					$(this).children('ul').css('display', 'block');
					$(this).css('background-color', '#FFFFFF');
				}
			});
			$('#butter_bot_menu > ul > li').mouseleave(function() {
				$(this).animate({
					backgroundPosition : '0px 0px'
				}, 200);
				$(this).children('a').css("text-decoration", "none");
				if ($(this).children('ul').length != 0) {
					$(this).children('ul').css('display', 'none');
					$(this).css('background-color', 'transparent');
				}
			});
			$('#butter_bot_menu > ul > li > ul > li:last')
					.css('border', 'none');
			$('#butter_bot_menu > ul > li > ul > li').mouseenter(function() {
				$(this).clearQueue();
				$(this).animate({
					backgroundPosition : '8px 0px'
				}, 300);
				$(this).children('a').css("text-decoration", "underline");
			});
			$('#butter_bot_menu > ul > li > ul > li').mouseleave(function() {
				$(this).animate({
					backgroundPosition : '0px 0px'
				}, 200);
				$(this).children('a').css("text-decoration", "none");
			});

			$(".anuncio_carro").corner();
			$(".anuncio_mais").corner();
			$(".anuncio_carro > img").corner();
			$('.noticia:last').css('border', 'none');
			$(".botao_detalhe").click(function() {
				location.href = '/veiculos-seminovos/index/interna/' + $(this).attr('name');
				});
			$('.menu_carro').hide();
			$("#header_car_carros > a").mouseover(function() {
				text = $(this).text().toLowerCase();
				text = text.replace(/ /g, "");
				var n = $('#menu_carro_' + text).queue("fx");
				n = n.length;
				$.each($(".menu_carro"), function(i, v) {
					j = $(v).queue("fx");
					j = j.length;
					$(v).clearQueue();
					$(v).hide();
					$(v).animate({
						left : 0
					}, 0);
				});
				if (n < 2)
					$('#menu_carro_' + text).show().animate({
						opacity : 0
					}, 0).animate({
						opacity : 1,
						left : 490
					}, 300);

			});
			$("#header_car_carros > a").mouseout(function() {
				text = $(this).text().toLowerCase();
				text = text.replace(/ /g, "");
				$('#menu_carro_' + text).animate({
					opacity : 0,
					left : 900
				}, 100).animate({
					left : 0
				}, 0);
				;
			});
			$('.blue_item:gt(3)').animate({
				opacity : 0
			}, 0);

			var bluecur = 0;
			$("#blue_prev").click(function() {
				if (bluecur == 0) {
				} else {
					$('.blue_item:eq(' + (bluecur + 3) + ')').animate({
						opacity : 0.00
					});
					$('.blue_item').animate({
						left : -(237 * (bluecur - 1))
					});
					$('.blue_item:eq(' + (bluecur - 1) + ')').animate({
						opacity : 1.00
					});

					bluecur--;
				}
			});

			var quantos = $('.blue_item').size();
			$("#blue_next").click(function() {
				if (bluecur == quantos - 4) {

				} else {
					$('.blue_item:eq(' + bluecur + ')').animate({
						opacity : 0.00
					});
					$('.blue_item').animate({
						left : -(237 * (bluecur + 1))
					});
					$('.blue_item:eq(' + (bluecur + 4) + ')').animate({
						opacity : 1.00
					});
					bluecur++;
				}
			});

			// Rotator
			rotator();
			$('div.butter_top_rot').fadeIn(1000);
			$('div.butter_top_rot ul li').fadeIn(1000); // tweek for IE

			$("#rotator_left").click(function() {
				rotate_anterior();
			});
			$("#rotator_right").click(function() {
				rotate_proximo();
			});
			$("#white_left .noticia").mouseover(
					function() {
						$(this).children('.ler_mais').fadeIn(1);
						$(this).children('.titulo').children('a').addClass(
								'titulo_destacado');
					});
			$("#white_left .noticia").mouseout(
					function() {
						$(this).children('.ler_mais').fadeOut(1);
						$(this).children('.titulo').children('a').removeClass(
								'titulo_destacado');
					});

			// Posicionamento do banner
			$('.blue_image').mouseover(function(e) {
				
				$('.flutuantes').css('display', 'none');
				nomeContainer = $(this).attr('name');
				viewportX = $(window).width();
				scrollX = $(document).scrollTop();
				posicao = $('#butter_blue_bar').position();
				alturaMenu = $(this).parent().height();
				larguraMenu = $(this).parent().width();
				larguraBanner = $('#' + nomeContainer).width();
				alturaBanner = $('#' + nomeContainer).height();
				offsetMenu = $(this).parent().offset();
				menuAbsolutoY = offsetMenu.left;
				menuAbsolutoX = offsetMenu.top;
				
				posicaoX = e.pageX;
				if (posicaoX < (viewportX / 2)) {
					posicaoXBanner = menuAbsolutoY + 'px';
				} else {
					posicaoXBanner = ( menuAbsolutoY - larguraBanner + larguraMenu ) + 'px';
				}
				/* 700 estimado. */
				if ((700 - scrollX) > alturaBanner) {
					posicaoYBanner = (menuAbsolutoX - alturaBanner + alturaMenu ) + 'px';
				} else {
					posicaoYBanner = menuAbsolutoX + 'px';
				}
				
				referencia = $('#blue_next').offset();
				if(menuAbsolutoY < referencia.left){
				$('#' + nomeContainer).css({
					"top" : posicaoYBanner,
					"left" : posicaoXBanner,
					"display" : "block"
				});
				}
			});
			$('.flutuantes').mouseout(function() {
				$(this).css('display', 'none');
			});
			$(".bt-menu").mouseover(function() {
				var diva = $(this).attr('name');
				$('#supercenter').hide();
				$('#matriz').hide();
				$('.bt-menu').css('background-color','#A4434C');
				$(this).css('background-color','#00434C');
				$('#'+diva).show();
			});
			

		});
$(document).ready(function() {
	if ($.browser.msie) {
		// Hi, My name is Ie7! I prettend that im a very nice boy but im as
		// nasty
		// as my older brothers.
		if ($.browser.version >= 7 && $.browser.version < 8) {
			$('#header_menu > a').css('top', '-2px');
			$('#header_search').css('top', '-8px');
			$('.ie7love').css('padding-top', '-4px');
			$('.ie7hate').css('height', '18px');
			$('#butter_top_menu > ul').css('margin-top', '0');
		}
	}
});

