var PropertyResult = Class.create();

PropertyResult.prototype = {
	initialize: function( property_id, el ){
		this.el = el;
		this.property_id = property_id;
	},
	compare_selected: function(){
		try{
			try{
				var checkbox = this.el.select( '.Compare' )[0].getElementsByTagName( 'input' )[0];
			}
			catch( e ){
				var checkbox = this.el.select( '.Compare' )[0].getElementsByTagName( 'input' )[0];
			}
			return checkbox.checked;
		}catch( e ){
			return false;
		}
	}
}

PropertyResult.__ParseId = function( $property_id ){
	return $property_id.substr( 9 );
}

PropertyResult.Results = new Array();

PropertyResult.InitPage = function(){
	var els = $$(".SearchResult");
	for( c=0; c<els.length; c++ )
		PropertyResult.Results.push( new PropertyResult( PropertyResult.__ParseId( els[c].id ), els[c] ) );
}

Event.observe( window, 'load', PropertyResult.InitPage );

PropertyResult.HookCompareProperties = function(){
	var cpEls = $$(".CompareProperties");
	for( f=0; f<cpEls.length; f++ )
		Event.observe( cpEls[f], 'click', PropertyResult.DoCompare );
}

Event.observe( window, 'load', PropertyResult.HookCompareProperties );

PropertyResult.DoCompare = function(){
	var url = '/compare_properties';
	var doCompare = false;
	var checkBoxCount = 0;
	var checkCountOk = false;
	for( cpp=0; cpp<PropertyResult.Results.length; cpp++ )
		if( PropertyResult.Results[cpp].compare_selected() ){
			url += '/' + PropertyResult.Results[cpp].property_id;
			doCompare = true;
			checkBoxCount++;
			if(checkBoxCount == 2){
				checkCountOk = true; // More than 2 checkboxes selected
			}
		}
	if( doCompare ) {
		if(checkCountOk){			
			/*
               here is the old code that doesn't work so hot.               
               window.open( url, 'compare_window', 'menubar=0, resizable=1, scrollbars=1, status=1, width=740, height=550' );
			window.focus();
               */
               var newWindow = window.open(url,'newWindow','width=740,height=550,menubar=0,resizable=1,status=1,scrollbars=1');
		} else {
			alert('Please select two or more properties to compare.');
		}
	} else {
		alert('Please select two or more properties to compare.');
	}
	
}