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;
     
    },
    getIcon: function( record,i ) {
        if ( typeof this.icon == 'function' ) {
            this.getIcon = function( record ) {
                return this.icon( record ,i);
            }
        } else {
            this.getIcon = function( record ,i) {
                return this.icon;
            }
        }
        return this.getIcon( record );
    },
    storeSearch: function( data, searchpoint ) {
//	alert(searchpoint);
    	this.map.setCenter(searchpoint, 16);
 		this.map.clearOverlays();
 		this.htmls=[];
        this.markers = [], this.records = [];
        var record, marker, bounds, infobox, html = [];
        bounds = new GLatLngBounds(); 
        var last_point_lat=0;
        var last_point_lon=0;
        var offset=0;
        if ( data.records && data.records.length > 0 ) {
            if ( data.records.length > 1 ) {
                for ( var i = 0, j = data.records.length; i < j; i++) {
                    record = data.records[i];
					infobox = this.getInfoBoxHtml( record, this.markerFields, 'infobox', 'div', i );
                    html.push( this.getHtml( record, this.htmlFields, 'result', 'table', i ) );
	            	this.htmls[i]=infobox;
	            	if (last_point_lat==record.point.lat && last_point_lon==record.point.lon) {
	            		var new_lat=Number(record.point.lat);
	            		offset=Number(offset)+Number(0.0002);
	            		var new_lon=Number(record.point.lon)+Number(offset);
	            	    var point = new GLatLng(new_lat, new_lon);	
	            	} else {
	            		offset=0;
	            		var point = new GLatLng(record.point.lat, record.point.lon);        	
	            	}
	            	marker=this.createMarker(point,record,infobox,i);
	            	this.map.addOverlay(marker);
                    this.markers.push( marker );
                    this.records.push( record );
                    bounds.extend( point );
	            	var last_point_lat=record.point.lat;
                    var last_point_lon=record.point.lon;
                }
                this.handleResults( html );
                bounds.extend( searchpoint );
				this.map.setCenter(searchpoint, this.map.getBoundsZoomLevel(bounds)); 
            } else {
                this.records.push( data.records[0] );
                this.markers.push( marker );
            }
        } else {
 				bounds.extend( searchpoint );
				this.map.setCenter(searchpoint,9); 

        }
  		if (Page.oParams.type == "funerals") {
           Page.OtherStoreSearch.getData( address.coords );
          }

        return true;
    },
    handleResults: function( results ) {
        if ( this.htmlResults ) {
            var header = 'Branch name';
            if ( Page.oParams.type.toLowerCase() == 'pharmacy' || Page.oParams.type.toLowerCase() == 'food' ) {
                header = 'Store';
            }
            this.htmlResults.innerHTML = '';
            var html = '';
            html += '<tr class="MMTableRoot MMTableHeader">';
            html += '<td class="MMResultFragment MMName">' + header + '</td>';
            html += '<td class="MMResultFragment MMDistance">Distance</td>';
            html += '<td class="MMResultFragment MMAddress">Contact details</td>';

            html += '<td class="MMResultFragment MMLinks">&nbsp;</td>';
            html += '</tr>';
            this.htmlResults.innerHTML = '<table cellspacing="0" cellpadding="0" class="MMresultRoot">' + html + results.join( '' ) + '</table>';
        }
    },
    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;
        }
    },
    
    
   createMarker:  function(point, record,infoboxhtml,i) {
   	 //          marker = createMarker( record.point, { 
          //              'label' : record.store_name, 
          //              'icon' : this.getIcon( record ), 
           //             'text' : i   + 1
           //         } );
           
  // Set up our GMarkerOptions object
	  markerOptions = { icon:Page.getIcon( record,i ),title : record.store_name,zIndexProcess:inverseOrder};
	  
	  var marker = new GMarker(point, markerOptions);
	 	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml( infoboxhtml);
	  last_marker=point;
  });
  return marker;
},
getHtml: function( record, fields, classname, rootel, id ) {
        var html;
        if ( typeof fields.customInsert == 'function' ) {
            return fields.customInsert( record, id );
        }
        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">';
        }
        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];
                html += '</' + fields[field].element + '>';
            }
        }
        html += '</' + rootel + '>';

        return html.replace( /(\<a)+/ig, '<a href="#"' );
    }
}
function inverseOrder(marker,b) {
        return -GOverlay.getZIndex(marker.getPoint().lat());
}
function orderOfCreation(marker,b) {
        return 1;
}

