/*
	* AutoDetect: user environment detection
	* version 1.0 (10/12/2007)
	* @requires jQuery (http://jquery.com)
	*
	*
	* Copyright (c) 2007-2008 Andy Matthews (amatthews@dealerskins.com)
	* Dual licensed under the MIT and GPL licenses:
	* http://www.opensource.org/licenses/mit-license.php
	* http://www.gnu.org/licenses/gpl.html
	*
	*
	* Description: AutoDetect detects and returns various user environment variables
	*					as reported by the browser. Yes, it's not 100% effective, but it's
	*					better than having nothing right?
	*
	*
	* Methods: There are no methods to call.
	*
	*
	* Setup: The following values must be provided.
	*		// an object named autoDetect
	*		autoDetect = {
	*			// an object named options
	*			options: {
	*				// a valid URL path
	*				fileLocation: 'loading.txt',
	*				// the size in bytes for the above file
	*				fileSize: 40960,
	*				// the bandwidth threshold, above this value will be considered high
	*				// below it will be considered low
	*				threshold: 130
	*			}
	*		};
	*
	* Comments are always welcome
*/
$(document).ready(function(){

	var options = autoDetect['options'];
	var userInfo = autoDetect['userInfo'];
	var start = new Date().getTime(); // Record Start time of text file loading.
	var loc = options['fileLocation'] + '?t=' + escape(start); // Append the Start time to the url to prevent caching

	// initiate the load of the text file
	$.get(loc, function(){

		var end = new Date();
		var flashDetect = new checkFlash();
		var bandwidthNumeric = computebandwidth(start,options['fileSize']);
		var bandwidth = (bandwidthNumeric > options['threshold']) ? 'High' : 'Low';
		
		userInfo['timeZoneDiff'] = getTimeZone();
		userInfo['screenResolution'] = getResolution();
		userInfo['bandwidth'] = getBandwidth();
		userInfo['flashVersion'] = getFlashversion();
		userInfo['javascriptEnabled'] = isJavascriptEnabled();
		
		function getTimeZone() {
			return end.getTimezoneOffset()/60;
		} // close getTimeZone

		function getResolution() {
			return screen.width + ' x ' + screen.height;
		} // close getResolution

		function getBandwidth() {
			return bandwidth; // return bandwidth + ' (' + bandwidthNumeric + 'k)';
		} // close getBandwidth

		function getFlashversion() {
			return isNaN(flashDetect.major) ? -1 : flashDetect.major;
		} // close getFlashversion

		function isJavascriptEnabled() {
			return 'YES';
		} // close isJavascriptEnabled

		function computebandwidth (start, fileSize ) {
			var end = (new Date()).getTime();
			var connectSpeed = (Math.floor((((fileSize * 8) / ((end - start) / 1000)) / 1024) * 10) / 10);
			return connectSpeed;
		} // close computebandwidth

		function checkFlash(){
			var self = this;
			self.installed = false;
			self.major = -1;
			var activeXDetectRules = [
				{
					"name":"ShockwaveFlash.ShockwaveFlash.7",
					"version":function(obj){
						return getActiveXVersion(obj);
					}
				},{
					"name":"ShockwaveFlash.ShockwaveFlash.6",
					"version":function(obj){
						var version = "6,0,21";
						try{
							obj.AllowScriptAccess = "always";
							version = getActiveXVersion(obj);
						}catch(err){}
						return version;
					}
				},{
					"name":"ShockwaveFlash.ShockwaveFlash",
					"version":function(obj){
						return getActiveXVersion(obj);
					}
				}
			];
			var getActiveXVersion = function(activeXObj){
				var version = -1;
				try{
					version = activeXObj.GetVariable("$version");
				}catch(err){}
				return version;
			};
			var getActiveXObject = function(name){
				var obj = -1;
				try{
					obj = new ActiveXObject(name);
				}catch(err){}
				return obj;
			};
			var parseActiveXVersion = function(str){
				var versionArray = str.split(","); //replace with regex
				return {
					"major":parseInt(versionArray[0].split(" ")[1], 10)
				};
			};

			self.checkFlash = function(){
				if (navigator.plugins && navigator.plugins.length>0){
					var type = 'application/x-shockwave-flash';
					var mimeTypes = navigator.mimeTypes;
					if(mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin && mimeTypes[type].enabledPlugin.description){
						var desc = mimeTypes[type].enabledPlugin.description;
						var descParts = desc.split(' ');//replace with regex
						var majorMinor = descParts[2].split('.');
						self.major = parseInt(majorMinor[0], 10);
						self.installed = true;
					}
				} else if (navigator.appVersion.indexOf("Mac")==-1 && window.execScript){
					var version = -1;
					for(var i=0; i<activeXDetectRules.length && version==-1; i++){
						var obj = getActiveXObject(activeXDetectRules[i].name);
						if(typeof obj == "object"){
							self.installed = true;
							version = activeXDetectRules[i].version(obj);
							if(version!=-1){
								var versionObj = parseActiveXVersion(version);
								self.major = versionObj.major;
							}
						}
					}
				} else {
					self.major = -1;
				}
			}();

			return self;
		}; // checkFlash

		// we're dumping the contents of the userinfo object to a post call
		$.post(
			 '/AjaxService/stats/stats.cfc?method=InsertDetailedBrowserStats',
			userInfo,
			 function(data){
				// console.log(data);
			}
		);

	}); // close $.get call

}); // close document.ready
