Page = {
    map: null,
    container: null,
    init: function( container ) {
        this.urlParams = {};
        this.getUrlParams();
        this.setUpElements();
        this.container = container;
        this.map = new MultimapViewer( this.container );
        this.addWidgets();
        this.setUpStoreSearch();
        this.setUpLocationSearch();
        this.map.goToPosition( new MMLocation ( new MMLatLon ( 53.61 , -2 ), 7 ) );
        DisamHandler.init( this.container );
        if ( this.urlParams['client_id'] ) {
            var storeID = this.urlParams['client_id'];
            var funcRef = DealerSearch.processSearchResults;
    		searcher = new MMSearchRequester( funcRef );
            search = new MMSearch();
            
            search.return_fields = undefined;
            search.radius_units = 'miles';
            search.data_source = 'mm.clients.mcdonalds_01_new';

            search.filters = new Array();
            search.filters.push( new MMSearchFilter('client_id', 'eq', storeID) );

            search.logic = 'AND';
       		searcher.search( search );
        }
        if ( this.urlParams['locationsearch'] ) {
            var loc = this.urlParams['locationsearch'];
            loc = loc.replace(/\53/g," ");
            loc = unescape(loc);
            this.locationSearch.search_field.value = loc;
            this.locationSearch.onFormSubmit();
        }
    },
    addWidgets: function() {
        this.map.addWidget( new MMPanZoomWidget() );
        this.map.addWidget( new MMMapTypeWidget() );
        this.map.addEventHandler( 'click', logClick);
    },
    getFilters: function( search ) {
        search.filters = [];
        var filter = document.getElementById( "drivethru" );
        if ( filter.checked ) {
            search.filters.push( new MMSearchFilter( 'meta1', 'eq', 1) );
        }
        var filter = document.getElementById( "playplace" );
        if ( filter.checked ) {
            search.filters.push( new MMSearchFilter( 'meta2', 'eq', 1) );
        }
        var filter = document.getElementById( "babychanging" );
        if ( filter.checked ) {
            search.filters.push( new MMSearchFilter( 'meta3', 'eq', 1) );
        }
        var filter = document.getElementById( "birthday" );
        if ( filter.checked ) {
            search.filters.push( new MMSearchFilter( 'meta4', 'eq', 1) );
        }
        var filter = document.getElementById( "wifi" );
        if ( filter.checked ) {
            search.filters.push( new MMSearchFilter( 'meta5', 'eq', 1) );
        }
        var filter = document.getElementById( "accessibility" );
        if ( filter.checked ) {
            search.filters.push( new MMSearchFilter( 'meta7', 'eq', 1) );
        }
        filter.processFilter = false;
        return search;
    },
    getUrlParams: function() {
        try {
            var url = window.location.search.split( '?' )[1].split( '&' );
            for ( var i = 0, j = url.length; i < j; i++ ) {
                this.urlParams[url[i].split( '=' )[0]] = url[i].split( '=' )[1];
            }
        } catch(e) {};
    },
    setUpLocationSearch: function() {
        var location = document.getElementById( 'locationsearch' );
        MMAttachEvent( location.form, 'submit', function() {
            DisamHandler.clearDisam();
        } );
        this.locationSearch = new LocationSearch(
            location, 'GB', this.map, undefined, this.storeSearch, function() { return false; }, DisamHandler.handleGeoError 
        );
    },
    setUpStoreSearch: function() {
        var icon = new MMIcon( 'i/map-yellow.png' );
        icon.iconSize = new MMDimensions( 25, 29 );
        icon.iconAnchor = new MMPoint( 0, 0 );
        icon.infoBoxAnchor = new MMPoint( -40, -17 );
        icon.textAnchor = new MMPoint( 4, 1 );
        icon.textDimensions = new MMDimensions( 12, 12 );
        icon.groupName = 'McD';
        decluttericon = new MMIcon( 'i/clustermarker.png' );
        decluttericon.iconSize = new MMDimensions( 32, 36 );
        Callbacks.init( this.map, MarkerFields, HtmlFields, document.getElementById( 'results' ), icon );
        this.storeSearch = new StoreSearch( 
            this.map, 
            'mm.clients.mcdonalds_01_new', 
            4, 
            this.getFilters,
            20, 
            function() { Callbacks.storeSearch( arguments[0], arguments[1] ); } 
        );
    },
    setUpElements: function() {
        ApplyMethods( document.body );
    }
}

MMAttachEvent( window, 'load', function() {
    var container = document.getElementById( 'mapviewer' );
    Page.init( container );
} );
