function initComparison()
{
	if( !document.getElementsByName ) { return; }
	compare_count = null;
	compare_ids = null;
	getCompareIds();
	
	if( compare_ids != null && compare_ids.length > 0 ) {
		compare_count = compare_ids.split(",").length;
		compare_ids = "," + compare_ids + ",";
	}
	displayCompareCount();
	
	
	var checkboxes = document.getElementsByName("compare_car_ids");
	var length = checkboxes.length;

	for( var i=0; i<length; i++ ) {
		if( checkboxes[i] != null ) {
			setCompareStyle( checkboxes[i] );
			if( checkboxes[i].checked ) {
				if( checkboxes[i].id.indexOf("Compared") > -1 ) {
					if( compare_ids.indexOf("," + checkboxes[i].value + ",") == -1 ) {
						el_row = document.getElementById("trCompared_" + checkboxes[i].value); // get the compare row
						el_row.parentNode.removeChild(el_row);
					}
					
					//checkboxes[i].checked = false;
					//setCompareStyle(checkboxes[i]);
					//compare_count += 1;	
				}
				else {
					if( addToCompareBasket( checkboxes[i] ) ) {
						//compare_count += 1;	
					}
				}
			}
		}
	}
}

function setCompareStyle( checkbox )
{
	var td = document.getElementById(checkbox.id.replace("chk", "td"));
	var className = checkbox.checked ? "compare-col-selected" : "compare-col";
	if( td.className.indexOf("firstlast") > 0 ) {
		className += "-firstlast";
	}
	else if( td.className.indexOf("first") > 0 ) {
		className += "-first";
	}
	else if( td.className.indexOf("last") > 0 )	{
		className += "-last";
	}
	td.className = className;
}

function toggleComparison( checkbox )
{
	if( checkbox.checked ) {
		if( !addCompareCarAd( checkbox.value, checkbox ) ) {
			checkbox.checked = false;
		} else {
			addToCompareBasket( checkbox );
		}
	}
	else {
		remCompareCarAd( checkbox.value );
		el_row = document.getElementById("trCompared_" + checkbox.value); // get the compare row
		el_row.parentNode.removeChild(el_row);
		/*
		if( compare_count == 0 ) {
			document.getElementById("compare_list_heading").style.display = "none";
			document.getElementById("compare_bottom").style.display = "none";
		}
		*/
		if( checkbox.id.indexOf("Compared") > -1 ) {
			chkbox = document.getElementById("chkCompare_" + checkbox.value);
			if( chkbox != null ) {
				chkbox.checked = false;
				setCompareStyle(chkbox);
			}
		}
		alignStyle();
	}
}

function addToCompareBasket( checkbox )
{
	if( document.getElementById("trCompared_" + checkbox.value) == null ) { // the row does not already exist
		
		var el_row = document.getElementById("tr_" + checkbox.value).cloneNode(true); // get a copy of the row
		var el_body = document.getElementById("search-result-body"); // get the table body containing search result								
		// find class name for the new row...
		var total_count = document.getElementsByName("compare_car_ids").length + 1;
		if( total_count % 2 == 0 ) {
			el_row.className = "even";
		} 
		else {
			el_row.className = "odd";
		}
		//el_row.className += "-last";
		el_row.id = "trCompared_" + checkbox.value; // give the row a new unique id
		
		var el_compare_td = el_row.firstChild; // get the compare column
		el_compare_td.id = "tdCompared_" + checkbox.value; // give the column a new unique id
		var el_compare_chkbox = el_compare_td.getElementsByTagName("input")[0]; // get the compare checkbox
		el_compare_chkbox.id = "chkCompared_" + checkbox.value; // give the checkbox a new unique id
		el_compare_chkbox.checked = true;
		
		/*
		var el_last = document.getElementById("compare_bottom"); // get the last element (compare bottom) in the search result
		el_body.insertBefore( el_row, el_last); // insert the new column just before the last element
		document.getElementById("compare_list_heading").style.display = ""; // make compare heading visible - see http://www.velocityreviews.com/forums/t160487-firefox-and-stylequotdisplayblockquot-on-table-row.html
		document.getElementById("compare_bottom").style.display = "";
		*/
		var el_last = document.getElementById("compare_bottom"); // get the last element (compare bottom) in the search result
		el_last.parentNode.insertBefore(el_row,el_last.nextSibling);
		document.getElementById("compare_list_heading").style.display = ""; // make compare heading visible - see http://www.velocityreviews.com/forums/t160487-firefox-and-stylequotdisplayblockquot-on-table-row.html
		document.getElementById("compare_bottom").style.display = "";
		
		alignStyle();
		return true;
	}
	else {
		return false;
	}
}

function alignStyle()
{
	// now we only need to set the correct compare column styles...
	var arrCheckbox = document.getElementsByName("compare_car_ids"); // get compare checkboxes
	for( i=0; i<arrCheckbox.length; i++ )
	{
		var td = document.getElementById(arrCheckbox[i].id.replace("chk", "td")); // get the compare column containing the checkbox
		var comp_footer = document.getElementById("compare_bottom"); // get compare footer. If null, we know that search resulted in 0 ads
		if( td != null ) { // the row with the colum may have been removed above
			if( td.id.indexOf("tdCompared") >= 0 ) {arrCheckbox[i].checked = true;} // IE6 forgets checked state of checkbox, so we make sure that it is checked
			// figure out the class name for the compare column...
			var className = arrCheckbox[i].checked ? "compare-col-selected" : "compare-col";
			if( i == 0 && comp_footer != null) {
				className += "-first";
			}
			else if( i == (arrCheckbox.length - 1 - compare_count) && comp_footer != null ) {
				className += "-last";
				// we also need to give the last row the correct class name
				var tr = td.parentNode;
				if( arrCheckbox.length % 2 == 0 ) {
					tr.className = "even-last";
				}
				else {
					tr.className = "odd-last";
				}
			}
			td.className = className; // set class name for the compare column
		}
	}
}

