var tree_items = new Object();
var win = null;

function unitno_from_listitem(element) {
	return element.parentNode.getElementsByTagName('th')[0].innerHTML.strip();
}

var Rules = {
	'#tree h4 img:click':	function(element) {
		var list = element.parentNode;
		while( list.tagName != 'TABLE' ) {
			list = list.nextSibling;
		}
		if( Element.visible(list) ) {
			Element.hide(list)
			element.src = element.src.replace('collapse', 'expand');
		} else {
			Element.show(list);
			element.src = element.src.replace('expand', 'collapse');
		}
	},
	'#tree tr':		function(element) {
		var key = element.getElementsByTagName('th')[0].innerHTML.strip();
		tree_items[key] = element;
		tree_items[key+'-1'] = element;
		tree_items[key+'-2'] = element;
	},
	'#tree tr:click':	function(element) {
		location.href = '?unit='+unitno_from_listitem(element);
	},
	'#tree tr:mouseover':	function(element) {
		var key = unitno_from_listitem(element);
		var unit = $(key);
		var unit1 = $(key+'-1');
		var unit2 = $(key+'-2');

		element.style.cursor = 'pointer';
		if( unit != null )
			unit.src = unit.hover_src.src;
		if( unit1 != null )
			unit1.src = unit1.hover_src.src;
		if( unit2 != null )
			unit2.src = unit2.hover_src.src;
		if( win != null )
			win.hide();
	},
	'#tree tr:mouseout':	function(element) {
		var key = unitno_from_listitem(element);
		var unit = $(key);
		var unit1 = $(key+'-1');
		var unit2 = $(key+'-2');

		element.style.cursor = 'auto';
		if( unit != null )
			unit.src = unit.orig_src;
		if( unit1 != null )
			unit1.src = unit1.orig_src;
		if( unit2 != null )
			unit2.src = unit2.orig_src;
	},

	// FIXME: Seems overkill. Is there a selector to narrow it down?
	'img':			function(element) {
		if( !element.src.match(/\/buttons\/.+?\-1\.gif$/) ) {return;}

		element.orig_src = element.src;
		element.hover_src = new Image();
		element.hover_src.src = element.src.replace(/\-[1|2]\.gif$/, '-2.gif');
	},
	'img:click':	function(element) {
		if( !element.src.match(/\/buttons\/.+?\-[1|2]\.gif$/) ) {return;}

		var unit_no = element.id.strip().replace(/\-\d$/, '');
		location.href = '?unit='+unit_no;
	},
	'img:mouseover':	function(element, event) {
		if( element.src.match(/\/buttons\/.+?\-0\.gif$/) )
			if( win != null )
				win.hide()
		if( !element.src.match(/\/buttons\/.+?\-[1|2]\.gif$/) ) {return;}

		var unit_no = element.id.strip();
		element.style.cursor = 'pointer';
		element.src = element.hover_src.src;
		if( tree_items[unit_no] )
			tree_items[unit_no].style.backgroundColor = '#91A48D';

		if( win == null )
			win = new Window('summary', {
				className:	'alert',
				width:		150,
				minWidth:	1,
				height:		70,
				minHeight:	1,
				showEffect:	Element.show,
				hideEffect:	Element.hide
			});

		var sum = summary[unit_no];
		if( sum == null ) {
			var base_id = unit_no.replace(/\-\d$/, '');
			sum = summary[base_id];
			if( sum != null ) {
				$(base_id+'-1').src = $(base_id+'-1').hover_src.src;
				$(base_id+'-2').src = $(base_id+'-2').hover_src.src;
			}
		}
		var bed_bath = sum.br && sum.ba ? '<br />' + sum.br +
			' Bdrm / ' + sum.ba + ' Bath' : '';
		win.getContent().innerHTML = '<div id="summary"><strong>' +
			sum.use + ' ' + sum.unitno + bed_bath +
			'</strong><br />' + sum.sf + 'sf &nbsp;&nbsp; ' +
			sum.rent + '</div>';
		win.setLocation(Event.pointerY(event)+15, Event.pointerX(event));
		win.show();
	},
	'img:mouseout':		function(element) {
		if( !element.src.match(/\/buttons\/.+?\-[1|2]\.gif$/) ) {return;}

		element.style.cursor = 'auto';
		element.src = element.orig_src;
		var base_id = element.id.strip().replace(/\-\d$/, '');
		if( $(base_id+'-1') )
			$(base_id+'-1').src = $(base_id+'-1').orig_src;
		if( $(base_id+'-2') )
			$(base_id+'-2').src = $(base_id+'-2').orig_src;
		if( tree_items[base_id] )
			tree_items[base_id].style.backgroundColor = '';
	}
};

appendLoad("EventSelectors.start(Rules)");
