/**
 * JS for comparison selection.
 *
 *@requires prototype.js <http://prototypejs.org/>
 *@author Sam McCallum <sam@palo-verde.us>
 */

var compareUriBase = "compare.php";

/** 
 * This wrapper executes when the document is done loading.
 */
Event.observe(window,'load', function () {		
	// Add checkboxes to items...
	$$("ul#sidebar-list li").each(addCheckbox);
		
	// attach event delegate to sidebar columon to monitor checkboxes
	$('sidebar-list').observe('click',checkboxDelegate);
	
	// Watch the compare button for clicks
	$('compare-button').observe('click', compareClicked);
});

/**
 * This adds a checkbox to each parking space item
 *
 *@param Element
 */
function addCheckbox(el) {	
	var aCheckbox = new Element('input', {type:'checkbox',id:'checkbox-for-'+el.id.sub('parking-area-',''),className: 'comparison-check'});
	el.insert({top: aCheckbox});
}

/** 
 * This watches the sidebar list for click events and checks to see if they where in the checkboxes
 * if so, then manage those clicks.
 *
 *@param Event
 */
function checkboxDelegate(ev) {
	// First we need to see if it was a checkbox that was clicked.
	if (ev.target.tagName == 'INPUT' && ev.target.hasClassName('comparison-check')) {
		toggleParkingSelection(ev.target);
		updateSelected()
	}
}

/**
 * toggles wether we have a selection or not
 */
function toggleParkingSelection(el) {
	if (el.hasClassName('selected')) {
		el.removeClassName('selected');
		if (countParkingSelections() == 0) noParkingSelections();
	} else {
		if (countParkingSelections() < 3) {
			el.addClassName('selected');
		} else {
			tooManyParkingSelections(el);
		}
	}
}

/**
 * Return the number of selected parking areas
 */
function countParkingSelections() {
	var count = $$('#sidebar-list li input.selected').length;
	return count;
}

/**
 * This is triggered if there are too many parking selections
 */
function tooManyParkingSelections(el) {
	el.checked = false;
	alert('You already have 3 selections');
}

/**
 * This is triggered whent here are no selections
 */
function noParkingSelections() {
	
}

/**
 * Update the number selected counter on the screen
 */
function updateSelected() {
	var num = countParkingSelections();
	var counter = $('compare-num-selected');
	var compareButton = $('compare-button');
	
	counter.update(num);
	
	if (num) {
		if (compareButton.hasClassName('disabled')) {
			compareButton.removeClassName('disabled');
		}
	} else {
		compareButton.addClassName('disabled');
	}
}

/**
 * This is executed whent he compare link is clicked
 */
function compareClicked(ev) {	
	var compareButton = $('compare-button');
	var compareUri = compareUriBase;
	
	// If compare is disabled then do nothing and cancel the event.
	if (compareButton.hasClassName('disabled')) {
		return;
	}
	
	// Submit to compare.php
	$$('ul#sidebar-list li input.selected').each(function (el, index) {
		if (index == 0) {
			var divider = '?';
		} else {
			var divider = '&';
		}
		
		compareUri = compareUri+divider+'comparisons[]='+el.id.sub('checkbox-for-','');
	});
	
	if (typeof near != "undefined") {
		compareUri = compareUri+"&near="+near;
	}
	
	new Ajax.Updater('comparison-container', compareUri);
}

 