var videos = [];
var PeerConnection = window.PeerConnection || window.webkitPeerConnection00 || window.webkitRTCPeerConnection || window.mozRTCPeerConnection || window.RTCPeerConnection;
function getNumPerRow() {
var len = videos.length;
var biggest;
// Ensure length is even for better division.
if(len % 2 === 1) {
len++;
}
biggest = Math.ceil(Math.sqrt(len));
while(len % biggest !== 0) {
biggest++;
}
return biggest;
}
function subdivideVideos() {
var perRow = getNumPerRow();
var numInRow = 0;
for(var i = 0, len = videos.length; i < len; i++) {
var video = videos[i];
setWH(video, i);
numInRow = (numInRow + 1) % perRow;
}
}
function setWH(video, i) {
var perRow = getNumPerRow();
var perColumn = Math.ceil(videos.length / perRow);
var width = Math.floor((window.innerWidth) / perRow);
var height = Math.floor((window.innerHeight - 190) / perColumn);
video.width = width;
video.height = height;
video.style.position = "absolute";
video.style.left = (i % perRow) * width + "px";
video.style.top = Math.floor(i / perRow) * height + "px";
}
function cloneVideo(domId, socketId) {
var video = document.getElementById(domId);
var clone = video.cloneNode(false);
clone.id = "remote" + socketId;
document.getElementById('videos').appendChild(clone);
videos.push(clone);
return clone;
}
function removeVideo(socketId) {
var video = document.getElementById('remote' + socketId);
if(video) {
videos.splice(videos.indexOf(video), 1);
video.parentNode.removeChild(video);
}
}
function addToChat(msg, color) {
var messages = document.getElementById('messages');
msg = sanitize(msg);
if(color) {
msg = '' + msg + '';
} else {
msg = '' + msg + '';
}
messages.innerHTML = messages.innerHTML + msg + '
';
messages.scrollTop = 10000;
}
function sanitize(msg) {
return msg.replace(/