// initialize global variables
var detectableWithVB = false;
var pluginFound = false;
var p2pReturnPort=0;
var p2pServerID="";
var p2pUUID="";
var isIE = "true";
var isNeedToStopP2P = "false";
window.onbeforeunload = stopP2P;
function setIFrameContent( contentSource )
{
    //alert("set source");
	document.getElementById("content").src = contentSource;
}

function hideIFrame()
{
    document.getElementById("content").style.visibility="hidden";
}

function showIFrame()
{
	//alert("showFrame");
    document.getElementById("content").style.visibility="visible";
}

function checkObj(){
}
function setIsIE(val){
 isIE=val;
}
function setIsNeedToStopP2P(val){
 isNeedToStopP2P=val;
}
function pluginDetection() {
    pluginFound = detectPlugin('P2POCX Control');
    //alert("pluginFound---------->"+pluginFound);
    //pluginFound = detectPlugin('Windows Media');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	//pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
	pluginFound = detectActiveXControl('P2POCX.P2POCXCtrl');
    }
    //alert("pluginFound---------->"+pluginFound);
    return pluginFound;
}

function canDetectPlugins() {
    if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
	return true;
    } else {
	return false;
    }
}
function startFABridge(){

}
function detectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
	var pluginsArrayLength = navigator.plugins.length;
	// for each plugin...
	for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
	    // loop through all desired names and check each against the current plugin name
	    var numFound = 0;
	    for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
		// if desired plugin name is found in either plugin name or description
		if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
		    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
		    // this name was found
		    numFound++;
		}   
	    }
	    // now that we have checked all the required names against this one plugin,
	    // if the number we found matches the total number provided then we were successful
	    if(numFound == daPlugins.length) {
		pluginFound = true;
		// if we've found the plugin, we can stop looking through at the rest of the plugins
		break;
	    }
	}
    }
    return pluginFound;
} // detectPlugin



// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}
function getP2PServerID(){
  return p2pServerID;
}
function getP2PPort(){
  return p2pReturnPort;
}


function embedP2PPlugIn(uuid,portNum){
	//document.getElementById("P2PDIV").innerHTML ='<object classid="clsid:B95E65A6-4950-4771-9EC6-AADEC414557C" id="P2PActiveXControl" width="100" height="100"/>';
	//alert("******** P2P Object embeded ***********");
	//StartP2P(uuid,portNum);

}

function StartP2P(uuid,portNum,IP1,IP2){
        //alert("********* start P2P **********"+IP1);
	    var n=portNum;
	    var P2PInvoker=null;
	    var returnedPort=0;
	    //window.document.fs
	    p2pUUID=uuid;
	    P2PInvoker=window.document.content.P2PActiveXControl;
	    if (P2PInvoker!=null)
	    {	
	    	P2PInvoker.StartP2PEx(uuid,portNum,IP1,IP2);
	    	var P2PServrID=P2PInvoker.bstrP2PServerID;
	    	p2pServerID=P2PServrID;
		    returnedPort=P2PInvoker.Port;
		    p2pReturnPort=returnedPort;
	    	//alert("p2pServerID------------>"+p2pServerID);
	    	//alert("p2pReturnPort---------->"+p2pReturnPort);
	    	return 	returnedPort;
	    }
	    else
	    {
	    	//alert("NULL obj");
	    	return 	returnedPort;
	    }	
}

function stopP2P(){
     /*
	 alert ("********** stop P2P **********");
	 alert ("isIE--->"+isIE);
	 alert ("p2pUUID---->"+p2pUUID);
	 alert ("isNeedToStopP2P----->"+isNeedToStopP2P);
	 */
	 var P2PInvoker=null;
	 if (isNeedToStopP2P=="true"){
	    if (isIE=="true"){
	      P2PInvoker=window.document.content.P2PActiveXControl;
	    }else if (isIE=="false"){
	      P2PInvoker=document.getElementById("p2pObject");
	    }
	    if (P2PInvoker!=null){	
		    P2PInvoker.StopP2P(p2pUUID);
	    }
	 }
}

function tryFireFoxP2P(uuid,portNum,IP1,IP2){
  p2pUUID=uuid;
  var p2pZone=document.getElementById("p2pFireFox");
  p2pZone.innerHTML='<embed type="application/x-cyberlink-p2p-plugin" width=0 height=0 hidden="true" id="p2pObject" name="p2pObject"/>'
  var p2pObj=document.getElementById("p2pObject");
  //alert("p2pObj=======>"+p2pObj);
  var returnPort=p2pObj.StartP2PEx(uuid,portNum,IP1,IP2);
  //var P2PServrID=p2pObj.strP2PServerID;
  p2pReturnPort=returnPort;
  P2PServerID=p2pObj.strP2PServerID;
  p2pServerID=P2PServerID;
  //alert(p2pReturnPort);
  //alert(p2pServerID);
  return p2pReturnPort;
}

function fireFoxPlugInDetection(){
  var hasP2PPlugin=false;
  var pluginLength=navigator.plugins.length;
  for (var i=0; i<pluginLength; i++){
    if (navigator.plugins[i].name=="CyberLink P2P Plugin"){
       hasP2PPlugin=true;
    }
  }
  return hasP2PPlugin;
}