/**
 *	Calls method in a specified context.
 *
 *	@param	function context Context of method to call in.
 *	@param	array parameters Call parameters.
 *	@return	void
 *	@access	public
 **/
if ( typeof(Function.prototype.apply) == undefined ) // This is required for IE5
Function.prototype.apply = function ( context, parameters ) {

    /* Initiate default values */
	if ( Object.isUndefined(context) || Object.isNull(context) ) { context = window; }
	if ( !Object.isArray(parameters) ) { parameters = new Array(); }
	var parameterStrings = new Array();

    /* Generate parameters string for eval */
	for ( var i = 0; i < parameters.length; i++ ) {

		parameterStrings[i] = 'parameters[' + i + ']';

	} // for i

	/* Execute method */
	context.__temporaryMethod__ = this;
	var result = eval('context.__temporaryMethod__('+ parameterStrings.join(', ') + ')');
	delete context.__temporaryMethod__;

	return result;

} // Function::apply()

/**
 *	Delegates specified method.
 *
 *	@param	object instance Instance to apply method to.
 *	@param	function method Function to apply.
 *	@param	mixed arguments Argument for method.
 *	@return	function Delegate function.
 **/
Function.createDelegate = function ( instance, method ) {

	return function (  ) {

		/* Collect parameters */
		var parameters = new Array();
		for ( var i = 0; i < arguments.length; i++  ) { parameters.push(arguments[i]); }

		/* Execute method */
		method.apply(instance, parameters);

	}

} // Function::createDelegate()

/**
 *	HttpSocket class.
 *
 *	@author ido <ido@gaumina.com>
 **/
function HttpSocket (  ) {

	/**
	 *	@var	XMLHttpRequest XmlHttpRequest object.
	 *	@access	private
	 **/
	this.socket = null;


	/**
	 *	Constructor.
	 *
	 *	@access	public
	 **/
	this.HttpSocket = function (  ) {

		/* Creates new XmlHttp object */
		if ( window.XMLHttpRequest ) { // Firefox, Opera

			try { this.socket = new XMLHttpRequest(); } catch ( error ) { this.socket = null; }

		} else if ( window.ActiveXObject ) { // Internet explorer

			try { this.socket = new ActiveXObject('Msxml2.XMLHTTP'); } catch ( error ) {

				try { this.socket = new ActiveXObject('Microsoft.XMLHTTP'); } catch( error ) { this.socket = null; }

			} // try

		} // else if

	} // HttpSocket::HttpSocket()

	/**
	 *	Reports if socket successfully created
	 *
	 *	@return	boolean
	 *
	 *	@return	boolean
	 *	@access	public
	 **/
	this.isInitialised = function (  ) {

		return this.socket != null;

	} // HttpSocket::isInitialised()

	/**
	 *	Sends value
	 *
	 *	@param	string	Url for request
	 *	@param	string	Content request content
	 *	@return	void
	 *	@access	private
	 **/
	this.load = function ( url ) {

		if ( this.socket.readyState == 0 ) {

			this.socket.open('POST', url, true);
			this.socket.onreadystatechange = Function.createDelegate(this, this.processReadyState);

			this.socket.send("");

		} // if

	} // HttpSocket::sendValue()

	/**
	 *	Aborts request
	 *
	 *	@access public
	 **/
	this.abort = function (  ) {

		this.socket.abort();

	} // HttpSocket::abort()

	/**
	 *	Processes ready state's changes
	 *
	 *	@return	void
	 *	@access	private
	 **/
	this.processReadyState = function (  ) {

		if ( this.socket.readyState == 4 ) {

			if ( this.socket.status == 200 ) {

				this.onFinished(this.reciever, this.socket.responseXML);

			} // if

		} // if

	} // HttpSocket::onreadyStateChange()

	/* Initiate object */
	this.HttpSocket();

} // class HttpSocket





/**
 * EXTSTATS
 * @author Gytis Galvanauskas <gytis@gaumina.lt>
 * @version 1.1
 * @access public
 * @static
 */

var is_flash   = false;

var is_flash_2 = false;
var is_flash_3 = false;
var is_flash_4 = false;
var is_flash_5 = false;
var is_flash_6 = false;
var is_flash_7 = false;
var is_flash_8 = false;
var is_flash_9 = false;

var flash_ver  = 0;

var detect_min_ver = 2;
var detect_max_ver = 9;

function detectIE() {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	for( var count = detect_min_ver; count <= detect_max_ver; count++ ) {
		document.write( 'is_flash_' + count + ' = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.' + count + '"))) \n' );
	}
	document.write('<\/SCR' + 'IPT\> \n');
	for( var count = detect_min_ver; count <= detect_max_ver; count++ ) {
		if( eval('is_flash_' + count ) == true ) {
			flash_ver = count;
		}
	}
	if( flash_ver > 0 ) {
		is_flash = true;
	}
}

function detectNS() {
	if( navigator.plugins['Shockwave Flash 2.0'] || navigator.plugins['Shockwave Flash'] ) {
		var is_version_2 = navigator.plugins['Shockwave Flash 2.0'] ? ' 2.0' : "";
		var flash_description = navigator.plugins['Shockwave Flash' + is_version_2].description;
		var flash_version = parseInt( flash_description.substring( 16 ) );
		if( flash_version > 0 ) {
			flash_ver = flash_version;
			is_flash = true;
		}
	}
}

function detectFlash() {

	if( navigator.plugins ) {
		detectNS();
	}
	if( ( ! is_flash ) && navigator.userAgent && navigator.userAgent.indexOf("MSIE") >= 0 && ( navigator.appVersion.indexOf( 'Win' ) != -1 ) ) {
		detectIE();
	}

}

function detectRes(){

	this.width = screen.width;
	this.height = screen.height;
	this.color = screen.colorDepth;
	this.resolution= (width+"X"+height);
}

detectRes();
detectFlash();

var EXTSTATS = {


	'action' 		: '',

	'logInfo' : function( user_id ) {

		// param which means taht we need to switch to another statistics - stickman_2 stats
		var is_stickman_user = getUrlParam( 'stickman_2' );

		var url = 'http://www.gaumina.lt/extstats.additional_info.php?';

		if ( is_stickman_user ) {
			url += 'stickman_2=1&';
		}

		url += this.varToURL(
								{
									'flash_title':flash_ver,
									'resolution_title':resolution,
									'user_id' : user_id
								}
							);

		/*var oImage = new Image( 1, 1 );
		oImage.src = url;*/




		oDiv = document.createElement( 'div' );
		var socket = new HttpSocket();


		socket.reciever = oDiv;
		socket.onFinished = function(){};
		socket.load( url );








	},

	'_varToURL' : function( variable, prefix ) {

		var ret_val = '';

		if( ( typeof( variable ) == 'object' ) || ( typeof( variable ) == 'array' ) ) {

			for( idx in variable ) {

				var prefix_variable = ( prefix.length > 0 ) ? ( prefix + '[' + idx + ']' ) : idx;

				if( ( typeof( variable[idx] ) != 'object' ) && ( typeof( variable[idx] ) != 'array' ) ) {

					ret_val += prefix_variable + '=' + escape( variable[idx], 1 ) + '&';

				} else {

					ret_val += this._varToURL( variable[idx], prefix_variable );
				}
			}

		}

		return( ret_val );
	},

	'varToURL' : function( variable ) {

		var ret_val = '';

		ret_val = this._varToURL( variable, '' );

		if( ret_val.length > 0 ) {
			ret_val = ret_val.substr( 0, ret_val.length - 1 );
		}

		return( ret_val );

	},

	'getRandString' : function() {

		var ret_val = '';

		for( var count = 0; count < 10; count++ ) {
			ret_val += Math.floor( Math.random() * 100000 ).toString();
		}

		return( ret_val );

	}

}

function logStats( user_id ){
	EXTSTATS.logInfo( user_id );

}

function getUrlParam( name ) {

	name = name.replace( /[\[]/,"\\\[" ).replace( /[\]]/,"\\\]" );

	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );

	if( results == null ) {
		return '';
	} else {
		return results[1];
	}
}