// ============================================= //
//           CommuniGate Media Plugin loader     //
//                                               //
// Version 1.2                                   //
// Copyright (c) 2008, Stalker Software, Inc.    //
// ============================================= //

  var objectActiveX2   = ' ></object>';
  var objectNetscape2   = ' ></object>';
  
  // CG plugin
  var mediaPluginMIMEType   = 'application/npCommuniGateAV-plugin';
  var mediaPluginObjectActiveX1   = '<object id="CommuniGateAV" classid="clsid:FA1F9352-0B8B-431D-BB18-7A3457F696F4" codebase=';
  var mediaPluginObjectNetscape1   = '<object id="CommuniGateAV" type="'+mediaPluginMIMEType+'" width="0" height="0" codebase=';
  
  var mediaPluginActiveXURL = 'www.communigate.com/MediaPlugin/1.0/CGateAVX.cab';
  var mediaPluginMacOSURL    = 'www.communigate.com/MediaPlugin/1.0/CommuniGateAV.dmg';
  var mediaPluginNetscapeURL = 'www.communigate.com/MediaPlugin/1.0/npCommuniGateAV.xpi';
  

  // GIPS plugin
  var gipsPluginMIMEType   = 'application/npCommuniGateAV-plugin-GIPS';
  var gipsPluginObjectActiveX1   = '<object id="CommuniGateAV" classid="clsid:675AAABC-F3E3-4332-A94F-EE174AF1F833" codebase=';
  var gipsPluginObjectNetscape1   = '<object id="CommuniGateAV" type="'+gipsPluginMIMEType+'" width="0" height="0" codebase=';
  
  var gipsPluginActiveXURL  = 'www.communigate.com/MediaPluginGIPS/1.0/CommuniGateAVPluginGIPS.CAB';
  var gipsPluginMacOSURL     = 'www.communigate.com/MediaPluginGIPS/1.0/CGateAVPluginGIPS.dmg';
  var gipsPluginNetscapeURL  = 'www.communigate.com/MediaPluginGIPS/1.0/MediaPluginGIPS.xpi';

  // common variables
  var isHttps                = false;
  var checkTimer             = null;
  var thePlugin              = null;
  var thePluginName          = "cgp";
  var theBrowser	         = null;
  var checkTimerCounter		 = 5; // check plugin for n times (~ n*200  milisec)

	
function setHttps(https) {
  isHttps = https;
}

function getHTTPSchema() {
  return(isHttps ? "https://" : "http://");
}

function getMIMEType(){
    return (thePluginName == "cgp") ? mediaPluginMIMEType : gipsPluginMIMEType;
}

// get plugin object for different browsers and plugins
function getPluginObject(browser) {
	return getObjectStart(browser)+ '"' + getHTTPSchema() + getPluginURL(browser) + '"' + getObjectEnd(browser);
}

function getObjectStart(browser) {
  if (browser == "IE") {
    return (thePluginName == "cgp") ? mediaPluginObjectActiveX1 : gipsPluginObjectActiveX1;
  } else {
   return (thePluginName == "cgp") ? mediaPluginObjectNetscape1 : gipsPluginObjectNetscape1;
  }
}

function getObjectEnd(browser) {
  if (browser == "IE") {
    return objectActiveX2;
  } else {
   return objectNetscape2;
  }
}


function getPluginURL(browser) {
  if (browser == "IE") {
    return (thePluginName == "cgp") ? mediaPluginActiveXURL : gipsPluginActiveXURL;
  } else if (browser == "MacOS_safari" || browser == "MacOS_firefox") {
   return (thePluginName == "cgp") ? mediaPluginMacOSURL : gipsPluginMacOSURL;
  } else if (browser == "chrome") {
   return (thePluginName == "cgp") ? mediaPluginNetscapeURL : gipsPluginNetscapeURL;
  } else if (browser == "Netscape") {
   return (thePluginName == "cgp") ? mediaPluginNetscapeURL : gipsPluginNetscapeURL;
  }		
}

function loadMediaPlugin(pluginName) {
  thePluginName = pluginName;
  if (thePluginName != "gips")
  	thePluginName = "cgp";
  	
  clearInterval(checkTimer);
  var isMacOS = navigator.userAgent != null && navigator.userAgent.toUpperCase().indexOf('MAC OS X') != -1;
  if (navigator.appName == "Microsoft Internet Explorer") {
  	theBrowser = 'IE'
  } else if(isMacOS && navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
  	theBrowser = 'MacOS_firefox';
  } else if(isMacOS && navigator.userAgent.toLowerCase().indexOf('safari') != -1) {
  	theBrowser = 'MacOS_safari';
  } else if(navigator.userAgent != null && navigator.userAgent.toLowerCase().indexOf('chrome') != -1) {
  	theBrowser = 'chrome';
  } else if (navigator.appName == "Netscape") {
  	theBrowser = 'Netscape';
  }
  
  
  var obj = getPluginObject(theBrowser);
  if(obj != null) {
	document.getElementById("MediaPlugin").innerHTML = obj;
	checkTimerCounter = 5;
    checkTimer = setInterval("checkLoaded();", 200);
    return "Loading";
  }

  return "Unsupported browser";
}

//
// This function is called periodically. It checks if the Plugin has been loaded
//
function checkLoaded() {
  checkTimerCounter--;
  if (checkTimerCounter < 0)
  {
      clearInterval(checkTimer);
      pronto.mediaPluginInitialized(false);
  }
  thePlugin =  document.getElementById("CommuniGateAV");
  switch(typeof thePlugin) {
  case 'undefined':
  break; case 'object': case 'function':
	if(typeof thePlugin.GetVersion != 'undefined' ) {
      clearInterval(checkTimer);
      pronto.mediaPluginInitialized(true);
	}
  }
}
// ------------------------------------------------
// methods

function traceResult( operationResult )	{
  return(operationResult === "" || operationResult == null ? "OK" : operationResult);
}	

// get link to load plugin
function GetLoadLink() {return getHTTPSchema() + getPluginURL(theBrowser);}

// legs methods	

function     createLeg(leg, inp, out) {return traceResult( thePlugin.CreateLeg (leg, inp, out));}
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) );}

//media mixing (for gips plugin only)
function attachToMixer(id)	   {return traceResult( thePlugin.AttachToMixer(id) );}
function attachToMixer(id)	   {return traceResult( thePlugin.AttachToMixer(id) );}

//media muting (for gips plugin only)
function setMute(leg, mute)	   {return traceResult( thePlugin.SetMute(leg, mute) );}
function isMute(leg)		   {return traceResult( thePlugin.IsMute(leg) );}
function setMuteRemote(leg, mute)  {return traceResult( thePlugin.SetMuteRemote(leg, mute) );}
function isMuteRemote(leg)	   {return traceResult( thePlugin.IsMuteRemote(leg) );}

//GIPS plugin license
function getLicenseChallenge() 				{return traceResult( thePlugin.GetLicenseChallenge() );}
function processLicenseResponse(responce)	{return traceResult( thePlugin.ProcessLicenseResponse(responce) );}

// connection  properties	
function setLocalIP(id)	 		{return traceResult( thePlugin.SetLocalIP(id) );}
function detachFromMixer(id)	{return traceResult( thePlugin.DetachFromMixer(id) );}
	
function getVersion()			{return thePlugin.GetVersion();}
	
// advanced methods		
function listDevices(value)		{return thePlugin.ListDevices(value);}	
function setLogLevel(logLevel)	{return traceResult( thePlugin.SetLogLevel(logLevel));}
