// Browser detection, cf. http://www.quirksmode.org/js/detect.html
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},	
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},	
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();



// Java plugin detection
var isJavaActivated_IE = true;


function init(proot) {
  // detect whether Java plugin is activated. navigator.javaEnabled() is not supportet by MSIE.
  // This workaround works for MSIE (tested versions: 5, 6, 7, 8)
  // If the Java plugin is deactivated, the method call "applet.getNewHeight(document.getClientHeight());"
  // throws an exception. 
  // Important: If the applet is not completely loaded this method call does not throw an exception (in MSIE)!
  try {
    var applet = document.applets['vcoApplet'];
    applet.height = applet.getNewHeight(document.getClientHeight());
  } catch (e) {
    isJavaActivated_IE = false;
  }

	
  // CCR_44: max_duration increased (5.000 to 10.000)
  var max_duration = 5000;

  if ( window.LOADING_ANIMATION_DURATION ){
    max_duration = window.LOADING_ANIMATION_DURATION;
  }	
  window.setTimeout('appletInitializedFunction()', max_duration); //switch for show applet information    	

  window.onresize = resizeWindow;

  if(proot && proot!=''){
    root=proot;
  } else {
    root='';
  }
}

function getCalculatedAppletHeight(){
	var windowWidth = CONTENT_WIDTH;
	var windowHeight = CONTENT_HEIGHT; 
		    	    
	windowWidth = document.getClientWidth();;
	windowHeight = document.getClientHeight();;		 

	
	// CONTENT_HEIGHT, CONTENT_WIDTH are defined in jsp page or html page with applet tag
	// This height should equals the height of the loading and error layers (defined in CSS)
	var minAppletHeight = CONTENT_HEIGHT; 
	var minAppletWidth = CONTENT_WIDTH;
	
	
	var newHeight = (windowHeight > minAppletHeight) ? windowHeight : minAppletHeight;
	return newHeight; 
}

function getWindowWidth() {
	var windowWidth;
	var minAppletWidth = CONTENT_WIDTH;
	// Achtung: CR_0002: Try/Catch fuehrt dazu, dass der IE keinen Java-Script Fehler meldet. JEDOCH: Als Seiteneffekt wird im VCO
	//                   MINI die Navigationsleiste beim Start nicht mehr angezeigt.
	//		     Loesung: getWindowWidth() wird nur noch von Firefox verwendet (dies ist zwingend erforderlich, andernfalls
        //		     passt sich das Applet beim Start nicht an die Ausgangsbreite des Browserfensters an).	           		    	    
	windowWidth = document.getClientWidth();
	
	var newWidth = (windowWidth > minAppletWidth) ? windowWidth : minAppletWidth;
	return newWidth; 			 
}


function resizeWindow() {
    var applet = document.applets['vcoApplet'];
    
    if (applet) {            
        // CR_0002, Cirquent: It must be guaranteed that the applet is completely loaded; cf.: Java-Script error "applet.getNewHeight is not a function" within Firefox.
    	//                    The error mentioned above was not reproduceable by Cirquent. The error occured (reproduceable) at Helmut Busch's computer.
    	//                    It is assumed that a timing problem may occur under certain circumstances, i.e., the applet is not completely loaded and it is tried to
    	//                    invoke an applets getNewHeight() method (UICApplet.getNewHeight())
    	try {
	    	applet.height = applet.getNewHeight(document.getClientHeight());	    			    			    			     
		applet.width = applet.getNewWidth(document.getClientWidth());		

		// vermeiden, dass beide scrollbars angezeigt werden ...
		var minAppletWidth = CONTENT_WIDTH;
		var intAppletWidth = parseInt(applet.width);
		if (intAppletWidth <= minAppletWidth)
		  applet.height=parseInt(applet.height)-applet.getScrollbarSize();        
		
		var minAppletHeight = CONTENT_HEIGHT;
		var intAppletHeight = parseInt(applet.height);
		if (intAppletHeight <= minAppletHeight)
		  applet.width=parseInt(applet.width)-applet.getScrollbarSize();    
                   
		applet.resize(applet.width,applet.height); // check for applet.resize fails when applet is enabled in IE 6.0                

	        hideLayer("info");
	        showLayer("applet");
	        hideLayer("appletError");
	} catch (e) {
	  window.setTimeout(resizeWindow,800);
	}    	    	
    }
}


// invoked in init()
// --------------------------------------
function appletInitializedFunction()
{  
  if (BrowserDetect.browser == "Explorer") {
    // check whether Java is enabled in MSIE
    if (!isJavaActivated_IE) {
	hideLayer("info");
	showLayer("appletError");
	SHOW_APPLET = true;	
    } 
  } else {
    // navigator.javaEnabled() is supported in Firefox etc. 
    if (!navigator.javaEnabled())
    {
	    hideLayer("info");
	    showLayer("applet");
	    SHOW_APPLET = true;
    } 
  }
  resizeWindow();
}


function checkJSCommunicationFunction(method, param)
{
  // MSIE already invokes the init & start method of the applet (even, if java is disabled). If
  // java is disabled, a java script error occurs.
  try {
    var applet = document.applets['vcoApplet'];
			
    if (applet)
    {
      applet[method](param);
      hideLayer("appletError"); // Mac OS damit bei mousedragg resizen der appletError Layer von hintergrund nicht angezeigt wird
    }
  } catch (e) {  
  }
}




