

hq.extentsSearch = {

	baseUrl : '/wp-content/plugins/hq_idx/ajax.php',

	id : 'extentsSearch',
	
	layerId  : null,

	zoomLevel : null,
	
	suspend : false,

	// If the map is dragged more than this distance, redo the listing search
	pixelDiff : 75,

	init : function() {		
		console.group('extentsSearch::init');


		// These two events are UNDOCUMENTED. They trigger when the user pans or zooms and do not trigger on a call to setCenter or setZoom
		GEvent.addListener( hq.map.gMap, 'zoomto', toolbox.delegate(this.moveEnd, this));
		GEvent.addListener( hq.map.gMap, 'panto', toolbox.delegate(this.moveEnd, this));

		$j('#contentWrapper').bind('layerChange', toolbox.delegate(this.onLayerChange, this));
	
		this.zoomLevel = $j('#' + this.id).attr('zoomLevel');
		this.layerId = $j('#' + this.id).attr('layerId');

		this.extents = toolbox.getExtents(hq.map.gMap);

		// Sutract the pixel difference from these vars until they roll over. Then do the search and reset the values
		this.xBuffer = this.yBuffer = this.pixelDiff; 
		
		this.origExt = this.extents;

		this.activeLayer = $j('a.activeLayer').attr('layerId');

		if(this.activeLayer==this.layerId) {
			// Set these to zero so we make sure to do the first search
                        if(!$j('#elist').val())
                            this.doSearch();
		}

		console.groupEnd();
	},

	onLayerChange : function(evt) {
		console.group('extentsSearch::onLayerChange');

		this.activeLayer = $j('a.activeLayer').attr('layerId');
		if(this.activeLayer==this.layerId) {
			this.doSearch(evt);
		}

		console.groupEnd();
	},

	doSearch : function(evt) {
		console.group('extentsSearch::doSearch');

		if(this.suspend) {
                        console.groupEnd();
                        return false;
                }

		if(this.activeLayer != this.layerId) {
			console.groupEnd();
			return;
		}

		var extents = toolbox.getExtents(hq.map.gMap);
                var loadedSpaces = false;

                var zoom = hq.map.gMap.getZoom();
                this.activeLayer = $j('a.activeLayer').attr('layerId');

                if(zoom < this.zoomLevel) {
                               console.info('aborting...zoom: ' + zoom  );
                               console.groupEnd();
                                return false;
                }

		// If this is a saved search we are modifying, include the id in the query
		var ssId = $j('#ssId').val();		
		var url = this.baseUrl + '?byExtents=1&minX=' + extents.minX + '&maxX=' + extents.maxX + '&minY=' + extents.minY + '&maxY=' + extents.maxY;	

		if(ssId)
			url += '&ssOp=run&ssId=' + ssId;

		hq.ajax.loadLink(url);

		console.groupEnd();
	},
	
	moveEnd : function(evt) {
		console.group('extentsSearch::moveEnd');

		if(this.suspend) {
			console.groupEnd(); 
			return false;
		}

		var zoom = hq.map.gMap.getZoom();
                if(zoom < this.zoomLevel) {
                               console.info('aborting...zoom: ' + zoom  );
                               console.groupEnd();
                               return false;
                }

		this.activeLayer = $j('a.activeLayer').attr('layerId');
		if(this.activeLayer != this.layerId) {
                        console.groupEnd();
                        return;
                }

		var oldExtents = this.extents;
		
		this.extents = toolbox.getExtents(hq.map.gMap);
 
		var newMin = new GLatLng(this.extents.minY, this.extents.minX);
		var newMinPt = hq.map.gMap.fromLatLngToContainerPixel(newMin);

		var oldMin = new GLatLng(oldExtents.minY, oldExtents.minX);
		var oldMinPt = hq.map.gMap.fromLatLngToContainerPixel(oldMin);


		var doSearch = false;
		var xDiff = Math.abs(newMinPt.x - oldMinPt.x);
		var yDiff = Math.abs(newMinPt.y - oldMinPt.y);

		if(xDiff > this.xBuffer) {
			doSearch = true;
			this.xBuffer = this.pixelDiff;
			console.info('x overflow');
		} else {
			this.xBuffer -= xDiff;
		}

		if(yDiff > this.yBuffer) {
                        doSearch = true;
                        this.yBuffer = this.pixelDiff;
			console.info('y overflow');
                } else {
                        this.yBuffer -= yDiff;
                }

		if(doSearch==true) {
			this.doSearch();		
		}

		console.groupEnd();
	}


};

$j(document).ready(toolbox.delegate(hq.extentsSearch.init, hq.extentsSearch));

