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 },MM_DECLUTTER_AGGREGATE,MM_CLUSTER_ACCURATE );
        }
        this.markerFields = markerFields;
        this.htmlFields = htmlFields;
        this.htmlResults = htmlResults;
        this.map = map; 

    },  
    addCustomizedMarker: function (point,text) {
	    var icon = new MMIcon( '/i/red-circle.png' );
	    icon.iconSize = new MMDimensions( 32, 32 );
	    icon.iconAnchor = new MMPoint( 16, 16 );
		marker = this.map.createMarker( point, {'label' : text, 'inert' : true, 'icon' : icon} );
	}, 
    storeSearch: function( data, searchpoint ,start) {
    	this.start=start;
        this.map.removeAllOverlays();
        var location = document.getElementById( 'locationsearch' );
    	if (location && location.value) {
    			this.addCustomizedMarker(searchpoint,location.value);
    	}
        this.markers = [], this.records = [];
        var record, marker, bounds, infobox, html = [];
        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 = this.getInfoBoxHtml( record, this.markerFields, 'infobox', 'div', i );
                if (start == "details") {
            		 html.push( this.getDetailsHtml( record, this.htmlFields, 'result', 'a', i ) );   	
 
                } else if (start != 'start') {
                	html.push( this.getLeftHtml( record, this.htmlFields, 'result', 'a', i ) );
                } 
                	
                marker = this.map.createMarker( record.point, { 
                    'label' : ( i + 1 ) + ' ' + record['companyname'], 
                    'icon' : this.icon 
                } );
                marker.setInfoBoxContent( infobox );
                marker.recordid = i;
                this.markers.push( marker );
                this.records.push( record );
                bounds.extend( record.point );
            }
            this.handleResults( html );
            if (start !="details") {
            	bounds.extend( searchpoint );
          	} else {
          		Callbacks.markers['0'].openInfoBox();
          	}
            this.map.goToPosition( this.map.getAutoScaleLocation( bounds ) );
            if (start !="start") {
            	document.body.removeCssClass( 'routesearch' );
            	document.body.addCssClass( 'storesearch' );
            	UpdateRouting( 0 );
            }
        } else {
            this.map.goToPosition( searchpoint );
            document.body.removeCssClass( 'routesearch' );
            document.body.removeCssClass( 'storesearch' );
        }
        return true;
    },
 	singleStoreSearch: function( data, searchpoint ,start) {
        this.map.removeAllOverlays();
        this.markers = [], this.records = [];
        var record, marker, bounds, infobox, html = [];
        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 = this.getInfoBoxHtml( record, this.markerFields, 'infobox', 'div', i );
                
                marker = this.map.createMarker( record.point, { 
                    'label' : ( i + 1 ) + ' ' + record['companyname'], 
                    'icon' : this.icon, 
                    'text' : i + 1  
                } );
                marker.setInfoBoxContent( infobox );
                marker.recordid = i;
                this.markers.push( marker );
                this.records.push( record );
                bounds.extend( record.point );
            }
           
            bounds.extend( searchpoint );
            this.map.goToPosition( this.map.getAutoScaleLocation( bounds ) );
            marker.openInfoBox();
            if (!start) {
            	document.body.removeCssClass( 'routesearch' );
            	document.body.addCssClass( 'storesearch' );

            	UpdateRouting( 0 );
            }
        } else {
            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( '' );
    },
	customOnclick : function( id ) {
        var record, marker;
        record = Callbacks.records[id];
        marker = Callbacks.markers[id];
        marker.openInfoBox();
        UpdateRouting( id );
    },
    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' ) {
            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 + '>';
            }
        }
  //     html=html.replace( /(\<a)+/ig, '<a href="#"' );
     		if ( typeof fields.customInsert2 == 'function' ) {
            html += fields.customInsert2( record, id + 1 );
        }
        html += '</' + rootel + '>';
        return html;
    },
    getLeftHtml: function( record, fields, classname, rootel, id ) {
         var html;
            html ='<a  onclick="Callbacks.customOnclick( '+id+' ); return false;" >';
            html += '<div class="MMLeftboxaddress" onmouseover="this.classname=\'over\';">';
            html += '<img  src="/i/elogo.png" style="float:left;margin:0 1em 1em 0;width:30px;"/>';
              if (Callbacks.start) {
        				html += '<div ><span  class="MMrecordName" >' + record['companyname'] + '<br /></span><br clear="left"/>';
              }			 else {	
         				html += '<div ><span  class="MMrecordName" >' + record['companyname'] + '<br /><span style="font-weight:normal">('+record['distance'].miles+' miles away)</span></span><br clear="left"/>'; 
            }
         			if (record['address1']) {
            	html += record['address1'] + '<br />';
          	}            
            if (record['address2']) {
            	html += record['address2'] + '<br />';
          	}
           	if (record['town']) {
            	html += record['town'] + '<br />';
          	}
            html += record['pc'] + '<br /><br /><span class="tel"><strong>Phone:</strong> ' + record.telephone + '</span><br />';
            if (record['fax']) {
           	 html += '<span class="fax"><strong>Fax:</strong> ' + record['fax'] + '</span><br />';
          	}
            if (record['email']) {
           	 html += '<span class="email"><strong>E-mail:</strong><a href="mailto:'+record['email']+'" title="Mail us">Click to E-Mail</a></span><br />';
          	}     
            if (record['web']) {     
            	html += '<span class="web"><strong>Web:</strong><a href="http://' + record['web'] + '" title="Visit our Web Site" target="_blank">Click to Visit Website</a></span><br />';
  					}

           	html += '<div class="links">';
           	html += '<a href="#" onclick="UpdateRouting('+id+');GetDirectionsFromInfoBox( this, ' + ( id - 1 ) + ' ); return false;">Get Driving Directions</a>';
      
            html += '<form class="directions" action="" method="post" style="display: none">';
            html += '<label for="infoboxDir' + id + '">';
           	html += 'From ';
            html += ' <input class="text" type="text" value="" id="infoboxDir' + id + '" />';
            html += '</label>';
            html += '<input type="image" src="/i/go_button.png" class="submit" />';
            html += '</form>';
            if (record['companyname']) {
            	record['companyname_clean'] = record['companyname'].replace(" ","_");
            	record['companyname_clean'] = escape(record['companyname_clean']);
            

          html += '<a href="/ecu-remapping-evolutionchips-installers/?'+record['companyname_clean']+'.htm" class="gotoloc">View more details >></a>';
               
         }
           	
           html += '</div>';
            html += '</div>';
            html += '</a>';
            return html;
    },
     getDetailsHtml: function( record, fields, classname, rootel, id ) {
         var html;
   
            html ='<img  src="/i/elogo.png" id="installerlogo"/>';
           	html += '<h1>'+record['companyname']+' - '+record['town']+'</h1>';
           
          	html += '<div id="company" >  ';
 
						if (record['address1']) {
            	html += record['address1'] + '<br />';
          	}            
            if (record['address2']) {
            	html += record['address2'] + '<br />';
          	}
           	if (record['town']) {
            	html += record['town'] + '<br />';
          	}
         
       			if (record['county']) {
            	html += record['county'] + '<br />';
          	}
    				html += record['pc'] + '<br /><br />';
           
 
         		html += ' </div>';
          	
          	html += '<div id="contact" >';
          	html += '<span class="tel"><strong>Phone:</strong> ' + record['telephone'] + '</span><br />';
            if (record['fax']) {
           	 html += '<span class="fax"><strong>Fax:</strong> ' + record['fax'] + '</span><br />';
          	}
            if (record['email']) {
           	 html += '<span class="email"><strong>Email:</strong> <a href="mailto:'+record['email']+'" title="Mail us">'+record['email']+'</a></span><br />';
          	}     
            if (record['web']) {     
            	html += '<span class="web"><strong>Web:</strong> <a href="http://' + record['web'] + '" title="Visit our Web Site" target="_blank">'+record['web']+'</a></span><br />';
  					}	
          	
          	html +='<div class="links">';
          	html += '<a href="#" onclick="UpdateRouting('+id+');GetDirectionsFromInfoBox( this, \'0\' ); return false;">Get Driving Directions</a>';
            html += '<form class="directions" action="" method="post" style="display: none">';
            html += '<label for="infoboxDir' + id + '">  From  ';
            html += '<input class="text" type="text" value="" id="infoboxDir' + id + '" />';
            html += ' </label>';
            html += '<input type="image" src="/i/go_button.png" class="submit" />';;
            html += '</form>';
            html += '</div>';
      
            html += '</div>';
            if (record['installertext']) {
            	html +='<div id="message" >'+BBC2HTML(record['installertext'])+'</div>';	
            }
            html += '<a href="/dealer2.html" class="back">Back To Search</a>'; 
            document.title='Ecu remapping evolutionchips installers '+record['companyname']+' - '+record['town']+' '+record['county'];
            return html;
    }
}
