var show_mapviewer = true; // populates mapviewer
var start_position = new MMLocation ( new MMLatLon( 51.5162, -0.0929 ), 12 );
var show_poi = true; // populates POI div
var show_search = true; // populates search div
var show_results_list = true; // populates resultslist div
var mapviewer_zoom_in_on_marker_click = false;
//var search_result_zoom_in = true;
//widget_types.local_info = {}
var search_params = {
    data_source : 'mm.clients.mfi',
    count : 10,
    start_index: undefined,
    order_by_fields : undefined,
    order_by_order : undefined,
    min_distance : undefined,
    max_distance : undefined,
    distance_units : 'miles',
    return_fields : undefined
}
//search_result_icon.groupName = 'store_results';
// Create info box html

var oCategories = {
    '1' : 'Home Office',
    '2' : 'Bedrooms',
    '3' : 'Lounge &amp; Dining',
    '4' : 'Kitchens',
    '5' : 'Bathrooms',
    '6' : 'Sofas'
}

MMAttachEvent( window, 'load', function() {
    addDecluttering();
} );

function addDecluttering( sGroup ) {
    sGroup = sGroup || search_result_icon.groupName;
    if ( mapviewer && mapviewer.declutterGroup )
        mapviewer.declutterGroup( sGroup );
}

function createSearchResultMarker( oRecord, iNum ) {
    var sContent = '<img src="i/logo.png" width="110" height="25" />';
    sContent += '<h3 style="margin-top: 0.4em;">' + oRecord.name + '</h3>';
    sContent += '<p class="MMResultAddr">';
    if ( oRecord.addr1 )
        sContent += oRecord.addr1 + '<br />';
    if ( oRecord.addr2 )
        sContent += oRecord.addr2 + '<br />';
    if ( oRecord.addr3 )
        sContent += oRecord.addr3 + '<br />';
    if ( oRecord.pc )
        sContent += oRecord.pc + '<br />';
    if ( oRecord.telephone )
        sContent += oRecord.telephone;
    sContent += '</p>';
    sContent += '<p class="MMResultLinks">';
    sContent += '<a href="#" onclick="startRouting( ' + iNum + ' ); return false;">Get directions to here</a><br />';
    sContent += '<a href="#" onclick="zoomToStore( ' + iNum + ' ); return false;">Zoom in to this location</a>';
    sContent += '</p>';
    var sTimes = '<div>';
    sTimes += '<h3>Opening Times</h3>';
    sTimes += '<p>Sunday: ' + oRecord['sunday_open'] + ' - ' + oRecord['sunday_close'] + '</p>';
    sTimes += '<p>Monday: ' + oRecord['monday_open'] + ' - ' + oRecord['monday_close'] + '</p>';
    sTimes += '<p>Tuesday: ' + oRecord['tuesday_open'] + ' - ' + oRecord['tuesday_close'] + '</p>';
    sTimes += '<p>Wednesday: ' + oRecord['wednesday_open'] + ' - ' + oRecord['wednesday_close'] + '</p>';
    sTimes += '<p>Thursday: ' + oRecord['thursday_open'] + ' - ' + oRecord['thursday_close'] + '</p>';
    sTimes += '<p>Friday: ' + oRecord['friday_open'] + ' - ' + oRecord['friday_close'] + '</p>';
    sTimes += '<p>Saturday: ' + oRecord['saturday_open'] + ' - ' + oRecord['saturday_close'] + '</p>';
    sTimes += '</div>';
    var sCategories = '<div>';
    sCategories += '<h3>Product Categories</h3>';
    sCategories += getCategories( oRecord.product ).join( ', ' );
    sCategories += '</div>';

    if ( iNum == 0 ) {
        var oElement = document.getElementById( 'MMFirstResult' );
        oElement.innerHTML = sContent;
    }

    var oMarker = mapviewer.createMarker( oRecord.point, { text: iNum + 1, label: oRecord.name } );
    oMarker.setInfoBoxContent( [ new MMInfoBoxTab( 'Store', sContent ), new MMInfoBoxTab( 'Opening Times', sTimes ), new MMInfoBoxTab( 'Product Categories', sCategories ) ] );
    return oMarker;
}
function getCategories( sCategories ) {
    var aCategories = sCategories.split( ',' ), rCategories = [];
    for ( var i = 0; i < aCategories.length; i++ )
        rCategories.push( oCategories[aCategories[i].trim()] );
    return rCategories;
}

function startRouting( iNum ) {
    return doRouting( [ search_start_location, searcher.record_sets[0].records[iNum].point ] );
}
function zoomToStore( iNum ) {
    return mapviewer.goToPosition( searcher.record_sets[0].records[iNum].point, 17 );
}
function getFilters() {
    var sField = 'product', sLogic = 'like', aElements, oFilters = [];
    aElements = getElementsByClassName( 'MMLabel', 'label', document.getElementById( 'MMFilters' ) );
    for ( var i = 0; i < aElements.length; i++ ) {
        var oInput = aElements[i].getElementsByTagName( 'input' )[0];
        if ( oInput && oInput.type == 'checkbox' && oInput.checked )
            oFilters.push( new MMSearchFilter( sField, sLogic, '%' + oInput.value + '%' ) );
    }
    return oFilters;
}
function getElementsByClassName( sCss, sTag, oElement ) {
    if ( !sCss ) return false;
    sTag = sTag || '*';
    oElement = oElement || document.body;
    var aElements = oElement.getElementsByTagName( sTag ), rElements = [];
    for ( var i = 0; i < aElements.length; i++ ) {
        if ( hasCssClass( sCss, aElements[i] ) )
            rElements.push( aElements[i] );
    }
    return rElements;
}
function hasCssClass( sCss, oElement ) {
    return oElement.className.indexOf( sCss ) > -1;
}

String.prototype.trim = function() {
    return this.replace( /^\s+|\s+$/g, '' );
}

var icon = new MMIcon( 'i/icon.png' );
icon.iconSize = new MMDimensions( 47, 17 );
icon.iconAnchor = new MMPoint( 0, 13 ); // offset from top left
icon.infoBoxAnchor = new MMPoint( 0, 13 ); // offset from top left
var search_result_icon = icon;
search_result_icon.groupName = 'search_results';
