var SaveAndCompare = Class.create({

	initialize: function(){
		this.maxCars = 3;
		this.cookieName = "saveAndCompare";

		this.boxes = new Array();
		this.boxes[0] = $("sac0");
		this.boxes[1] = $("sac1");
		this.boxes[2] = $("sac2");
		this.boxes[3] = $("sac3");
		this.boxes[4] = $("sac4");
		this.boxes[5] = $("sac5");

		this.savedCars = new Array();
	},

	/*
		Add a car to the save and compare list
	*/
	addCar: function(id, imageURL, title){
		//Store a cookie containing the cars in the
		//compare list, then add the image to the
		//save and compare thumbnails, by AJAX requesting
		//it.
		

		if(this.savedCars.length < this.maxCars){

			//Check that this car is not already saved.
			var duplicated = false;
			this.savedCars.each(function(car){
				if(id == car.id){
					duplicated = true;
				}
			});
			if(!duplicated){
				//Store the car in savedCars Array
				this.savedCars[this.savedCars.length] = {
					'id': id,
					'imageURL': imageURL,
					'title': title
				};
				
				//Store the entire savedCars array
				//in our cookie
                
				document.cookie = this.cookieName + "=" + this.savedCars.toJSON() + "; path=/";
                
				//Update the thumbnails.
				//this.updateThumbs();
				
				return true;
			}else{
				alert("This car is already in your compare list.");
				return false;
			}

		}else{
			alert("You may only save " + this.maxCars + " cars for comparison." );
		}

		return false;
	},

	/*
		Loads each of the thumbnails for the cars, if you need to restyle the images
		then modify this method.
		TODO: Stop cars that are already in the list from reloading
	*/
	updateThumbs: function(){
		
		this.boxes.each(function(box){
			box.update();
		})

		this.savedCars.each(function(car, i){
			if(car.imageURL == ""){
				car.fullImageURL = baseHref+'images/layup/noImage94x71.jpg';
			}else{
				car.fullImageURL = baseHref+'images/global_auto/thumbs/'+car.imageURL;
			}
				
			this.boxes[i].update('<img src="'+car.fullImageURL+'" title="'+car.title+'" style="" />');
		}, this);
	},

	/*
	 * Loads the saved cars array from the cookie,
	 * Only do this once per page load, after this, the array
	 * should be in sync anyway
	 */
	populateSavedCars: function(){
		
		var carJSON = document.cookie.substring(
			document.cookie.indexOf(this.cookieName + "="),( 
			document.cookie.indexOf(";", 
			document.cookie.indexOf(this.cookieName + "="))!= -1)?
			document.cookie.indexOf(";",document.cookie.indexOf(this.cookieName + "=")):
			document.cookie.length).substring((this.cookieName + "=").length);
            
		
			try{
				cars = carJSON.evalJSON();
                
				cars.each(function(car){
					this.addCar(car.id, car.imageURL, car.title);
				}, this);
			}catch(e){

			}

	},

	/**
	 * Moves to the compare page, this page can run using PHP and
	 * Cookies
	 */
	callComparePage: function(){

	},

	clearCars: function(){
		this.savedCars.clear();
		//Remove the savedCars cookie, by making it expire some time ago
		document.cookie = this.cookieName+"=; expires=Thu, 01-Jan-70 00:00:01 GMT;path=/";
		this.updateThumbs();
	},

	removeCar: function(id){
		if(confirm('Are you sure you would like to remove this car?')){
			var delCar = false;
			this.savedCars.each(function(car){
				if(car.id == id){
					delCar = car;
				}
			});

			if(delCar){
				this.savedCars = this.savedCars.without(delCar)
				//this.updateThumbs();
				document.cookie = this.cookieName + "=" + this.savedCars.toJSON() + "; path=/";
                
                var newUrl = baseHref  + "used-cars/compare";
                
                window.location.href = newUrl;
			}
			//Look for the car in the list and remove it
			//But only if we are in save and compare mode!
			if(compare && $('car_' + id)){
				$('car_' + id).remove();
			}
		}
	}
});


var save
Event.observe(window, "load", function(){
	save = new SaveAndCompare();
	save.populateSavedCars();
})



function removeAll ()  {
        
        if(confirm('Are you sure you would like to remove all cars?')){
        

            document.cookie = "saveAndCompare" + "=" + "" + "; path=/";
            
            var newUrl = baseHref  + "used-cars/compare";
            
            window.location.href = newUrl;
            
        }
    }

function saveCar(id, imageURL, title){
	if(save.addCar(id, imageURL, title)){
		alert("The car has been added to your compare list");
        
        // redirect to compare list
        var newUrl = baseHref  + "used-cars/compare";
        
        window.location.href = newUrl;
        
	};
	return false;
}

function clearCars(){
	save.clearCars();
	return false;
}

function removeCar(id){
	save.removeCar(id);
	return false;
}