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_GRID );
        }
        this.markerFields = markerFields;
        this.htmlFields = htmlFields;
        this.htmlResults = htmlResults;
        this.map = map;
    },
    storeSearch: function( data, searchpoint ) {
    
		if ($("#print-directions").css("display") == "block") {
			$("#print-directions").hide();
		}
		// wiredminds tagging
		/*if (wiredminds != undefined) {
			wiredminds.count("Searched");
		}*/
		// atlas tagging
		var atlas = document.createElement("img");
		atlas.src = "http://switch.atdmt.com/action/brmdfs_StoreFoundMap_6";
		atlas.width = "1";
		atlas.height = "1";
		atlas.alt = "";
		document.getElementsByTagName("body")[0].appendChild(atlas);
		
		/*elementFrame = document.createElement("iframe");
		elementFrame.setAttribute("src", "http://view.atdmt.com/iaction/brmdfs_SpecificStoreDetailsUAT_1");
		elementFrame.setAttribute("height", "1");
		elementFrame.setAttribute("width", "1");
		elementFrame.addClassName("wired-minds");
		document.getElementsByTagName("body")[0].appendChild(elementFrame);*/
						
        this.map.removeAllOverlays();
        this.markers = [], this.records = [];
        var record, marker, bounds, infobox, html = [];
        bounds = new MMBounds();
        recordCount = data.records.length;
        record = data.records[0];
        if (recordCount > 5 && record.region.toLowerCase() != 'scotland')
        {
        	recordCount = 5;
        }
        if ( record.region == 'Northern Ireland' ) {
            recordCount = 3;
        }
        // if viewing ROI, show only 1 store
        if (viewing_roi && viewing_belfast) {
        	recordCount = 1;
        } 
        
        if ( data.totalRecordCount > 0 && data.records && data.records.length > 0 ) {
            for ( var i = 0, j = recordCount; i < j; i++ ) {
                record = data.records[i];
                infobox = this.getInfoBoxHtml( record, this.markerFields, 'infobox', 'div', i );
                html.push( this.getHtml( record, this.htmlFields, 'result', 'a', i ) );
				var recordPoint;
				if (record['pc'] == 'HU3 4SA'){
                    recordPoint = new MMLatLon(53.727970, -0.374050);
                } else if (record['pc'] == 'LE19 1UT') {
                    recordPoint = new MMLatLon(52.595290, -1.178910);
                } else {
                    recordPoint = new MMLatLon(record.point.lat, record.point.lon);
                }
                marker = this.map.createMarker( recordPoint, {
                    'label' : ( i + 1 ) + ' ' + record['storename'],
                    'icon' : this.icon,
                    'text' : i + 1 
                } );
                marker.setInfoBoxContent( infobox );
                marker.recordid = i;
                this.markers.push( marker );
                this.records.push( record );
                bounds.extend( record.point );
            }
            this.handleResults( html );
            bounds.extend( searchpoint );
			if (recordCount == 1) {
				record = data.records[0];
				this.map.goToPosition( this.map.getAutoScaleLocation( bounds ) );
				this.map.goToPosition( new MMLatLon( record.point.lat, record.point.lon ));
			} else {
				this.map.goToPosition( this.map.getAutoScaleLocation( bounds ) );
			}
            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( '' );
    },
    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 + '>';
            }
        }
		if ( typeof fields.customInsertPost == 'function' ) {
            html += fields.customInsertPost( record, id + 1 );
        }
        html += '</' + rootel + '>';
        return html.replace( /(\<a)+/ig, '<a href="#"' );
    }
}

