function PropertyCompare(bar, properties)
{
	var object = this;
	this.bar_id = bar;
	this.bar = $j('#'+bar);
	
	this.bar.click(function(){
			object.doToggle()
	});
	
	this.iframe = $j(document.createElement('iframe'));
	this.iframe.css('position', 'absolute').css('display', 'none').css('border', '0px').css('z-index', 1);
	this.div = $j(document.createElement('div'));
	this.div.css('position', 'absolute').css('background-color', 'white').css('display', 'none').css('z-index', 2).css('border', '1px solid #285483').css('overflow', 'hidden');
	
	var html = new Array(
		'<div style="border: 5px solid #6988a7">',
			'<div style="background-color: #285483; color: white; padding: 2px;">Click on a property\'s Compare button to add it below.</div>',
			'<div style="padding: 5px; text-align: center;">',
				'<div class="button" style="width: 60px;" id="compare_btn"><div>Compare</div></div><div class="button" style="width: 60px;" id="compare_save_btn"><div>Save</div></div><div class="button" style="width: 60px;" id="remove_btn"><div>Remove</div></div>',
				'<div style="clear: both;"></div>',	
			'</div>',
			'<div style="background-color: white; padding: 2px; height: 310px; clear: both; overflow: auto">',
				'<table cellpadding="4" cellspacing="0" border="0">',
					'<tbody id="comp_table">',
					'</body>',
				'</table>',
			'</div>',
		'</div>'); 
	this.div.html(html.join(''));
	$j(document.body).prepend(this.iframe);
	$j(document.body).prepend(this.div);
	
	this.table = $j('#comp_table')[0];
		
	//create 
	this.properties = properties;
	for(var i = 0; i < this.properties.length;i++)
	{
		this.addRow(this.properties[i]);
	}
	
	this.updateLabel();
	$j('#remove_btn').click(function(){ object.removeProperties()});
	$j('#compare_btn').click(function(){ object.launchCompare()});
	$j('#compare_save_btn').click(function(){ object.saveProperties()});
}

PropertyCompare.prototype = {
	launchCompare: function()
	{
		var inputs = $j('[name=compare_property]:checked');
		if(inputs.length < 2)
		{
			showMessage('error', 'You must select at least two properties to compare.');
			return;
		}
		else if(inputs.length > 4)
		{
			showMessage('error', 'Maximum four properties can be compared at a time.');
			return;
		}
		
		var ids = new Array();
		for(var i = 0;i < inputs.length;i++)
		{
			ids[i] = 'property_id[]='+inputs[i].value;
		}
		
		popup('/compare_details.php?'+commifyArray(ids, '&'), '910px', '700px', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes');
	},
	doToggle: function()
	{
		var child = this.bar.children('div');
		var id = child.attr('id');
		if(id == 'opened')
		{
			this.doHide();
		}
		else
		{
			this.doShow();
		}
	},
	doHide: function()
	{
		this.bar.children('div').removeAttr('id');
		this.iframe.animate({ 
        width: '0px',
        height: '0px'
      }, 1500 );
		this.div.animate({ 
        width: '0px',
        height: '0px',
		opacity: 0
      }, 1500 );
	},
	doShow: function()
	{
		this.bar.children('div').attr('id', 'opened');
		
		var pos = this.bar.offset();
		this.iframe.css('top', pos.top+24).css('left', pos.left).css('display', 'block').css('height', '0px').css('width', '0px').css('opacity', 0);
		this.div.css('top', pos.top+24).css('left', pos.left).css('display', 'block').css('height', '0px').css('width', '0px').css('opacity', 0);
			
		
		this.iframe.animate({ 
        width: '223px',
        height: '390px'
		}, 1500 );
		
		this.div.animate({ 
        width: '223px',
        height: '390px',
		opacity: 1.0
      }, 1500 );
	},
	addProperty:function(id)
	{
		var object = this;
		jQuery.getJSON('/ajax/compare.php', {action: 'add', property_id: id}, function(data, textString){ object.addPropertyCB(data, textString);} );
	},
	addPropertyCB: function(data, textString)
	{
		if(data.status == 'error')
		{
			return false;
		}
		this.properties[this.properties.length] = data.object;
		this.addRow(data.object);
		this.updateLabel();
		
		//hide button
		$j('#comp_btn_'+data.object.id).hide();
	},
	updateLabel: function()
	{
		$j('div > div', this.bar).html('Compare Properties&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;('+this.table.rows.length+')&nbsp;Added');
	},
	addRow: function(prop)
	{
		var row = this.table.insertRow(this.table.rows.length);
		$j(row.insertCell(0)).html('<input type="checkbox" name="compare_property" id="compare_property" value="'+prop.id+'"/>');
		$j(row.insertCell(1)).html(prop.image);
		$j(row.insertCell(2)).css('font-size', '10px').html(prop.location);
	},
	removeProperties: function()
	{
		var v = new Array();
		var cbs = $j('input:checked', this.table);
		for(var i = 0; i < cbs.length; i++)
		{
			v[v.length] = cbs[i].value;
		}
		
		if(v.length > 0)
		{
			var object = this;
			jQuery.getJSON('/ajax/compare.php', {action: 'remove', property_list: v.join(',')}, function(data, textString){ object.removePropertiesCB(data, textString);} );
		}
	},
	removePropertiesCB: function(data, textString)
	{
		if(data.status == 'error')
		{
			return false;
		}
		
		var cbs = $j('input:checked', this.table);
		var v = new Array();
		for(var i = 0; i < cbs.length; i++)
		{
			var cb = cbs[i];
			v[v.length] = cb.value;
			$j('#comp_btn_'+cb.value).show();
			var tr = cb.parentNode.parentNode;
			this.table.removeChild(tr);
		}
		
		var props = new Array();
		for(var j = 0; j < this.properties.length; j++)
		{
			var prop = this.properties[j];
			var found = false;
			for(var i = 0; i < v.length;i++)
			{
				if(v[i] == prop.id)
				{
					found = true;
					break;
				}
			}
			
			if(!found)
			{
				props[props.length] =prop;
			}
		}
		
		this.properties = props;
		
		this.updateLabel();
	},
	saveProperties: function()
	{
		var object = this;
		var data = {};
	
		var inputs = $j('[name=compare_property]:checked');
		if(inputs.length == 0)
		{
			showMessage('error', 'Please select at least one property to save.');
			return;
		}
		
		var qs = '';
		for(var i = 0;i < inputs.length;i++)
		{
			qs += 'add_property[]='+inputs[i].value+'&';
		}
		
		jQuery.getJSON('/ajax/search_ajax.php?'+qs, data, function(data, status){ object.savePropertiesCB(data, status);} );
	},
	savePropertiesCB: function(data, status)
	{
		var object = this;
		if(data.status == 'error')
		{
			showMessage('error', data.message);
		}
		else
		{
			showMessage('info', data.message);
			$j('[name=compare_property]:checked').attr('checked', false);
		}
	},
	hideSave: function(doHide)
	{
		if(doHide == null || doHide == undefined)
		{
			doHide = true;
		}
		
		if(doHide)
		{
			$j('#compare_save_btn').hide();
		}
		else
		{
			$j('#compare_save_btn').show();
		}
	}
}
