$.fn.loopright = function(gor,gol) {
	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();
		}
		return gor;
	}
};
$.fn.loopleft = function(gor) {
	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();
		}
	}
	return gor;
};

function rotator() {
	$('#rotator ul li').css('opacity','0.0');
	$('#rotator ul li:first').css('opacity','1.0');
	setInterval('rotate()', 10000);
}
function rotate() {
	var current = ($('#rotator ul li.show') ? $('#rotator ul li.show') : $('#rotator ul li:first'));
	if (current.length == 0) { current = $('#rotator ul li:first');}
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#rotator ul li:first')
			: current.next())
			: $('#rotator 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 = ($('#rotator ul li.show') ? $('#rotator ul li.show')
			: $('#rotator ul li:last'));
	if (current.length == 0)
		current = $('#rotator ul li:last');
	var next = ((current.prev().length) ? ((current.prev().hasClass('show')) ? $('#rotator ul li:last')
			: current.prev())
			: $('#rotator 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 = ($('#rotator ul li.show') ? $('#rotator ul li.show')
			: $('#rotator ul li:first'));
	if (current.length == 0)
		current = $('#rotator ul li:first');
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#rotator ul li:first')
			: current.next())
			: $('#rotator 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() {
	//Linkand o click do logo
	$("#logo").click(function() {location.href = "/";});
	$('.flutuantes').hide();
	
	// Barra azul
	$('.blue_image').mouseover(function(e) {
		$('.flutuantes').css('display', 'none');
		nomeContainer = $(this).attr('name');
		viewportX = $(window).width();
		scrollX = $(document).scrollTop();
		alturaBanner = $('#' + nomeContainer).height();
		posicaoX = e.pageX;
		var left=0, right=0, top=0, bottom=0;
		if (posicaoX < (viewportX / 2)) {
			right='auto';
		} else {
			left='auto';
		}
		if ((700 - scrollX) > alturaBanner) {
			top='auto';
		} else {
			bottom='auto';
		}
		$('#' + nomeContainer).css({'z-index':'666','position': 'absolute', 'top': top, 'bottom': bottom, 'left': left, 'right': right});
		$('#' + nomeContainer).show('fast');
	});
	$('.flutuantes').mouseout(function() {$(this).css('display', 'none');});
	$('.blue_item:gt(3)').hide();
	
	var bluecur = 0;
	$("#barra_left").click(function() {
		if (bluecur == 0) {
		} else {
			$('.blue_item:eq(' + (bluecur + 3) + ')').hide("slow");
			$('.blue_item:eq(' + (bluecur - 1) + ')').show("slow");
			bluecur--;
		}
		
	});
	var quantos = $('.blue_item').size();
	$("#barra_right").click(function() {
		if (bluecur == quantos - 4) {

		} else {
			$('.blue_item:eq(' + bluecur + ')').hide("slow");
			$('.blue_item:eq(' + (bluecur +4) + ')').show("slow");

			/*
			$('.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++;
		}
	});
	
	// MENU 
	$('#menu > ul > li:last').css('border', 'none');
	$('#menu > ul > li').mouseenter(function() {
		$(this).clearQueue();
		$(this).children('a').css("text-decoration", "underline");
	});
	$('#menu > ul > li').mouseleave(function() {
		$(this).children('a').css("text-decoration", "none");
	});

	
	//Menu dos carros
	$('.menu_carro').hide();
	var barSize = 0;
	$("#header_car_carros").children('a').each(function(){barSize= barSize+$(this).width();});
	barSize=barSize/2;
	
	var gol = barSize;
	var gor = 0;
	
	$("#header_car_right")
		.mouseenter(function() {
		this.iid = setInterval(function() {
		    gor = $("#header_car_right").data('loop', true).loopright(gor,gol);
		},325);
		})
		.mouseleave(function() {
			this.iid && clearInterval(this.iid);
		});
	$("#header_car_left")
		.mouseenter(function() {
		this.iid = setInterval(function() {
			gor = $("#header_car_left").data('loop', true).loopleft(gor);
		},325);
		})
		.mouseleave(function() {
			this.iid && clearInterval(this.iid);
		});
	$("#header_car_carros > a").corner("cc:#d4cfd3 round 10px");
	$("#header_car_carros > a").mouseenter(function() {
		$('#container_carros').css('top','0px');
		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);
		$(this).css('background-color','#ebe7eb');
	});
	$("#header_car_carros > a").mouseleave(function() {
		text = $(this).text().toLowerCase();
		text = text.replace(/ /g, "");
		$('#menu_carro_' + text).animate({opacity : 0,left : 900}, 100).animate({left : 0}, 0);
		$(this).css('background-color','#d4cfd3');
		$('#container_carros').css('top','-200px');
	});
	
	// Anuncios Seminovos
	$(".anuncio_carro > img").corner();
	$(".botao_detalhe").click(function() {
		location.href = '/veiculos-seminovos/index/interna/' + $(this).attr('title');
	});
	$('#anuncio_mais_carros').click(function(){
		location.href = '/veiculos-seminovos/';
	});
	
	
	// Noticias
	$('.noticia:last').css('border', 'none');
	
	$("#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');
            });

	//Video
    $("#video_box a").prettyPhoto({
		animationSpeed : 'fast',
		hideflash: true,
		slideshow : 10000
	});
    $("a[rel^='prettyVideo']").prettyPhoto({
		animationSpeed : 'slow',
		hideflash: true
	});
    
    // Rotator
    rotator();
    rotate_proximo();
    rotate_anterior();
	$('#rotator').fadeIn(1000);
	$('#rotator ul li').fadeIn(1000); // tweek for IE
	$("#rotator_left").click(function() {
		rotate_anterior();
	});
	$("#rotator_right").click(function() {
		rotate_proximo();
	});
	
    
    
	
/*	
	
	$(".gallery a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed : 'slow',
		theme : 'light_square',
		slideshow : 2000,
		hideflash: true,
		autoplay_slideshow : true
	});
	$(".gallery a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed : 'fast',
		hideflash: true,
		slideshow : 10000
	});
	$("a[rel^='prettyPhotoMenu']").prettyPhoto({
		animationSpeed : 'fast',
		hideflash: true,
		slideshow : 10000
	});
	
	// Car Tab

	$("#header_car_carros").children('a').corner();
	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;
	

	// 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');
			});

	$(".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();
	});
	*/
});
