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 ( 57 , -4 ), 7 ) );
        if ( this.urlParams['location'] ) {
            this.locationSearch.search_field.value = this.urlParams['location'];
            this.locationSearch.onFormSubmit();
        }
        this.addClickEvent();
		this.addInfoBoxEvent();
        DisamHandler.init( this.container );
    },
    addWidgets: function() {
        this.map.addWidget( new MMPanZoomWidget( new MMBox( 40, undefined, undefined, 10 ) ) );
        var local_info_widget;
        this.local_container = document.getElementById ('widgetcontainer');
        local_info_widget  = new MMLocalInfoWidget ( undefined, undefined, new MMBox () );
        local_info_widget.setContainer ( this.local_container );
        this.map.addWidget( local_info_widget );
        var maptype_widget;
        this.maptype_container = document.getElementById ('typecontainer');
        maptype_widget  = new MMMapTypeWidget ( undefined, new MMBox( 0, 0 ) );
        maptype_widget.setContainer ( this.maptype_container );
        this.map.addWidget( maptype_widget );
    },
    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 );
        }
    },
    getFilters: function( search ) {
        search.filters = [];
        var filter = document.getElementById( "f_filter4" );
        if ( filter.checked ) {
            search.filters.push( new MMSearchFilter( 'civil', 'eq', 'Yes') );
            var select = document.getElementById( "firmtypeList" );
            selectVal = select.options[select.selectedIndex].value;
            if ( selectVal ) {
                search.filters.push( new MMSearchFilter( selectVal, 'eq', 'Yes') );
            }
        }
        var filter = document.getElementById( "f_filter2" );
        if ( filter.checked ) {
            search.filters.push( new MMSearchFilter( 'criminal', 'eq', 'Yes') );
        }
        var filter = document.getElementById( "f_filter3" );
        if ( filter.checked ) {
            search.filters.push( new MMSearchFilter( 'childrens', 'eq', 'Yes') );
        }
        var filter = document.getElementById( "firmname" );
        if ( filter.value != '' ) {
            search.filters.push( new MMSearchFilter('name','contains', filter.value ));
        }
        filter.processFilter = false;
        return search;
    },
    addInfoBoxEvent: function() {
        this.map.addEventHandler( 'openInfoBox', function() {
            UpdateRouting( arguments[2].recordid - (Page.storeSearch.iIndex) );
        } );
    },
	addClickEvent: function() {
        this.map.addEventHandler( 'click', Page.getCoordinate);
    },
	getCoordinate: function (eventType, eventTarget, coord, arg2, arg3) {
		locator.handleClickResult( coord );
	},
    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( 'i/marker.png' );
        icon.iconSize = new MMDimensions( 26, 34 );
        icon.iconAnchor = new MMPoint( 13, 17 );
        icon.groupName = 'o2';
        decluttericon = new MMIcon( 'i/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.scotaid_api12_new', 
            10, 
            this.getFilters, 
            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 );
    var checkBox = document.getElementById( 'f_filter4' );
    var selectList = document.getElementById( 'firmtypeList' );
    if(checkBox.checked == true) {
        selectList.style.display = 'block';
    } else {
        selectList.style.display = 'none';
    }
} );
