OtherStoreSearch = function( map, dataSource, count, filters, radius, callback, resultdiv, nearestresultdiv, icon ) {
    this.callback = callback;
    this.map = map;
    this.count = count;
    this.radius = radius;
    this.dataSource = ( typeof dataSource != 'undefined' ) ? dataSource : '';
    this.filters = filters;
    this.resultdiv = resultdiv;
    this.nearestresultdiv = nearestresultdiv;
    this.icon = icon;
    this.init();
}

OtherStoreSearch.prototype = {
    searcher: null,
    init: function() {
        var me = this;
    },
  getData: function( point, start_index ) {
   		callback=this.callback;
   		var lat=point.lat();
		var lon=point.lng();
   		var downloadString="/php/json.php?"+"lat="+lat+"&lon="+lon+"&type=private+funerals";
    	GDownloadUrl(downloadString,function(data,responseCode) {
			 if(responseCode == 200) {
			 	var jsonData = eval("(" + data + ")");
			 	this.callback( jsonData, point );	
			} else if(responseCode == -1) {
			    alert("Data request timed out. Please try later.");
			  } else { 
			    alert("Request resulted in error. Check XML file is retrievable.");
			  }
    });  
    },
    getRadius: function() {
        if ( typeof this.radius == 'undefined' ) {
            this.getRadius = function() {
                return false;
            }
        } else if ( typeof this.radius == 'string' || typeof this.radius == 'number' ) {
            this.getRadius = function() {
                return this.radius;
            }
        } else if ( typeof this.radius == 'function' ) {
            this.getRadius = function() {
                return this.radius();
            }
        } else {
            this.getRadius = function() {
                return this.radius.value;
            }
        }
        return this.getRadius();
    },
    getFilters: function( search ) {
        if ( typeof this.filters == 'function' ) {
            this.getFilters = function( search ) {
                return this.filters( search );
            }
        } else if ( typeof this.filters == 'undefined' ) {
            this.getFilters = function( search ) {
                return search;
            }
        } else {
            this.getFilters = function( search ) {
                search.filters = [];
                search.logic = 'AND';
                for ( var i = 0, j = this.filters.length; i < j; i++ ) {
                    var filter = this.filters[i].element;
                    if ( filter.value != '' ) {
                        search.filters.push( new MMSearchFilter( filter.name, this.filters[i].logic, filter.value ) );
                    }
                }
                return search;
            }
        }
        return this.getFilters( search );
    },
    getIcon: function( record ) {
        if ( typeof this.icon == 'function' ) {
            this.getIcon = function() {
                return this.icon( record );
            }
        } else {
            this.getIcon = function( record ) {
                return this.icon;
            }
        }
        return this.getIcon( record );
    },
    addMarkers: function( data ) {
        this.map.removeAllOverlays();
        var infobox, marker, bounds, html, record;
        html = [];
        this.markers = [];
        this.records = [];
        bounds = new MMBounds();
        if ( data.totalRecordCount > 0 && data.records && data.records.length > 0 ) {
            for ( var i = 0, j = data.records.length; i < j; i++ ) {
                record = data.records[i];
                infobox = GenerateInfoBox( record, this.index + i );
                marker = this.map.createMarker( record.point, { label: record.name, icon: this.getIcon( record ), text: this.index + i + 1, index_title: record.caption } );
                marker.setInfoBoxContent( infobox );
                marker.index_title = record.caption;
                this.markers.push( marker );
                this.records.push( record );
                bounds.extend( record.point );
                if ( i == 0 && this.nearestresultdiv ) {
                    this.nearestresultdiv.innerHTML = '';
                    this.nearestresultdiv.innerHTML = '<h4>Your nearest store</h4>' + GenerateHtml( record, this.index + i, true );
                } else {
                    html.push( GenerateHtml( record, this.index + i ) );
                }
            }
        } else {
            html.push( '<div class="noresult"><h4>Sorry!</h4><p>There are no branches matching this search.</p></div>' );
        }
        if ( typeof this.resultdiv != 'undefined' ) {
            this.resultdiv.innerHTML = '';
            this.resultdiv.innerHTML = '<h4>Other stores in this area</h4>' + html.join( '' );
        }
        if ( typeof bounds != 'undefined' && bounds instanceof MMBounds ) {
            bounds.extend( this.point );
            this.map.goToPosition( this.map.getAutoScaleLocation( bounds ) );
        } else {
            this.map.goToPosition( this.point );
        }
    },
    displayMarkers: function() {
        this.map.removeAllOverlays();
        if ( this.markers && this.markers.length > 0 ) {
            var bounds = new MMBounds();
            for ( var i = 0, j = this.markers.length; i < j; i++ ) {
                this.map.addOverlay( this.markers[i] );
                bounds.extend( this.records[i].point );
            }
            this.map.goToPosition( this.map.getAutoScaleLocation( bounds ) );
        }
    },
    generalCallback: function() {
        var data = arguments[0];
        if ( data[0][1] != null ) {
            alert( 'An error occurred' );
        } else {
            if ( typeof this.callback != undefined ) {
                this.callback( data[0][0], this.point );
            } else {
                this.addMarkers( data[0][0] );
            }
        }
        MMElement.prototype.addCssClass( 'StoreSearch', MMElement.prototype.removeCssClass( 'RouteSearch', document.body ) );
    }
}

