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 pagination = document.getElementById( 'paginationButtons' );
        pagination.innerHTML = '';
        var capRecords = 10;
        if ( data.totalRecordCount > 0 && data.records && data.records.length > 0 ) {
            var resultsHead = document.getElementById( 'resultsHead' );
            var searchText = document.getElementById( 'locationsearch' ).value;
            if ( data.totalRecordCount < capRecords ){
                resultsHead.innerHTML =  ' In ' +  searchText  + ' we have ' + data.totalRecordCount + ' Interpart approved stockist(s) near you ' ;
            } else {
                resultsHead.innerHTML = ' In ' +  searchText  + ' we have ' + capRecords + ' Interpart approved stockist(s) near you ' ;
            }
            startIndex = Page.storeSearch.index;
            pageIndex = Page.storeSearch.pageIndex;
            for ( var i = 0, j = data.records.length; i < j; i++ ) {
                record = data.records[i];
                infobox = this.getInfoBoxHtml( record, this.markerFields, 'clrInfobox', 'div', i + startIndex );
                html.push( this.getHtml( record, this.htmlFields, 'result', 'a', i + startIndex, startIndex ) );
                marker = this.map.createMarker( record.point, { 
                    'label' : ( i + startIndex ) + ' ' + record['name'], 
                    'icon' : this.icon, 
                    'text' : i + startIndex  
                } );
                marker.setInfoBoxContent( infobox, { className : 'clrInfobox', min_width : 310 } );
                marker.recordid = i + startIndex;
                marker.recordPage = startIndex;
                this.markers.push( marker );
                this.records.push( record );
                bounds.extend( record.point );
            }
            
            if ( pageIndex > 1 ) {
                var a = document.createElement( 'a' );
                a.href = '#';
                a.className = '';
                attachLinkEvent(a, 1);
                var a_text = '';
                a.appendChild( document.createTextNode( a_text ) );
                pagination.appendChild( a );
                pagination.appendChild( document.createTextNode( ' | ' ) );
                var a = document.createElement( 'a' );
                a.href = '#';
                a.className = '';
                attachLinkEvent(a, pageIndex - 1);
                var a_text = '';
                a.appendChild( document.createTextNode( a_text ) );
                pagination.appendChild( a );
                pagination.appendChild( document.createTextNode( ' | ' ) );
            }
            if ( data.totalRecordCount > 10 && data.totalRecordCount < capRecords ) {
                for (i = 0; i * 10 < data.totalRecordCount; i++ ) {
                    var el = document.createElement ( 'a' );
                    el.href = '#';
                    pageStart = i * 10;
                    var test = i;
                    attachLinkEvent(el, i + 1);
                    el_text = i + 1;
                    el.appendChild( document.createTextNode( el_text ) );
                    pagination.appendChild( el );
                    pagination.appendChild( document.createTextNode( ' ' ) );
                }
            } else if ( data.totalRecordCount > 10 ){
                for (i = 0; i * 10 < capRecords; i++ ) {
                    var el = document.createElement ( 'a' );
                    el.href = '#';
                    pageStart = i * 10;
                    var test = i;
                    attachLinkEvent(el, i + 1);
                    el_text = i + 1;
                    el.appendChild( document.createTextNode( el_text ) );
                    pagination.appendChild( el );
                    pagination.appendChild( document.createTextNode( ' ' ) );
                }
            }
            if ( startIndex <= data.totalRecordCount - 4 && startIndex < capRecords - 3 ) {
                pagination.appendChild( document.createTextNode( '' ) );
                var a = document.createElement( 'a' );
                a.href = '#';
                a.className = '';
                attachLinkEvent(a, pageIndex + 1);
                var a_text = '';
                a.appendChild( document.createTextNode( a_text ) );
                pagination.appendChild( a );
                pagination.appendChild( document.createTextNode( '' ) );
                var a = document.createElement( 'a' );
                a.href = '#';
                a.className = '';
                attachLinkEvent(a, i);
                var a_text = '';
                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 ) );
        } else {
            this.map.goToPosition( searchpoint );
            var resultsHead = document.getElementById( 'resultsHead' );
            resultsHead.innerHTML = 'Sorry, there are no Interpart approved stockist(s) near your search location';
            html = '';
            this.handleResults( html );
        }
        return true;
    },
    handleResults: function( results ) {
        this.htmlResults.innerHTML = '';
        if (results) 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, startIndex ) {
        var html;
        if ( fields.addOnClick ) {
            if ( typeof fields.customOnclick == 'function' ) {
                window[classname + 'customonclick'] = fields.customOnclick;
				
				
			 
		
                html = '<div id="rowNum' + (id) + '" class="resultRow">';


				html += '<div class="col-left">';
				html += '<br/>';
                html += '<p><strong>' + record.name + '</strong></p>';
				html += '<p id="telephone-text">tel: ' + record.tel + '</p>';
                html += '<p><div id="address">fax: ' + record.fax + '</div></p>';
				html += '</div>';
				
				html += '<div class="col-mid">';
				html += '<br/>';
				html += '<p><strong>Address:</strong></p>';
                                html += '<p><div id="address">' + record.addr1 + '</div></p>';
                                html += '<p><div id="address">' + record.addr2 + '</div></p>';
                                html += '<p><div id="address">' + record.addr3 + '</div></p>';
				html += '<p><div id="address">' + record.addr4 + '</div></p>';
				html += '<p><div id="address">' + record.addr5 + '</div></p>';
				html += '<p><div id="address">' + record.pc + '</div></p>';
				html += '</div>';
				
                                
 
    			html += '<div class="col-right">';
				html += '<br/>';
                html += '<p> <strong>Web:</strong> ' + record.url + '</p>';
				html += '</div>';
				
				html += '<div class="col-far-right">';
				html += '<br/>';
				if ( record.distance ) html += '<p><a onclick="resultcustomonclick(' + (id) + ', ' + startIndex + '); return false;" href="#">' + record.distance.miles + ' miles</a>';
				
				html += '</div>'; 
				html += '</div>';
 


				
            } else {
                html = '<' + rootel + ' class="MM' + classname + 'Root" onclick="Callbacks.markers[' + id + '].openInfoBox(); return false;">';
            }
            return html;
        } else {
            html = '<' + rootel + ' class="MM' + classname + 'Root">';
        }
        if ( typeof fields.customInsert == 'function' ) {
            html += fields.customInsert( record, id + 1 );
        }
        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 + '>';
            }
        }
        return html.replace( /(\<a)+/ig, '<a href="#"' );
    }
}
function attachLinkEvent(paglink, i) {
    paglink.onclick = function() {
        Page.storeSearch.getData(undefined, i);
        return false;
    };   
}

