var total = null;
var operators = [];
var sectors = [];
var free = null;
var rpp;

function initialize() {
	
	
	
	$(document.body).addEvent({
		'unload': GUnload
	});
	
	
	if (!GBrowserIsCompatible()) {
		window.alert("Google Maps can't play with your browser.");
		return;
	}
	
	// Fire up Google Maps...
	var map = new GMap2($('map'));
	
	var bounds = getLocationBounds();
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	
	rpp = 50;
	/* no results per page not neccessary...
	$('rpp').addEvent('change', function() {
		rpp = $('rpp').value;
		scroll = new Fx.Scroll(window);
		scroll.toElement('directory');
		fetchData(1);
	});
	*/

	
	
	fetchData(1);
	
}

function fetchData(page) {
		
	$('hotspotsTableLoading').removeClass('hide');
	
	
	var jSonRequest = new Request.JSON( {
		url : window.location.href,
		onSuccess : function(results) {
			if(results.total) {
				displayResults(results);
			} else {
				displayNoResults(results);
			}
		}
	}).get( {
		'page' : page,
		'sectors': sectors,
		'operators': operators,
		'free' : free,
		'rpp' : rpp
	});	
}

function displayNoResults(){
	
	$('hotspotsContainer').empty();
	
}

function displayResults(results) {
	
	$('hotspotsTableLoading').addClass('hide');
	$('hotspotsTableContent').empty();
	
//	console.log(results);
	
	/*
	headers = new Element('tr');
	
	['location', 'address', 'operators'].each(function(title) {
		headers.adopt(new Element('th').set('text', title));
	});
	
	$('hotspotsTable').adopt(headers);
	*/
	
	results.hotspots.each(function(hotspot, index) {
	
		tr = new Element('tr');
		
		site = new Element('a', {'href':hotspot.loc_full_path, 'title': 'Click for more info...'}).set('text', hotspot.site);

		td = new Element('td').adopt(site)
		if(hotspot.free == 1) {
			td.adopt(new Element('img', {'src': '/images/icons/free.png', 'alt':'free'}).addClass('icon'));
		}
		
		tr.adopt(td);
		
		td = new Element('td').set('text', hotspot.address1);
		tr.adopt(td);
		
		ops = [];
		
		hotspot.Operator.each(function(op) {
			ops.push( op.company );
		});
		
		td = new Element('td').set('text', ops.join(', '));
		
		tr.adopt(td);
		
		$('hotspotsTableContent').adopt(tr);
		
	});
	
	$('hotspotsPager').empty();
	
	if(!results.pagerRange.contains(1)) {
		firstPageLink = new Element('a', {'title':'First Page'}).set('text', '<<');
		
		firstPageLink.addEvent('click', function() {
			scroll = new Fx.Scroll(window);
			scroll.toElement('directory');
			fetchData(1);
		});
		
		$('hotspotsPager').adopt(firstPageLink);
	}
	
	if(results.page != 1) {	
		prevPageLink = new Element('a', {'title':'Previous Page'}).set('text', '<');
		
		prevPageLink.addEvent('click', function() {
			scroll = new Fx.Scroll(window);
			scroll.toElement('directory');
			fetchData(results.page - 1);
		});
		
		$('hotspotsPager').adopt(prevPageLink);
	}
	
	results.pagerRange.each(function(page){
		
		if (page == results.page) {
			
			pageLink = new Element('span').set('text', page);
			pageLink.addClass("current");
			
		} else {
		
			pageLink = new Element('a', {'title':'page' + page}).set('text', page);
			pageLink.addEvent('click', function() {
				scroll = new Fx.Scroll(window);
				scroll.toElement('directory');
				fetchData(page);
			});

		}
		
		$('hotspotsPager').adopt(pageLink);
		
	});
	
	if(results.page != results.lastPage) {
		nextPageLink = new Element('a', {'title':'Next Page'}).set('text', '>');
		
		nextPageLink.addEvent('click', function() {
			scroll = new Fx.Scroll(window);
			scroll.toElement('directory');
			fetchData(results.page + 1);
		});
		
		$('hotspotsPager').adopt(nextPageLink);
	}
	
	if(!results.pagerRange.contains(results.lastPage)) {
		lastPageLink = new Element('a', {'title':'Last Page'}).set('text', '>>');
		
		lastPageLink.addEvent('click', function() {
			scroll = new Fx.Scroll(window);
			scroll.toElement('directory');
			fetchData(results.lastPage);
		});
		
		$('hotspotsPager').adopt(lastPageLink);
	}
	
//	rebuild results per page
//	$('rpp').empty();
//	[20,50,100,200].each(function(n) {
//		if(n < results.total) {
//			rppOpt = new Element('option').set('text', n);
////			if(n == rpp) {
////				rppOpt.setProperty('selected', 'selected');
////			}
//			$('rpp').adopt(rppOpt);
//		}
//	});
	
//	$('rpp').value = rpp;
	
	$$('.pageRange').set('text', 'Displaying ' + results.firstIndice + '-' + results.lastIndice + ' of ' + results.total + ' hotspots. ');
	
//	$('totalHotspots').set('text', results.total);

}


window.addEvent('domready', function(ev) { 
	// console.log('the dom is ready'); 
	initialize(); 
});
