// Copyright 2009 Google Inc.  All Rights Reserved.

/**
 * @fileoverview Namespace for Google Apps website.
 * @author drewniak
 * modifed by jessicako
 */

/**
 * Namespace for Google Apps website.
 * @type {Object}
 */
var apps = {};

/**
 * Namespace for global variables used on the Apps website.
 * @type {Object}
 */
apps.globals = {};

/**
 * Trims leading and trailing spaces around a string.
 * @return {string} String without leading and trailing spaces.
 */
String.prototype.trim = function() {
  return this.replace(/^\s*|\s*$/, '');
};


/**
 * Stops default browser action.
 * @param {Event} e Event
 */
apps.stopDefault = function(e) {
  if (e && e.preventDefault) {
    e.preventDefault();
  } else {
    window.event.returnValue = false;
  }
};

/**
 *  Extracts variables from the url based on the specified delimeter.
 */
apps.parseURLVar = function() {
  var DELIMETERS = ['#', '\\?', '&'];
  var pipe = '|';
  var url = unescape(location.href);
  var arrURL = [];
  var vars = {};
  var tempVar = [];
  
  for (var i = 0; i < DELIMETERS.length; i++) {
    var regx = new RegExp(DELIMETERS[i], 'ig');
    url = url.replace(regx, pipe);
  }
  arrURL = url.split(pipe);
  
  for (var i = 1; i < arrURL.length; i++) {
    tempVar = arrURL[i].split('=');
    vars[tempVar[0]] = tempVar[1];
  }
  
  return vars;
};
    


/**
 * Extracts current locale info from language dropdown array.
 * @return {Object} object containing domain (.co.jp) and locale (ja).
 */
apps.getLocale = function() {
  if (apps.dropdown.locale) return apps.dropdown.locale;
  if (apps.globals.locale) return apps.globals.locale;

  var langsSize = goog.web.languages.length;

  // Example languages[i]: ['Dansk', '.com', 'da', false]
  for (var i = 0; i < langsSize; i++) {
    if (goog.web.languages[i][3]) {
      var locale = {};
      locale['domain'] = goog.web.languages[i][1];
      locale['lang'] = goog.web.languages[i][2];
      return locale;
    }
  }

  if (!locale) {
    var locale = {
      'domain': '.com',
      'lang': 'en'
    };
  }
  return locale;
};


/**
 * Dynamically localizes links to some outbound sites.
 */
apps.localizeLinks = function() {
  // English, do nothing
  if (apps.globals.locale['lang'] == 'en') {
    return;
  }

  /**
   * List of element ids which need to be localized.
   * 'element id' : 'type of the link (hl, intl)'
   * @type {Object}
   */
  var linkElements = {
    'linkSupport': '\?hl',
    'linkGallery': '&hl'
  };

  /**
   * Languages who's lang codes are different than those of the server.
   * 'lang + linkElements[el]': 'new lang'
   ' 'iw\?hl': 'he'
   */
  var langOverride = {
  };

  var lang = apps.globals.locale['lang'];

  for (var el in linkElements) {
    var currLink = document.getElementById(el);
    var overrideKey = lang + linkElements[el]

    if (linkElements[el] == '\?hl' || linkElements[el] == '&hl') {
      updateHl();
    } else {
      updateIntl();
    }
  }

  /**
   * Appends hl parameter to links.
   */
  function updateHl() {
    if (langOverride[overrideKey]) {
      // Update link with overridden locale info
      try {
        currLink.href += linkElements[el] + '=' + langOverride[overrideKey];
      } catch (e) {
        // Element doesn't exist.
      }
    } else {
      // Update link with currect locale info
      try {
        currLink.href += linkElements[el] + '=' + lang;
      } catch (e) {
        // Element doesn't exist.
      }
    }
  }

  /**
   * Appends or injects intl/xx directory into the path
   */
  function updateIntl() {
    // TODO: add code for changing intl links
  }
};

/**
 * Initializes functions used throughout Apps website.
 * Creates instances of objects used on the website.
 */
apps.setUp = function() {
  apps.dropdown = new goog.web.LangDropdown();
  apps.globals.locale = apps.getLocale();

  //apps.localizeLinks();

};


function changeLanguage(dropdown) {
  if (apps.dropdown) {
    apps.dropdown.changeLanguage(dropdown);
  }
}


