/******************************************************************************
 *
 * Purpose: Activity logger plugin
 * Author:  Thomas Raffin, SIRAP
 *
 ******************************************************************************
 *
 * Copyright (c) 2008 SIRAP
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. See the COPYING file.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with p.mapper; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *******************************************************************************
 *
 * Original ideaand part of code: phpMyVisites and piwik
 *
 ******************************************************************************/

var al_params = al_getPluginsIntalled();

/**
 *
 */
var al_timeout = 180000;
var al_checkIncrement = 1;
var al_clientConfigOK = false;

function al_getTimeout() {
	var timeout;
	if (al_checkIncrement > 1) {
		al_checkIncrement--;
	} else { // in case of negative values...
		al_checkIncrement = 1;
	}
	timeout = al_timeout * al_checkIncrement;
	return timeout;
}

function al_init() {
	var timeout = al_getTimeout();
	setTimeout('al_sendClientConfig()', 10000);
	setTimeout('al_status()', 15000);
}

function al_sendClientConfig() {
	if (!al_clientConfigOK) {
		var url = PM_PLUGIN_LOCATION + '/activitylogger/x_activitylogger.php';
		var params = al_params;
		params += '&act=clientconfig&' + SID;
		 
		$.ajax({
			url: url,
			data: params,
			type: 'post',
			dataType: 'json',
			success: function(response) {
			al_checkIncrement = 1;
				al_clientConfigOK = true;
			},
			error: function() {
				if (!al_clientConfigOK && (al_checkIncrement <= 10)) {
					al_checkIncrement += 2;
					var timeout = al_getTimeout();
					setTimeout('al_sendClientConfig()', timeout);
				}
			}
		});
	}
}

function al_status() {
	var url = PM_PLUGIN_LOCATION + '/activitylogger/x_activitylogger.php';
	var params = 'act=status&' + SID;
	 
	$.ajax({
		url: url,
		data: params,
		type: 'post',
		dataType: 'json',
		success: function(response) {
			var timeout = al_getTimeout();
			setTimeout('al_status()', timeout);
		},
		error: function() {
			if (al_checkIncrement <= 10) {
				al_checkIncrement += 2;
				var timeout = al_getTimeout();
				setTimeout('al_status()', timeout);
			}
		}
	});
}

/**
 * 
 */
function al_getPluginsIntalled() {
	var al_agent = navigator.userAgent.toLowerCase();
	var al_windows = ( (al_agent.indexOf('win') != -1) || (al_agent.indexOf('32bit') != -1) );

	var al_java = (navigator.javaEnabled()) ? '1' : '0';
	var al_cookie = '0';
	if (typeof(navigator.cookieEnabled) == 'undefined') {
		document.cookie = 'al_testcookie';
		if (document.cookie.indexOf('al_testcookie') >= 0) {
			al_cookie =  '1';
		}
	} else if (navigator.cookieEnabled){
		al_cookie = '1';
	}
	var al_dir = '0';
	var al_fla = '0';
	var al_pdf = '0';
	var al_qt = '0';
	var al_realp = '0';
	var al_wma='0'; 
	var al_res = screen.width + 'x' + screen.height;
	var al_col = screen.colorDepth;
	if (al_windows && $.browser.msie) {
		al_dir = al_isPluginInstalledInIE('SWCtl.SWCtl.1');
		al_fla = al_isPluginInstalledInIE('ShockwaveFlash.ShockwaveFlash.1');
		if ( (al_isPluginInstalledInIE('PDF.PdfCtrl.1') == '1') || (al_isPluginInstalledInIE('PDF.PdfCtrl.5') == '1') || (al_isPluginInstalledInIE('PDF.PdfCtrl.6') == '1') ) {
			al_pdf = '1';
		}
		al_qt = al_isPluginInstalledInIE('Quicktime.Quicktime'); 
		al_realp = al_isPluginInstalledInIE('rmocx.RealPlayer G2 Control.1');
		al_wma = al_isPluginInstalledInIE('wmplayer.ocx');
	} else {
		var al_mimeTypes = '';
		for (var i=0; i < navigator.mimeTypes.length; i++) {
			al_mimeTypes += navigator.mimeTypes[i].type.toLowerCase();
		}
		al_dir = al_isPluginInstalled(al_mimeTypes, 'application/x-director');
		al_fla = al_isPluginInstalled(al_mimeTypes, 'application/x-shockwave-flash');
		al_pdf = al_isPluginInstalled(al_mimeTypes, 'application/pdf');
		al_qt  = al_isPluginInstalled(al_mimeTypes, 'video/quicktime');
		al_realp = al_isPluginInstalled(al_mimeTypes, 'audio/x-pn-realaudio-plugin');
		al_wma = al_isPluginInstalled(al_mimeTypes, 'application/x-mplayer2');
	}
	var result = '';
	result += 'al_res=' + al_res;
	result += '&al_col=' + al_col;
	result += '&al_fla=' +  al_fla;
	result += '&al_dir=' + al_dir;
	result += '&al_qt=' + al_qt;
	result += '&al_realp=' + al_realp;
	result += '&al_pdf=' + al_pdf;
	result += '&al_wma=' + al_wma;
	result += '&al_java=' + al_java;
	result += '&al_cookie=' + al_cookie;
	return result;
}

/**
 * 
 */
function al_isPluginInstalled(al_mimeTypes, searchedPlugin) {
	var result = '0';
	if ( (al_mimeTypes.indexOf(searchedPlugin) != -1) && (navigator.mimeTypes[searchedPlugin].enabledPlugin != null) ) { 
		result = '1';
	}
	return result;
}
function al_isPluginInstalledInIE(al_plugin) {
	var result = '0';
	al_plugin_found = false;
	document.write('<SCR' + 'IPT LANGUAGE=VBScript>\n on error resume next \n al_plugin_found = IsObject(CreateObject("' + al_plugin + '")) </SCR' + 'IPT>\n');
	if (al_plugin_found) {
		result = '1';
	}
	return result;
}
