// Copyright 2009 Google Inc.  All Rights Reserved.

/**
 * @fileoverview Helper scripts for Explore Street View.
 *
 * @author jeremydw (Jeremy Weinstein)
 */

/**
 * Initialize sv namespace.
 */
var sv = sv || {};


/**
 * Callback function for when a user clicks on a scrolller tab.
 * Sets the title for the currently selected scroller tab.
 * @param {Object} item The currently selected scroller tab item.
 */
sv.setBucketTitle = function(item) {
  var titleSlides = gweb.dom.query('div.bucket-title-slide');

  for (var i = 0, len = titleSlides.length; i < len; i++) {
    titleSlides[i].style.display = 'none';
  }
  gweb.dom.getElement('bucket-title-slide_' + item.id).style.display = 'block';
};


/**
 * Initializes a left-hand navigation list of tabs.
 * @constructor
 * @param {Object} conf A configuration object.
 */
sv.placeList = function(conf) {
  this.listEl = gweb.dom.getElement(conf.listId);
  this.frameEl = gweb.dom.getElement(conf.frameId);
  this.buttonEl = gweb.dom.getElement(conf.buttonId);
  this.frameContents = conf.frameContents;
  this.analyticsId = conf.analyticsId;
  this.selectedPlace;
  this.panorama;
  this.placeListItems = gweb.dom.query('li a.spot', this.listEl);
  this.spotFromHash = this.getSpotFromHash();
  this.init_();
  if (this.spotFromHash) {
    for (var i = 0, len = this.placeListItems.length; i < len; i++) {
      if (this.placeListItems[i].getAttribute('rel') == this.spotFromHash) {
        this.selectItem(this.placeListItems[i]);
      }
    }
  }
  if (!this.selectedPlace) {
    this.selectItem(this.placeListItems[0]);
  }
};


/**
 * Callback on item select. Selects item and shows panorama.
 * @param {HTMLElement} listItem Item in the list.
 */
sv.placeList.prototype.selectItem = function(listItem) {
  if (this.selectedPlace) {
    gweb.dom.classes.remove(this.selectedPlace, 'selected');
  }
  gweb.dom.classes.toggle(listItem.parentNode, 'selected');
  var descNodes = gweb.dom.query('div.description', listItem.parentNode);

  if (descNodes.length > 0) {
    gweb.fx.fadeIn(descNodes[0], 500);
  }

  this.selectedPlace = listItem.parentNode;

  var spotId = listItem.getAttribute('rel');
  var embedString = this.frameContents[spotId]['embed_code'];
  var link = /href="([^"]*)/.exec(embedString);
  var gll = /cbll=([^&]*)/.exec(embedString);
  var pov = /cbp=([^&]*)/.exec(embedString);
  var gll_temp = gll[1].split(',');
  var gll_final = new GLatLng(gll_temp[0], gll_temp[1]);
  var pov_temp = pov[1].split(',');
  var pov_final = {pitch: parseFloat(pov_temp[4]),
    yaw: parseFloat(pov_temp[1]),
    zoom: parseFloat(pov_temp[3])};

  var TRACKING_CODES = '&utm_campaign=en&utm_medium=et&utm_source=' +
                       'en-et-na-us-gns-svn';
  var link_final = link[1].replace(/\&amp;/gi, '&') + TRACKING_CODES;

  this.panorama = new GStreetviewPanorama(this.frameEl);
  this.panorama.setLocationAndPOV(gll_final, pov_final);
  this.buttonEl.href = link_final;
  if (gweb.dom.getElement('open-' + spotId)) {
    gweb.dom.getElement('open-' + spotId).href = link_final;
  }
};


/**
 * Sets the URL hash for the selected item.
 * @param {HTMLElement} listItem Item in the list.
 */
sv.placeList.prototype.setHash = function(listItem) {
  if (location.hash) {
    var spotId = listItem.getAttribute('rel');
    var bucketIdWithHash = location.hash.split('&')[0];

    window.location.replace(bucketIdWithHash + '&' + spotId);
  }
};


/**
 * Gets the spotId from the URL hash.
 * @return {string} The spotId or nothing.
 */
sv.placeList.prototype.getSpotFromHash = function() {
  if (location.hash) {
    var splitHash = location.hash.split('&');
    if (splitHash.length > 1) {
      return splitHash[1];
    }
  }
};


/**
 * Sends an Analytics Event Track hit for a specific item.
 * @param {HTMLElement} listItem Item in the list.
 */
sv.placeList.prototype.trackSpotView = function(listItem) {
  var spotId = listItem.getAttribute('rel');

  if (!this.pageTracker && this.analyticsId) {
    if (_gat) {
      this.pageTracker = _gat._getTracker(this.analyticsId);
      this.pageTracker._initData();
    }
  }
  if (this.pageTracker) {
    this.pageTracker._trackEvent('Street View Gallery', 'Click Spot', spotId);
  }
};


/**
 * Attaches click listener to all items in list and inits panorama.
 * @private
 */
sv.placeList.prototype.init_ = function() {
  for (var i = 0, len = this.placeListItems.length; i < len; i++) {
    gweb.events.listen(this.placeListItems[i], 'click', function(e) {
      this.selectItem(e.target);
      this.setHash(e.target);
      this.trackSpotView(e.target);
    }, null, this);
  }
  this.panorama = new GStreetviewPanorama(this.frameEl);
  GEvent.addListener(this.panorama, 'error', function(e) {
    // Handle errors.
  });
};

