// #region CLMS Javascript Utilities
function classCLMS_Utilities(){
	//Properties
	this["nLatestFlashVersion"] = 10;
	this["nLatestAcrobatVersion"] = 9;
	this["sUserAgent"] = navigator.userAgent.toLowerCase();
	this["nScreenWidth"] = window.screen.width;
	this["nScreenHeight"] = window.screen.height;
	this["sLibraryPath"] = '/Scripts/glibrary.asp';
	this["sLibraryPathCTR"] = '/Scripts/gctrlogging.asp';
	
	//Assign methods
	this.addEventHandlerOnload = addEventHandlerOnload;
	this.addEventHandlerOnresize = addEventHandlerOnresize;
	this.addEventHandlerOnscroll = addEventHandlerOnscroll;
	this.addEventHandlerOnunload = addEventHandlerOnunload;
	this.addEventHandlerOnbeforeunload = addEventHandlerOnbeforeunload;
	this.getAcrobatVersion = getAcrobatVersion;
	this.getBrowser = getBrowser;
	this.getBrowserVersion = getBrowserVersion;
	this.getOperatingSystem = getOperatingSystem;
	this.getFlashVersion = getFlashVersion;
	this.getInnerWidth = getInnerWidth;
	this.getInnerHeight = getInnerHeight;
	this.getPageHeight = getPageHeight;
	this.getMediaPlayerVersion = getMediaPlayerVersion;
	this.getScreenWidth = getScreenWidth;
	this.getScreenHeight = getScreenHeight;
	this.getScrollTop = getScrollTop;
	this.setInnerHTMLext = setInnerHTMLext;
	this.setBasePath = setBasePath;
	this.showPopupWindow = showPopupWindow;
		
	//Add onload event handler for specified function
	function addEventHandlerOnload(oCall){
		//Placeholder for inpage function
		var tempFunction;

		//Define onload log event flow, makes sure added function executes the last
		if (typeof window.onload == 'function'){
			tempFunction = window.onload;
			window.onload = function(){
				tempFunction();
				eval(oCall);
			}
		}
		else{
			window.onload = function(){
				eval(oCall);
			}
		}
	}
	
	//Add onresize event handler for specified function
	function addEventHandlerOnresize(oCall){
		//Placeholder for inpage function
		var tempFunction;

		//Define onload log event flow, makes sure added function executes the last
		if (typeof window.onresize == 'function'){
			tempFunction = window.onresize;
			window.onresize = function(){
				tempFunction();
				eval(oCall);
			}
		}
		else{
			window.onresize = function(){
				eval(oCall);
			}
		}
	}
	
	//Add onresize event handler for specified function
	function addEventHandlerOnscroll(oCall){
		//Placeholder for inpage function
		var tempFunction;

		//Define onload log event flow, makes sure added function executes the last
		if (typeof window.onscroll == 'function'){
			tempFunction = window.onscroll;
			window.onscroll = function(){
				tempFunction();
				eval(oCall);
			}
		}
		else{
			window.onscroll = function(){
				eval(oCall);
			}
		}
	}
	
	function addEventHandlerOnunload(oCall){
		//Placeholder for inpage function
		var tempFunction;

		//Define onload log event flow, makes sure added function executes the last
		if (typeof window.onunload == 'function'){
			tempFunction = window.onunload;
			window.onunload = function(){
				tempFunction();
				eval(oCall);
			}
		}
		else{
			window.onunload = function(){
				eval(oCall);
			}
		}
	}
	
	function addEventHandlerOnbeforeunload(oCall){
		//Placeholder for inpage function
		var tempFunction;

		//Define onload log event flow, makes sure added function executes the last
		if (typeof window.onbeforeunload == 'function'){
			tempFunction = window.onbeforeunload ;
			window.onbeforeunload  = function(){
				tempFunction();
				eval(oCall);
			}
		}
		else{
			window.onbeforeunload  = function(){
				eval(oCall);
			}
		}
	}
	
	

	
	//Adobe Acrobat Detection
	function getAcrobatVersion() {
		var nActobatVersion = 0;
		
		//NS3+, Opera3+, IE5+ Mac, Safari (support plugin array):  check for Acrobat plugin in plugin array
		if (navigator.plugins != null && navigator.plugins.length > 0) {
			for (i=0; i < navigator.plugins.length; i++ ) {
				var plugin = navigator.plugins[i];
				if (plugin.name.indexOf("Adobe Acrobat") > -1) {
					nActobatVersion = parseFloat(plugin.description.substring(30));
					break;
				}
			}
		}
		//IE4+ Win32:  attempt to create an ActiveX object
		else if (window.ActiveXObject){
			for (x=2; x <= this.nLatestAcrobatVersion; x++){
				try	{
					obAcro=eval("new ActiveXObject('PDF.PdfCtrl."+x+"');");
					if (obAcro){ nActobatVersion=x; }
				}
				catch(e) {}
			}			
			try	{
				obAcro4=new ActiveXObject('PDF.PdfCtrl.1');
				if (obAcro4){nActobatVersion='4'; }
			}
			catch(e) {}
		}
		//Can't detect in all other cases
		else {
			nActobatVersion = -1;
		}
		//Not installed
		if (nActobatVersion == 0) {nActobatVersion=-1}
		
		return nActobatVersion;
	}
	
	//Determine the browser details based on the user agent string
	function getBrowser()
	{
		var sBrowser = '';
		
		if (this.sUserAgent.indexOf('konqueror') + 1) sBrowser = 'konqueror';
		else if (this.sUserAgent.indexOf('safari') + 1) sBrowser = 'safari';
		else if (this.sUserAgent.indexOf('firefox') + 1) sBrowser = 'firefox'
		else if (this.sUserAgent.indexOf('chrome') + 1) sBrowser = 'chrome';
		else if (this.sUserAgent.indexOf('omniweb') + 1) sBrowser = 'omniweb';
		else if (this.sUserAgent.indexOf('opera') + 1) sBrowser = 'opera';
		else if (this.sUserAgent.indexOf('webtv') + 1) sBrowser = 'webtv';
		else if (this.sUserAgent.indexOf('icab') + 1) sBrowser = 'icab';
		else if (this.sUserAgent.indexOf('msie') + 1) sBrowser = 'msie';
		else if (this.sUserAgent.indexOf('browserng') + 1) sBrowser = 'browserng';
		else if (this.sUserAgent.indexOf('netfront') + 1) sBrowser = 'netfront';
		else if (this.sUserAgent.indexOf('jasmine') + 1) sBrowser = 'jasmine';
		else if (this.sUserAgent.indexOf('dolfin') + 1) sBrowser = 'dolfin';
		else if (this.sUserAgent.indexOf('netscape') + 1) sBrowser = 'netscape';
		else sBrowser = 'unknown';
		
		return sBrowser;
	}
	
	function getBrowserVersion(){
		var sBrowser = this.getBrowser();
		var sBrowserVersion = '';
		var nIndex = 0;
		var nLenght = 0;
		
		if (sBrowser != 'netscape'){
			nIndex = this.sUserAgent.indexOf(sBrowser) + 1;
			nLenght = sBrowser.length;
			sBrowserVersion = this.sUserAgent.substring(nIndex + nLenght, nIndex + nLenght + 3);
		}
		else{
			sBrowserVersion = this.sUserAgent.charAt(8);
		}
		
		return sBrowserVersion;
	}
	
	function getOperatingSystem(){
		var sBrowser = this.getBrowser();
		var sOS = '';
		if (this.sUserAgent.indexOf('windows phone os') + 1) sOS = "WinPhoneOS";
		else if (this.sUserAgent.indexOf('android') + 1) sOS = "Android";
        else if (this.sUserAgent.indexOf('iphone') + 1) sOS = "iOS";
		else if (this.sUserAgent.indexOf('ipad') + 1) sOS = "iOS";
		else if (this.sUserAgent.indexOf('ipod') + 1) sOS = "iOS";
		else if (this.sUserAgent.indexOf('blackberry') + 1) sOS = "BlackBerry";
		else if (this.sUserAgent.indexOf('symbian') + 1) sOS = "Symbian";
		else if (this.sUserAgent.indexOf('bada') + 1) sOS = "Bada";
		else if (this.sUserAgent.indexOf('win') + 1) sOS = "Windows";
		else if (this.sUserAgent.indexOf('mac') + 1) sOS = "MacOS";
        else if (this.sUserAgent.indexOf('linux') + 1) sOS = "Linux";
		else if (this.sUserAgent.indexOf('x11') + 1) sOS = "Unix";
		else sOS = "unknown";
		return sOS;
	}
						
	//Check if a string exists in the agent string and set the searched value
	function checkIt(string)
	{
		place = c_oCLAgent.indexOf(string) + 1;
		sCLCheckstring = string;
		return place;
	}

	//Flash Version Detection
	function getFlashVersion() {
		var nFlashVersion = 0;
		
		// NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Flash plugin in plugin array
		if (navigator.plugins != null && navigator.plugins.length > 0) {
			var flashPlugin = navigator.plugins['Shockwave Flash'];
			if (typeof flashPlugin == 'object') { 
				for (var i = this.nLatestFlashVersion; i >= 3; i--) {
					if (flashPlugin.description.indexOf(i + '.') != -1) {
						nFlashVersion = i;
						break;
					}
				}
			}
		}
		//IE4+ Win32:  attempt to create an ActiveX object
		else if (window.ActiveXObject) {
			for (x = 2; x <= this.nLatestFlashVersion; x++) {
				try {
					obFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
					if(obFlash) {nFlashVersion = x;}
				}
				catch(e) {}
			}
		}
		//Can't detect in all other cases
		else {
			nFlashVersion = -1;
		}
		//Not installed
		if (nFlashVersion == 0) {nFlashVersion = -1}
			
		return nFlashVersion;
	}
	
	//Get width of the window
	function getInnerWidth() {
		var nWidth = 0;
		
		if (window.innerWidth) {
			nWidth = window.innerWidth;
		}
		else if (window.document.documentElement) {
			if (window.document.documentElement.clientWidth){
				nWidth = window.document.documentElement.clientWidth;
			}
			else if(window.document.documentElement.offsetWidth){
				nWidth = window.document.documentElement.offsetWidth;
			}
		}
		else if (document.body) {
			nWidth = window.document.body.clientWidth;
		}
		return nWidth;
	}
								
	//Get height of the window
	function getInnerHeight() {
		var nHeight = 0;
		
		if (window.innerHeight) {
			nHeight = window.innerHeight;
		}
		else if (window.document.documentElement) {
			if (window.document.documentElement.clientHeight){
				nHeight = window.document.documentElement.clientHeight;
			}
			else if(window.document.documentElement.offsetHeight){
				nHeight = window.document.documentElement.offsetHeight;
			}
		}
		else if (window.document.body) {
			nHeight = window.document.body.clientHeight;
		}
		return nHeight;
	}
	
	function getPageHeight(){
		var y;
		var test1;
		var test2;
		
		/* Test for content height */	
		test1 = document.body.scrollHeight;
		test2 = document.body.offsetHeight;
		if (test1 > test2) // all but Explorer Mac
		{
			y = document.body.scrollHeight;
		}
		else // Explorer Mac;
		     //would also work in Explorer 6 Strict, Mozilla and Safari
		{
			y = document.body.offsetHeight;
		}
		/* Test for inner height */
		test1 = y;
		test2 = this.getInnerHeight();
		if (test1 < test2) // all but Explorer Mac
		{
			y = test2
		}
		return y;
	}
	
	//Windows Media Player Detection
	function getMediaPlayerVersion(){
		sMediaplayerVersion = 0;
		
		if (window.ActiveXObject){
			try{
				obWMP=new ActiveXObject('WMPlayer.OCX.7');
				if (obWMP){			
					sMediaplayerVersion=parseFloat(obWMP.versionInfo);
				}
			}
			catch(e) {}
			if (sMediaplayerVersion == 0){
				sMediaplayerVersion = -1;
			}
		}
		else if (navigator.plugins != null && navigator.plugins.length > 0) {
			if (navigator.mimeTypes["video/x-ms-wm"] &&  navigator.mimeTypes["video/x-ms-wm"].enabledPlugin && 	navigator.mimeTypes["video/x-ms-wmv"] && navigator.mimeTypes["video/x-ms-wmv"].enabledPlugin) {
				sMediaplayerVersion = '6.4';
			}
			else if (navigator.mimeTypes && navigator.mimeTypes["application/x-mplayer2"] &&  navigator.mimeTypes["application/x-mplayer2"].enabledPlugin){
				sMediaplayerVersion = '5.2';
			}
		}
		//Can't detect in all other cases
		else{
			sMediaplayerVersion = -1;
		}
		//Not installed
		if (sMediaplayerVersion == 0){sMediaplayerVersion = -1}
		
		return sMediaplayerVersion;
	}
	
	//Get screen width
	function getScreenWidth(){
		return this.nScreenWidth;
	}
	
	//Get screen height
	function getScreenHeight(){
		return this.nScreenHeight;
	}
	
	//Determin scroll distance
	function getScrollTop() {
		var scrollTop = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrollTop = window.pageYOffset;
		} 
		else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrollTop = document.body.scrollTop;
		} 
		else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrollTop = document.documentElement.scrollTop;
		}
		return scrollTop;
	}

	//Write innerHTML from external script (workaround for IE object functionality)
	function setInnerHTMLext(element,html) {
		element.innerHTML = html;
	}
	
	//Set basepath for logging script
	function setBasePath(sPath){
		this.sLibraryPath = sPath + "glibrary.asp"
		this.sLibraryPathCTR = sPath + "gctrlogging.asp"
	}
	
	//Show popup window with specified url relative to screen width and height
	function showPopupWindow(sUrl){
		var oWindow;
		oWindow = window.open(sUrl,'popupWindow','scrollbars=yes,status=no,toolbar=no,width=' + (this.getScreenWidth() - 200) + ',height=' + (this.getScreenHeight() - 200) + ',top=80,left=100');
		oWindow.focus();
		return false;
	}

}// #endregion

// #region CLMS Logging object
function classCLMS_Logging(){
	//Properties
	this["bCalculateSpeed"] = true;
	this["bResizeWait"] = false;
	this["nFileSize"] = 20876;
	this["nStartTime"] = 0;
	this["nResizeIntervalId"] = 0;
	this["nWindowHeight"] = 0;
	this["nWindowResizeHeight"] = 0;
	this["nWindowResizeWidth"] = 0;
	this["nWindowWidth"] = 0;
	this["oConnectionImg"] = null;
	this["nLogModeManual"] = 1;
	this["nLogModeAutomatic"] = 2;
	
	//Initialise utilities
	this["oUtilities"] = classCLMS_Utilities; 			
	this.oUtilities();
	
	//Initialise variables
	this.nWindowHeight = this.getInnerHeight();
	this.nWindowWidth = this.getInnerWidth();
	this.nWindowResizeHeight = this.getInnerHeight();
	this.nWindowResizeWidth = this.getInnerWidth();
		
	//Assign methods
	this.logClient = logClient;
	this.logClientResize = logClientResize;
	this.logClientSpeed = logClientSpeed;
	this.logMultimedia = logMultimedia;
	this.logPageview = logPageview;
	this.logEvent = logEvent;
	this.onResizeHandler = onResizeHandler;
		
	//Log client values
	function logClient(){	
		var sSrc = this.sLibraryPath + '?L=2&';
			sSrc += 'sw=' + this.getScreenWidth() + '&';
			sSrc += 'sh=' + this.getScreenHeight() + '&';
			sSrc += 'ww=' + this.getInnerWidth() + '&';
			sSrc += 'wh=' + this.getInnerHeight() + '&';
			sSrc += 'os=' + this.getOperatingSystem() + '&'; 
			sSrc += 'bn=' + this.getBrowser() + '&';
			sSrc += 'bv=' + this.getBrowserVersion() + '&';
			sSrc += 'fl=' + this.getFlashVersion() + '&';
			sSrc += 'ar=' + this.getAcrobatVersion() + '&';
			sSrc += 'mp=' + this.getMediaPlayerVersion() + '&';
			sSrc += 'nc=' + (new Date()).getTime();
		
			var oImg = new Image();
			oImg.src = sSrc;
			//alert(sSrc);
			
			//Show message on screen (element with id "status" nessecary)		
			//document.getElementById('status').innerHTML = document.getElementById('status').innerHTML + Date() + '&nbsp;&nbsp;&nbsp;' + src + "</br>";
	}
	
	//Log window values after resize
	function logClientResize(){
		this.nWindowWidth = this.getInnerWidth();
		this.nWindowHeight = this.getInnerHeight();
		
		// If stopped resizing then
		if (this.nWindowWidth == this.nResizeWindowWidth && this.nWindowHeight == this.nResizeWindowHeight){
			//Clear interval
			clearInterval(this.nResizeIntervalId);
			
			//Build image source and log
			var sSrc = this.sLibraryPath + '?L=3&';
			sSrc += 'ww=' + this.nWindowWidth + '&';
			sSrc += 'wh=' + this.nWindowHeight + '&';
			sSrc += 'nc=' + (new Date()).getTime();
				
			var oImg = new Image();
			oImg.src = sSrc;
			//alert(sSrc);
			
			//Show message on screen (element with id "status" nessecary)
			//document.getElementById('status').innerHTML = document.getElementById('status').innerHTML + Date() + '&nbsp;&nbsp;&nbsp;' + src + "<br />";
			
			this.bResizeWait = false;
		}
	}
	
	//Log connectionspeed          
	function logClientSpeed(sLogging){
		if (this.bCalculateSpeed == true) {
			//Set speedlogging to false
			this.bCalculateSpeed = false;
			
			//Declare image for connectionspeed;
			oConnectionImg = new Image()
			oConnectionImg.onload = function(){eval(sLogging + '.logClientSpeed()')};
			
			//Start speedTime
			this.nStartTime = (new Date()).getTime();
			oConnectionImg.src = this.sLibraryPath + "?L=4&nc=" + (new Date()).getTime();
		}
		else{
			//Calculate speedTime and when pingTime larger then speedTime, pingTime = 0
			var nSpeedTime = ((new Date()).getTime() - this.nStartTime);
			
			//Log client speed
			var sSrc = this.sLibraryPath + '?L=5&';
			sSrc += 'br=' + parseInt((((this.nFileSize * 8) / (nSpeedTime / 1000)) / 1024)) + '&';
			
			sSrc += 'nc=' + (new Date()).getTime();
		
			var img = new Image();
			img.src = sSrc;
			//alert(sSrc);
			
	//		Show message on screen (element with id "status" nessecary)		
	//		document.getElementById('status').innerHTML = document.getElementById('status').innerHTML + Date() + '&nbsp;&nbsp;&nbsp;' + src + "</br>";
		}
	}
	
	//Log pageview when a multimediaitem is viewed/selected
	function logMultimedia(nId, nType, nMode){
		var logmm;
		logmm = new Image();
		logmm.src = this.sLibraryPath +'?L=1&Id=' + nId + '&T=' + nType + '&M=' + nMode + '&Nc=' + (new Date()).getTime();
		//document.location.href = logmm.src;
		
		if(window["_ctr"]){
			var logmmctr;
			logmmctr = new Image();
			logmmctr.src = this.sLibraryPathCTR +'?L=3&Id=' + nId + '&T=' + nType + '&M=' + nMode + '&Nc=' + (new Date()).getTime();
			//document.location.href = logmmctr.src;
		}
	}
	
	//Log pageview
	function logPageview(nType){
		var logpv;
		logpv = new Image();
		logpv.src = this.sLibraryPath +'?L=6&T=' + nType + '&Nc=' + (new Date()).getTime();
	}
	
	//Log event
	function logEvent(nType, nSource){
		var logpv;
		logpv = new Image();
		logpv.src = this.sLibraryPath +'?L=7&T=' + nType + '&S=' + nSource + '&Nc=' + (new Date()).getTime();
	}
	
	//Handle resize in window for logging resize when window is enlarged
	function onResizeHandler(sLogging){
		if ((this.nResizeWindowWidth != this.getInnerWidth()) || (this.nResizeWindowHeight != this.getInnerHeight())){
			this.nResizeWindowWidth = this.getInnerWidth();
			this.nResizeWindowHeight = this.getInnerHeight();	
			if (this.bResizeWait == false && ( (this.nResizeWindowWidth > this.nWindowWidth && this.nResizeWindowHeight >= this.nWindowHeight) || (this.nResizeWindowWidth >= this.nWindowWidth && this.nResizeWindowHeight > this.nWindowHeight) ) ){
				this.bResizeWait = true;
				this.nResizeIntervalId = setInterval("eval('" + sLogging + ".logClientResize()')", 500);
			}
		}
	}
}// #endregion
