var VehicleCompare = Class.create(
{
	initialize : function(cookie_manager, compare_path)  {
		this.compare_path = compare_path;
		this.signature = '.comparable-vehicle';
		this.cookie_manager = cookie_manager;
		this.to_compare = new Array();
		
		// load the current compare list
		this.load_compare();
		
		// attach the click events and so on
		this.attach_events();
		
		// update the display now
		this.resync_boxes();
	},

	load_compare : function() {
		var list = this.cookie_manager.get('compare_vehicles');
		
		if (list) {
			list.split(',').each(function(stocklist_id){
				this.to_compare.push(stocklist_id);
			}.bind(this));
		}
	},

	save_compare : function() {
		var list = this.to_compare.join(',');
		this.cookie_manager.set('compare_vehicles', list);
	},
	
	attach_events : function() {
		// first for the checkboxes
		$$(this.signature).each(function(elm){
			Event.observe(elm, 'click', this.compare_change.bind(this));
		}.bind(this));
		
		// and also for the compare button
		$$('.compareAction').each(function(elm){
			Event.observe(elm, 'click', this.run_compare.bind(this));
		}.bind(this));

		// and for the remove vehicle link as well
		$$('.compare-remove-vehicle').each(function(elm){
			Event.observe(elm, 'click', this.remove_element.bind(this));			
		}.bind(this));
	},
	
	remove_element : function(e){
		// stop the default behaviour
		Event.stop(e);
		var elm = e.target;
		var sid = elm.getMetaData().stocklistid;
		// delete the element that was removed
		this.to_compare = this.to_compare.without(sid);
		
		//save the list
		this.save_compare();
		
		//run the compare again
		this.run_compare(e);
	},
	
	compare_change : function(e) {
		// find the box that has been clicked on
		var baseURL = window.location.protocol + "//" + window.location.host;
		var image = e.target;
		var checkbox = document.getElementById('checkbox_'+image.id);
		var input = document.getElementById('checkbox_'+image.id);
		var stocklist_id = input.value;
		
		if (image.src == baseURL + '/images/checkbox_off.gif')
		{
			image.src = baseURL + '/images/checkbox_on.gif';
			input.checked = true;
		}
		else
		{		
			image.src = baseURL + '/images/checkbox_off.gif'
			input.checked = false;
		}
		if (input.checked) {
			// adding to the list
			if (!this.to_compare.member(stocklist_id)) {
				if (this.to_compare.length > 2) {
					this.to_compare.pop();
				}
				this.to_compare.reverse();
				this.to_compare.push(stocklist_id);
				this.to_compare.reverse();
			}
		} else {
			// remove it from the list
			this.to_compare = this.to_compare.without(stocklist_id);
		}
		
		// update the display
		this.resync_boxes();

		// resave the list
		this.save_compare();
	},
	
	run_compare : function(e) {
		Event.stop(e);
		var list = this.to_compare.join();
		window.location.href = this.compare_path + list;
	},
	
	resync_boxes : function () {
		$$(this.signature).each(function(elm){
			var checkbox = document.getElementById('checkbox_'+elm.id);
			var baseURL = window.location.protocol + "//" + window.location.host;
			checkbox.checked = this.to_compare.member(checkbox.value);
			if(checkbox.checked)
				elm.src = baseURL + '/images/checkbox_on.gif';
			else
				elm.src = baseURL + '/images/checkbox_off.gif';
	}.bind(this));
	}
	
}
);
