////// TEST DRIVE - STARRETT ///////////////////////////////////////////////////////////////////////////	
var galleryTimer = "";

function testDrive(){
	// Carrega Conteúdo
	$.ajax({
		url: "/test-drive/default.asp",
		context: document.body,
		contentType: 'application/x-www-form-urlencoded; charset=iso-8859-1;', 
		success: function(contentData)
		{			
			// Incluí Html
			$("body").append(contentData);						
			
			// Transparência do BG
			$("#stdOverlay").fadeTo("fast", 0.9);				
			
			// Inicia Slide capa (imagens Furgão)
			$("#stdHomeGallery .image").hide();												
			count = 1;
			qtdImages = $("#stdHomeGallery .image").length;			
			gallerySpeedAnimation = 600;
			galleryTimer = setInterval(galleryFade, 5000);
			galleryFade();
					
			//$("#stdCalendar").hide();	
			$("#stdAbout").hide();			
			
			// Comportameto click da galeria
			$("#stdGallery a").colorbox({opacity:0, top:65, transition:'elastic', initialWidth:"200", initialHeight:"100"});
			
			
			// Navegação				
			$("#btnSaibaMais").click(function(evt) {
				evt.preventDefault();			
				// Para Galeria
				clearInterval(galleryTimer);
				 
				$('#stdCalendar').fadeOut(function(){
					$('#stdContent #stdPane').animate({
					/*opacity: 0.25,*/
					width: '760'
					/*height: '+=100'*/
					}, 700, function() {
						// Animation complete.
						$("#stdAbout").fadeIn();
					});				
				});				
			});			
					
			$("#btnVoltar").click(function(evt) {		
				evt.preventDefault();				
				// Reinicia Galeria
				galleryTimer = setInterval(galleryFade, 5000);
				
				$('#stdAbout').fadeOut(function(){
					$('#stdContent #stdPane').animate({
					width: '370'
					}, 700, function() {
						// Animation complete.
						$("#stdCalendar").fadeIn();
						clique = 0;
						navigate();
					});
				});						
			});	
	
			//Paginação agenda
			//variáveis paginação
			clique = 0;
			coeficiente = 3;
			qtdItens = $("#calendarList li").size();				
			qtdItens = Math.ceil(qtdItens/coeficiente);
			
			//alert(qtdItens)			
			if(qtdItens <= 1){
				$("#btnPrev, #btnNext").addClass("disabled");
			}else{
				$("#btnPrev").addClass("disabled");
				$("#btnNext").removeClass("disabled");
			}
						
			$("#paneNavigate .label").html((clique+1) + " de " + qtdItens );
			
					
			$("#btnPrev, #btnNext").bind("click",function(evt) {
				evt.preventDefault();
				navigate($(this));				
			});
			
			// Fechar			
			$("#btnClose").click(function(evt) {
				evt.preventDefault();					
				speed = 350;

				$("#stdPane").fadeOut(speed,function(){
					clearInterval(galleryFade);
					$("#stdHomeGallery").fadeOut(speed,function(){
						$("#stdWrapper").fadeOut(speed,function(){
							$("#stdOverlay").fadeOut(speed,function(){
								clearInterval(galleryTimer);
								$("#stdOverlay, #stdWrapper").remove();								
								$("#tabTestDrive").fadeIn();
							});
						});
					});
				});
			});						
		},
		error: function(){
			alert("error");
		}			
	});
	}
	
	
	
	
// Funções Gerais //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function galleryFade(){

	switch (count) {
		case 1:
			$("#stdHomeGallery .image:eq(0)").fadeIn(gallerySpeedAnimation);
			$("#stdHomeGallery .image:eq(2)").fadeOut(gallerySpeedAnimation);
			break;
		case 2:
			$("#stdHomeGallery .image:eq(1)").fadeIn(gallerySpeedAnimation);
			$("#stdHomeGallery .image:eq(0)").fadeOut(gallerySpeedAnimation);
			break;			
		case 3:
			$("#stdHomeGallery .image:eq(2)").fadeIn(gallerySpeedAnimation);
			$("#stdHomeGallery .image:eq(1)").fadeOut(gallerySpeedAnimation);
			count = 0;
			break;			
	}
	count++;
}
	
// Função Paginação
function navigate(obj){		
	if(obj){
		animarCalendario = obj.is(".disabled") == false;
		
		if( obj.attr("id") == "btnNext" && clique < qtdItens -1 ){
			clique++;						
		}
		else if( obj.attr("id") == "btnPrev" && clique > 0 ){
			clique--;
		}
	}
	
	if(clique == 0){
		$("#btnPrev")
		.addClass("disabled")
		.attr("disabled","disabled")
		//.unbind("click");
	}else{
		$("#btnPrev")
		.removeClass("disabled")
		.removeAttr("disabled")
	}		
	
	if(clique == qtdItens-1){
		$("#btnNext")
		.addClass("disabled")
		.attr("disabled","disabled")
		//.unbind("click");				
	}else{
		$("#btnNext")
		.removeClass("disabled")
		.removeAttr("disabled")
	}			
		
	$("#paneNavigate .label").html((clique+1) + " de " + qtdItens );			
	/*
	$("#calendarContainer").animate({
		//scrollTop: $(reference).offset().top
		scrollTop: coeficiente*clique*83
	}, 600);
	*/
	
	if(obj){
		if(animarCalendario){
			$("#calendarContainer").fadeTo(600, 0.1, function(){
				$(this)
				.scrollTop(coeficiente*clique*83)
				.fadeTo(600, 1)
			})
		}
	}
}
