/*
* Copyright (c) 2002-2005 Victor A.Spirin <victor_aspirin [at] mail [dot] ru>
* v. 1.2.1 - 2006.03.05
*/



var userAgent = new RiniX_UA_UserAgent();

function RiniX_UA_UserAgent()
	{
	/*string*/
	var type;
	
	/*string*/
	var version;
	
	/*string*/
	var jsRoot;
	
	var This = this;
	
	
	
	/*constructor*/
	function __construct()
		{
		var REs = {firefox: /Firefox\x2f([0-9\.]+)/, mozilla: /rv:([0-9\.]+)\) Gecko/, opera: /Opera[ \x2f]([0-9\.]+)/, msie: /MSIE ([0-9\.]+)/};
		
		var ua = navigator.userAgent;
			for( var n in REs )
			{
			var ms = ua.match(REs[n]);
				if( ms != null )
				{
				type = n;
				version = ms[1];
				break;
				}
			}
		}
	
	
	
	/*string*/
	this.getJSRoot = function ()
		{
			if( jsRoot == null ) _searhJsRoot();
		return jsRoot;
		}
	
	
	
	/*string*/
	this.getType = function ()
		{
		return type;
		}
	
	/*boolean*/
	this.is = function ( /*string*/ vendor )
		{
			if( typeof(vendor) != 'string' || vendor == '' )
			throw 'RiniX_UA_UserAgent.is: Illegal argument "vendor"';
		
		return type == vendor.toLowerCase();
		}
	
	/*string*/
	this.getVersion = function ()
		{
		return version;
		}
	
	
	
	/*void*/
	this.load = function ( /*string*/ pointer, /*string[]*/ types, /*boolean*/ noCache )
		{
			if( typeof(pointer) != 'string' || pointer.search(/^[a-z0-9_\.]+$/gi) == -1 )
			throw 'RiniX_UA_UserAgent.load: Illegal argument "pointer"';
		
			if( jsRoot == null ) _searhJsRoot();
		
			if( jsRoot != null )
			This.loadFile(jsRoot + pointer.replace(/\./gi, '/') + '.js', types, noCache);
		}
	
	
	
	/*void*/
	this.loadFile = function ( /*string*/ url, /*string[]*/ types, /*boolean*/ noCache )
		{
			if( typeof(url) != 'string' || url.substring(url.length - 3) != '.js' )
			throw 'RiniX_UA_UserAgent.loadFile: Script file "' + url + '" must ends with ".js"';
			if( types != null && !(typeof(types) == 'object' && types.length != null) )
			throw 'RiniX_UA_UserAgent.loadFile: Illegal argument "types"';
			if( noCache != null && typeof(noCache) != 'boolean' )
			throw 'RiniX_UA_UserAgent.loadFile: Illegal argument "noCache"';
		
			if( types == null ) _load(url, noCache);
			else
			{
			var f = _getFileName(url, types);
				if( f != null ) _load(f, noCache);
			}
		}
	
	
	
	/*string*/
	function _getFileName( /*string*/ url, /*string[]*/ types )
		{
		var base = url.substring(0, url.length - 3);
		var ua = type + Math.floor(10*parseFloat(version));
		var f = null;
		var exists = false;
			for( var i = 0 ; i < types.length ; i++ )
			{
				if( types[i] == type ) exists = true;
				else if( types[i] == ua )
				{
				f = base + '.' + ua + '.js';
				break;
				}
			}
			if( f == null && exists ) f = base + '.' + type + '.js';
		return f;
		}
	
	
	
	/*void*/
	function _load( /*string*/ url, /*boolean*/ noCache )
		{
		var e = document.createElement('script');
		e.setAttribute('type', 'text/javascript');
		document.documentElement.firstChild.appendChild(e);
		e.setAttribute('src', noCache ? url + '?v=' + Math.random() : url);
		}
	
	
	
	/*void*/
	function _searhJsRoot()
		{
		var es = document.getElementsByTagName('script');
			for( var i = 0 ; i < es.length ; i++ )
			{
			var src = es[i].src;
				if( src != null )
				{
				var j = src.toLowerCase().indexOf('rinix/ua/useragent.js');
					if( j != -1 )
					{
					jsRoot = src.substring(0, j);
					break;
					}
				}
			}
		}
	
	
	
	__construct();
	}
