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.startpoint=new MMLatLon( 54.57278,-3.16278);
        this.map.goToPosition( this.startpoint,6 );
        if ( this.urlParams['location'] ) {
            this.locationSearch.search_field.value = this.urlParams['location'];
            this.locationSearch.onFormSubmit();
        }
        this.startStoreSearch.getData( this.startpoint );
        this.addInfoBoxEvent();
        DisamHandler.init( this.container );
    },
    addWidgets: function() {
			this.map.addWidget( new MMPanZoomWidget() );
			this.map.addWidget( new MMMapTypeWidget() );
    },
    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' );
    },
    backToSearch: function() {
    	   this.locationSearch.onFormSubmit();
    	   // Page.map.removeAllOverlays();
    	    document.body.removeCssClass( 'routesearch' );
          document.body.addCssClass( 'storesearch' );
    },
    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' );
        var country = document.getElementById( 'countrylist' );
        MMAttachEvent( location.form, 'submit', function() {
            DisamHandler.clearDisam();
        } );
        this.locationSearch = new LocationSearch( 
            location, country, this.map, undefined, this.storeSearch, this.startStoreSearch,function() { return false; }, DisamHandler.handleGeoError 
        );
    }, 
    getFilters: function( oSearch ) {
        oSearch.filters = [];
       
        oSearch.filters.push( new MMSearchFilter( 'showmap', 'eq', 'Yes' ) );
        oSearch.filters.push( new MMSearchFilter( 'country', 'eq', 'Yes' ) );           

        return oSearch;
    },
    setUpStoreSearch: function() {
        var icon = new MMIcon( '/i/round_marker.png' );
        icon.iconSize = new MMDimensions( 28, 34 );
        icon.iconAnchor = new MMPoint( 13, 17 );
        icon.groupName = 'evo';
        decluttericon = new MMIcon( '/i/marker.png' );
        decluttericon.iconSize = new MMDimensions( 26, 34 );
     	decluttericon.iconAnchor = new MMPoint( 13, 17 );
        decluttericon.groupName='evo';
        Callbacks.init( this.map, MarkerFields, HtmlFields, document.getElementById( 'results' ), icon, '' );
        this.storeSearch = new StoreSearch( 
            this.map, 
            'mm.clients.evochips_api12', 
            10, 
            this.getFilters, 
            3000, 
            function() { Callbacks.storeSearch( arguments[0], arguments[1],'' ); } 
        ); 
        this.startStoreSearch = new StoreSearch( 
            this.map, 
            'mm.clients.evochips_api12', 
            1000, 
            this.getFilters, 
            400, 
            function() { Callbacks.storeSearch( arguments[0], arguments[1] ,'start'); } 
        );      
    },
    setUpElements: function() {
        ApplyMethods( document.body );
    }
}
String.prototype.ucfirst = function() {
    return this.substring( 0, 1 ).toUpperCase() + this.substring( 1 ).toLowerCase();
}
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
MMAttachEvent( window, 'load', function() {
    var container = document.getElementById( 'map' );
    Page.init( container );
} );
