var KM_KEY = "a2a752fbf3b352f61f9a9f51a617b74794f5550e";

this.SK = this.SK || {};

SK.extend = function(destination, source) {
  if (!source) return destination;
  for (var key in source) {
    if (destination[key] !== source[key])
      destination[key] = source[key];
  }
  return destination;
};

SK.Extensible = {
  withExtensions: function(name, argument, callback, scope) {
    if (!this._extensions) return callback.call(scope);

    var extensions = this._extensions[name];
    if (!extensions) return callback.call(scope);

    var list = extensions.slice();
    var ping = function() {
      var extension = list.shift();
      if (!extension)
        return callback.call(scope);

      extension[0].call(extension[1], argument, ping);
    };

    ping();
  },

  addExtension: function(name, callback, scope) {
    this._extensions       = this._extensions || {};
    this._extensions[name] = this._extensions[name] || [];
    this._extensions[name].push([callback, scope]);
  }
};

SK.Evented = {
  on: function(eventType, block, scope) {
    this._observers = this._observers || {};
    var list = this._observers[eventType] = this._observers[eventType] || [];
    list.push([block, scope]);
  },

  trigger: function(eventType, args) {
    if (!this._observers) return;
    var list = this._observers[eventType];
    if (!list) return;
    for (var i = 0, n = list.length; i < n; i += 1)
      list[i][0].apply(list[i][1] || null, args || []);
  }
};

JS.Packages(function() { with(this) {
    var kmHost = ("https:" == document.location.protocol) ? "https://s3.amazonaws.com/" : "http://";
    file(kmHost + "scripts.kissmetrics.com/t.js")
        .provides('KM', 'KM.record');

    loader(function(callback) {
      var container = document.createElement('div');
      container.id = 'fb-root';
      document.body.insertBefore(container, document.body.firstChild);
      
      var url = document.location.protocol + '//connect.facebook.net/en_US/all.js';
      load(url, callback);
    })
    .provides('FB', 'FB.init');

    file(SK_ASSET_HOST + '/javascripts/sk.facebook.js')
        .provides('SK.Facebook')
        .requires('FB', 'FB.init');

    file(SK_ASSET_HOST + '/javascripts/sk.live-search-service.js')
        .provides('SK.LiveSearchService')
        .requires('SK');

    file(SK_ASSET_HOST + '/javascripts/sk.change-location-module.js')
        .provides('SK.ChangeLocationModule')
        .requires('SK', 'SK.LiveSearchService');

    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    file(gaJsHost + 'google-analytics.com/ga.js')
        .provides('_gat');
    
    loader(function(callback) {
      try {
        window.pageTracker = _gat._getTracker("UA-2610254-1");
      } catch(err) {}
      callback();
    })
    .provides('pageTracker')
    .requires('_gat');
}});

// defer async file loads until after window.onload
var require = (function() {
  var loaded = false, queue = [];
  
  var handleLoad = function() {
    loaded = true;
    var args;
    while (args = queue.shift()) {
      JS.require.apply(JS, args);
    }
  };
  
  // we don't necessarily have jQuery at this point
  if (window.attachEvent)
    window.attachEvent('onload', handleLoad);
  else
    window.addEventListener('load', handleLoad, false);
  
  return function() {
    if (loaded) JS.require.apply(JS, arguments);
    else queue.push(arguments);
  };
})();

require('KM', function() {});

