
Apideo={}
Apideo.defaultUrl="video.apideo.com:80";Apideo.swfUrl="./";Apideo.apideoWebSite="www.apideo.com";Apideo.counter=0
Apideo.connectedRooms=new Array
var scriptsTag=document.getElementsByTagName("script")
var i
for(i=0;i<scriptsTag.length;i++){var scriptSrc=scriptsTag[i].src
var lastIndexOfApideoJS=scriptSrc.lastIndexOf("apideo.js")
if(lastIndexOfApideoJS!=-1){Apideo.swfUrl=scriptSrc.substring(0,lastIndexOfApideoJS)
break;}}
Apideo.connect=function(key){if(window.location.protocol=="file:"){Apideo.handleError('invalid.protocol','Invalid protocol detected: file://.\nYou cannot access Apideo by double-clicking on an HTML file on your file system. Because of security restrictions, Apideo needs to be executed from a web site.')
return null}
return new ApideoConnection(key)}
function ApideoConnection(key){this.apideo_key=key}
ApideoConnection.prototype.joinRoom=function(room_name,user_obj,security_token,debug_div_id){return new ApideoRoom(this,room_name,user_obj,security_token,debug_div_id)}
function ApideoRoom(connection,room_name,user_obj,security_token,debug_div_id){Apideo.counter++
this.uid="room"+Apideo.counter
Apideo.connectedRooms[this.uid]=this
this.connection=connection
this.room_name=room_name
this.user_obj=user_obj
this.security_token=security_token
this.videoServerIP=null
this.videoServerPort=null
this.videoServerInstanceId=null
this.awaitingTasks=new Array
this.eventListeners=Array();this.hasCameraVar=null
this.hasMicrophoneVar=null
this.cameras=new Array
this.viewers=new Array
this.divId="apideo"+this.uid
var width
var height
var flash_div
var invisible
if(debug_div_id){width=445
height=400
flash_div=document.getElementById(debug_div_id)
if(!flash_div){alert("APIDEO: Unable to find div with ID '"+debug_div_id+"'")
return}}else{width=0
height=0
flash_div=document.getElementsByTagName("body")[0]
if(!flash_div){alert("APIDEO: Unable to find &lt;body&gt; tag.")
return}}
this.insertFlash(this.divId,Apideo.swfUrl+'apideoevents.swf',width,height,flash_div,this.uid,'lcId='+this.uid+'&apideo_key='+encodeURIComponent(connection.apideo_key)+'&room_name='+encodeURIComponent(room_name)+'&security_token='+encodeURIComponent(security_token)+'&apideourl='+encodeURIComponent(Apideo.defaultUrl)+'&apideowebsiteurl='+encodeURIComponent(Apideo.apideoWebSite));}
ApideoRoom.prototype.insertFlash=function(divId,swfFile,width,height,tag,flashId,params,invisible){var div=document.createElement("div");div.id=divId
if(width==0||height==0){div.style.width=0
div.style.height=0
div.style.fontSize=0}
tag.appendChild(div);div.innerHTML='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="'+flashId+'" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="'+swfFile+'?'+params+'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="'+swfFile+'?'+params+'" quality="high" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="'+flashId+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';}
Apideo.getMovie=function(movieName){var isIE=navigator.appName.indexOf("Microsoft")!=-1
return(isIE)?window[movieName]:document[movieName]}
ApideoRoom.prototype.getEventMovie=function(){return Apideo.getMovie(this.uid)}
Apideo.receiveRoomLocation=function(uid,videoServerIP,videoServerPort,videoServerInstanceId){var room=Apideo.connectedRooms[uid]
if(!room){alert("APIDEO: internal error! Unable to find the room matching the server response.")
return}
room.videoServerIP=videoServerIP
room.videoServerPort=videoServerPort
room.videoServerInstanceId=videoServerInstanceId
room.processAwaitingTasks()}
Apideo.onPlayerLoaded=function(roomUid,viewerUid){var room=Apideo.connectedRooms[roomUid]
if(!room){alert("APIDEO: internal error! Unable to find the room matching the flash response.")
return}
var viewer=room.viewers[viewerUid]
if(!viewer){alert("APIDEO: internal error! Unable to find the player object matching the flash response.")
return}
viewer.started=true
viewer.processAwaitingTasks()}
Apideo.onRecorderLoaded=function(roomUid,cameraUid){var room=Apideo.connectedRooms[roomUid]
if(!room){alert("APIDEO: internal error! Unable to find the room matching the flash response.")
return}
var camera=room.cameras[cameraUid]
if(!camera){alert("APIDEO: internal error! Unable to find the camera object matching the flash response.")
return}
camera.started=true
camera.processAwaitingTasks()}
Apideo.handleError=function(className,message){alert("Error code: "+className+"\nMessage: "+message);}
Apideo.popupWindow=function(url){window.open(url)}
ApideoRoom.prototype.processAwaitingTasks=function(){while(this.awaitingTasks.length!=0){task=this.awaitingTasks.shift()
task()}}
ApideoRoom.prototype.sendEvent=function(event,listenerName,suppressEcho,returnObject){if(suppressEcho==null){suppressEcho=false}
var room=this
this.delayUntilLoad(function(){room.getEventMovie().sendEvent(event,listenerName,suppressEcho,returnObject);});}
ApideoRoom.prototype.registerListener=function(eventListener,listenerName,blockPropagation){if(listenerName==null)
listenerName="";if(blockPropagation==null)
blockPropagation=false;this.eventListeners[listenerName]={eventListener:eventListener,blockPropagation:blockPropagation};var room=this
this.delayUntilLoad(function(){room.getEventMovie().registerListener(listenerName);});};ApideoRoom.prototype.delayUntilLoad=function(func){if(this.videoServerIP){func()}else{this.awaitingTasks.push(func)}}
ApideoRoom.prototype.unregisterListener=function(listenerName){if(listenerName==null)
listenerName="";this.eventListeners[listenerName]=null;var room=this
this.delayUntilLoad(function(){room.getEventMovie().unregisterListener(listenerName);});}
ApideoRoom.prototype.updateUserData=function(userData){this.user_obj=userData
var room=this
this.delayUntilLoad(function(){room.getEventMovie().updateUserData(userData);});}
Apideo.receiveJsonMessage=function(msg,targetListener,senderId,uid,timeStamp){var room=Apideo.connectedRooms[uid]
var dateStamp=new Date(timeStamp);if(!room){alert("APIDEO: internal error! Unable to find the room matching the server response.")
return}
do{if(room.eventListeners[targetListener]!=null){room.eventListeners[targetListener].eventListener(msg,senderId,targetListener,dateStamp);if(room.eventListeners[targetListener].blockPropagation){break;}}
if(targetListener==""){break;}
var lastIndex=targetListener.lastIndexOf('.')
if(lastIndex==-1){targetListener=""}else{targetListener=targetListener.substr(0,lastIndex)
alert("Trying listener "+targetListener)}}while(true)}
Apideo.receiveUserList=function(userList,myId,uid){var room=Apideo.connectedRooms[uid]
if(!room){alert("APIDEO: internal error! Unable to find the room matching the server response.")
return}
room.userList=userList
room.myId=myId
if(room.onLoad){room.onLoad()}}
Apideo.onUserJoin=function(userid,userobj,uid){var room=Apideo.connectedRooms[uid]
if(!room){alert("APIDEO: internal error! Unable to find the room matching the server response.")
return}
room.userList[userid]=userobj
if(room.onUserJoin){room.onUserJoin(userid,userobj)}}
Apideo.onUserQuit=function(userid,uid,timeStamp){var room=Apideo.connectedRooms[uid]
var dateStamp=new Date(timeStamp);if(!room){alert("APIDEO: internal error! Unable to find the room matching the server response.")
return}
var userObj=room.userList[userid]
room.userList[userid]=null
if(room.onUserQuit){room.onUserQuit(userid,userObj,dateStamp)}}
Apideo.onUserUpdate=function(userid,userObj,uid){var room=Apideo.connectedRooms[uid]
if(!room){alert("APIDEO: internal error! Unable to find the room matching the server response.")
return}
room.userList[userid]=userObj
if(room.onUserUpdate){room.onUserUpdate(userid,userObj)}}
Apideo.getUserObject=function(uid){var room=Apideo.connectedRooms[uid]
if(!room){alert("APIDEO: internal error! Unable to find the room matching the server response.")
return}
return room.user_obj}
Apideo.hasCamAndMics=function(uid,hasCamera,hasMicrophone){var room=Apideo.connectedRooms[uid]
if(!room){alert("APIDEO: internal error! Unable to find the room matching the server response.")
return}
room.hasCameraVar=hasCamera
room.hasMicrophoneVar=hasMicrophone}
Apideo.onCameraStarted=function(uid){var room=Apideo.connectedRooms[uid]
if(!room){alert("APIDEO: internal error! Unable to find the room matching the server response.")
return}
if(room.onCameraStarted){room.onCameraStarted()}}
ApideoRoom.prototype.hasCamera=function(){return this.hasCameraVar}
ApideoRoom.prototype.hasMicrophone=function(){return this.hasMicrophoneVar}
ApideoRoom.prototype.startCamera=function(divId,streamId,config){var width=320
var height=240
var startCamera=1
var cameraWidth=320
var cameraHeight=240
var cameraFps=30
var cameraQuality=0
var cameraBandwidth=100000
var startMicrophone=1
var micRate=11
if(config){if(config.width!=undefined){if(config.width<215){alert("Apideo: Error starting camera. The camera size must be at least 215 x 138 px.");}
width=config.width}
if(config.height!=undefined){if(config.height<138){alert("Apideo: Error starting camera. The camera size must be at least 215 x 138 px.");}
height=config.height}
if(config.camera!=undefined){if(config.camera.startCamera!=undefined){startCamera=(config.camera.startCamera==true)?1:0}
if(config.camera.width!=undefined){cameraWidth=config.camera.width}
if(config.camera.height!=undefined){cameraHeight=config.camera.height}
if(config.camera.fps!=undefined){cameraFps=config.camera.fps}
if(config.camera.quality!=undefined){cameraQuality=config.camera.quality}
if(config.camera.bandwidth!=undefined){cameraBandwidth=config.camera.bandwidth}
if(config.camera.startMicrophone!=undefined){startMicrophone=(config.camera.startMicrophone==true)?1:0}
if(config.camera.micRate!=undefined){micRate=config.camera.micRate}}}
var room=this
var camera=new ApideoCamera()
this.cameras[camera.uid]=camera
this.delayUntilLoad(function(){room.insertFlash(camera.divId,Apideo.swfUrl+'apideorecorder.swf',width,height,document.getElementById(divId),camera.flashId,'videoServerIP='+room.videoServerIP+'&videoServerPort='+room.videoServerPort+'&videoServerInstanceId='+room.videoServerInstanceId+'&streamId='+streamId+'&swfwidth='+width+'&swfheight='+height+'&startCamera='+startCamera+'&cameraWidth='+cameraWidth+'&cameraHeight='+cameraHeight+'&cameraFps='+cameraFps+'&cameraBandwidth='+cameraBandwidth+'&cameraQuality='+cameraQuality+'&startMicrophone='+startMicrophone+'&micRate='+micRate+'&apideowebsiteurl='+encodeURIComponent(Apideo.apideoWebSite)+'&apideo_key='+encodeURIComponent(room.connection.apideo_key)+'&lcId='+room.uid+'&cameraUid='+camera.uid)});return camera}
ApideoRoom.prototype.playStream=function(divId,streamId,config){var width=320
var height=240
if(config){if(config.width){width=config.width}
if(config.height){height=config.height}}
var room=this
var viewer=new ApideoViewer();this.viewers[viewer.uid]=viewer
this.delayUntilLoad(function(){room.insertFlash(viewer.divId,Apideo.swfUrl+'apideoplayer.swf',width,height,document.getElementById(divId),viewer.flashId,'videoServerIP='+room.videoServerIP+'&videoServerPort='+room.videoServerPort+'&videoServerInstanceId='+room.videoServerInstanceId+'&streamId='+streamId+'&apideowebsiteurl='+encodeURIComponent(Apideo.apideoWebSite)+'&apideo_key='+encodeURIComponent(room.connection.apideo_key)+'&roomUid='+room.uid+'&viewerUid='+viewer.uid)});return viewer}
ApideoCamera=function(){Apideo.counter++
this.uid="camera"+Apideo.counter
this.divId="apideo"+this.uid
this.flashId="apideorecorder"+Apideo.counter}
ApideoCamera.prototype.getCameraMovie=function(){return Apideo.getMovie(this.flashId)}
ApideoCamera.prototype.delayUntilLoad=function(func){if(this.started){func()}else{this.awaitingTasks.push(func)}}
ApideoCamera.prototype.processAwaitingTasks=function(){while(this.awaitingTasks.length!=0){task=this.awaitingTasks.shift()
task()}}
ApideoViewer=function(){Apideo.counter++
this.uid="playstream"+Apideo.counter
this.divId="apideo"+this.uid
this.flashId="apideoplayer"+Apideo.counter
this.started=false
this.awaitingTasks=new Array}
ApideoViewer.prototype.getViewerMovie=function(){return Apideo.getMovie(this.flashId)}
ApideoViewer.prototype.showDelay=function(){var viewer=this
this.delayUntilLoad(function(){viewer.getViewerMovie().showDelay()})}
ApideoViewer.prototype.hideDelay=function(){var viewer=this
this.delayUntilLoad(function(){viewer.getViewerMovie().hideDelay()})}
ApideoViewer.prototype.delayUntilLoad=function(func){if(this.started){func()}else{this.awaitingTasks.push(func)}}
ApideoViewer.prototype.processAwaitingTasks=function(){while(this.awaitingTasks.length!=0){task=this.awaitingTasks.shift()
task()}}
ApideoRoom.prototype.removeCamera=function(camera){camera.getCameraMovie().unloadFlashMovie();var div=document.getElementById(camera.divId);if(div!=null){div.parentNode.removeChild(div)}
var i=0
for(currentCamera in this.cameras){if(this.cameras[currentCamera]==camera){this.cameras[camera.uid]=null
break;}}}
ApideoRoom.prototype.removeStream=function(viewer){viewer.getViewerMovie().unloadFlashMovie();var div=document.getElementById(viewer.divId);if(div!=null){div.parentNode.removeChild(div)}
var i=0
for(currentViewer in this.viewers){if(this.viewers[currentViewer]==viewer){this.viewers[viewer.uid]=null
break;}}}
ApideoRoom.prototype.removeAllCameras=function(){for(currentCamera in this.cameras){if(this.cameras[currentCamera]!=null&&this.cameras[currentCamera]instanceof ApideoCamera){this.cameras[currentCamera].getCameraMovie().unloadFlashMovie();var div=document.getElementById(this.cameras[currentCamera].divId);if(div!=null){div.parentNode.removeChild(div)}}}
this.cameras=new Array}
ApideoRoom.prototype.removeAllStreams=function(){for(currentViewer in this.viewers){if(this.viewers[currentViewer]!=null&&this.viewers[currentViewer]instanceof ApideoViewer){this.viewers[currentViewer].getViewerMovie().unloadFlashMovie();var div=document.getElementById(this.viewers[currentViewer].divId);if(div!=null){div.parentNode.removeChild(div)}}}
this.viewers=new Array}
ApideoConnection.prototype.quitRoom=function(room){room.removeAllStreams()
room.removeAllCameras()
room.getEventMovie().unloadFlashMovie()
var div=document.getElementById(room.divId);if(div!=null){div.parentNode.removeChild(div)}}