var _hd_refer = null;
function _hdCO() {
	var _hd_refer_cookie = GetCookie("hd_referer");
	var _hd_firstaccessurl_cookie = GetCookie("hd_firstaccessurl");
	//alert(_hd_refer_cookie);
    if(_hd_refer_cookie == null){
    	judgeTGRefer();
    	if(_hd_refer == null){
    		_hd_refer = document.referrer;
    	}
    	if((_hd_refer.toLowerCase()).indexOf("wikibar.hudong.com")!=-1 && (_hd_refer.toLowerCase()).indexOf(",")!=-1)
    	{
    		setCookies("hd_referer", _hd_refer.replace(/,/g, "%2c"));
    	}else
    	{
    		setCookies("hd_referer", _hd_refer);
    	}
    }
    
    if(_hd_firstaccessurl_cookie == null){
    	var hd_firstaccessurl_tmp = window.location.href;
    	if((hd_firstaccessurl_tmp.toLowerCase()).indexOf("wikibar.hudong.com")!=-1 && (hd_firstaccessurl_tmp.toLowerCase()).indexOf(",")!=-1)
    	{
    		setCookies("hd_firstaccessurl", hd_firstaccessurl_tmp.replace(/,/g, "%2c"));
    	}else
    	{
    		setCookies("hd_firstaccessurl", hd_firstaccessurl_tmp);
    	}
    }
	
	set2ndPV();
    
	_hdtcp = window.location;
	var d = 'stat.hudong.com';
	if((_hdtcp.href.toLowerCase()).indexOf("wikibar.hudong.com")!=-1 && (_hdtcp.href.toLowerCase()).indexOf(",")!=-1)
	{
		setCookies("hd_accessurl", (_hdtcp.href).replace(/,/g, "%2c"));
	}else
	{
		setCookies("hd_accessurl", _hdtcp.href);
	}
	if((document.referrer.toLowerCase()).indexOf("wikibar.hudong.com")!=-1  && (document.referrer.toLowerCase()).indexOf(",")!=-1)
	{
		setCookies("hd_accessrefer", (document.referrer).replace(/,/g, "%2c"));
	}else
	{
		setCookies("hd_accessrefer", document.referrer);
	}
	var sc = document.createElement('script');
	sc.type = 'text/javascript';
	sc.id = "_hdssojs"; 
	sc.src = 'http://' + d + '/hdWebStat.do?random=' + Math.random();
	document.getElementsByTagName('head')[0].appendChild(sc);
}

if (window.addEventListener) {
	window.addEventListener('load', _hdCO, false);
} else if (window.attachEvent) {
	window.attachEvent('onload', _hdCO);
}

//handle cookie
function setCookies(name, value) {
	var day = 1;
	var d = new Date();
	d.setTime(d.getTime() + (day * 24 * 60 * 60 * 1000));
	document.cookie = name + "=" + value + "; expires=" + d.toGMTString()
			+ "; path=/;domain=.hudong.com";
}
function Deletecookie(name) { //删除名称为name的Cookie  
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = GetCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString()
			+ "; path=/;domain=.hudong.com";
}

function getCookieVal(offset) { //取得项名称为offset的cookie值  
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) { //取得名称为name的cookie值  
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}
	return null;
}

function judgeTGRefer(){
	var host = window.location.host;
	var query = window.location.search;
	
	query = query.substring(1);
	
	if(query.indexOf("hf=")<0){
		return;
	}
	
	var arrQuery = query.split("&");
	
	var pf_i = -1;
	var hf_i = -1;
	
	for(var i=0;i<arrQuery.length;i++){
		pf_i = -1;
		hf_i = -1;
		pf_i = arrQuery[i].indexOf("pf=");
		hf_i = arrQuery[i].indexOf("hf=");
		if(hf_i>=0){
			_hd_refer = arrQuery[i].substring(hf_i+"hf=".length);
		}
	}
	
}

function judgeGA(){
		if(document.body.innerHTML.indexOf("_uacct") > -1) {
			//urchin.js
			return 1;
	     } else if (document.body.innerHTML.indexOf("gaJsHost") > -1) {
	    	 //ga.js
	    	 return 2;
	     } else {
	    	 return 0;
	     }
}

function set2ndPV(){

	if(judgeGA()==0){
		//alert("no ga");
		return;
	}

	var _1stAccessURL = GetCookie("hd_firstaccessurl");
	if(_1stAccessURL.indexOf("http://")>-1 && _1stAccessURL.indexOf("hf")>-1 && _1stAccessURL.indexOf("pf")>-1){
	    var p = new Poly9.URLParser(_1stAccessURL);
	    var para_hf = getURLPara("hf",p.getQuerystring());
		var para_pf = getURLPara("pf",p.getQuerystring());
		var fromHost = p.getHost();
		fromHost = fromHost.substring(0,fromHost.indexOf("."));
		var curHost = window.location.host.substring(0,window.location.host.indexOf("."));
		if(curHost.toUpperCase() == fromHost.toUpperCase()){
			return;
		}
		var curAccessURLPath = window.location.pathname;
		var gaPath = curAccessURLPath+"_from"+fromHost+"_"+para_pf+"_hdrefer_"+para_hf+".html";
        if (judgeGA() == 1) {
        	urchinTracker(gaPath);
			return;
        }
        else 
            if (judgeGA() == 2) {
            	pageTracker._trackPageview(gaPath);
				return;
            }
	}
	
}

function getURLPara(paraName,str){
    var arrQuery = str.split("&");
    var i_para = -1;
	var i_value = null;
    for(var i=0;i<arrQuery.length;i++){
		i_para = arrQuery[i].toUpperCase().indexOf(paraName.toUpperCase()+"=");
		if(i_para>-1){
			i_value = arrQuery[i].substring(i_para+paraName.length+1);
			return i_value;
		}
	}
}

if (typeof Poly9 == 'undefined')
{
	var Poly9 = {};
}
Poly9.URLParser = function(url) {

	this._fields = {
		'Username' : 4, 
		'Password' : 5, 
		'Port' : 7, 
		'Protocol' : 2, 
		'Host' : 6, 
		'Pathname' : 8, 
		'URL' : 0, 
		'Querystring' : 9, 
		'Fragment' : 10
	};

	this._values = {};
	this._regex = null;
	this.version = 0.1;
	this._regex = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/;
	for(var f in this._fields)
	{
		this['get' + f] = this._makeGetter(f);
	}

	if (typeof url != 'undefined')
	{
		this._parse(url);
	}
}
Poly9.URLParser.prototype.setURL = function(url) {
	this._parse(url);
}

Poly9.URLParser.prototype._initValues = function() {
	for(var f in this._fields)
	{
		this._values[f] = '';
	}
}

Poly9.URLParser.prototype._parse = function(url) {
	this._initValues();
	var r = this._regex.exec(url);
	if (!r) throw "DPURLParser::_parse -> Invalid URL";

	for(var f in this._fields) if (typeof r[this._fields[f]] != 'undefined')
	{
		this._values[f] = r[this._fields[f]];
	}
}
Poly9.URLParser.prototype._makeGetter = function(field) {
	return function() {
		return this._values[field];
	}
}
