 /**
 USE:
    Yandex Map Api >= 1.0
    prototype >= 1.5
 **/   

 var YaMapController = new function()
 {
    var me = this;
    
    this.map = null;
    this.map_id = 'YMapsID';
    this.container = null;
    this.container_id = 'ya_map_container';

    this.ini = function()
    {
        if( me.map != null ) return;

        var div = window.document.createElement( 'DIV' ) ;

        div.id = me.container_id
        //div.className = 'divcategory';
        div.style.position = 'absolute';
        div.style.display = 'block';
        div.style.zIndex = '10';
                   //
        div.innerHTML =                                                                                                                                 
            '<div class="yamap">\
                <div class="tb_close"><a style="top: -5px; left: 0pt;" onclick="YaMapController.hideMap();return false;" href="#"><img width="13" height="13" border="0" src="/09/2010/img/tb_close_gray.gif"></a></div>\
                <div id="'+ me.map_id +'" class="yamap_cont"></div>\
            </div>';        

        me.container = document.body.appendChild( div );
        
        me.map = new YMaps.Map(document.getElementById( me.map_id ));
        me.map.setCenter(new YMaps.GeoPoint(37.64, 55.76), 10);
        me.map.addControl(new YMaps.ToolBar());
        me.map.addControl(new YMaps.Zoom());
        //me.map.addControl(new YMaps.MiniMap(), new YMaps.ControlPosition(YMaps.ControlPosition.TOP_RIGHT) );
        
         
    }
    
    
    this.showBallon = function( addr, label, img_url )
    {
        //map.removeOverlay(geoResult);
        var geocoder = new YMaps.Geocoder(addr, {results: 1, boundedBy: me.map.getBounds()});

        YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
            if (this.length()) {
                
                var ballon_html = '<span style="font-family:tahoma,verdana,arial; !important; font-size:8pt; !important;">'; 
                    ballon_html += ((label==null)||(label=='')) ? '' :  label+'<br>';
                    ballon_html += ((img_url==null)||(img_url=='')) ? '' : '<img src="'+img_url+'" width="70" height="70"><br>';
                    ballon_html += addr + '</span>';
                
                var ballon = this.get(0); // geoResult, который вернулся 
                me.map.addOverlay( ballon );
                ballon.setBalloonContent( ballon_html );
                me.map.setBounds(ballon.getBounds());
                ballon.openBalloon();
            } else {
                alert("Ничего не найдено")
            }
        });
        
        YMaps.Events.observe(geocoder, geocoder.Events.Fault,
        function (error) {alert("Произошла ошибка: " + error.message)});

    }
    
    this.showMapFromAhref = function( a_href )
    {
        this.ini();
        
        var centerize = 1;
        
        var a = $j( a_href );
        //a = a[0];
        var a_xy = me.cumulativeOffset( a_href );

        var top = a_xy.top - 350 - 25 - 15;
        
        if( top < 415 ) {
           me.container.style.top = a_xy.top + 20 + 'px';
        } else {
           me.container.style.top = top + 'px';
        }
        //me.container.style.left = a_xy.left + 'px';
        var wi = 370;
        me.container.style.left = parseInt( document.body.offsetWidth - wi ) / 3 + 'px';//(a_xy.left - wi/2) + 'px';//
        me.container.style.display = 'block';

        me.map.removeAllOverlays();
        me.showBallon( a.attr( 'ym_addr' ),
                       a.attr( 'ym_label' ),
                       a.attr( 'ym_img_url' )
        );
    }
    
    this.hideMap = function()
    {
         me.container.style.display = 'none';
    }
       
    /* from prototype 1.6 
       source: http://www.prototypejs.org/
    */
    this.cumulativeOffset = function(element)
    {
        var valueT = 0, valueL = 0;
        do {
          valueT += element.offsetTop  || 0;
          valueL += element.offsetLeft || 0;
          element = element.offsetParent;
        } while (element);
        return {'top': valueT, 'left': valueL};
    }

    
    // Инициализация компонента яндекс карт
    // this.ini();
}