Aris.extend(new function(){
	//private variables to work with
	var options = null;
	var timer = null;
	var loading = null;

	//set up call back to get park details
	new HttpClient('/ajax/other-parks.aspx', {
		id: 'getParkDetail',
		maxReuse: 50
	});
	
	var tip = new Tip('interactivemap', ['AREA']);
	HttpClient.getParkDetail.obj.setConfig({ update: tip.content });
	tip.onShow.add(function(sender,args) {
		args.content.innerHTML = '<span class="loading">Loading...</span>';
		HttpClient.getParkDetail({ requestId: 'getParkDetail', park: args.target.getAttribute('parkId') });
	});
	
	//set up call back to filter parks
	new HttpClient('/ajax/other-parks.aspx', {
		id: 'getParks',
		onComplete: function(sender, args) {
			DOM.setStyle(loading, 'display', 'none');
			var update = DOM.matchOne('#parksList ul');
			DOM.clear(update);
			var parks = args.xml.getElementsByTagName('Park');
			if ( parks.length > 0 ){
			forEach(parks, function(p, idx) {
				var li = DOM.create("li", {
					className: 'park col idx' +(idx %2),
					children: {
						tag:'a', attr: {
							href: p.getElementsByTagName('Url')[0].firstChild.nodeValue,
							children: p.getElementsByTagName('Name')[0].firstChild.nodeValue
						}}
				});
				update.appendChild(li);
			});
			}
			else {
				update.appendChild(DOM.create('li', { 'className': 'error message', children: 'There were no Meeting Facilities matching your request.' }));
			}
		},
		onSend: function(sender, args) {
			DOM.setStyle(loading, 'display','');
		},
		onError: function(sender,args) {
			alert('error opening url: '+sender.url);
		}
	});

	return {
		cbChanging: function(e) {
			clearTimeout(timer);
			timer = this.cbChanged.delay(300, this);			
			
		},
		cbChanged: function() {
			var al = options.filter(function(cb){ return cb.checked; }).map(function(cb) {
				return cb.value;
			});
			HttpClient.getParks({ requestId: 'getParks', attrList: al.join(','), attrCount: al.length });
		},
		init: function() {
			DOM.addClass(document.documentElement,'hasJS');
		},
		pageReady: function() {
			options = DOM.match('#mfOptions input');
			options.forEach( function(cb) {
				var ev = BOM.is('ie') ? 'click':'change';
				// ie is slow to fire the onchange event but fires the onclick event on a controls associated label
				Event.add(cb,ev, this.cbChanging, this);
			},this);
			var pl = DOM.get('parksList');
			//fix the height (should always be at its maximum to start with)
			DOM.setStyle(pl, 'height', DOM.getDim(pl).height + 'px');
			
			//create our loading indicator
			loading = DOM.create('div', { className: 'loading', children: 'Loading...', style: {display:'none'} });
			pl.appendChild(loading);
		}
	};
});
