var eventsA		= new Array();
var eventsB		= new Array();
var trackType	= null;

var numVehiclesSelected	= 0;
var preSelected			= null;
var selectNum			= 0;
var selectedItem		= null;
var vehicles			= new Array();
var vehiclesSelected	= new Array(4);

$(document).ready(function(){
	if(preSelected != null)
		setVehicle(0,preSelected);
	else
		buildCarousel();
});


function buildCarousel(){
	$('#carouselOverlay').fadeOut('normal');
	$('#previousVehicle').fadeIn('normal;')
	$('#nextVehicle').fadeIn('normal');
	var carousel	= '<ul id="vehiclesCarousel">';
	for(var i=0;i < vehicles.length;i++){
		var selected	= false;
		for (var j=0;j<vehiclesSelected.length;j++){
			if(vehicles[i]==vehiclesSelected[j]){
				selected = true;
			}
		}
		if(!selected){
			carousel+= '<li><h3>'+vehicles[i].name+'</h3><button class="addMe" onclick="setVehicle('+selectNum+',vehicles['+i+'])" type="button"><img alt="" src="/medias/pictures/pages/experiences/multi/add.png"/>Ajouter</button><img alt="" src="/medias/pictures/pages/experiences/multi/carousel/'+vehicles[i].manufacturerUri+'-'+vehicles[i].uri+'.png"/><div class="specification"><table class="specificationsTable" summary=""><caption>Caractéristiques</caption><tbody><tr class="odd"><th scope="row">Moteur</th><td>'+vehicles[i].specifications.engine+'</td></tr></tr><tr class="even"><th scope="row">Puissance</th><td>'+vehicles[i].specifications.power+'<abbr title="chevaux">ch</abbr></td></tr><tr class="odd"><th scope="row">0 à 100 Km/h</th><td>'+vehicles[i].specifications.acceleration+' s</td></tr><tr class="even"><th scope="row">Vitesse max.</th><td>'+vehicles[i].specifications.topSpeed+' km/h</td></tr></tbody></table></div></li>';
		}
	}
	carousel+= '</ul>';
	$('#carousel').html(carousel);
	$("#carousel").jCarouselLite({
		afterEnd:function(items){showCarDetails(items);},
		beforeStart:function(items){hideCarDetails(items);},
		btnNext: ".next",
		btnPrev: ".prev",
		speed:800,
		visible: 1
	});
	showCarDetails($("#vehiclesCarousel li")[1]);
}//End buildCarousel

function buildForm(){
	if(numVehiclesSelected < 2){
		$('#productListContainer').fadeOut('normal');
		var neededVehicles = 2 - selectNum;
		//$('#min2vehicles').html('<h3>Information :</h3><p>Veuillez choisir encore '+neededVehicles+' vehicule(s)</p>')
		$('#min2vehicles').html('<h3>Information :</h3><p>Vous pouvez encore ajouter'+(4 - selectNum)+' vehicule(s)</p>')
		$('#min2vehicles').fadeIn('normal');
	}
	else{
		$('#min2vehicles').fadeOut('normal');
		$('#productListContainer').html('<div id="waitData"><img alt="" src="medias/pictures/_global/ajax-loader.gif"/></div>');
		$('#productListContainer').fadeIn('slow');
		var vehicle0	= vehiclesSelected[0] instanceof Vehicle ? vehiclesSelected[0].id : '';
		var vehicle1	= vehiclesSelected[1] instanceof Vehicle ? vehiclesSelected[1].id : '';
		var vehicle2	= vehiclesSelected[2] instanceof Vehicle ? vehiclesSelected[2].id : '';
		var vehicle3	= vehiclesSelected[3] instanceof Vehicle ? vehiclesSelected[3].id : '';
		$.ajax({
			type: 'POST',
			url: 'ajax/multi/salesItem/',
			data: 'vehicles[]='+vehicle0+'&vehicles[]='+vehicle1+'&vehicles[]='+vehicle2+'&vehicles[]='+vehicle3,
			success: function(msg){
				$('#productListContainer').html(msg);
				$("#multiForm").attr("action",formAction);
				$('#multiForm').submit(function() {
					var hasEvent	= false;
					//alert(selectedItem);
					if($('#eventsSelect') && $('#eventsSelect').val() != 0){
						hasEvent = true;
					}
					else if(!$('#eventsSelect')){
						hasEvent = true;
					}
					if(selectedItem == null){
						alert('Vous devez choisir un stage');
						return false;
					}
					if(!hasEvent){
						alert('Vous devez choisir une date pour votre stage');
						return false;
					}
					if(hasEvent && selectedItem != null){
						//alert('niquel');
						return true;
					}
				});
			}
		});
	}
}//End buildForm()

function hideCarDetails(o){
	$('div.specification',o).fadeOut("normal");
	$('button.addMe',o).fadeOut("normal");
}//End showMeLove()

function setVehicle(vehicleNum,vehicle){
	var humanIndex = vehicleNum + 1;
	if(vehicle){
		vehiclesSelected[vehicleNum] = vehicle;
		$('#vehicleE'+humanIndex).html('<div><h3>'+vehicle.name+'</h3><img alt="" class="selected" src="/medias/pictures/pages/experiences/multi/selected/'+vehicle.manufacturerUri+'-'+vehicle.uri+'.png"/><img alt="" class="removeImg" onclick="setVehicle('+vehicleNum+')" src="/medias/pictures/pages/experiences/multi/remove.png" title="retirer de ma selection"/></div>');
		numVehiclesSelected++;
	}
	else{
		vehiclesSelected[vehicleNum] = undefined;
		$('#vehicleE'+humanIndex).empty();
		numVehiclesSelected--;
	}
	selectNum	= nextSelectNum();
	showSelected();
	buildForm();
	if(selectNum != null){
		buildCarousel();
	}
	else{
		$('#carousel').empty();
		$('#carouselOverlay').html('<div id="maxVehicles"><h3>Information</h3><p>Vous avez atteint le nombre maximum de 4 voitures pour votre Stage multivolants</p></div>');
		$('#previousVehicle').fadeOut('normal;')
		$('#nextVehicle').fadeOut('normal');
		$('#carouselOverlay').fadeIn('normal');
	}
}//End setVehicle()

function showCarDetails(o){
	$('div.specification',o).fadeIn("normal");
	$('button.addMe',o).fadeIn("slow");
}//End showMeLove()

function nextSelectNum(){
	var returnValue	= null;
	for(var i=0;i<vehiclesSelected.length;i++){
		if(vehiclesSelected[i] == undefined){
			returnValue = i;
			break;
		}
	}
	return returnValue;
}//End nextSelectNum()

function showSelected(){
	var newWidth	= 0;
	newWidth = (100 / numVehiclesSelected);
	for(var i =0;i < vehiclesSelected.length;i++){
		j = i + 1;
		if(vehiclesSelected[i] != null){
			$('#vehicleE'+j).animate({width: newWidth+'%'},"fast");
		}
		else{
			$('#vehicleE'+j).animate({width: 0+'em'},"fast");
		}
	}
}//End showCar()

function updateEvents(type){
	//alert('debug en cours : ');
	var eventsArray	= null;
	var options		= '';
	var select		= $('#eventsSelect');
	if(trackType == type)
		return;
	
	trackType = type;
	switch(type){
		case 1:
			eventsArray = eventsA;
			break;
		case 2:
			eventsArray = eventsB;
			break;
		default:
			eventsArray = eventsA;
			break;
	}
	for(var i=0; i < eventsArray.length;i++){
		options+= '<option value="'+eventsArray[i].id+'">'+eventsArray[i].name+'</option>';
	}
	$('#eventsSelect').empty();
	$('#eventsSelect').append(options);
	//$('select#eventsSelect').empty();
	//.html(options);
}//End updateEvents()



function Vehicle(id,name,specifications,uri,manufacturerUri){
	this.id				= id;
	this.name			= name;
	this.specifications	= specifications;
	this.uri			= uri;
	this.manufacturerUri= manufacturerUri;
}//End this.uri

function VehicleSpecification(acceleration,capacity,description,engine,power,topSpeed){
	this.acceleration	= acceleration != '' ? acceleration : 'N.C';
	this.capacity		= capacity != '' ? capacity : 'N.C';
	this.description	= description != '' ? description : 'N.C.';
	this.engine			= engine != '' ? engine : 'N.C';
	this.power			= power != '' ? power : 'N.C.';
	this.topSpeed		= topSpeed != '' ? topSpeed : 'N.C.';
}//End VehicleSpecification()

function Event(id,name){
	this.id		= id;
	this.name	= name;
}//End Event class definition


function updateSelectedItem(item){
	selectedItem = item;
}