/**
 *  web.alive 2009 - hatko
 *  Initilization code
 */
 

 // Define webalive server specific values	
 var WAServerVars = {
 "ServerTitle":    'webalive server', "MicroInstallerName":    'microsetup-web.alive-client.exe',
 "versionString":  '2.0beta.8', "buildNumber":  'b568',
 "downloadLocation": 'http://downloads.projectchainsaw.com/fci/',
 "controlName": 'WebPlugin.WebPluginControl.2.0beta.8',
 "appDir": "Apex-2.0-b596",		
 "controlClassID": 'CLSID:688787DD-DD40-4847-A5E8-AF1EF6AE33A3', "mimeType": 'application/x-webalive-2.0beta.8'
 }
 
var xa="";  /* don't like globals like this ... Ali code */

/*
Script and css dynamic cross-domain loading object. Supports one callback once all scripts have been loaded.
*/
function WALoaderObj(parameter){
	// Paramters
	WALoaderObj.prototype.element = parameter; /* Loading context document */
	this.WAJavaScriptArray = new Array(); /* Array to track all parallel script loads */
	this.InvokerCallback = function(){alert("undefined callback");}; /* Overwritten callback to be done once all scripts are loaded */
}

/*
Add script to list of scripts to be injected
*/
WALoaderObj.prototype.WAAddScript = function(scriptSource)
{
	this.WAJavaScriptArray.push({'file':scriptSource, 'status':'0'});
}

/*
Call to inject scripts
*/
WALoaderObj.prototype.WAInjectScripts = function(callback)
{
	// Setup callback inside loader object
	this.InvokerCallback = callback;
	for (var i = 0, len=this.WAJavaScriptArray.length; i<len; ++i)
	{
		// Call to inject script
		this.WAInjectScript(this.element, this.WAJavaScriptArray[i].file, i, "text/javascript");
	}
}

/*
Script injection tracking callback
*/
WALoaderObj.prototype.WAGenericCallback = function(arrayIndex)
 {
	// check is any elements remain to be loaded	
	this.WAJavaScriptArray[arrayIndex].status = '1';
	var arlen = this.WAJavaScriptArray.length;
	
	for (var i = 0, len=this.WAJavaScriptArray.length; i<len; ++i)
	{
		//check status of each element
		if (this.WAJavaScriptArray[i].status == '0')
		{
			return;
		}
	}	
	this.InvokerCallback();
 }

/*
Logic to inject scripts into DOM
*/ 
WALoaderObj.prototype.WAInjectScript = function(element, url, scriptID ,myType)
{
	var headID = element.getElementsByTagName("head")[0]; 
	if (!headID) { return; }
		
	// IE detection
    var ver = this.WAGetInternetExplorerVersion();

  	// Script Injection
	var script = element.createElement('script');
    script.type = myType;
    script.src = url;
	
	var myObj = this; // make a copy for event callback context

    // Setup tracker handlers
    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
				myObj.WAGenericCallback(scriptID);	
            }
        };
    } else {  //Others
        script.onload = function () 
		{ //FF and Opera ONLY
			myObj.WAGenericCallback(scriptID);		
		} 
    }
	if ( ver > -1 )
  	{
   		//element.body.appendChild(script);
		headID.appendChild(script);
   		return;
  	}
  	// FF and Chrome
  	headID.appendChild(script);
}

/*
Logic to inject CSS into DOM
*/
WALoaderObj.prototype.WAInjectStylesheet = function(urlPath)
{
	var head = this.element.getElementsByTagName('head')[0];  
	var tag = this.element.createElement('link');  
    	tag.setAttribute('rel', 'stylesheet');
    	tag.setAttribute('type', 'text/css');
    	tag.setAttribute('href', urlPath);
	head.appendChild(tag);
}

/*
Utility function  to get browser version
*/
WALoaderObj.prototype.WAGetInternetExplorerVersion = function()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

/*
Called from customer page to start web.alive
*/
 function WAStart()
 {
	var WALoaderObjInstance;
	WALoaderObjInstance= new WALoaderObj(this.document);
	WALoaderObjInstance.WAInjectStylesheet("http://" +  WAUrlVars.WebServerFQDN + "/" + WAServerVars.appDir + "/_css/WACustomerStyles.css");
	WALoaderObjInstance.WAAddScript("http://" + 	WAUrlVars.WebServerFQDN + "/" + WAServerVars.appDir +'/javascript/lib/x.js');
	WALoaderObjInstance.WAAddScript("http://" + 	WAUrlVars.WebServerFQDN + "/" + WAServerVars.appDir +'/javascript/WACustomerSpecific.js');
	WALoaderObjInstance.WAInjectScripts(function(){
		xa = new xAnimation(); //don't like globals like this ... Ali code
		window.onresize = onWindowResize; // this setts resize for the system message code ... kind of ugly. ... Ali code
		WAStartApp();
	});
 }
 