// File: loadtime.js
// Version: 1.05
// Desc: javascript file containing functions to
//       support webpage parsing and initial world
//       load.  This file is unloaded after the
//       page loads. Why keep the methods around
//       if we will never  user them again.
//
// Copyright (c) 2002-2008 KimballSoftware 

window.onload = function() {

    var splash = document.getElementById('divSplash');
    splash.style.paddingTop= ((document.body.clientHeight/2)-48)+'px';
    splash.style.visibility='';
    if ( document.all && navigator.appVersion.indexOf("Win") != -1 ) {
	// MSIE found on the windows platform
    }
    else {
	//location.replace("http://vrmlworld.net/abnet2/need_ie.html");
    }

    ABNetApp.User.me = new Person();
    ABNetApp.User.me.isMe = true;

    // parse the url of this and extract the query args
    parseQueryArgs();

    // set the title of this page
    document.title = ABNetApp.title;

    ABNetApp.Chat.init();

    // try to setup a 3d player .. bs contact in this case
    try {
	ABNetApp.Scene.browser = document.getElementById('CC3D');
	if (typeof ABNetApp.Scene.browser != 'object')
	    throw "NoBrowser";
	   var contactVersion=parseFloat(ABNetApp.Scene.browser.getVersion().split(' ')[0]);
    }
    catch(e) {
	showDownloadInstructions();
	return;
    }

    if ( contactVersion < 6.1 ) {
	showDownloadInstructions(contactVersion);
	return;
    }

    // establish an abnet chat connection
    try {
	if ( parseFloat(ABNet.version) < 2.049 ) {
	    throw "VersionError";
	}

	ABNetApp.Comm.bABNetState=ABNetApp.Comm.STATE_UNINITIALIZED;
	ABNet.OnEvent=OnABNetEvent;
	ABNet.SetHost(ABNetApp.server.chathost);
	ABNet.SetPort(ABNetApp.server.port);
	ABNet.Connect();
    
    }
    catch(e) {
	alert("You need the ABNet 2.05 ActiveX Client installed to use this page ("+ABNet.version+")");
	location.replace(abnetDownload);
	return;
    }

    // load the 3d scene
    try {
	ABNetApp.Scene.b3DState=ABNetApp.Scene.X3D_LOADING;
	ABNetApp.Chat.append('[loading 3d scene...]');
	ABNetApp.Scene.browser.loadURL(ABNetApp.the3DURL,'');
    }
    catch(e) {
	showDownloadInstructions();
	return;
    }

    // worldRefresh(3000);
    window.status=DEF_STATUS;

    // show the splash screen for a couple of seconds
    window.onresize=resize;
    setTimeout(function() {
        resize();
        var div3d = document.getElementById('div3D');
        var chatBox = document.getElementById('_3dchat');
        splash.style.display='none';
        splash.style.visibility='hidden';
        div3d.style.visibility='';
        chatBox.style.visibility='';
    }, 2000);

    // schedule an unload of this file now that we are done with initialization
    setTimeout(function() {
	    var headDoc = document.getElementsByTagName('head')[0];
	    var initFuncs=document.getElementById('loadtimejs');
	    if ( initFuncs ) {
		headDoc.removeChild(initFuncs);
		initFuncs = null;
	    }
	}, 10000);

}

//========================================================================
// parseQueryArgs() - look the page url and pluck off the abnet arguments
//========================================================================

function parseQueryArgs() {
  var queryArgs = location.search.substring(1);
  var pairs = queryArgs.split('&');

  for ( var indx=0; indx < pairs.length; indx++ ) {
    var kvalue = pairs[indx];
    var splitIndx;
    var kv=new Array();

    if ( bDebug & DEBUG_UI ) { /* alert('HTML:queryArg=[' + kvalue + ']'); */ }
    splitIndx = kvalue.indexOf('=');
    if ( splitIndx >= 0 ) {
      kv.push(kvalue.substr(0,splitIndx));
      kv.push(kvalue.substr(splitIndx+1));
    }
    else {
      kv.push(kvalue);
      kv.push('');
    }

    if ( kv.length == 2 && kv[1].length > 0 ) {
      switch(kv[0]) {

	// use a custom avatar
      case 'avatar':
      case 'avatarurl':
	ABNetApp.User.me.avatarURL=unescape(kv[1]);
	break;

	// capture the builder name of this 3d scene
      case 'builder':
	theBuilder=unescape(kv[1]);
	break;

	// go into debug mode and spew stuff to the console
      case 'debug':
	bDebug=parseInt(kv[1],16);
	break;

	// provide a 2d snapshot of this 3d scene
      case 'default2DImage':
      case 'snapshot':
	ABNetApp.snapshot=unescape(kv[1]);
	break;

	// select the default avatar based on sex
      case 'sex':
	if ( kv[1] == 'f' || kv[1] == 'F' ) {
	  ABNetApp.User.me.avatarURL=ABNetApp.defaults.avatarF;
	}
	else {
	  ABNetApp.User.me.avatarURL=ABNetApp.defaults.avatarM;
	}
	break;

	// the title of this 3d scene
      case 'title':
	var title = unescape(kv[1]);
	ABNetApp.setTitle(title.replace(/\+/g," "));
	break;

	// use a custom name instead of Visitor_12345
      case 'user':
      case 'userid':
      case 'username':
      case 'name':
	ABNetApp.User.me.username=unescape(kv[1]).replace(/\+/g," ");
	break;
                
	// load a custom 3d scene instead of the default
      case 'x3durl':
      case 'url':
      case 'vrmlurl':
	ABNetApp.the3DURL=unescape(kv[1]);
	break;

      default:
	// the caller gave us a query argument we don't know about
      }
    }
  }
}

//========================================================================
//=== rewrite the page when the user doesn't have a 3d player or abnet

function showDownloadInstructions(contactVersion) {
    var href=ABNetApp.pluginurls.bsContactDownload;
    var msg='';

    msg ="<DIV style='width: 800; font-weight:900; font-size: .8em; padding-top: 10px; padding-left:20px'>";
    if ( contactVersion ) {
	msg += "Sorry, you are running an older version of BS Contact (" + contactVersion + ").<br>";
	msg += "ABNet2 needs BS Contact version 6.1 or greater.<BR><BR>";
	msg += "<A href=\"" + href + "\"> Download BS Contact VRML/X3D 7.0 version here</A><BR><BR>";
	msg += "<A href=\"" + "http://vrmlworld.net/forums/viewtopic.php?t=120" + 
	    "\"> Read this article to see how to switch between Blaxxun and Bitmanagement</A>" +
	    "</DIV>";
    }
    else {
	msg += "Sorry, Before you can use ABNet, you need to install 2 things: BS Contact version 7.0 or greater,<BR>";
	msg += "and the ABNet ActiveX client.  After you install these components, close all<br>";
	msg += "Internet Explorer windows and try again.  The picture above is a snapshot of<br>";
	msg += "a typical 3d world.<BR>";
	msg += "<A target=_blank href=\"" + ABNetApp.pluginurls.abnetDownload + "\"> Download ABNet2 ActiveX client</A><BR>";
	msg += "<A target=_blank href=\"" + href + "\"> Download BS Contact VRML/X3D 7.0 version here</A><BR>";
	msg += "</DIV>";
    }

    var defaultImage = '<IMG src="image/lobby.gif" border="0" width="600" height="400">';
    CC3D.outerHTML=msg+defaultImage;
    return;
}
