/* 
 * This file rewrites some of the original MapSearch prototype methods.
 * So refer to original file if you have some questions.
 * 
 * @author David Galvis
 * 28.11.2008
 */

MapSearch.prototype.addMarkerAndShowInfoWindow = function(data){
    var marker = this.addMarker(data);
    var _this = this;
    this.markerMgr.refresh();
    
    setTimeout(function(){
        _this.markerClick(marker, data);
        
        //_this.map.panTo(marker.getLatLng());
    },1000);
}

MapSearch.prototype.alignRight = false;

/**
 * Serialize user choice like
 * @return json obj
 */
MapSearch.prototype.serializeChoice = function(){
    var choice = Array();
    jQuery("input[name='recommendationChoice']").each(function(){
        var comment = jQuery("#comment_" + this.value).text();
        choice.push({
            idRestaurant : this.value,
            comment : comment
        });
    });

    return choice;
}

MapSearch.prototype.monitorActivity = function()
{
    var _this = this;
    setTimeout(function(){
        _this.request();
    }, 400);
}
MapSearch.prototype.request = function(){
    _this = this;
    jQuery.ajax({
        url: '/application/ajax/activity.php',
        type: 'get',
        dataType : 'json',
        success: function(response){
            if (response != null){
                jQuery("#feed-container").prepend(response.feed);
                _this.addMarker(response.data, response.feed, response.coords);
                try{
                    if (typeof FB != 'undefined')
                        setTimeout( function(){FB.XFBML.Host.parseDomTree}, 0);
                }catch(e){}
            }
            setTimeout(function(){
                _this.request();
            },7000);
        },
        error: function(){
            _this.request();
        }
    });
}
MapSearch.prototype.addMarker = function(data, oneLineStory, coords)
    {
        var _this = this;
        var point = new GLatLng(coords.lat, coords.lng);
        this.bounds.extend(point);
        var icon = this.initIcon(data);
        var marker = new GMarker(point, {icon:icon});
        this.map.addOverlay(marker);
        
        // Moving current view to the right
        var panPoint = new GLatLng(point.lat()+3.5, point.lng()-3.85);

            marker.openInfoWindowHtml(oneLineStory,{
                pixelOffset: new GSize(320, 0)
            });
        if (this.alignRight){
            setTimeout(function(){
                _this.map.panTo(panPoint);
        }, 200);
        }
        return marker;
    }