GA = function( id, siteName ) {
	this.id = id;
	this.trackerName = ( this.id ? this.id + '.' : '' );
	//this.trackerName = '';
	if( !window._gaq ) window._gaq = [];

	var _self = this,
		//domain = document.location.href.replace(/https?:\/\/(?:www)?([^\/]+).*/, '$1');
		siteNames = {};

	this.lang = ( window.PageStatus ? PageStatus.Lang() : ( window.lang ? window.lang : window.location.href.replace( /https?:\/\/([^\/]+)\/([^\/]+).*/, '$2' ) ) );
	if( this.lang.length != 2 ) {
		for( var c in siteNames ) {
			if( new RegExp( siteNames[c] ).test( window.location.href ) ) {
				this.lang = c;
				break;
			}
		}
	};
	this.domain = siteName || siteNames[this.lang];

	this.addOrganic();
	_gaq.push( [this.trackerName + '_setAccount', this.id],
		[this.trackerName+'_setDomainName', this.domain],
		[this.trackerName+'_setAllowLinker', true],
		[this.trackerName+'_setAllowHash', false],
		//[this.trackerName+'_trackPageview', '?gaSkip=true'],
		function() {
			var tracker = _gat._getTrackerByName( _self.id ),
				track = tracker._trackPageview;

			tracker._setCustomVar( 5, 'userGAID', tracker._visitCode(), 1 );
			//tracker._initData();
			tracker._trackPageview = function( link ) {
				var link = link || document.location.href.replace(/https?:\/\/.+?(?=\/|$)/, ''),
					addition = 'timeGAID=' + new Date().getTime() + '&randGAID=' + Math.random(),
					pound_sign_pos = link.indexOf('#');

				if(pound_sign_pos == -1) {
					pound_sign_pos = link.length;
				}
				if( ( pos = link.substr( 0, pound_sign_pos ).indexOf( '?' ) ) != -1 ) {
					if( link.length != pos + 1) {
						if( link[pos + 1] != '#' ) {
							url = link.substr( 0, pos + 1 ) + addition + '&' + link.substr( pos + 1 );
						} else {
							url = link.substr( 0, pos + 1 ) + addition + link.substr( pos + 1 );
						}
					} else {
						url = link + addition;
					}
				} else {
					if( pound_sign_pos != link.length ) {
						url = link.substr( 0, pound_sign_pos ) + '?' + addition + link.substr( pound_sign_pos );
					} else {
						url = link + '?' + addition;
					}
				}

				//track( url );
				track()
			};
	});

    this.sendAnalytics();	
};


GA.prototype.trackPageview = function( url ) {
	_gaq.push( [this.trackerName + '_trackPageview', url] );
	return this;
}

GA.prototype.sendAnalytics = function() {
	if( _gaq instanceof Array )(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		//ga.src = 'http://www.alpari.ru/static/javascript/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();

	return this;
}

GA.prototype.addOrganic = function( ) {
	var aO = this.trackerName + '_addOrganic';
	_gaq.push([aO, 'go.mail.ru', 'q']);
	_gaq.push([aO, 'go.mail.ru/search', 'q']);
	//_gaq.push(['_addOrganic', 'go.mail.ru', 'q']);
    //_gaq.push(['_addOrganic', 'go.mail.ru/search', 'q']);
	return this;
}


var GAO = new GA( 'UA-10074081-1', 'forum.alpari.ru' );
//GAO.addOrganic();
window.setTimeout( function(){ GAO.trackPageview(); }, 30 );

/*
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-10074081-1']);
_gaq.push(['_addOrganic', 'go.mail.ru', 'q']);
_gaq.push(['_addOrganic', 'go.mail.ru/search', 'q']);
_gaq.push(['_trackPageview']);

 (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();
 */
