/*******************************************************************************************
 Places v.1.0
 License: proprietary license product, please read license.txt before using this product.
 Author: Trofimov Alexander (kolimarfey@gmail.com)
*******************************************************************************************/
function watchForSymbol(options)
{
    var stopAt;

    if (!options || !options.symbol || !Object.isFunction(options.onSuccess))
    {
        throw "Missing required options";
    }
    options.onTimeout = options.onTimeout || Prototype.K;
    options.timeout = options.timeout || 10;
    stopAt = (new Date()).getTime() + (options.timeout * 1000);
    new PeriodicalExecuter(function(pe) {
        if (typeof window[options.symbol] != "undefined")
        {
            options.onSuccess(options.symbol);
            pe.stop();
        }
        else if ((new Date()).getTime() > stopAt)
        {
            options.onTimeout(options.symbol);
            pe.stop();
        }
    }, 0.25);
}


function GMKPlacesView (id, lat, lon, zoom, type)
{
    this._iSaveLocationId = 0;
    this._sLoadingId = '';

    this._iId = id;
    this._fLat = lat.length ? parseFloat (lat) : 0;
    this._fLon = lon.length ? parseFloat (lon) : 0;
    this._iZoom = zoom.length ? parseInt (zoom) : 1;

    this._map = null;
    this._marker = null;
    this._PopupMarker = null;
    this._MaxContentTab = null;
    this._panoClient = null;
    this._node = null;

    this._iMapType = type.length ? parseInt(type) : 0;
    this._iMapControl = 1;
    this._isTypeControl = true;
    this._isScaleControl = true;
    this._isOverviewControl = true;
    this._isDragable = true;
    this._sCustomType = 'all';

    this._iSaveMapType = this._iMapType;
    this._iSaveZoom = this._iZoom;
    this._fSaveLat = this._fSaveLng = 0;

    this._iClickable = 0;

    this.sLangPositionSaved = 'Location has been succesfully saved';
    this.sLangPositionSaveFailed = 'Location saving failed';
    this.sLangGeocodeFailed = "Can not geocode following location:\n";
    this.sLangSelectLocation = "Please select location first";

    this.sActionsFile = 'gmk/gmk_events.php';
    this.sActionShowLocations = 'get_places';

    this._isLocationsDisplayed = 0;
}

GMKPlacesView.prototype = GMK.prototype;

GMKPlacesView.prototype.showLocations = function ()
{
    var $this = this;

    var showPanoData = function (panoData)
    {
        if (panoData.code != 200) {
            $this._PopupMarker.setText('StreetView not available');
            //GLog.write('showPanoData: Server rejected with code: ' + panoData.code);
            return;
        }
        $this._PopupMarker.showPopup();
        GEvent.addListener($this._marker, 'click', function(latlng) {
            $this._PopupMarker.hidePopup();
            if (latlng) {
              var regular = '<div>You can see street view if available</div>'
              var summary = '<div id="sum"></div>';
              var panoDiv = document.createElement('div');
              panoDiv.style.width = "400px"; // can be anything, will be auto resized
              panoDiv.style.width = "200px";
              var tabs = [new MaxContentTab('streetview', panoDiv)];
              $this._marker.openMaxContentTabsHtml($this._map, regular, summary, tabs, {
                maximized: true,
                maxTitle: "More Info",
                selectedTab: 'streetview',// or use index 1,
                style: {
                  tabOff: {
                    backgroundColor: '#CCCCFF'
                  }
                }
              });
            }
          });
          GEvent.addListener($this._map.getTabbedMaxContent(), 'selecttab', function(tab) {
              var node = tab.getContentNode();
              var latlng = $this._map.getInfoWindow().getPoint();
              switch (tab.getLabel()) {
              case 'streetview':
                if (!node.pano) {
                  var pano = new GStreetviewPanorama(node);
                  GEvent.addListener(pano, 'error', function(errorCode) {
                    if (errorCode == 603) {
                      node.innerHTML = 'StreetView requires flash plugin. Click <a href="http://get.adobe.com/flashplayer/" target="_blank"> here</a> to download';
                    }
                  });
                  pano.setLocationAndPOV(latlng);
                  node.pano = pano;
                }
                break;
              }
          });
    }


    if (1 == this._isLocationsDisplayed) return;
    var point = new GLatLng(this._fLat, this._fLon);
    var marker = this.createMarker(point, '');
    $this._marker = marker;

    if ($this._panoClient == null) {
        $this._panoClient = new GStreetviewClient();
    }
/*
    if (typeof PopupMarker == 'undefined') {
        loadJS("./places/application/gmk/popupmarker.js", "iPopupMarker");
    }

    if (typeof MaxContentTab == 'undefined') {
        loadJS("./places/application/gmk/tabbedmaxcontent.js", "iTabbedMaxContent");
    }
*/
    var opts = {text : "Click on the marker to view details."};

    PopupMarker = new PopupMarker(point, opts);
    $this._PopupMarker = PopupMarker;
    $this._map.addOverlay($this._PopupMarker);
    $this._PopupMarker.hidePopup();

    GEvent.addListener(marker,"mouseover", function(){
        $this._PopupMarker.showPopup();
    });
    GEvent.addListener(marker,"mouseout", function(){
        $this._PopupMarker.hidePopup();
    });

    panoClient = new GStreetviewClient();
    panoClient.getNearestPanorama(point, showPanoData);


    this._map.addOverlay(marker);
    this._isLocationsDisplayed = 1;
}
