if(hq==undefined) {
	var hq = {};
}

hq.savedSearch = 
{

	savedSearchFormUrl : null,

	init : function() {
		$j( '#contentWrapper' ).bind( 'postPaneLoad', toolbox.delegate( this.onPostPaneLoad, this ) );

		// Enable saved search drop down clickability
                $j('.savedSearchLink').unbind('click').click(toolbox.delegate(this.onSavedSearchClick, this));
		$j('.addSavedSearchLink').unbind('click').click(toolbox.delegate(this.openSavedSearchForm, this, 'save'));
                $j('.saveAsSavedSearchLink').unbind('click').click(toolbox.delegate(this.openSavedSearchForm, this, 'saveAs'));

                this.savedSearchFormUrl = $j('#pluginPath').val();
                
		this.onPostPaneLoad();

	},

	onPostPaneLoad : function() {

		$j('.deleteSearchBtn').unbind('click').click(function(evt) {
			if(confirm('Are you sure you want to delete this search?')) {
				var $tgt = $j(evt.target);
				var href = $tgt.attr('href');

				if(!href)
					href = $tgt.parents('a:first').attr('href');

				if(href)
					hq.ajax.loadLink(href);

				return false;
			} else {
				evt.stopPropagation();
				return false;		
			}
		});
		// Add search link to wherever
		//$j('.addSavedSearchLink').click(toolbox.delegate(this.openSavedSearchForm, this, 'save'));
//		$j('.saveAsSavedSearchLink').click(toolbox.delegate(this.openSavedSearchForm, this, 'saveAs'));
	
		// Enable saved search drop down clickability
//		$j('.savedSearchLink').click(toolbox.delegate(this.onSavedSearchClick, this));
	},
	
	openSavedSearchForm : function(obj, saveType) {
		console.group('SavedSearch::openSavedSearchForm');
		var url = this.savedSearchFormUrl + '/ajax.php';
		var ssId = $j('#ssId').val();
                var mapType = hq.map.gMap.getCurrentMapType().getName();

                if(!url) {
			console.groupEnd();
 	        	return false;
		}

                if(url.indexOf('?')==-1)
         	       url += '?';

		// Are we adding a search or updating?
		if(ssId && saveType=='save') 
			url += '&ssId=' + ssId;
		
                url += '&mapType=' + mapType;

		$filters = $j('#filterStore');

                url += '&lowPrice=' + $filters.attr('lowprice');
                url += '&highPrice=' + $filters.attr('highprice');
                url += '&lowBeds=' + $filters.attr('lowbeds');
                url += '&highBeds=' + $filters.attr('highbeds');
                url += '&lowYear=' + $filters.attr('lowyear');
                url += '&highYear=' + $filters.attr('highyear');
                url += '&lowSqFt=' + $filters.attr('lowsqft');
                url += '&highSqFt=' + $filters.attr('highsqft');

		var type = $filters.attr('proptype');
                if(type) {
                	var pTypes = type.split(',');
                        for(var i=0;i<pTypes.length;i++) {
                        	url += '&type[]=' + pTypes[i];
                        }
                }

		if($j('#metroId').val()==1) {
			url += '&bankOwned=' + $filters.attr('bankowned');
			url += '&shortSale=' + $filters.attr('shortsale');
		}

		var spaces = $j('#loadedSpaceIds').val();
		spaces = spaces.split(',');

		for(var i=0; i<spaces.length; i++) {
			url += '&spaces[]=' + spaces[i];
		}	
		
		var extents = hq.map.getBounds();
		for(i in extents) {
			url += '&'+i + '=' + extents[i];
		}
		var spaceType = $j('#loadedSpaceLayerId').val();
		url += '&layerId=' + spaceType;

		if(saveType=='save') {
			var ssId = $j('#ssId').val();
			url += '&ssId=' + ssId;
		}

		url += '&ssOp=' + saveType;
		hq.ajax.loadLink(url);
			
		console.groupEnd();

		return false;
	},

	/*
	saveSearch : function() {
		return;
		$j('#permalinkBox').hide();
		
		$j('#getPermalinkAnchor').click(function() {
			var url = $j('#permalinkUrl').val();
			var mapType = hq.map.gMap.getCurrentMapType().getName();
		
			if(!url) 	
				return false;
				
			if(url.indexOf('?')==-1)
				url += '?';
				
			url += '&mapType=' + mapType;
			
			$filters = $j('#filterStore');
			
			url += '&lowPrice=' + $filters.attr('lowprice');
			url += '&highPrice=' + $filters.attr('highprice');
			url += '&lowBeds=' + $filters.attr('lowbeds');
			url += '&highBeds=' + $filters.attr('highbeds');
			url += '&lowYear=' + $filters.attr('lowyear');
			url += '&highYear=' + $filters.attr('highyear');
			url += '&lowSqFt=' + $filters.attr('lowsqft');
			url += '&highSqFt=' + $filters.attr('highsqft');
			
			var type = $filters.attr('proptype');
		        if(type) {
        	    		var pTypes = type.split(',');
	        	        for(var i=0;i<pTypes.length;i++) {
                			url += '&type[]=' + pTypes[i];
		                }
		        }
				
			var spaceId = $j('#curSpaceId').val();
			var spaceType = $j('#curSpaceType').val();
			
			if(spaceId && spaceType) {
				url += '&spaceId=' + spaceId + '&spaceType=' + spaceType;
				url += '&action=filters';
			}
			
			url += '&filterOp=Update'; 
			
			pl = this;
			
			$j.ajax({
				url : '/wp-content/plugins/hq_idx/utilities/tinyurl.php',
				data : {
					url: url
				},
				success : function(res) { 
					
					var offset = $j('#getPermalinkWrapper').offset();
					
					$j('#getPermalinkBubble').css('top', offset.top + 33).css('left', offset.left+20).show();
					
					$j('#permalinkTextbox').val(res);
					
					$j('#permalinkClose').click(function() {
						$j('#getPermalinkBubble').hide();
					});
					
					//$j('#permalinkBox').show().val(res);
				},//toolbox.delegate( this.process, this)
			});
			
		});
	},
	*/

	onSavedSearchClick : function(evt) {
		var $target = $j( evt.target );
		var ssId = $target.attr('id');
		var url = this.savedSearchFormUrl + '/ajax.php?';
		var prevLayerId = $j('.activeLayer').attr('layerId');
		var layerId = $target.attr('layerId');
		
		$j('#loadedSpaceLayerId').val(layerId);

		if(hq.extentsSearch)
			hq.extentsSearch.suspend = 1;
		
		if(layerId!=prevLayerId) {
			var overlay = $j(".overlay[layerId='"+layerId+"']");
			overlay.trigger('click', {'noClear' : true});
		}

		ssId = ssId.substring(3);
		url += 'ssOp=run&noFilters=1&ssId=' + ssId ;
			
		if(url.indexOf('activeLayer')==-1)
                        url += '&activeLayer=' + layerId;

		/*
		var event = jQuery.Event('layerChange');
                event.id =  
                event.layerLocation = '';
                event.noLayer = false;
		event.noClear = true;
*/
		// Hack to make sure the extents search doesn't execute when we switch layers		
//		hq.extentsSearch.suspend = 1;

//                $j('#contentWrapper').trigger(event);

		hq.listings.clear();

		$j('#contentWrapper').bind('paneLoaded', function() {
                        hq.extentsSearch.suspend = 0;
                });


		hq.ajax.loadLink(url);

		return false;		
	},

	
	close : function() {
		$j('#getPermalinkBubble').hide();
	},
	process : function(res) {
		
	
	}
	
};


$j( '#contentWrapper' ).bind( 'paneLoaded', toolbox.delegate( hq.savedSearch.init, hq.savedSearch ) );
