/**
 *
 * Alipay Log Analytics Module, v2.5
 * @author taibo, taibo@alipay.com, Mar 29, 2010
 * Copyright 2010 Alipay, All Rights Reserved.
 *
 * Usage (obsoleted):
 *		<script type="text/javascript" src="analytic.js?ver=2.5"></script>
 * include Tracker instead:
 *		<script type="text/javascript" src="//static.alipay.com/build/js/app/tracker.js?ver=0.5"></script>
 *
 **/

// create alistat, do nothing if alistat exists
window.alistat || (function(doc, stat){
		// defines the url where track data should be sent to
		stat.serverUrl = '//img.alipay.com/datacookie/stat.gif';
		
		/**
		 * @class alistat.Param Represents a parameter builder
		 * @constructor
		 * @param splitter {String} Optional, a string used to concatenate all concatenated key-value pairs
		 * @param connector {String} Optional, a string used to concatenate key & value
		 **/
		stat.Param = function(splitter, connector) {
			this.splitter = splitter || '&';
			this.connector = connector || '=';
			this.clear();
		};

		// prototype
		stat.Param.prototype = {
			/**
			 * @method add Adds a key-value pair
			 * @param key {Param|String} key to add, or another Param to add
			 * @param value {String} value to add
			 * @return none
			 **/
			add: function(key, value) {
				if(key instanceof alistat.Param) {
					for(var i = 0, len = key.items.length; i < len; i++) {
						var pair = key.items[i].split(key.connector, 2);
						this.add(decodeURIComponent(pair[0]), decodeURIComponent(pair[1]));
					}
				} else {
					this.items.push(encodeURIComponent(key) + this.connector + encodeURIComponent(value));
				}
			},
			/**
			 * @method clear Removes all items
			 * @return none
			 **/
			clear: function() {
				this.items = [];
			},
			/**
			 * @method unique Adds an item makes it unique after toString() called
			 * @return none
			 **/
			unique: function() {
				this.add('rnd', new Date().valueOf());
			},
			/**
			 * @method addClientInfo Adds client info
			 * @return none
			 **/
			addClientInfo: function() {
				var sr = "-",
					sc = "-",
					n = navigator;
				if (self.screen) {
					sr = screen.width + "x" + screen.height;
					sc = screen.colorDepth + "-bit";
				} else if (self.java) {
					var s = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
					sr = s.width + "x" + s.height;
				}
				this.add('screen', sr);
				this.add('color', sc);
			},
			/**
			 * @method toString Gets a string which represent the Param
			 * @return {String} A string includes all key-value pairs
			 **/
			toString: function() {
				return this.items.join(this.splitter);
			}
		};

		var _taste = 100, // The sampling % of visitors to track (1-100).
		loc = doc.location,
		param = new alistat.Param();

		param.unique();
		param.addClientInfo();
		param.add('utmhn', loc.hostname);
		_taste && _taste != 100 && param.add('tst', _taste);
		param.add('ref', doc.referrer || '-');
		param.add('pg', loc.pathname + loc.search);

		// add additional parameters if 'analytic_var' exists. by Taibo, Mar 12, 2010
		window.analytic_var && param.add('analytic_var', analytic_var);

		if(loc.protocol != 'file:' && !stat.firstrun) {
			stat.firstrun = true;
			new Image().src = stat.serverUrl + '?' + param;
		}
})(document, window.alistat = {});

