/**
 * Sprout Mixer
 * Copyright 2008 ChipIn Inc. dba Sprout
 *
 * This code is the property of Sprout and is subject to the terms and
 * conditions set forth in the Software Development Toolkit Agreement.  Under
 * no circumstance should this code be made available to any party that is not
 * an employee of Sprout or party to the Software Development Toolkit Agreement.
 *
 * This code is distributed AS IS and WITHOUT WARRANTY OF ANY KIND, INCLUDING
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Usage:
 * <script type="text/javascript">
 * <!--
 *   sprout_width = 640;
 *   sprout_height = 480;
 *   sprout_id = '<YOUR_SPROUT_CAMPAIGN_ID>';
 * //-->
 * </script>
 * <script type="text/javascript" src="http://mix.sproutmixer.com/public/popup.js"></script>
 */
(function() {

  /* Client-side access to querystring name=value pairs
    Version 1.3
    28 May 2008

    License (Simplified BSD):
    http://adamv.com/dev/javascript/qslicense.txt
  */
  function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

  // Turn <plus> back to <space>
  // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

  // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
      var pair = args[i].split('=');
      var name = decodeURIComponent(pair[0]);

      var value = (pair.length==2)
        ? decodeURIComponent(pair[1])
        : name;

      this.params[name] = value;
    }
  }

  Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
  };

  Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
  };

  function C() {
    this.qs = new Querystring();
  }

  C.prototype.createWidget = function() {
    if (this.startID == null && this.win.sprout_portal_id == null && this.win.sprout_id == null)
      return;

    if (this.win.sprout_id == null)
      this.win.sprout_id = this.win.sprout_portal_id;

    this.popUp(this.win.sprout_base_url + '/mix/'
      + (this.startID != null ? this.startID : this.win.sprout_id)
      + '?t=' + new Date().getTime());
  };

  C.prototype.popUp = function(url) {
    // Create a safe window ID from the Sprout base URL.
    if (this.windowID == null)
      this.windowID = this.win.sprout_base_url.replace(/[\.\/:\s]/g, '_');

    var left = Math.max(0, ((this.win.screen.availWidth
      ? this.win.screen.availWidth
      : this.win.screen.width) - this.win.sprout_width) / 2);
    this.windowReference = window.open(url, this.windowID,
      'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0'
      + ',width=' + this.win.sprout_width + ',height=' + this.win.sprout_height
      + ',left=' + left + ',top=0');
    if (this.windowReference == null)
      return false;
    else {
      this.windowReference.focus();
      return true;
    }
  };

  C.prototype.detectIDAndPopup = function() {
    if (this.qs.contains('id')) {
      this.startID = this.qs.get('id');
      this.createWidget();
    }
  };

  C.prototype.run = function(doc, win) {
    this.doc = doc;
    this.win = win;
    this.detectIDAndPopup();
  };

  C.prototype.exportSingleton = function(name, obj, attrTable) {
    if (!window[name]) {
      var g = window[name] = new obj;

      // Hook the methods/variables into the new object
      for (var k = 0; k < attrTable.length; ++k) {
        g[attrTable[k][0]] = attrTable[k][1];
      }
    }
  };

  // Hook the functions defined above into the window
  // object under key "SproutLite", thus making available
  // globally the Sprout object.
  //
  var proto = C.prototype,
    attrTable = [['createWidget', proto.createWidget],
      ['popUp', proto.popUp],
      ['detectIDAndPopup', proto.detectIDAndPopup],
      ['run', proto.run],
      ['exportSingleton', proto.exportSingleton]];

  C.prototype.exportSingleton('SproutPopup', C, attrTable);

  function fillInUnsetVars(win) {
    var origErrorHandler = win.onerror;

    win.onerror = errorHandler;

    if (win.sprout_base_url == null) {
      win.sprout_base_url = 'http://mix.sproutmixer.com';
    }

    if (win.sprout_width == null) {
      win.sprout_width = 720;
    }

    if (win.sprout_height == null) {
      win.sprout_height = 650;
    }

    win.onerror = origErrorHandler;
  }

  function errorHandler(unused1, unused2, unused3) {
    SproutPopup.run(document, window);
    return true;
  }

  var win = window, doc = document;
  fillInUnsetVars(win);
  SproutPopup.run(doc, win);
})()
