/* *** INFORMAÇÕES DE LICENCIAMENTO (LICENSE INFORMATION)  ***

   @author Nilson Pontello de Freitas <nilson@hostminas.com>
   @date 05/04/2007
	
   Version: HostMinas License 1.0

   O conteúdo desse arquivo está sujeito às regras da Licença de Software
   HostMinas, versão 1.0. Está proibido o uso dos códigos contidos neste arquivo
   por terceiros, bem sua cópia, no todo ou em parte, sem expressa autorização
   dos detententores da propriedade intelectual e dos direitos comerciais. Uma
   cópia da licença pode ser obtida em http://www.hostminas.com/license. A
   propriedade intelectual e comercial deste código pertence à HostMinas
   Internet Services Ltda.

   Copyright (c) 2007. Todos os direitos reservados.

   ----

   This paragraph is a free translation from Brazilian-Portuguese to
   American-English of the license information above, for better understanding
   purposes. Any intepretation of it must consider the original document. The
   contents of this file are subject to the HostMinas Software License, version
   1.0. It is forbidden the use of the codes included on this file, even
   partially, for third parties, such as its copy, without express
   authorization of the intelectual property and commercial rights owners. A
   copy of the license may be obtained at http://www.hostminas.com/license.
   The intelectual property and comercial rights of this file and codes belong
   to HostMinas Internet Services Ltd.

   Copyright (c) 2007. All rights reserved.

   *** FIM DAS INFORMAÇÕES DE LICENCIAMENTO (END OF LICENSE INFORMATION) ***
 */

if (typeof(AC) == "undefined") { AC = {}; }

AC.Detector = {

	getAgent: function() {
		return navigator.userAgent.toLowerCase();
	},
	
	// detect platform
	isMac: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return agent.match(/mac/i);
	},
	
	isWin: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return agent.match(/win/i);
	},
	
	isWin2k: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return this.isWin(agent) && (agent.match(/nt\s*5/i));
	},
	
	isWinVista: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return this.isWin(agent) && (agent.match(/nt\s*6/i));
	},
	
	// detect browser
	isWebKit: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return agent.match(/AppleWebKit/i);
	},
	
	isOpera: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return agent.match(/opera/i);
	},
	
	isIE: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return agent.match(/msie/i);
	},
	
	isIE6: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return agent.match(/msie 6.0/i);
	},	
	
	isIEStrict: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return agent.match(/msie/i) && !this.isOpera(agent);
	},
	
	isFirefox: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return agent.match(/firefox/i);
	},

	isiPhone: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return agent.match(/iPhone/i);
	},
	
	// itunes compabibility
	isiTunesOK: function(userAgent) {
		var agent = userAgent || this.getAgent();
		return this.isMac(agent) || this.isWin2k(agent);
	},
	
	isQTInstalled: function() {
		
		var qtInstalled = false;
		
		if(navigator.plugins && navigator.plugins.length) {
			
			for(var i=0; i < navigator.plugins.length; i++ ) {
				
				var plugin = navigator.plugins[i];
				
				if(plugin.name.indexOf("QuickTime") > -1) { 
					qtInstalled = true; 
				}
			}
		} else {
			qtObj = false; //global variable written to by vbscript for ie
			execScript('on error resume next: qtObj = IsObject(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1"))','VBScript');
			qtInstalled = qtObj;
		}
		
		return qtInstalled;
	},
	
	getQTVersion: function() {
		
		var version = "0";
		
		if (navigator.plugins && navigator.plugins.length) {
			for (var i = 0; i < navigator.plugins.length; i++) {
				
				var plugin = navigator.plugins[i];
				
				//Match: QuickTime Plugin X.Y.Z
				var match = plugin.name.match(/quicktime\D*([\.\d]*)/i);
				if (match && match[1]) {
					version = match[1];
				}
			}
		} else {
			ieQTVersion = null; //global for vbscript to write to
			
			execScript('on error resume next: ieQTVersion = (Hex(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1").QuickTimeVersion)/1000000)','VBScript');

			if (ieQTVersion) {
				//only grab the major version:  7.xyz => 7
				version = (ieQTVersion +"").split(/\./)[0];
			}
		}
		
		return version;
	},
	
	isQTCompatible: function(required, actual) {
		
		function areCompatible(required, actual) {
			
			var requiredValue = parseInt(required[0])
			if (isNaN(requiredValue)) {
				requiredValue = 0;
			}
			
			var actualValue = parseInt(actual[0])
			if (isNaN(actualValue)) {
				actualValue = 0;
			}
			
			if (requiredValue == actualValue) {
				if (required.length > 1) {
					return areCompatible(required.slice(1), actual.slice(1));
				} else {
					return true;
				}
			} else if (requiredValue < actualValue) {
				return true;
			} else {
				return false;
			}
		}

		var expectedVersion = required.split(/\./);
		var actualVersion = actual ? actual.split(/\./) : this.getQTVersion().split(/\./);
		
		return areCompatible(expectedVersion, actualVersion);
		
	},
	
	isValidQTAvailable: function(required) {
		return this.isQTInstalled() && this.isQTCompatible(required)
	}
	
};
