var old_GStreetviewFlashCallback_1;
//OBJ
function SVC(svdiv, pano, map, controluiselector, controltextselector, iconpath){
  this._svclient = new GStreetviewClient();
  this._map = map;
  this._pano = new GStreetviewPanorama(pano);
  this._pano_container = pano;
  this._container = svdiv;
  this._controlUI = jQuery(controluiselector)[0];
  this._controlText = jQuery(controltextselector)[0];
  this._overlay = new GStreetviewOverlay();
  this._icon_image_path = iconpath;
};
SVC.prototype = new GControl();
SVC.prototype.initialize = function(map){
  var control = this;
  this._map = map;
  this._control_left = 39,
  this._control_top = parseInt(jQuery(this._controlText).position().top - jQuery(this._container).position().top),
  this.place_man();
  GEvent.addListener(this._map, "move", function(){
    control.place_man();
  });
  this._click_trigger = this._controlText;
  this._map.getContainer().appendChild(this._container);
  this.enableDraggableControl();
  return this._container;
};
SVC.prototype.place_man = function(){
  var control = this,
      jqmap = jQuery(this._map.getContainer()),
      micon = this.man_icon(),
      bb = this._map.getBounds(),
      px_left = this._control_left,
      px_top = this._control_top+(micon.iconSize.height*2),
      px_point = new GPoint(px_left, px_top);
      px_latlng = this._map.fromContainerPixelToLatLng(px_point);
      ;
  this._man_position = px_latlng;
  if(this._man_marker == null){
    this._man_marker = new GMarker(this._man_position, {icon:micon, title:'street view', draggable:true});
    this._map.addOverlay(this._man_marker);
  }else this._man_marker.setLatLng(this._man_position);

};
SVC.prototype.man_icon = function(){
  var guy = new GIcon(G_DEFAULT_ICON);
  guy.image = this._icon_image_path;
  guy.transparent = false;
  guy.shadow = false;
  guy.shadowSize = new GSize(false,false);
  guy.iconSize = new GSize(13, 24);
  return guy;
};

SVC.prototype.addSVOverlay = function(){
  this._map.addOverlay(this._overlay);
  this._layer_enabled = true;
};
SVC.prototype.removeSVOverlay = function(){
  this._map.removeOverlay(this._overlay);
  this._layer_enabled = false;
};
SVC.prototype.dragError = function(){
  this.removeSVOverlay();
  this.place_man();
  return;
};
SVC.prototype.enableDraggableControl = function(){
  var control = this;
  GEvent.addListener(this._man_marker, 'dragstart', function(){
    control.removeSVOverlay();
    control.addSVOverlay();
  });

  GEvent.addListener(this._pano, "error", function(){
    return control.dragError();
  });

  GEvent.addListener(this._man_marker, 'dragend', function(){
    var ll = control._man_marker.getLatLng();
    control._svclient.getNearestPanorama(ll,function(res){
      if(res.code == 200){
        control._pano.setLocationAndPOV(res.location.latlng, res.location.pov);
        jQuery(control._pano_container).show().css('margin-top', "-"+jQuery(control._map.getContainer()).outerHeight()+'px');
      }else{
        return control.dragError();
      }
    });
  });
  control.init_streetview_init_after_hook();
};
SVC.prototype.init_streetview_init_after_hook = function(){
  control = this;
  var justonce = GEvent.addListener(this._pano, "initialized", function(){
    control.street_view_started();
    GEvent.removeListener(justonce);
  });
};
SVC.prototype.street_view_started = function(){
  var control = this,
      p = jQuery("param[name='flashvars']"),
      fvars = p.attr('value').replace("context=api", "content=maps_sv")
      ;
  p.attr('value', fvars);
  p.parent().attr('data',p.parent().attr('data').replace("googlepano.112.swf", "googlepano.121.swf") );

  old_GStreetviewFlashCallback_1 = GStreetviewFlashCallback_1;
  GStreetviewFlashCallback_1 = function(){
    old_GStreetviewFlashCallback_1.apply(this, arguments);
    if(arguments[0] == "FlashCrossClick" || arguments[0] == "FlashZoomMinusClick"){
      control.init_streetview_init_after_hook();
      control.removeSVOverlay();
      control.place_man();
      control._pano.remove();
      jQuery(control._pano_container).html("");
    }
  }
};
SVC.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(14, 7));
};

SVC.prototype._icon_image_path = null;
SVC.prototype._svclient = null;
SVC.prototype._map = null;
SVC.prototype._pano = null;
SVC.prototype._pano_container = null;
SVC.prototype._man_marker = null;
SVC.prototype._click_trigger = null;
SVC.prototype._container = null;
SVC.prototype._position = null;
SVC.prototype._controlUI = null;
SVC.prototype._controlText = null
SVC.prototype._layer_enabled = false;
SVC.prototype._overlay = null;
SVC.prototype._drag = null;
SVC.prototype._drag_start = null;
SVC.prototype._drag_end = null;


/** NEW **/

(function(jQuery) {

  jQuery.fn.streetview = function(options){
    var config = jQuery.extend({}, jQuery.fn.streetview.data, options, {container:this});
    return this.each(function(){
      if(this.__sv) return;
      else this.__sv = true;
      jQuery.streetview.setup(config);
    });
  };

  jQuery.fn.streetview.data = {
                              map:false,
                              icon_image: "http://www.trovavetrine.it/_sv_inc/street-view.png",
                              control:false,
                              selector_ids:{
                                map_container:false,
                                control_container:"sv_control",
                                control_ui:"sv_ui",
                                control_text:"sv_text",
                                street_view: "gstreetview"
                                }
                              };


  jQuery.streetview = {
    setup:function(config){
      var selectors = config.selector_ids;
      config.map_container = config.map.getContainer();
      config.map_parent = jQuery(config.map_container).parent();
      //look for controller
      if(!jQuery("#"+selectors.control_container).length){
        jQuery(config.map_parent).append('<div id="'+selectors.control_container+'"><div id="'+selectors.control_ui+'" title="click to trigger street view"><div id="'+selectors.control_text+'"></div></div></div>');
      }
      //look for street view container
      if(!jQuery("#"+selectors.street_view).length){
        jQuery(config.map_parent).append('<div id="'+selectors.street_view+'"></div>');
      }
      jQuery("#"+selectors.control_container)[0].index=1;
      config.control = new SVC(jQuery("#"+selectors.control_container)[0], jQuery("#"+selectors.street_view)[0], config.map,'#'+selectors.control_ui, '#'+selectors.control_text, config.icon_image);
      config.map.addControl(config.control);
    }

  };

})(jQuery);


