/**
 * @fileoverview Loads generic modules required for all widgets.
 *
 */

if (typeof BuyerGuide == 'undefined') {
  /**
   * Namespace definition.
   * @constructor
   */
  BuyerGuide = function() {};
}

/**
 * Returns path from the last loaded script element. Splits src attribute value
 * and returns path without js file name.
 *
 * <p><strong>
 * Note: This function should not be called from another function. It must be
 * invoked during page load to determine path to js file from which it is called
 * correctly.
 * </strong></p>
 *
 * @private
 * @return Path to the script, e.g. '../src/' or '' if path is not found
 * @type string
 */
BuyerGuide.getPath = function() {
  // Get last script element
  var objContainer = document.body;
  if (!objContainer) {
    objContainer = document.getElementsByTagName('head')[0];
    if (!objContainer) {
      objContainer = document;
    }
  }
  var objScript = objContainer.lastChild;
  // Get path
  var strSrc = objScript.getAttribute('src');
  if (!strSrc) {
    return '';
  }
  var arrTokens = strSrc.split('/');
  // Remove last token
  arrTokens = arrTokens.slice(0, -1);
  if (!arrTokens.length) {
    return '';
  }
  return arrTokens.join('/') + '/';
};

/**
 * Simply writes script tag to the document.
 *
 * @private
 * @param {string} strSrc Src attribute value of the script element
 */
BuyerGuide.include = function(strSrc) {
  document.write('<s' + 'cript type="text/javascript" src="' + strSrc +
   '"></s' + 'cript>');
};

/**
 * Path to main BuyerGuide script.
 * @private
 */
BuyerGuide.BuyerGuidePath = BuyerGuide.getPath();

// Include required scripts
BuyerGuide.include(BuyerGuide.BuyerGuidePath + 'utils.js');
BuyerGuide.include(BuyerGuide.BuyerGuidePath + 'transport.js');
BuyerGuide.include(BuyerGuide.BuyerGuidePath + 'bgwidget.js');

// Replace BuyerGuide.include with more complex function from transport library
if (BuyerGuide.Transport && BuyerGuide.Transport.include) {
  BuyerGuide.include = BuyerGuide.Transport.include;
}
