Callbacks = {
    map: null,
    icon: null,
    markers: [],
    records: [],
    markerFields: {},
    htmlFields: {},
    init: function( map, markerFields, htmlFields, htmlResults, icon, cluster_icon ) {
        this.icon = icon;
        if ( cluster_icon ) {
            map.declutterGroup( icon.groupName, { 'cluster_icon' : cluster_icon } );
        }
        this.markerFields = markerFields;
        this.htmlFields = htmlFields;
        this.htmlResults = htmlResults;
        this.map = map;
    },
    storeSearch: function( data, searchpoint ) {
        this.map.removeAllOverlays();
        this.markers = [], this.records = [];
        var record, marker, bounds, infobox, html = [];
        bounds = new MMBounds();
        var resultsText = '<p class="resultsText"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Your nearest legal aid solicitors ';
        var filter = document.getElementById( "locationsearch" );
        if ( filter.value != '' ) {
            resultsText += 'to ' + filter.value;
            var filter4 = document.getElementById( "f_filter4" );
            if ( filter4.checked ) {
                resultsText += ' , for Civil Cases ';
                var select = document.getElementById( "firmtypeList" );
                selectVal = select.options[select.selectedIndex].value;
                if ( selectVal ) {
                    resultsText += ' (' + selectVal + ')';
                }
            }
            var filter2 = document.getElementById( "f_filter2" );
            if ( filter2.checked ) {
                resultsText += ' , for Criminal Cases';
            }
            var filter3 = document.getElementById( "f_filter3" );
            if ( filter3.checked ) {
                resultsText += ' , for Children\'s hearings and appeals';
            }
        }
        resultsText += '...</strong></font></p>';
        html.push( resultsText );
        var pagination = document.getElementById( 'paginationButtons' );
        pagination.innerHTML = '';
        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];
                startIndex = Page.storeSearch.iIndex - 1;
                infobox = this.getInfoBoxHtml( record, this.markerFields, 'infobox', 'div', i + startIndex );
                html.push( this.getHtml( record, this.htmlFields, 'result', 'a', i + startIndex ) );
                marker = this.map.createMarker( record.point, { 
                    'label' : ( i + 1 + startIndex ) + ' ' + record['name'], 
                    'icon' : this.icon, 
                    'text' : i + 1 + startIndex 
                } );
                marker.setInfoBoxContent( infobox );
                marker.recordid = i + 1 + startIndex;
                this.markers.push( marker );
                this.records.push( record );
                bounds.extend( record.point );
            }
            if ( Page.storeSearch.iIndex > 1 ) {
                var a = document.createElement( 'a' );
                a.href = '#';
                a.className = 'prev';
                a.onclick = function () {
                    Page.storeSearch.getData(undefined, -10);
                };
                var a_text = '< prev ';
                a.appendChild( document.createTextNode( a_text ) );
                pagination.appendChild( a );
            }   
                var a = document.createElement( 'a' );
                a.href = '#';
                a.className = 'next';
                a.onclick = function () {
                    Page.storeSearch.getData(undefined, 10);
                };
                var a_text = ' next >';
                a.appendChild( document.createTextNode( a_text ) );
                pagination.appendChild( a );
                var paginationDiv = document.getElementById( 'pagination' );
                paginationDiv.style.display = 'block';
            this.handleResults( html );
            bounds.extend( searchpoint );
            this.map.goToPosition( this.map.getAutoScaleLocation( bounds ) );
            document.body.removeCssClass( 'routesearch' );
            document.body.addCssClass( 'storesearch' );
            UpdateRouting( 0 );
        } else {
            resultsDiv = document.getElementById( 'results' );
            resultsDiv.innerHTML = '';
            noResults = document.createElement( 'div' );
            noResults.innerHTML = 'No Results Found';
            resultsDiv.appendChild( noResults );
            resultsDiv.style.display = 'block';
            this.map.goToPosition( searchpoint );
            document.body.removeCssClass( 'routesearch' );
            document.body.removeCssClass( 'storesearch' );
        }
                
            
        return true;
    },
    handleResults: function( results ) {
        this.htmlResults.innerHTML = '';
        this.htmlResults.innerHTML = results.join( '' );
    },
    getInfoBoxHtml: function( record, tabs, classname, rootel, id ) {
        var markertabs = [], tabhtml, validtab;
        for ( var tab in tabs ) {
            tabhtml = this.getHtml( record, tabs[tab], classname, rootel, id );
            if ( tabhtml.match( /class/ig ).length > 1 ) {
                validtab = tabhtml;
                markertabs.push( new MMInfoBoxTab( tab, tabhtml ) );
            }
        }
        if ( markertabs.length > 1 ) {
            return markertabs;
        } else {
            return validtab;
        }
    },
    getHtml: function( record, fields, classname, rootel, id ) {
        var html;
        if ( fields.addOnClick ) {
            if ( typeof fields.customOnclick == 'function' ) {
                window[classname + 'customonclick'] = fields.customOnclick;
                html = '<' + rootel + ' class="MM' + classname + 'Root" onclick="' + classname + 'customonclick( ' + id + ' ); return false;">';
            } else {
                html = '<' + rootel + ' class="MM' + classname + 'Root" onclick="Callbacks.markers[' + id + '].openInfoBox(); return false;">';
            }
        } else {
            html = '<' + rootel + ' class="MM' + classname + 'Root">';
        }
        if ( typeof fields.customInsert == 'function' ) {
            name = record['name'];
            html += fields.customInsert( record, id + 1, name );
        }
        for ( var field in fields ) {
            if ( record[fields[field].fieldname] ) {
                html += '<' + fields[field].element + ' class="MM' + classname + fields[field].fieldname + '">';
                html += record[fields[field].fieldname].toLowerCase();
                html += '</' + fields[field].element + '>';
            }
        }
        html += '</' + rootel + '>';
        if ( classname == 'infobox' ) {
            return html;
        } else {
            return html.replace( /(\<a)+/ig, '<a href="#"' );
        }
    }
}

