Page = {
    map: null,
    container: null,
    init: function( container ) {
        this.urlParams = {};
        this.getUrlParams();
        this.setUpElements();
        this.container = container;
        this.map = new MultimapViewer( this.container );
        this.handleLayout();
        this.addWidgets();
        this.addResizeEvent();
        this.addRoutingEvent();
        this.setUpStoreSearch();
        this.setUpLocationSearch();
        this.map.goToPosition( new MMLocation ( new MMLatLon ( 53.61 , -2 ), 7 ) );			
        if ( this.urlParams['location'] ) {
						var tmpUrlParams = this.urlParams['location'].replace(/\+/g, ' ');
            this.locationSearch.search_field.value = tmpUrlParams.replace(/%20/g, ' ');
            this.locationSearch.onFormSubmit();
        }
        this.addInfoBoxEvent();
        DisamHandler.init( this.container );	
    },
    addWidgets: function() {
        this.map.addWidget( new MMPanZoomWidget( new MMBox( 40, undefined, undefined, 10 ) ) );
        this.map.addWidget( new MMMapTypeWidget( undefined, new MMBox( 10, 10 ) ) );
    },
    addRoutingEvent: function() {
        var to = document.getElementById( 'directions_to' ), from = document.getElementById( 'directions_from' ), map = this.map;
        var results = document.getElementById( 'directionsResults' );
        from.form.onsubmit = function() {
            new RouteSearch( 
                map, new MMAddress( { 'qs' : from.value, 'country_code' : 'GB' } ), new MMLocation( to.point ), results, Page.createRouteSteps, Page.routingCallback,
                DisamHandler.handleGeoError
            );
            return false;
        }
    },
    addResizeEvent: function() {
        if ( navigator.userAgent.indexOf( 'MSIE 6' ) > -1 ) {
            MMAttachEvent( window, 'resize', Page.handleLayout );
        }
    },
    addInfoBoxEvent: function() {
        this.map.addEventHandler( 'openInfoBox', function() {
            UpdateRouting( arguments[2].recordid );
        } );
    },
    handleLayout: function() {
        var left = document.getElementById( 'left' );
        var right = document.getElementById( 'right' );
        Page.map.manageLayout( right );
        Page.map.manageLayout( left );
        Page.map.manageLayout( this.container );
    },
    createRouteSteps: function( step, id, type ) {
        return Callbacks.getHtml( step, RoutingFields, 'result', 'a', id );
    },
    routingCallback: function() {
        document.body.removeCssClass( 'storesearch' );
        document.body.addCssClass( 'routesearch' );
    },
    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( 'css/'+campaign_var+'/images/multimap/marker.png' );
        icon.iconSize = new MMDimensions( 26, 34 );
        icon.iconAnchor = new MMPoint( 13, 17 );
        icon.groupName = 'o2';
        decluttericon = new MMIcon( 'css/'+campaign_var+'/images/multimap/clustermarker.png' );
        decluttericon.iconSize = new MMDimensions( 29, 38 );
        Callbacks.init( this.map, MarkerFields, HtmlFields, document.getElementById( 'results' ), icon, decluttericon );
        this.storeSearch = new StoreSearch( 
            this.map, 
            'mm.clients.dfs_api', 
            5, 
            undefined, 
            undefined, 
            function() { Callbacks.storeSearch( arguments[0], arguments[1] ); } 
        );
    },
    setUpElements: function() {
        ApplyMethods( document.body );
    }
}

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