
var currentCarImage = 0;
var carImages = new Array();

var currentPSStart = 0;
var currentPSEnd = 0;
var psMax = 0;


function parseId(id) {

	//alert(id);
	return id.substr(id.lastIndexOf("-")+1);

}


function carImageRotate(data) {	

	// prepare images
	carImages = data;
	
	// Only loop if there is more than 1 banner
	if (carImages.length > 1)
	var timer1 = setInterval( function() { rotateCarImage(); }, 6000);
	
	// Show first one
	rotateCarImage();
}

function rotateCarImage() {
	
	$(".car_g").append("<div class='loader'></div>");
	
	// Show next image in line
	var img = new Image();
		$(img).load(function () {
			$('.car_g').hide();
			$('.car_g').empty();
			
			//$('.car_g').append('<div style="position:absolute; background:#FFF; display:block; filter:alpha(opacity=1); opacity:0; width:100%; height:100%;"></div>');
			
			$('.car_g').append(this);
			
			// Fix againse IE-PNG-FADE problem
			if ($.browser.msie) $('.car_g').show();
			else $('.car_g').fadeIn(800);
			
			// REmove loader
			$(".loader").remove();
	
			
		}).error(function () {
	}).attr('src', carImages[currentCarImage]);
		
	// Prepare next image
	currentCarImage++;
	if (currentCarImage > (carImages.length-1)) currentCarImage = 0;
}

// Check if brochure email is valid
function brochureEmailIsValid() {


	var valid = $.session("brochure-valid-email");

	if (valid == "yes") return true;
	else return false;
	
}

// CHeck if the email on brochure form is to be shown
function showBrochureEmailForm() {


	if (!brochureEmailIsValid()) {
         
		 document.write("<div class='brochure-email-area'><h3>Your Email address</h3><input type='text' name='email' id='email' value='' size='20' /><div class='bmsg'>Please enter a valid Email address</div></div>");
				
	}

}

// Check is the download page is being accessed on its own
function checkDownloadPage() {
	
	if (!brochureEmailIsValid()) $("html").empty();
	
}

function debugClearSession() {
	$.session("brochure-valid-email","no");
}

// Click
function handleBrochureClick(e, obj) {
	
	// Check if valid, if yes submit
	if (checkValid() || brochureEmailIsValid()) {
	
		// Write to session
		$.session("brochure-valid-email","yes");
		var email = $(".brochure-email-area .vArea #email").val();
		if ( email != "undefined") $.session("brochure-email",email);
		
		// Prepare email
		email = $.session("brochure-email");
		
		// Add value to href
		var href = $(obj).attr('href');
		href += "&email="+encodeURIComponent(email);
		$(obj).attr('href', href);
		
		// Proceed
		return true;
		
	}
	else {
		
		// Stop
		$.session("brochure-valid-email","no");
		e.preventDefault(); 	
	}

	
}

// Brochure page
function brochurePage() {

	// Disable default link
	$('.brochure-trigger').click(function(e) { handleBrochureClick(e, this); });
	$('.brochure-trigger-right').click(function(e) { handleBrochureClick(e, this); });

}

// All Functionality on the Car Page
function carPage() {

	// Show first tab
	$('#section0').show(500);
	$('#tab-overview0').show(500);
	
	// Click on Section
	$('.section-link').click(function() {
		
		$('.section-info').hide();
		$('#section'+parseId(this.id)).show();
		
		// Hide all tabs and show Overview
		$('.tab-info').hide();
		$('#tab-overview'+parseId(this.id)).show(500);
		
	});
	
	// If use clicks on top models, still activate the respective model on the left sidebar
	$(".civic_text .section-link").click(function() {

		var active = ".model_variations ul #left-"+parseId(this.id)+" a";
		
		if ( !$(active).hasClass('current') ) {
			$('.model_variations ul li a.current').addClass('no-back').removeClass('current',400).removeClass('no-back');
			$(active).addClass('current');
		}

	});
	
	// Click on Tab
	$('.tab-link').click(function() {
		
		$('.tab-info').hide();
		$('#tab-'+parseId(this.id)).fadeIn(300);
		
	});
	
	window.onload = function () {
		
		var container = $('div.sliderGallery');
		var ul = $('ul', container);
		var handle = $('.handle', container);

		var itemsWidth = ul.innerWidth() - container.outerWidth();
		
		$('.scrollbar', container).slider({
			min: 0,
			max: itemsWidth,
			handle: '.handle',
			stop: function (event, ui) {
				ul.animate({'left' : ui.value * -1}, 500);
			},
			slide: function (event, ui) {
				ul.css('left', ui.value * -1);
				//var pos = $('a.ui-slider-handle').css('left');
				
				//handle.css('left', pos);
			}
		});

	};	
	
	// Scroll down to car when click on model
	$('.scroll-down').click(function() {
		//$(window).animate({ scrollTop: 450 }, 1000);
		$('html, body').animate({ scrollTop: 450}, 1400);
		
		
		// Make overview the active tab
		$('.section-info ul li').removeClass('current');
		$('.section-info ul li.overview').addClass('current');

		
	});
	
	// Highlight model when clicked on sidebar
	$('.section-link a').click(function() {
				
		// Hide current active
		//$('.model_variations ul li a.current').stop().animate({ color:  '#000' }, 400).removeClass('current');
		
		if ( !$(this).hasClass("current") ) {
		
			$('.model_variations ul li a.current').addClass('no-back').removeClass('current',400).removeClass('no-back');
				
				// Make current one active
				$(this).addClass('current');
				
				// Make overview the active tab
				$('.section-info ul li').removeClass('current');
				$('.section-info ul li.overview').addClass('current');
			}
		
	});
			
	
	// Animate tabs
	$('.section-info ul li').click(function() {
				
		// Hide current active
		$('.section-info ul li').removeClass('current');
		
		// Make current one active
		$(this).addClass('current');
	});
	
		
	
}


// General on page load functionality
$(document).ready(function(){
	
	$('a.blank').attr('target','_blank');

});
	

