// ============================================= //
//           CommuniGate Media Plugin loader     //
//                                               //
// Version 1.1                                   //
// Copyright (c) 2008, Stalker Software, Inc.    //
// ============================================= //
  var mediaPluginActiveX1   = '<object id="CommuniGateAV" classid="clsid:FA1F9352-0B8B-431D-BB18-7A3457F696F4" codebase=';
  var mediaPluginActiveX2   = ' ></object>';
  var mediaPluginActiveXURL = 'www.communigate.com/MediaPlugin/1.0/CGateAVX.cab';

  var mediaPluginNetscape1   = '<object id="CommuniGateAV" type="application/npCommuniGateAV-plugin" width="0" height="0" codebase=';
  var mediaPluginNetscape2   = ' ></object>';
  var mediaPluginMacOSURL    = 'www.communigate.com/MediaPlugin/1.0/CommuniGateAV.dmg';
  var mediaPluginNetscapeURL = 'www.communigate.com/MediaPlugin/1.0/npCommuniGateAV.xpi';

  var isHttps                = false;
  var checkTimer             = null;
  var thePlugin              = null;
	
function setHttps(https) {
  isHttps = https;
}

function getHTTPSchema() {
  return(isHttps ? "https://" : "http://");
}

function loadMediaPlugin(version) {
  clearInterval(checkTimer);
  var obj         = null;

  if (navigator.appName == "Microsoft Internet Explorer") {
    obj = mediaPluginActiveX1+ '"'  + getHTTPSchema() + mediaPluginActiveXURL + '"' + mediaPluginActiveX2;
  } else if(navigator.userAgent != null && navigator.userAgent.toUpperCase().indexOf('MAC OS X') != -1) {
    obj = mediaPluginNetscape1+ '"' + getHTTPSchema() + mediaPluginMacOSURL + '"' + mediaPluginNetscape2;
  } else if (navigator.appName == "Netscape") {
    obj = mediaPluginNetscape1+ '"' + getHTTPSchema() + mediaPluginNetscapeURL + '"' + mediaPluginNetscape2;
  }
  if(obj != null) {
    document.getElementById("MediaPlugin").innerHTML = obj;
    checkTimer = setInterval("checkLoaded();", 200);
    return "Loading";
  }
  return "Unsupported browser";
}

//
// This function is called periodically. It checks if the Plugin has been loaded
//
function checkLoaded() {
  thePlugin =  document.getElementById("CommuniGateAV");
  switch(typeof thePlugin) {
  case 'undefined':
  break; case 'object': case 'function':
	if(typeof thePlugin.GetVersion != 'undefined' ) {
      clearInterval(checkTimer);
	  pronto.mediaPluginInitialized();
	}
  }
}


// ------------------------------------------------
// methods

function traceResult( operationResult )	{
  return(operationResult == "" || operationResult == null ? "OK" : operationResult);
}	

// legs methods	

function     createLeg(leg) {return traceResult( thePlugin.CreateLeg (leg,"",""));}
function    destroyLeg(leg) {return traceResult( thePlugin.DestroyLeg(leg));}
function   activateLeg(leg) {return traceResult( thePlugin.Activate  (leg));}
function deactivateLeg(leg)	{return traceResult( thePlugin.Deactivate(leg));}
function       setHold(leg,flag ) {return traceResult(thePlugin.SetHold(leg, flag));}

// sdp methods	
function  getLocalSDP(leg)        {return traceResult( thePlugin.GetLocalSDP(leg,"xml") );}
function setRemoteSDP(leg,sdp)    {return traceResult(thePlugin.SetRemoteSDP(leg, sdp, "xml"));}
function     sendDTMF(leg,symbol) {return traceResult(thePlugin.SendDTMF    (leg, symbol) );}
	
// play, volume methods	
function playToLocal(leg, data, loop)   {return traceResult( thePlugin.PlayToLocal(leg, data, loop) );}
function playToRemote(leg, data, loop)	{return traceResult( thePlugin.PlayToRemote(leg, data, loop) );	}
function setInputVolume(leg, level)  	{return traceResult( thePlugin.SetInputVolume(leg, level) );}
function setOutputVolume(leg, level)	{return traceResult( thePlugin.SetOutputVolume(leg, level) );}
	
// connection  properties	
function setLocalIP(ip)	   {return traceResult( thePlugin.SetLocalIP(ip) );}
function setLocalPorts(ip) {return traceResult( thePlugin.SetLocalPorts(from, till, true) );}
	
function getVersion()	{return thePlugin.GetVersion();}
	
// advanced methods		
function listDevices()	{return thePlugin.ListDevices("audioInput")+"; "+thePlugin.ListDevices("audioOutput");}	
function setLogLevel(logLevel) {return traceResult( thePlugin.SetLogLevel(logLevel));}

