function VirtualAgentLoader() {
	this.alreadyLoaded = false;
	this.baseDir = "";
}

VirtualAgentLoader.prototype = {
	load: function( force ) {
		if(force === true) {
			document.cookie = "popinOpen_" + VZ_FlashVars.origin +"=L;path=/";
		}
		if(!this.alreadyLoaded && this.getCookie("popinOpen_" + VZ_FlashVars.origin) != "" ) {
			// This will trigger an asynchronous load of the scripts so it won't block the page load.
			var thisObject = this;
			window.setTimeout(function() {
				thisObject.loadExternalFiles();
			}, 1);
		}
	},
	loadExternalFiles: function () {
		this.baseDir = this.getscriptpath("VirtuOzLoader.js");
		
		var e = document.createElement("link");
		e.rel = "stylesheet";
		e.type = "text/css";
		e.href = this.baseDir + "VirtuOz.css";
		e.media = "all";
		document.getElementsByTagName("head")[0].appendChild(e);

		// Test for IE6 and load the adequate CSS
		if(navigator.appVersion.indexOf("MSIE") != -1 && parseFloat(navigator.appVersion.split("MSIE")[1]) < 7) {
			e = document.createElement("link");
			e.rel = "stylesheet";
			e.type = "text/css";
			e.href = this.baseDir + "VirtuOz-ie6.css";
			e.media = "all";
			document.getElementsByTagName("head")[0].appendChild(e);
		}
		
		e = document.createElement("script");
		e.src = this.baseDir + "VirtuozAgent.js";
		e.type="text/javascript";
		document.getElementsByTagName("head")[0].appendChild(e);		
	},
	getscriptpath: function(scriptname){
		// Check document for our script
		var scriptobjects = document.getElementsByTagName('script');
		for (i=0; i<scriptobjects.length; i++) {
			if(scriptobjects[i].src && scriptobjects[i].src.toLowerCase().lastIndexOf(scriptname.toLowerCase()) != -1){
				// we found our script.. lets get the path
				return scriptobjects[i].src.toString().substring(0, scriptobjects[i].src.lastIndexOf(scriptname));
			};
		}
		return "";
	},
	getCookie: function( cookieName ) {		
		/*only cookies for this domain and path will be retrieved */
		var cookieJar = document.cookie.split( "; " );
		for( var x = 0; x < cookieJar.length; x++ ) {
			var oneCookie = cookieJar[x].split( "=" );
			if( oneCookie[0] == escape( cookieName ) ) { return oneCookie[1] ? unescape( oneCookie[1] ) : ''; }
		}
		return '';
	}
}

var VirtuOzAgentLoader = new VirtualAgentLoader();
VirtuOzAgentLoader.load();
