/**
 * @baishan
 */

var _trackManager = new Object(
{
	webId:"",
	language:"",
	visitorId:"",
	cookiePath:"",
	//visitorId:"test",
	bstpURL:"tracking.myjeemoo.cn",
	bstpSourceURL:"",
	referPage:"",
	url:document.URL,
	cookieSourceName:"_bstpSource",
	cookieMediaName:"_bstpMedia",
	cookieContentName:"_bstpContent",
	cookieProductName:"_bstpProduct",
	userId:"",
	_initData:function()
	{
		if(this.webId == "")
		  return;
		this.visitorId = getCookieVal("_bstpVid");
		if(this.visitorId == null || this.visitorId == "")
		{
			this.visitorId = NewGuid();
			setCookie("_bstpVid",this.visitorId,900,this.cookiePath,null,false);
		}
		this.referPage=document.referrer;
	    var sourceURLPara = "";
		if (this.url.indexOf("bstp_") > -1) 
			sourceURLPara = this._SetSourceCookie();
		else {
			var fromSource = getCookieVal(this.cookieSourceName);
			var fromMedia = getCookieVal(this.cookieMediaName);
			if (fromSource != "" || fromMedia != "") {
				sourceURLPara = "&source="+fromSource+"&media="+fromMedia+
				"&content="+ getCookieVal(this.cookieContentName)+
				"&product="+getCookieVal(this.cookieProductName);
			}
		}
		var sessionId = this._SetSessionId();
		var gaJsHost = (("https:" == document.location.protocol) ? "https://" : "http://");
		this.bstpSourceURL = gaJsHost + this.bstpURL+"/images/webtrack.jpg?visitorid="+this.visitorId
		                     +"&webId="+this.webId+"&language="+this.language+"&sessionid="+sessionId
							 +"&referpage="+this.referPage;
		this.bstpSourceURL = this.bstpSourceURL+sourceURLPara;
		
			
	},
	_trackPage:function(page,hasPara)
	{
		var currentPage = "";
		if(page != null && page != "" && typeof(page)!= "undefined")
		{
			currentPage = page;
		}
		else
		{
			var startOffSet = this.url.indexOf(".");
			startOffSet = this.url.indexOf("/",9);
			if(startOffSet == -1)
			  currentPage ="/";
			else
			{
				if(typeof(hasPara) == "undefined")
				   hasPara = false;
				var paraStartSet = 0;
				if (hasPara)
					paraStartSet = this.url.indexOf("bstp_")-1;
				else 
				    paraStartSet = this.url.indexOf("?");
				if (paraStartSet < 0) 
						paraStartSet = this.url.length;
			    currentPage = this.url.substr(startOffSet, paraStartSet - startOffSet);
				if (currentPage.charAt(currentPage.length - 1) == "?") {
					currentPage = currentPage.substr(0, currentPage.length - 1);
				}
			}
		}
		var imgSrcUrl = this.bstpSourceURL;
		if (this.userId != null && this.userId != "") {
			imgSrcUrl += "&userid=" + this.userId;
			this.userId = "";
		}
		   
		imgSrcUrl += "&currentpage="+ escape(currentPage);
		var img = new Image(1,1);
		img.src = imgSrcUrl;
		//alert("11");
	},
	
	_SetSourceCookie:function()
	{
		var fromSource ="", fromMedia = "",
		fromContent = "", fromProduct = "";
		
		fromSource = getQueryString("bstp_source");	
		if(fromSource != "" && fromSource != null)
		{
			setCookie(this.cookieSourceName,fromSource,900,this.cookiePath,null,false);
			fromMedia = getQueryString("bstp_medium");
			setCookie(this.cookieMediaName,fromMedia,900,this.cookiePath,null,false);
			fromContent = getQueryString("bstp_content");
			if(fromContent != "" && fromContent != null)
			  setCookie(this.cookieContentName,fromContent,900,this.cookiePath,null,false);
			fromProduct = getQueryString("bstp_product");
			if(fromProduct != "" && fromProduct != null)
			  setCookie(this.cookieProductName,fromProduct,900,this.cookiePath,null,false);
		}
		return "&source="+fromSource+"&media="+fromMedia+"&content="+fromContent+"&product="+fromProduct;
	},
	_SetSessionId:function()
	{
		var sessionId = getCookieVal("_bstpSid");
		if (sessionId == null || sessionId == "") {
			sessionId = NewGuid();
			setCookie("_bstpSid",sessionId,null,this.cookiePath,null,false);
		}
		return sessionId;
	},
	_SetUser:function(uid)
	{
		this.userId = uid;
	}
} )
/*Cookie的读写函数*/
function getCookieVal(cookieName)
{
	var  search  =  cookieName  +  "="
	var  returnvalue  =  "";
	if  (document.cookie.length  >  0) 
	 {
		offset  =  document.cookie.indexOf(search);
		if  (offset  !=  -1)  
		{
			offset  +=  search.length
			end  =  document.cookie.indexOf(";",  offset);
			if  (end  ==  -1)
				end  =  document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset,  end))
		}
	}
	return  returnvalue;
}

function setCookie( name, value, expires, path, domain, secure ) {
	
	var today = new Date();
	today.setTime( today.getTime() );
	
	
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

/*其他基本函数*/

function getQueryString(fieldName){
	/*try{
		var sPattern = "\\?.*" + sParamName + "=([^&]*)&|\\?.*" + sParamName + "=(.*)";
		var re       = new RegExp(sPattern,"gi");
		re.exec(sUrl);
		return unescape(RegExp.$1 + RegExp.$2);
	}
	catch(e){
		return "";
	}*/
	
	var urlString = document.location.search;
      if(urlString != null)
      {
           var typeQu = fieldName+"=";
           var urlEnd = urlString.indexOf(typeQu);
           if(urlEnd != -1)
           {
                var paramsUrl = urlString.substring(urlEnd+typeQu.length);
                var isEnd =  paramsUrl.indexOf('&');
                if(isEnd != -1)
                {
                     return paramsUrl.substring(0, isEnd);
                }
                else
                {
                    return paramsUrl;
                }
           }
           else 
           return "";
      }
     else
     return "";
}
/*生成Guid的函数*/
function S4() {   
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);   
 } 
 function NewGuid() {   
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
 }  
