﻿/** 
 * See if the global namespace object 'goog' exists. If it doesn't, create it. 
 */
var goog = goog || {};

/** 
 * See if the object goog.web exists. If it doesn't create it.
 */
goog.web = goog.web || {};

/**
 * Language dropdown class.
 * @constructor
 * @param {sting} Current domain without .com, .es, .it, etc.
 */
goog.web.LangDropdown = function(domain) {
  this.LANGNAME = 0;
  this.DOMAIN = 1;
  this.LANG = 2;
  this.SELECTED = 3;
  
  /**
   * Domain of the project to which the dropdown is added to.
   * @type {string}
   */
  this.hostname = goog.web.host || 'google';

  /**
   * Current locale (en, es_es, pl, etc).
   * @type {string}
   */
  this.locale = this.getCurrentLocale();
  
  this.updateLangArray_();
  this.buildOptions_();
};


/**
 * Redirects user to the proper locale of the page they are on.
 * @param {Element} dropdown Language dropdown.
 */
goog.web.LangDropdown.prototype.changeLanguage = function(dropdown) {
  var selected = dropdown.value;
  var pathname = window.location.pathname;
  var langdropSize = goog.web.languages.length;
  
  // Find selected language
  for (var i = 0; i < langdropSize; i++) {
    if (goog.web.languages[i][this.LANGNAME] == selected) {
      this.locale.domain = goog.web.languages[i][this.DOMAIN];
      this.locale.lang = goog.web.languages[i][this.LANG];
      break;
    }
  }
  
  // Construct new host (host + localized domain)
  var host = goog.web.host + this.locale.domain;

  var hasIntl = pathname.match(/(\/intl\/[^\/]*\/)/ig);
  if (hasIntl) {
    pathname = pathname.replace(/(\/intl\/[^\/]*\/)/ig, '/intl/' +
                                this.locale.lang + '/');
  } else {
    pathname = '/intl/' + this.locale.lang + pathname;
  }
  
  window.location = 'http://' + host + pathname;
};


/**
 * Creates a language dropdown based using goog.web.languages array.
 * @private
*/
goog.web.LangDropdown.prototype.buildOptions_ = function() {
  var dropdown = document.getElementById('languages');
  var langdropSize = goog.web.languages.length;

  for (var i = 0; i < langdropSize; i++) {
    var option = document.createElement('option');
    option.value = goog.web.languages[i][this.LANGNAME];
    option.text = goog.web.languages[i][this.LANGNAME];
    option.selected = goog.web.languages[i][this.SELECTED];
    dropdown.options.add(option);
  }
};


/**
 * Modifies language array to flag new language as selected
 * @private 
*/
goog.web.LangDropdown.prototype.updateLangArray_ = function() {
  var langdropSize = goog.web.languages.length;

  for (var i = 0; i < langdropSize; i++) {
    if (goog.web.languages[i][this.DOMAIN] == this.locale.domain &&
        goog.web.languages[i][this.LANG] == this.locale.lang) {
      // Both domain and language match entry in the lang array.
      goog.web.languages[i][this.SELECTED] = true;
    } else if (goog.web.languages[i][this.LANG] == this.locale.lang &&
               this.locale.domain == '.com') {
      // Language matches but domain was set to .com (instead for example .fr)
      goog.web.languages[i][this.SELECTED] = true;
    } else {
      // No match
      goog.web.languages[i][this.SELECTED] = false;
    }
  }
};


/**
 * Parses url to get current locale
 * @return {Object} domain (.com) and language ('en') 
*/
goog.web.LangDropdown.prototype.getCurrentLocale = function() {
  var path = window.location.pathname;
  var domain = window.location.hostname.replace(this.hostname, '');
  
  var lang = path.match(/\/intl\/[^\/]*\//ig);
  if (lang) {
    // Found intl/xx
    lang = lang[0].split('/')[2];
  } else if (!lang && domain == '.com') {
    // earth.google.com
    lang = 'en';   
  } else {
    // No intl found and domain is not .com
    var langSize = goog.web.languages.length;
    
    for (var i = 0; i < langSize; i++) {
      lang = 'en';
      if (goog.web.languages[i][this.DOMAIN] == domain) {
        lang = goog.web.languages[i][this.LANG];
        break;
      }
    }
  }

  var locale = {
    'domain': domain,
    'lang': lang
  }
  return locale;
};