//<!--
 
var Map = Class.create({
    initialize: function(options)
    {
        this.options = Object.extend
        (
            {
                container:      $('mapContainer'),
                wrapper:        $('mapWrapper'),
                swf_id:         'map',
                swf:            $('map'),
                swf_path:       '/swf/cvba_map.swf',
                map_id:         'cvbaMap', // <object> id attrib
                close_duration: 0.5,
                open_duration:  1.0
            },
            options || {}
        );
        
        this.map = null;
    },
    
    
    /*
     * embeds the SWF 
     * 
     * 
     */
    embed: function()
    {
        var flashVars   = { host: '' };
        
        var params      = 
        { 
            base: '/swf/',
            loop: 'false', 
            menu: 'false', 
            quality: 'high', 
            swliveconnect: 'true', 
            bgcolor: '#000000', 
            allowScriptAccess: 'always'
        };
        
        var attributes  = 
        { 
            id: 'cvbaMap', 
            name: 'cvbaMap' 
        };
        
        swfobject.embedSWF(this.options.swf_path, this.options.swf_id, '770', '405', '10.0.0', '/webassets/swf/expressInstall.swf', flashVars, params, attributes);
        this.map = $(this.options.map_id);
        //Element.extend(this.map);
        //this.open();
    },
    
    /*
     * showBusiness
     * 
     * opens map and shows business information
     * 
     * @param id   Number       business['id']
     * 
     */
    showBusiness: function(id)
    {
        this.open(true, id);
        //this.map.highlightBusiness(id);
        //document.getElementById(this.options.map_id).highlightBusiness(id);
    },
    
    /*
     * appears mapContainer 
     * 
     */
    open: function(show, id)
    {
        var m = this;
        this.options.container.setOpacity(0);
        this.options.container.setStyle({
            width: '100%',
            height: '100%',
            'z-index': '-1'
        });
        this.options.wrapper.setStyle({margin: 'auto'});
        new Effect.Opacity
        (
            this.options.container,
            {
                duration: this.options.open_duration,
                from: this.options.container.getOpacity(),
                to: 1.0,
                beforeStart: function()
                {
                    m.putInForeground();
                },
                afterFinish: function()
                {
                    if(show)
                        m.map.highlightBusiness(id)
                }
            }
        );
    },
    
    
    /*
     * fades mapContainer 
     * 
     */
    close: function()
    {
        var m = this;
        
        new Effect.Opacity
        (
            this.options.container,
            {
                duration: this.options.open_duration,
                from: this.options.container.getOpacity(),
                to: 0,
                afterFinish: function()
                {
                    m.putInBackground();
                }
            }
        );
    },
    
    
    /*
     * moves mapContainer to lower z-index, behind content 
     * 
     */
    putInBackground: function()
    {
        this.options.container.setStyle({ zIndex: -1 });
        this.map.setStyle({ visibility: 'hidden' })
    },
    
    
    /*
     * moves mapContainer to higher z-index, in front of content
     * 
     */
    putInForeground: function()
    {
        this.options.container.setStyle({ zIndex: 1000 });
        if(this.map)
            this.map.setStyle({ visibility: 'visible' })
        else
        {
            this.options.map_id = 'needFlash';
            $('needFlash').setStyle({ visibility: 'visible' })
            if($('SWFObjectExprInst'))
                $('SWFObjectExprInst').setStyle({ visibility: 'visible' })
        }
    },
    
    initFindMeOnTheMapLinks: function()
    {
        var d = $('directoryList');
        if (d)
        {
            $('directoryList').select('.mapLink').invoke('show')
        }
    },
    
    /*
     * handles plotting a business when editing in admin
     * 
     * @param icon_id           String      HTML id of current business icon
     * @param x_input_tag       String      HTML id of form input tag for x coord
     * @param y_input_tag       String      HTML id of form input tag for y coord
     * @return void
     * 
     */
    initializePlotting: function(icon_id, x_input_tag, y_input_tag, map_ratio)
    {
        //store once, instead of recalc on mouse move
        this.icon_dims      = { width: 13, height: 37 };
        this.map_offset     = $('map').cumulativeOffset();
        this.business_icon  = $(icon_id.replace(new RegExp("plotting_"), ""));
        this.x_input_tag    = $(x_input_tag);
        this.y_input_tag    = $(y_input_tag);
        this.map_ratio      = map_ratio != undefined ? map_ratio : 1;
        
        //init the plotting icon (hide + set position to mouse x,y)
        //this.hidePlottingIcon(this, icon_id);
        this.movePlottingIcon(this, icon_id);
        
        
        Element.observe
        (
           'mapOverlay',
           'mousemove',
           this.movePlottingIcon.bindAsEventListener(this, icon_id)
        );
               
        Element.observe
        (
           'mapOverlay',
           'click',
           this.plotIcon.bindAsEventListener(this, icon_id)
        );
    },
    
    
    /*
     * Hides the plotting icon on map mouseout and init of plotting
     * 
     */
    hidePlottingIcon: function(event)
    {
        $('cvba_business_directory_summary').update($('cvba_business_directory_summary').innerHTML+"\n"+'hiding');
        var icon = $(arguments[1]); 
        //sometimes, the mouse will hit the plotting icon...
        if($(Event.element(event)) != (icon))
            icon.hide(); 
    },
    
    
    /*
     * Shows the plotting icon on map mouseover
     * 
     */
    showPlottingIcon: function()
    {
        $(arguments[1]).show(); 
    },
    
    
    /*
     * 
     * 
     * @param event Event
     * @return void
     * 
     */
    movePlottingIcon: function(event)
    {
        var icon_id = $(arguments[1]);
        var coords  = this.getIconCoordinates(event);
        
        icon_id.setStyle
        ({
            top:    String(((isNaN(coords.y)) ? 0 : coords.y)+'px'),
            left:   String(((isNaN(coords.x)) ? 0 : coords.x)+'px')
        });
        
        
    },
    
    /*
     * 
     * @param event Event
     * @return void
     * 
     */
    plotIcon: function(event)
    {
        var coords = this.getIconCoordinates(event);
        
        this.business_icon.setStyle
        ({
            top:    String(((isNaN(coords.y)) ? 0 : coords.y)+'px'),
            left:   String(((isNaN(coords.x)) ? 0 : coords.x)+'px')   
        });
        
        //round to 1 decimal place
        $(this.x_input_tag).value = Math.round(((coords.x + this.icon_dims.width)/this.map_ratio)*10)/10;
        $(this.y_input_tag).value = Math.round(((coords.y + this.icon_dims.height)/this.map_ratio)*10)/10;
    },
    
    /*
     * finds the icon coordinates when following the mouse
     * 
     * @param event Event
     * @return object
     * 
     */
    getIconCoordinates: function(event)
    {
        return {
            x: Event.pointerX(event) - this.map_offset.left - (this.icon_dims.width / 2) - 5,
            y: Event.pointerY(event) - this.map_offset.top - this.icon_dims.height
        };
    }
    
    
});

//-->
