StoreSearch = 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 ) ? icon : MM_DEFAULT_ICON.copy();
    this.init();
}

StoreSearch.prototype = {
    searcher: null,
    init: function() {
        var me = this;
        this.searcher = new MMSearchRequester( function() {
            me.generalCallback( arguments );
        } );
    },
    getData: function( point, start_index ) {      
       if ( point && !start_index ){
            this.point = point;
            var search = new MMSearch();
            search.count = this.count;
            if ( this.dataSource ) {
                search.data_source = this.dataSource;
            }
            if ( point && point instanceof MMLatLon ) {
                search.point = point;
                search.radius_units = 'miles'; 
                search.max_distance = this.getRadius();
				search.order_by_fields = 'distance';
                search.order_by_order = 'asc';
            } else {
                search.bounding_box = this.map.getMapBounds();
            }
            if ( this.filters ) {
                search = this.getFilters( search );
            }
            this.index = 1;
            this.pageIndex = 1;
            search.start_index = 0;
            this.search = search;
        } else {
            var sIndex = Number( ( ( Number(start_index) - 1 ) * Number(0) ) + 1 );
            this.pageIndex = start_index;
            this.index = sIndex;
            this.search.start_index = sIndex;
        }
        this.searcher.search( this.search );
    },
    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 );
    },
    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] );
            }
        }
    }
}
