var MMEmbed = {
    map:null,
    rawMapData:null,
    red_circle_icon:null,
    init:function (){
        if(!this.rawMapData.map_center || !this.rawMapData.map_center || MMEmbed.rawMapData.error_explanation) {
            document.getElementById('mapWrapper').innerHTML = "No results found<br />We found no results for this search term. Please try searching again or report the problem.";
	    return false; 
	}
        this.map = MMFactory.createViewer( document.getElementById( 'mapviewer' ));
        if(this.rawMapData.map_type)
            this.map.setMapType(this.rawMapData.map_type);
	this.map.goToPosition( new MMLocation( new MMLatLon( this.rawMapData.map_center.point.lat, this.rawMapData.map_center.point.lon ) , this.rawMapData.zoom_factor ) );
        if(this.rawMapData.redCircle == 'true' && this.rawMapData.marker[0]) {
	    this.addRedCircle(new MMLocation( new MMLatLon( this.rawMapData.marker[0].point.lat, this.rawMapData.marker[0].point.lon ) , this.rawMapData.zoom_factor ));
	    if(document.location.search.indexOf("zoomFactor") == -1) {
                this.rawMapData.zoom_factor = 16;
                this.map.goToPosition(new MMLocation( new MMLatLon( this.rawMapData.marker[0].point.lat, this.rawMapData.marker[0].point.lon ) , this.rawMapData.zoom_factor ));
	    }
	}
        this.processDataPreferences();
        if ( navigator.userAgent.toLowerCase().indexOf("safari") == -1 ) {
            this.map.setOption( "mousewheel:wheeldown", "zoomout" );
            this.map.setOption( "mousewheel:wheelup", "zoomin" );
        }
    },
    addRedCircle: function( location ) {
        if (this.red_circle_icon == null) {
            this.red_circle_icon = new MMIcon( '/mmw/i/markers/circle.png' );
            this.red_circle_icon.printImage = '/mmw/i/markers/circle.gif';
            this.red_circle_icon.iconSize = new MMDimensions( 34, 34 );
            this.red_circle_icon.iconAnchor = new MMPoint( 17, 17 );
            this.red_circle_icon.cssClass = 'MMMarkerRedCircle';
        }
        this.red_circle = this.map.createMarker( location, { 'icon' : this.red_circle_icon, 'inert' : true } );
        return true;
    },
    processDataPreferences: function() {
        
        if( this.rawMapData.map_data.length < 1 )
            return;
        var map_type = this.map.getMapType();
        var data = [];
        for( var i = 0, li = this.rawMapData.map_data.length; i < li; ++i ){
            data.push(this.rawMapData.map_data[i].id);
        }
        var old_prefs = MMDataResolver.getDataPreferences( map_type );
        var new_prefs = data.concat();
        for( var i = 0, li = old_prefs.length; i < li; ++i ) {
            var found = false;
            for( var j = 0, lj = data.length; j < lj; ++j ) {
                if( data[j] && typeof( data[j] ) == 'string' ) {
                    data[j] = parseInt( data[j] );
                    if( isNaN( data[j] ) )
                        data[j] = undefined;
                }
                if( data[j] && data[j] == old_prefs[i] ) {
                    found = true;
                    break;
                }
            }
            if( ! found )
                new_prefs.push( old_prefs[i] );
        }
        MMDataResolver.setDataPreferences( map_type, new_prefs );
        if( this.map.beenDrawn() ) {
            this.map.redrawMap();
       }
    }    
}

MMAttachEvent( window, 'load', function(){MMEmbed.init();} );

