/*!
 * EventEmitter v4.2.6 - git.io/ee
 * Oliver Caldwell
 * MIT license
 * @preserve
 */


(function () {
	'use strict';

	/**
	 * Class for managing events.
	 * Can be extended to provide event functionality in other classes.
	 *
	 * @class EventEmitter Manages event registering and emitting.
	 */
	function EventEmitter() {}

	// Shortcuts to improve speed and size
	var proto = EventEmitter.prototype;
	var exports = this;
	var originalGlobalValue = exports.EventEmitter;

	/**
	 * Finds the index of the listener for the event in it's storage array.
	 *
	 * @param {Function[]} listeners Array of listeners to search through.
	 * @param {Function} listener Method to look for.
	 * @return {Number} Index of the specified listener, -1 if not found
	 * @api private
	 */
	function indexOfListener(listeners, listener) {
		var i = listeners.length;
		while (i--) {
			if (listeners[i].listener === listener) {
				return i;
			}
		}

		return -1;
	}

	/**
	 * Alias a method while keeping the context correct, to allow for overwriting of target method.
	 *
	 * @param {String} name The name of the target method.
	 * @return {Function} The aliased method
	 * @api private
	 */
	function alias(name) {
		return function aliasClosure() {
			return this[name].apply(this, arguments);
		};
	}

	/**
	 * Returns the listener array for the specified event.
	 * Will initialise the event object and listener arrays if required.
	 * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them.
	 * Each property in the object response is an array of listener functions.
	 *
	 * @param {String|RegExp} evt Name of the event to return the listeners from.
	 * @return {Function[]|Object} All listener functions for the event.
	 */
	proto.getListeners = function getListeners(evt) {
		var events = this._getEvents();
		var response;
		var key;

		// Return a concatenated array of all matching events if
		// the selector is a regular expression.
		if (typeof evt === 'object') {
			response = {};
			for (key in events) {
				if (events.hasOwnProperty(key) && evt.test(key)) {
					response[key] = events[key];
				}
			}
		}
		else {
			response = events[evt] || (events[evt] = []);
		}

		return response;
	};

	/**
	 * Takes a list of listener objects and flattens it into a list of listener functions.
	 *
	 * @param {Object[]} listeners Raw listener objects.
	 * @return {Function[]} Just the listener functions.
	 */
	proto.flattenListeners = function flattenListeners(listeners) {
		var flatListeners = [];
		var i;

		for (i = 0; i < listeners.length; i += 1) {
			flatListeners.push(listeners[i].listener);
		}

		return flatListeners;
	};

	/**
	 * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful.
	 *
	 * @param {String|RegExp} evt Name of the event to return the listeners from.
	 * @return {Object} All listener functions for an event in an object.
	 */
	proto.getListenersAsObject = function getListenersAsObject(evt) {
		var listeners = this.getListeners(evt);
		var response;

		if (listeners instanceof Array) {
			response = {};
			response[evt] = listeners;
		}

		return response || listeners;
	};

	/**
	 * Adds a listener function to the specified event.
	 * The listener will not be added if it is a duplicate.
	 * If the listener returns true then it will be removed after it is called.
	 * If you pass a regular expression as the event name then the listener will be added to all events that match it.
	 *
	 * @param {String|RegExp} evt Name of the event to attach the listener to.
	 * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.addListener = function addListener(evt, listener) {
		var listeners = this.getListenersAsObject(evt);
		var listenerIsWrapped = typeof listener === 'object';
		var key;

		for (key in listeners) {
			if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) {
				listeners[key].push(listenerIsWrapped ? listener : {
					listener: listener,
					once: false
				});
			}
		}

		return this;
	};

	/**
	 * Alias of addListener
	 */
	proto.on = alias('addListener');

	/**
	 * Semi-alias of addListener. It will add a listener that will be
	 * automatically removed after it's first execution.
	 *
	 * @param {String|RegExp} evt Name of the event to attach the listener to.
	 * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.addOnceListener = function addOnceListener(evt, listener) {
		return this.addListener(evt, {
			listener: listener,
			once: true
		});
	};

	/**
	 * Alias of addOnceListener.
	 */
	proto.once = alias('addOnceListener');

	/**
	 * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad.
	 * You need to tell it what event names should be matched by a regex.
	 *
	 * @param {String} evt Name of the event to create.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.defineEvent = function defineEvent(evt) {
		this.getListeners(evt);
		return this;
	};

	/**
	 * Uses defineEvent to define multiple events.
	 *
	 * @param {String[]} evts An array of event names to define.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.defineEvents = function defineEvents(evts) {
		for (var i = 0; i < evts.length; i += 1) {
			this.defineEvent(evts[i]);
		}
		return this;
	};

	/**
	 * Removes a listener function from the specified event.
	 * When passed a regular expression as the event name, it will remove the listener from all events that match it.
	 *
	 * @param {String|RegExp} evt Name of the event to remove the listener from.
	 * @param {Function} listener Method to remove from the event.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.removeListener = function removeListener(evt, listener) {
		var listeners = this.getListenersAsObject(evt);
		var index;
		var key;

		for (key in listeners) {
			if (listeners.hasOwnProperty(key)) {
				index = indexOfListener(listeners[key], listener);

				if (index !== -1) {
					listeners[key].splice(index, 1);
				}
			}
		}

		return this;
	};

	/**
	 * Alias of removeListener
	 */
	proto.off = alias('removeListener');

	/**
	 * Adds listeners in bulk using the manipulateListeners method.
	 * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added.
	 * You can also pass it a regular expression to add the array of listeners to all events that match it.
	 * Yeah, this function does quite a bit. That's probably a bad thing.
	 *
	 * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once.
	 * @param {Function[]} [listeners] An optional array of listener functions to add.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.addListeners = function addListeners(evt, listeners) {
		// Pass through to manipulateListeners
		return this.manipulateListeners(false, evt, listeners);
	};

	/**
	 * Removes listeners in bulk using the manipulateListeners method.
	 * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
	 * You can also pass it an event name and an array of listeners to be removed.
	 * You can also pass it a regular expression to remove the listeners from all events that match it.
	 *
	 * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once.
	 * @param {Function[]} [listeners] An optional array of listener functions to remove.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.removeListeners = function removeListeners(evt, listeners) {
		// Pass through to manipulateListeners
		return this.manipulateListeners(true, evt, listeners);
	};

	/**
	 * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level.
	 * The first argument will determine if the listeners are removed (true) or added (false).
	 * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
	 * You can also pass it an event name and an array of listeners to be added/removed.
	 * You can also pass it a regular expression to manipulate the listeners of all events that match it.
	 *
	 * @param {Boolean} remove True if you want to remove listeners, false if you want to add.
	 * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once.
	 * @param {Function[]} [listeners] An optional array of listener functions to add/remove.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) {
		var i;
		var value;
		var single = remove ? this.removeListener : this.addListener;
		var multiple = remove ? this.removeListeners : this.addListeners;

		// If evt is an object then pass each of it's properties to this method
		if (typeof evt === 'object' && !(evt instanceof RegExp)) {
			for (i in evt) {
				if (evt.hasOwnProperty(i) && (value = evt[i])) {
					// Pass the single listener straight through to the singular method
					if (typeof value === 'function') {
						single.call(this, i, value);
					}
					else {
						// Otherwise pass back to the multiple function
						multiple.call(this, i, value);
					}
				}
			}
		}
		else {
			// So evt must be a string
			// And listeners must be an array of listeners
			// Loop over it and pass each one to the multiple method
			i = listeners.length;
			while (i--) {
				single.call(this, evt, listeners[i]);
			}
		}

		return this;
	};

	/**
	 * Removes all listeners from a specified event.
	 * If you do not specify an event then all listeners will be removed.
	 * That means every event will be emptied.
	 * You can also pass a regex to remove all events that match it.
	 *
	 * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.removeEvent = function removeEvent(evt) {
		var type = typeof evt;
		var events = this._getEvents();
		var key;

		// Remove different things depending on the state of evt
		if (type === 'string') {
			// Remove all listeners for the specified event
			delete events[evt];
		}
		else if (type === 'object') {
			// Remove all events matching the regex.
			for (key in events) {
				if (events.hasOwnProperty(key) && evt.test(key)) {
					delete events[key];
				}
			}
		}
		else {
			// Remove all listeners in all events
			delete this._events;
		}

		return this;
	};

	/**
	 * Alias of removeEvent.
	 *
	 * Added to mirror the node API.
	 */
	proto.removeAllListeners = alias('removeEvent');

	/**
	 * Emits an event of your choice.
	 * When emitted, every listener attached to that event will be executed.
	 * If you pass the optional argument array then those arguments will be passed to every listener upon execution.
	 * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately.
	 * So they will not arrive within the array on the other side, they will be separate.
	 * You can also pass a regular expression to emit to all events that match it.
	 *
	 * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
	 * @param {Array} [args] Optional array of arguments to be passed to each listener.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.emitEvent = function emitEvent(evt, args) {
		var listeners = this.getListenersAsObject(evt);
		var listener;
		var i;
		var key;
		var response;

		for (key in listeners) {
			if (listeners.hasOwnProperty(key)) {
				i = listeners[key].length;

				while (i--) {
					// If the listener returns true then it shall be removed from the event
					// The function is executed either with a basic call or an apply if there is an args array
					listener = listeners[key][i];

					if (listener.once === true) {
						this.removeListener(evt, listener.listener);
					}

					response = listener.listener.apply(this, args || []);

					if (response === this._getOnceReturnValue()) {
						this.removeListener(evt, listener.listener);
					}
				}
			}
		}

		return this;
	};

	/**
	 * Alias of emitEvent
	 */
	proto.trigger = alias('emitEvent');

	/**
	 * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on.
	 * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it.
	 *
	 * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
	 * @param {...*} Optional additional arguments to be passed to each listener.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.emit = function emit(evt) {
		var args = Array.prototype.slice.call(arguments, 1);
		return this.emitEvent(evt, args);
	};

	/**
	 * Sets the current value to check against when executing listeners. If a
	 * listeners return value matches the one set here then it will be removed
	 * after execution. This value defaults to true.
	 *
	 * @param {*} value The new value to check for when executing listeners.
	 * @return {Object} Current instance of EventEmitter for chaining.
	 */
	proto.setOnceReturnValue = function setOnceReturnValue(value) {
		this._onceReturnValue = value;
		return this;
	};

	/**
	 * Fetches the current value to check against when executing listeners. If
	 * the listeners return value matches this one then it should be removed
	 * automatically. It will return true by default.
	 *
	 * @return {*|Boolean} The current value to check for or the default, true.
	 * @api private
	 */
	proto._getOnceReturnValue = function _getOnceReturnValue() {
		if (this.hasOwnProperty('_onceReturnValue')) {
			return this._onceReturnValue;
		}
		else {
			return true;
		}
	};

	/**
	 * Fetches the events object and creates one if required.
	 *
	 * @return {Object} The events storage object.
	 * @api private
	 */
	proto._getEvents = function _getEvents() {
		return this._events || (this._events = {});
	};

	/**
	 * Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version.
	 *
	 * @return {Function} Non conflicting EventEmitter class.
	 */
	EventEmitter.noConflict = function noConflict() {
		exports.EventEmitter = originalGlobalValue;
		return EventEmitter;
	};

	// Expose the class either via AMD, CommonJS or the global object
	if (typeof define === 'function' && define.amd) {
		define(function () {
			return EventEmitter;
		});
	}
	else if (typeof module === 'object' && module.exports){
		module.exports = EventEmitter;
	}
	else {
		this.EventEmitter = EventEmitter;
	}
}.call(this));
/*
palava v1.0.0 | LGPL | https://github.com/palavatv/palava-client

Copyright (C) 2013 Jan Lelis       jan@signaling.io
Copyright (C) 2013 Marius Melzer   marius@signaling.io
Copyright (C) 2013 Stephan Thamm   thammi@chaossource.net
Copyright (C) 2013 Kilian Ulbrich  kilian@innovailable.eu

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


(function() {


}).call(this);
(function() {
  var palava;

  this.palava = palava = {
    browser: {}
  };

}).call(this);
(function() {
  palava.browser.PeerConnection = window.PeerConnection || window.webkitPeerConnection00 || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;

  palava.browser.IceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;

  palava.browser.SessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;

  palava.browser.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

  palava.browser.isMozilla = function() {
    if (window.mozRTCPeerConnection) {
      return true;
    } else {
      return false;
    }
  };

  palava.browser.isChrome = function() {
    return /Chrome/i.test(navigator.userAgent);
  };

  palava.browser.getUserAgent = function() {
    if (palava.browser.isMozilla()) {
      return 'firefox';
    } else if (palava.browser.isChrome()) {
      return 'chrome';
    } else {
      return 'unknown';
    }
  };

  palava.browser.checkForWebrtcError = function() {
    var e;
    try {
      new palava.browser.PeerConnection({
        iceServers: []
      });
    } catch (_error) {
      e = _error;
      return e;
    }
    return !(palava.browser.PeerConnection && palava.browser.IceCandidate && palava.browser.SessionDescription && palava.browser.getUserMedia);
  };

  palava.browser.chromeVersion = function() {
    var matches, version, _;
    matches = /Chrome\/(\d+)/i.exec(navigator.userAgent);
    if (matches) {
      _ = matches[0], version = matches[1];
      return parseInt(version);
    } else {
      return false;
    }
  };

  palava.browser.checkForPartialSupport = function() {
    return palava.browser.isChrome() && palava.browser.chromeVersion() < 26;
  };

  palava.browser.getConstraints = function() {
    var constraints;
    constraints = {
      optional: [],
      mandatory: {
        OfferToReceiveAudio: true,
        OfferToReceiveVideo: true
      }
    };
    if (palava.browser.isMozilla()) {
      constraints.mandatory.MozDontOfferDataChannel = true;
    }
    return constraints;
  };

  palava.browser.getPeerConnectionOptions = function() {
    if (palava.browser.isChrome()) {
      return {
        "optional": [
          {
            "DtlsSrtpKeyAgreement": true
          }
        ]
      };
    } else {
      return {};
    }
  };

  palava.browser.patchSDP = function(sdp) {
    var chars, crypto, i, key, _i, _j, _k, _results, _results1;
    if (palava.browser.isChrome() && palava.browser.chromeVersion() >= 31) {
      return sdp;
    }
    chars = (function() {
      _results1 = [];
      for (_j = 33; _j <= 58; _j++){ _results1.push(_j); }
      return _results1;
    }).apply(this).concat((function() {
      _results = [];
      for (_i = 60; _i <= 126; _i++){ _results.push(_i); }
      return _results;
    }).apply(this)).map(function(a) {
      return String.fromCharCode(a);
    });
    key = '';
    for (i = _k = 0; _k < 40; i = ++_k) {
      key += chars[Math.floor(Math.random() * chars.length)];
    }
    crypto = 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:' + key + '\r\nc=IN';
    if (sdp.sdp.indexOf('a=crypto') === -1) {
      sdp.sdp = sdp.sdp.replace(/c=IN/g, crypto);
    }
    return sdp;
  };

  palava.browser.registerFullscreen = function(element, eventName) {
    if (element[0].requestFullscreen) {
      return element.on(eventName, function() {
        return this.requestFullscreen();
      });
    } else if (element[0].mozRequestFullScreen) {
      return element.on(eventName, function() {
        return this.mozRequestFullScreen();
      });
    } else if (element[0].webkitRequestFullscreen) {
      return element.on(eventName, function() {
        return this.webkitRequestFullscreen();
      });
    }
  };

  if (palava.browser.isMozilla()) {
    palava.browser.attachMediaStream = function(element, stream) {
      if (stream) {
        return $(element).prop('mozSrcObject', stream);
      } else {
        $(element).each(function(key, el) {
          return el.pause();
        });
        return $(element).prop('mozSrcObject', null);
      }
    };
    palava.browser.fixAudio = function(videoWrapper) {};
  } else if (palava.browser.isChrome()) {
    palava.browser.attachMediaStream = function(element, stream) {
      if (stream) {
        return $(element).prop('src', webkitURL.createObjectURL(stream));
      } else {
        $(element).each(function(key, el) {
          return el.pause();
        });
        return $(element).prop('src', null);
      }
    };
    palava.browser.fixAudio = function(videoWrapper) {
      if (videoWrapper.attr('data-peer-muted') !== 'true') {
        return $([200, 400, 1000, 2000, 4000, 8000, 16000]).each(function(_, n) {
          return setTimeout((function() {
            videoWrapper.find('.plv-video-mute').click();
            return videoWrapper.find('.plv-video-mute').click();
          }), n);
        });
      }
    };
  }

}).call(this);
(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  palava.Gum = (function(_super) {
    __extends(Gum, _super);

    function Gum(config) {
      this.releaseStream = __bind(this.releaseStream, this);
      this.getStream = __bind(this.getStream, this);
      this.requestStream = __bind(this.requestStream, this);
      this.config = config || {
        video: true,
        audio: true
      };
      this.stream = null;
    }

    Gum.prototype.requestStream = function() {
      var _this = this;
      palava.browser.getUserMedia.call(navigator, this.config, function(stream) {
        _this.stream = stream;
        return _this.emit('stream_ready', _this);
      }, function() {
        return _this.emit('stream_error', _this);
      });
      return true;
    };

    Gum.prototype.getStream = function() {
      return this.stream;
    };

    Gum.prototype.releaseStream = function() {
      if (this.stream) {
        this.stream.stop();
        this.stream = null;
        this.emit('stream_released', this);
        return true;
      } else {
        return false;
      }
    };

    return Gum;

  })(EventEmitter);

}).call(this);
(function() {
  palava.Identity = (function() {
    function Identity(o) {
      this.userMediaConfig = o.userMediaConfig;
      this.name = o.name;
    }

    Identity.prototype.newUserMedia = function() {
      return new palava.Gum(this.userMediaConfig);
    };

    Identity.prototype.getName = function() {
      return this.name;
    };

    return Identity;

  })();

}).call(this);
(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  palava.Peer = (function(_super) {
    __extends(Peer, _super);

    function Peer(id, status) {
      this.isLocal = __bind(this.isLocal, this);
      this.isReady = __bind(this.isReady, this);
      this.isMuted = __bind(this.isMuted, this);
      this.hasAudio = __bind(this.hasAudio, this);
      var _base;
      this.id = id;
      this.status = status || {};
      (_base = this.status).user_agent || (_base.user_agent = palava.browser.getUserAgent());
      this.joinTime = (new Date()).getTime();
    }

    Peer.prototype.hasAudio = function() {
      return palava.browser.checkForPartialSupport() || this.getStream() && this.getStream().getAudioTracks().length;
    };

    Peer.prototype.isMuted = function() {
      if (this.muted) {
        return true;
      } else {
        return false;
      }
    };

    Peer.prototype.isReady = function() {
      if (this.ready) {
        return true;
      } else {
        return false;
      }
    };

    Peer.prototype.isLocal = function() {
      if (this.local) {
        return true;
      } else {
        return false;
      }
    };

    return Peer;

  })(EventEmitter);

}).call(this);
(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  palava.LocalPeer = (function(_super) {
    __extends(LocalPeer, _super);

    function LocalPeer(id, status, room) {
      this.leave = __bind(this.leave, this);
      this.toggleMute = __bind(this.toggleMute, this);
      this.hasAudio = __bind(this.hasAudio, this);
      this.updateStatus = __bind(this.updateStatus, this);
      this.getStream = __bind(this.getStream, this);
      this.setupRoom = __bind(this.setupRoom, this);
      this.setupUserMedia = __bind(this.setupUserMedia, this);
      this.muted = true;
      this.local = true;
      LocalPeer.__super__.constructor.call(this, id, status);
      this.room = room;
      this.userMedia = room.userMedia;
      this.setupRoom();
      this.setupUserMedia();
    }

    LocalPeer.prototype.setupUserMedia = function() {
      var _this = this;
      this.userMedia.on('stream_released', function() {
        _this.ready = false;
        return _this.emit('stream_removed');
      });
      this.userMedia.on('stream_ready', function(e) {
        _this.ready = true;
        return _this.emit('stream_ready', e);
      });
      if (this.getStream()) {
        this.ready = true;
        return this.emit('stream_ready');
      }
    };

    LocalPeer.prototype.setupRoom = function() {
      var _this = this;
      this.room.peers[this.id] = this.room.localPeer = this;
      this.on('update', function() {
        return _this.room.emit('peer_update', _this);
      });
      this.on('stream_ready', function() {
        return _this.room.emit('peer_stream_ready', _this);
      });
      return this.on('stream_removed', function() {
        return _this.room.emit('peer_stream_removed', _this);
      });
    };

    LocalPeer.prototype.getStream = function() {
      return this.userMedia.getStream();
    };

    LocalPeer.prototype.updateStatus = function(status) {
      var key, _base;
      if (!status || !(status instanceof Object) || Object.keys(status).length === 0) {
        return status;
      }
      for (key in status) {
        this.status[key] = status[key];
      }
      (_base = this.status).user_agent || (_base.user_agent = palava.browser.getUserAgent());
      this.room.channel.send({
        event: 'update_status',
        status: this.status
      });
      return this.status;
    };

    LocalPeer.prototype.hasAudio = function() {
      return false;
    };

    LocalPeer.prototype.toggleMute = function() {
      return false;
    };

    LocalPeer.prototype.leave = function() {
      this.ready = false;
      return this.emit('left');
    };

    return LocalPeer;

  })(palava.Peer);

}).call(this);
(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  palava.Distributor = (function() {
    function Distributor(channel, peerId) {
      if (peerId == null) {
        peerId = null;
      }
      this.send = __bind(this.send, this);
      this.on = __bind(this.on, this);
      this.channel = channel;
      this.peerId = peerId;
    }

    Distributor.prototype.on = function(event, handler) {
      var _this = this;
      return this.channel.on('message', function(msg) {
        if (_this.peerId) {
          if (msg.sender_id === _this.peerId && event === msg.event) {
            return handler(msg);
          }
        } else {
          if (!msg.sender_id && event === msg.event) {
            return handler(msg);
          }
        }
      });
    };

    Distributor.prototype.send = function(msg) {
      var payload;
      if (this.peerId) {
        payload = {
          event: 'send_to_peer',
          peer_id: this.peerId,
          data: msg
        };
      } else {
        payload = msg;
      }
      return this.channel.send(payload);
    };

    return Distributor;

  })();

}).call(this);
(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  palava.RemotePeer = (function(_super) {
    __extends(RemotePeer, _super);

    function RemotePeer(id, status, room) {
      this.mozillaCheckAddStream = __bind(this.mozillaCheckAddStream, this);
      this.oaError = __bind(this.oaError, this);
      this.sdpSender = __bind(this.sdpSender, this);
      this.sendAnswer = __bind(this.sendAnswer, this);
      this.sendOfferIf = __bind(this.sendOfferIf, this);
      this.sendOffer = __bind(this.sendOffer, this);
      this.setupRoom = __bind(this.setupRoom, this);
      this.setupDistributor = __bind(this.setupDistributor, this);
      this.setupPeerConnection = __bind(this.setupPeerConnection, this);
      this.toggleMute = __bind(this.toggleMute, this);
      this.hasAudio = __bind(this.hasAudio, this);
      this.getStream = __bind(this.getStream, this);
      this.muted = false;
      this.local = false;
      RemotePeer.__super__.constructor.call(this, id, status);
      this.room = room;
      this.remoteStream = null;
      this.setupRoom();
      this.setupPeerConnection();
      this.setupDistributor();
    }

    RemotePeer.prototype.getStream = function() {
      return this.remoteStream;
    };

    RemotePeer.prototype.hasAudio = function() {
      return this.remoteStream && (palava.browser.checkForPartialSupport() || this.remoteStream.getAudioTracks().length);
    };

    RemotePeer.prototype.toggleMute = function() {
      return this.muted = !this.muted;
    };

    RemotePeer.prototype.setupPeerConnection = function() {
      var _this = this;
      this.peerConnection = new palava.browser.PeerConnection({
        iceServers: [
          {
            url: this.room.options.stun
          }
        ]
      }, palava.browser.getPeerConnectionOptions());
      this.peerConnection.onicecandidate = function(event) {
        if (event.candidate) {
          return _this.distributor.send({
            event: 'ice_candidate',
            sdpmlineindex: event.candidate.sdpMLineIndex,
            sdpmid: event.candidate.sdpMid,
            candidate: event.candidate.candidate
          });
        }
      };
      this.peerConnection.onaddstream = function(event) {
        _this.remoteStream = event.stream;
        _this.ready = true;
        return _this.emit('stream_ready');
      };
      this.peerConnection.onremovestream = function(event) {
        _this.remoteStream = null;
        _this.ready = false;
        return _this.emit('stream_removed');
      };
      if (this.room.localPeer.getStream()) {
        this.peerConnection.addStream(this.room.localPeer.getStream());
      } else {

      }
      return this.peerConnection;
    };

    RemotePeer.prototype.setupDistributor = function() {
      var _this = this;
      this.distributor = new palava.Distributor(this.room.channel, this.id);
      this.distributor.on('peer_left', function(msg) {
        if (_this.ready) {
          _this.remoteStream = null;
          _this.emit('stream_removed');
          _this.ready = false;
        }
        _this.peerConnection.close();
        return _this.emit('left');
      });
      this.distributor.on('ice_candidate', function(msg) {
        var candidate;
        candidate = new palava.browser.IceCandidate({
          candidate: msg.candidate,
          sdpMLineIndex: msg.sdpmlineindex,
          sdpMid: msg.sdpmid
        });
        return _this.peerConnection.addIceCandidate(candidate);
      });
      this.distributor.on('offer', function(msg) {
        _this.peerConnection.setRemoteDescription(new palava.browser.SessionDescription(msg.sdp));
        _this.emit('offer');
        return _this.sendAnswer();
      });
      this.distributor.on('answer', function(msg) {
        _this.peerConnection.setRemoteDescription(new palava.browser.SessionDescription(msg.sdp));
        return _this.emit('answer');
      });
      this.distributor.on('peer_updated_status', function(msg) {
        _this.status = msg.status;
        return _this.emit('update');
      });
      return this.distributor;
    };

    RemotePeer.prototype.setupRoom = function() {
      var _this = this;
      this.room.peers[this.id] = this;
      this.on('left', function() {
        delete _this.room.peers[_this.id];
        return _this.room.emit('peer_left', _this);
      });
      this.on('offer', function() {
        return _this.room.emit('peer_offer', _this);
      });
      this.on('answer', function() {
        return _this.room.emit('peer_answer', _this);
      });
      this.on('update', function() {
        return _this.room.emit('peer_update', _this);
      });
      this.on('stream_ready', function() {
        return _this.room.emit('peer_stream_ready', _this);
      });
      this.on('stream_removed', function() {
        return _this.room.emit('peer_stream_removed', _this);
      });
      return this.on('oaerror', function(e) {
        return _this.room.emit('peer_oaerror', _this, e);
      });
    };

    RemotePeer.prototype.sendOffer = function() {
      this.peerConnection.createOffer(this.sdpSender('offer'), this.oaError, palava.browser.getConstraints());
      return this.mozillaCheckAddStream();
    };

    RemotePeer.prototype.sendOfferIf = function(cond) {
      if (cond) {
        return this.sendOffer();
      }
    };

    RemotePeer.prototype.sendAnswer = function() {
      this.peerConnection.createAnswer(this.sdpSender('answer'), this.oaError, palava.browser.getConstraints());
      return this.mozillaCheckAddStream();
    };

    RemotePeer.prototype.sdpSender = function(event) {
      var _this = this;
      return function(sdp) {
        sdp = palava.browser.patchSDP(sdp);
        _this.peerConnection.setLocalDescription(sdp);
        return _this.distributor.send({
          event: event,
          sdp: sdp
        });
      };
    };

    RemotePeer.prototype.oaError = function(error) {
      return this.emit('oaerror', error);
    };

    RemotePeer.prototype.mozillaCheckAddStream = function() {
      var timeouts,
        _this = this;
      if (palava.browser.isMozilla()) {
        return timeouts = $([100, 200, 400, 1000, 2000, 4000, 8000, 12000, 16000]).map(function(_, n) {
          return setTimeout((function() {
            var remoteTrack;
            if (remoteTrack = (_this.peerConnection.remoteStreams && _this.peerConnection.remoteStreams[0]) || (_this.peerConnection.getRemoteStreams() && _this.peerConnection.getRemoteStreams()[0])) {
              timeouts.each(function(_, t) {
                return clearTimeout(t);
              });
              return _this.peerConnection.onaddstream({
                stream: remoteTrack
              });
            }
          }), n);
        });
      }
    };

    return RemotePeer;

  })(palava.Peer);

}).call(this);
(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  palava.Room = (function(_super) {
    __extends(Room, _super);

    function Room(roomId, channel, userMedia, options) {
      if (options == null) {
        options = {};
      }
      this.getAllPeers = __bind(this.getAllPeers, this);
      this.getRemotePeers = __bind(this.getRemotePeers, this);
      this.getLocalPeer = __bind(this.getLocalPeer, this);
      this.getPeerById = __bind(this.getPeerById, this);
      this.leave = __bind(this.leave, this);
      this.join = __bind(this.join, this);
      this.setupDistributor = __bind(this.setupDistributor, this);
      this.setupOptions = __bind(this.setupOptions, this);
      this.setupChannel = __bind(this.setupChannel, this);
      this.setupUserMedia = __bind(this.setupUserMedia, this);
      this.id = roomId;
      this.userMedia = userMedia;
      this.channel = channel;
      this.peers = {};
      this.options = options;
      this.setupUserMedia();
      this.setupChannel();
      this.setupDistributor();
      this.setupOptions();
    }

    Room.prototype.setupUserMedia = function() {
      var _this = this;
      this.userMedia.on('stream_ready', function(event) {
        return _this.emit('local_stream_ready', event.stream);
      });
      return this.userMedia.on('stream_released', function() {
        return _this.emit('local_stream_removed');
      });
    };

    Room.prototype.setupChannel = function() {
      var _this = this;
      this.channel.on('not_reachable', function(e) {
        return _this.emit('signaling_not_reachable', e);
      });
      this.channel.on('error', function(e) {
        return _this.emit('signaling_error', e);
      });
      return this.channel.on('close', function(e) {
        return _this.emit('signaling_close', e);
      });
    };

    Room.prototype.setupOptions = function() {
      var _base, _base1;
      (_base = this.options).joinTimeout || (_base.joinTimeout = 1000);
      return (_base1 = this.options).ownStatus || (_base1.ownStatus = {});
    };

    Room.prototype.setupDistributor = function() {
      var _this = this;
      this.distributor = new palava.Distributor(this.channel);
      this.distributor.on('joined_room', function(msg) {
        var newPeer, peer, _i, _len, _ref;
        clearTimeout(_this.joinCheckTimeout);
        new palava.LocalPeer(msg.own_id, _this.options.ownStatus, _this);
        _ref = msg.peers;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          peer = _ref[_i];
          newPeer = new palava.RemotePeer(peer.peer_id, peer.status, _this);
          newPeer.sendOfferIf(!palava.browser.isChrome());
        }
        return _this.emit("joined", _this);
      });
      this.distributor.on('new_peer', function(msg) {
        var newPeer;
        newPeer = new palava.RemotePeer(msg.peer_id, msg.status, _this);
        newPeer.sendOfferIf(msg.status.user_agent === 'chrome');
        return _this.emit('peer_joined', newPeer);
      });
      this.distributor.on('error', function(msg) {
        return _this.emit('signaling_error', msg.message);
      });
      return this.distributor.on('shutdown', function(msg) {
        return _this.emit('signaling_shutdown', msg.seconds);
      });
    };

    Room.prototype.join = function(status) {
      var key, _base, _i, _len,
        _this = this;
      if (status == null) {
        status = {};
      }
      this.joinCheckTimeout = setTimeout((function() {
        _this.emit('join_error', 'Not able to join room');
        return _this.leave();
      }), this.options.joinTimeout);
      for (_i = 0, _len = status.length; _i < _len; _i++) {
        key = status[_i];
        this.options.ownStatus[key] = status[key];
      }
      (_base = this.options.ownStatus).user_agent || (_base.user_agent = palava.browser.getUserAgent());
      return this.distributor.send({
        event: 'join_room',
        room_id: this.id,
        status: this.options.ownStatus
      });
    };

    Room.prototype.leave = function() {
      this.emit('leave');
      this.channel && this.channel.close();
      return this.localPeer && this.localPeer.stream && this.localPeer.stream.close();
    };

    Room.prototype.getPeerById = function(id) {
      return this.peers[id];
    };

    Room.prototype.getLocalPeer = function() {
      return this.localPeer;
    };

    Room.prototype.getRemotePeers = function() {
      return this.getAllPeers(false);
    };

    Room.prototype.getAllPeers = function(allowLocal) {
      var id, peer, peers, _ref;
      if (allowLocal == null) {
        allowLocal = true;
      }
      peers = [];
      _ref = this.peers;
      for (id in _ref) {
        peer = _ref[id];
        if (allowLocal || !peer.local) {
          peers.push(peer);
        }
      }
      return peers;
    };

    return Room;

  })(EventEmitter);

}).call(this);
(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  palava.WebSocketChannel = (function(_super) {
    __extends(WebSocketChannel, _super);

    function WebSocketChannel(address) {
      this.checkConnectionTimeout = __bind(this.checkConnectionTimeout, this);
      this.close = __bind(this.close, this);
      this.send = __bind(this.send, this);
      this.setupEvents = __bind(this.setupEvents, this);
      var _this = this;
      this.reached = false;
      this.socket = new WebSocket(address);
      this.socket.onopen = function(handshake) {
        _this.setupEvents();
        return _this.emit('open', handshake);
      };
    }

    WebSocketChannel.prototype.setupEvents = function() {
      var _this = this;
      this.socket.onmessage = function(msg) {
        var SyntaxError;
        try {
          return _this.emit('message', JSON.parse(msg.data));
        } catch (_error) {
          SyntaxError = _error;
          return _this.emit('error_invalid_json', msg);
        }
      };
      this.socket.onerror = function(msg) {
        return _this.emit('error', msg);
      };
      return this.socket.onclose = function() {
        return _this.emit('close');
      };
    };

    WebSocketChannel.prototype.send = function(data) {
      this.socket.send(JSON.stringify(data));
      if (!this.reached) {
        return this.checkConnectionTimeout();
      }
    };

    WebSocketChannel.prototype.close = function() {
      return this.socket.close();
    };

    WebSocketChannel.prototype.checkConnectionTimeout = function() {
      var _this = this;
      return setTimeout((function() {
        if (_this.socket.readyState === 3) {
          return _this.emit('not_reachable', _this.serverAddress);
        } else {
          return _this.reached = true;
        }
      }), 500);
    };

    return WebSocketChannel;

  })(EventEmitter);

}).call(this);
(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  palava.Session = (function(_super) {
    __extends(Session, _super);

    function Session(o) {
      this.destroy = __bind(this.destroy, this);
      this.setupRoom = __bind(this.setupRoom, this);
      this.getRoom = __bind(this.getRoom, this);
      this.getUserMedia = __bind(this.getUserMedia, this);
      this.getChannel = __bind(this.getChannel, this);
      this.checkRequirements = __bind(this.checkRequirements, this);
      this.assignOptions = __bind(this.assignOptions, this);
      this.init = __bind(this.init, this);
      this.channel = null;
      this.userMedia = null;
      this.roomId = null;
      this.roomOptions = {};
      this.assignOptions(o);
    }

    Session.prototype.init = function(o) {
      this.assignOptions(o);
      this.checkRequirements();
      this.setupRoom();
      return this.userMedia.requestStream();
    };

    Session.prototype.assignOptions = function(o) {
      this.roomId = o.roomId || this.roomId;
      if (o.channel) {
        this.channel = o.channel;
      } else if (o.web_socket_channel) {
        this.channel = new palava.WebSocketChannel(o.web_socket_channel);
      }
      if (o.identity) {
        this.userMedia = o.identity.newUserMedia();
        this.roomOptions.ownStatus = {
          name: o.identity.getName()
        };
      }
      if (o.options) {
        this.roomOptions.stun = o.options.stun || this.roomOptions.stun;
        return this.roomOptions.joinTimeout = o.options.joinTimeout || this.roomOptions.joinTimeout;
      }
    };

    Session.prototype.checkRequirements = function() {
      var e;
      if (!this.channel) {
        this.emit('argument_error', 'no channel given');
        return;
      }
      if (!this.userMedia) {
        this.emit('argument_error', 'no user media given');
        return;
      }
      if (!this.roomId) {
        this.emit('argument_error', 'no room id given');
        return;
      }
      if (!this.roomOptions.stun) {
        this.emit('argument_error', 'no stun server given');
        return;
      }
      if (e = palava.browser.checkForWebrtcError()) {
        this.emit('webrtc_no_support', 'WebRTC is not supported by your browser', e);
        return;
      }
      if (palava.browser.checkForPartialSupport()) {
        return this.emit('webrtc_partial_support');
      }
    };

    Session.prototype.getChannel = function() {
      return this.channel;
    };

    Session.prototype.getUserMedia = function() {
      return this.userMedia;
    };

    Session.prototype.getRoom = function() {
      return this.room;
    };

    Session.prototype.setupRoom = function() {
      var _this = this;
      this.room = new palava.Room(this.roomId, this.channel, this.userMedia, this.roomOptions);
      this.room.on('local_stream_ready', function(s) {
        return _this.emit('local_stream_ready', s);
      });
      this.room.on('local_stream_error', function(s) {
        return _this.emit('local_stream_error');
      });
      this.room.on('local_stream_removed', function() {
        return _this.emit('local_stream_removed');
      });
      this.room.on('join_error', function(e) {
        return _this.emit('room_join_error', _this.room, e);
      });
      this.room.on('full', function() {
        return _this.emit('room_full', _this.room);
      });
      this.room.on('joined', function() {
        return _this.emit('room_joined', _this.room);
      });
      this.room.on('peer_joined', function(p) {
        return _this.emit('peer_joined', p);
      });
      this.room.on('peer_offer', function(p) {
        return _this.emit('peer_offer', p);
      });
      this.room.on('peer_answer', function(p) {
        return _this.emit('peer_answer', p);
      });
      this.room.on('peer_update', function(p) {
        return _this.emit('peer_update', p);
      });
      this.room.on('peer_stream_ready', function(p) {
        return _this.emit('peer_stream_ready', p);
      });
      this.room.on('peer_stream_removed', function(p) {
        return _this.emit('peer_stream_removed', p);
      });
      this.room.on('peer_left', function(p) {
        return _this.emit('peer_left', p);
      });
      this.room.on('signaling_shutdown', function(p) {
        return _this.emit('signaling_shutdown', p);
      });
      this.room.on('signaling_close', function(p) {
        return _this.emit('signaling_close', p);
      });
      this.room.on('signaling_error', function(p) {
        return _this.emit('signaling_error', p);
      });
      this.room.on('signaling_not_reachable', function(p) {
        return _this.emit('signaling_not_reachable', p);
      });
      return true;
    };

    Session.prototype.destroy = function() {
      this.emit('session_before_destroy');
      this.room && this.room.leave();
      this.channel && this.channel.close();
      this.userMedia && this.userMedia.releaseStream();
      return this.emit('session_after_destroy');
    };

    return Session;

  })(EventEmitter);

}).call(this);
(function() {
  palava.PROTOCOL_NAME = 'palava';

  palava.PROTOCOL_VERSION = '1.0.0';

  palava.protocol_identifier = function() {
    return palava.PROTOCOL_NAME = "palava.1.0";
  };

}).call(this);


