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 = 48;
        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 = data.totalRecordCount + ' McDonald\'s restaurants near you in ' + searchText;
            } else {
                resultsHead.innerHTML = capRecords + ' McDonald\'s restaurants near you in ' + searchText;
            }
            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, 'mcdInfobox', '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 : 'mcdInfobox', 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 = 'next';
                attachLinkEvent(a, 1);
                var a_text = 'First';
                a.appendChild( document.createTextNode( a_text ) );
                pagination.appendChild( a );
                pagination.appendChild( document.createTextNode( ' | ' ) );
                var a = document.createElement( 'a' );
                a.href = '#';
                a.className = 'prev';
                attachLinkEvent(a, pageIndex - 1);
                var a_text = 'Prev';
                a.appendChild( document.createTextNode( a_text ) );
                pagination.appendChild( a );
                pagination.appendChild( document.createTextNode( ' | ' ) );
            }
            if ( data.totalRecordCount > 4 && data.totalRecordCount < capRecords ) {
                for (i = 0; i * 4 < data.totalRecordCount; i++ ) {
                    var el = document.createElement ( 'a' );
                    el.href = '#';
                    pageStart = i * 4;
                    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 > 4 ){
                for (i = 0; i * 4 < capRecords; i++ ) {
                    var el = document.createElement ( 'a' );
                    el.href = '#';
                    pageStart = i * 4;
                    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 = 'next';
                attachLinkEvent(a, pageIndex + 1);
                var a_text = 'Next';
                a.appendChild( document.createTextNode( a_text ) );
                pagination.appendChild( a );
                pagination.appendChild( document.createTextNode( ' | ' ) );
                var a = document.createElement( 'a' );
                a.href = '#';
                a.className = 'next';
                attachLinkEvent(a, i);
                var a_text = 'Last';
                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 McDonald\'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="markerCol">';
                html += '<span>' + (id) + '</span>';
                html += '</div>';
                html += '<div class="detailsCol">';
                html += '<div class="detailsWrapper">';
                html += '<h3>' + record.name + '</h3>';
                html += '<p>' + record.street + '</p>';
                html += '<p>' + record.town + '</p>';
                html += '<p>' + record.pc + '</p>';
                html += '<p>tel: ' + record.telephone + '</p>';
                html += '</div>';
                html += '<div class="serviceIcons">';
                if(record.meta1 == 1) html += '<img src="i/drive-thru.gif"/>';
                if(record.meta2 == 1) html += '<img src="i/playplace.gif"/>';
                if(record.meta3 == 1) html += '<img src="i/babychange.gif"/>';
                if(record.meta4 == 1) html += '<img src="i/birthday-parties.gif"/>';
                if(record.meta5 == 1) html += '<img src="i/wifi.gif"/>';
                if(record.meta7 == 1) html += '<img src="i/accessibility.gif"/>';
                html += '</div>';                    
                html += '</div>';
                html += '<div class="rightWrapper">';
                html += '<div class="timesCol">';
                html += '<h4>Opening Times</h4>';
                html += '<p><span class="dayWeek">Monday</span> <span class="timesWeek">' + record.info1 + '</span></p>';
                html += '<p><span class="dayWeek">Tuesday</span> <span class="timesWeek">' + record.info2 + '</span></p>';
                html += '<p><span class="dayWeek">Wednesday</span> <span class="timesWeek">' + record.info3 + '</span></p>';
                html += '<p><span class="dayWeek">Thursday</span> <span class="timesWeek">' + record.info4 + '</span></p>';
                html += '<p><span class="dayWeek">Friday</span> <span class="timesWeek">' + record.info5 + '</span></p>';
                html += '<p><span class="dayWeek">Saturday</span> <span class="timesWeek">' + record.info6 + '</span></p>';
                html += '<p><span class="dayWeek">Sunday</span> <span class="timesWeek">' + record.info7 + '</span></p>';
                html += '</div>';
                html += '<div class="linkCol">';
                if ( record.distance ) html += '<h4>' + record.distance.miles + ' miles</h4>';
                html += '<p><a onclick="resultcustomonclick(' + (id) + ', ' + startIndex + '); return false;" href="#">Show location</a> on map</p>';
                var domain = document.domain;
                html += '<p><a href="mailto:?subject=McDonald\'s Map Link&body=http://' + domain + '/share/mcd/searchpage.htm?client_id=' + record.client_id + ' ">Email</a> this item</p>';
                html += '<p><a href="javascript: window.print();">Print</a> this item</p>';
                html += '</div>';
                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;
    };   
}
