var visit_timeout=1*30*60*1000;
var idle_timeout=60*1000;

var woopra_server_id=parseInt(woopra_id.substring(0,woopra_id.length-8));

var woopra_engine_host="http://engine"+woopra_server_id+".woopra.com";
var woopra_static_host="http://static.woopra.com";


var chat_init;
var woopra_date=new Date();
var last_action_date=new Date();
var _idle=0;
//

function _Chat_Init(msg,owner,avatar){
	this.msg=msg;
	this.owner=owner;
	this.avatar=avatar;
}

//0x0
function _WaitFor(millis){
	var date = new Date();
	var curDate = null;
	do { 
	curDate = new Date(); 
	}
	while(curDate-date < millis);
}

function _GetRandomString(){
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var s = '';
	for (var i = 0; i < 32; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		s += chars.substring(rnum, rnum + 1);
	}
	return s;
}
function _GetNavigatorToken(){
	if(window.opera || window.Opera){return 'O';}
	if(navigator.userAgent){return 'U';}
	return "X";
}

function _GetLanguageToken(){
	return (navigator.browserLanguage || navigator.language || "");
}

function _GetCookie(k){
	if (_ReadCookie(k)) {
		return _ReadCookie(k);
	}
	var scookie = _GetRandomString();
	_CreateCookie(k,scookie,'Thu, 2 Aug 2050');
	rcookie = _ReadCookie(k);
	if (scookie == rcookie) {
		return rcookie;
	}
	return '0';
}

function _ReadCookie(k) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(k);
	if (ind==-1 || k=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+k.length+1,ind1));
}
function _CreateCookie(k,v,exp){
	cookieval = k + '=' + v + '; ' + ((exp)?('expires=' + exp + ' ;'):'') + 'path=/';
	document.cookie = cookieval;
}

function _TrackClick (e) {
	var cElem = (e.srcElement) ? e.srcElement : e.target;
	if(cElem.tagName == "A"){    
		var link=cElem;
		var _download = link.pathname.match(/(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&)/);
		if(_download){
			WoopraEvent("download",link.href);
			_WaitFor(100);
		}
		if (!_download&&link.hostname != location.host && link.hostname.indexOf('javascript')==-1 && link.hostname!=''){
			WoopraEvent("exit",link.href);
			_WaitFor(400);
		}
	}
}

function _ResetIdle(){
	last_action_date=new Date();
	if(_idle==1){
		WoopraEvent("status","0");
	}
	_idle=0;
}

function _CheckIdle(){
	if((new Date())-last_action_date> visit_timeout){
		return;
	}
	if((new Date())-last_action_date> idle_timeout){
		if(_idle==0){
			WoopraEvent("status","1");
		}
		_idle=1;
	}
	setTimeout('_CheckIdle()',5 * 1000);
}

function _Request(url){
	var _script=document.createElement('script');
	_script.type="text/javascript";
	try{_script.defer="defer";}catch(err){}
	_script.src = url;
	document.getElementsByTagName('head')[0].appendChild(_script);
}

function _ChatTest(){
	if((new Date())-last_action_date> visit_timeout){
		return;
	}
	var _url = woopra_engine_host + '/ping/cookie='+_GetCookie('wooTracker')+'&ra='+_GetRandomString();
	_Request(_url);
}

function _InitChat(msg,owner,avatar){
	chat_init=new _Chat_Init(msg,owner,avatar);
	var _url= 'http://woopra.com/_servers/w_chat.js';
	_Request(_url);
}

function WoopraEvent(typ, val){
	var _url= woopra_engine_host + '/customevent/cookie=' + _GetCookie('wooTracker') + '&type=' + typ+ '&name='+ val + '&ra='+_GetRandomString();
	_Request(_url);
}

function _ReqTrack(){
	var woopra_request=new Array();

	woopra_request['sessioncookie']=_GetCookie('sessionCookie');
	woopra_request['cookie']=_GetCookie('wooTracker');
	woopra_request['browsertoken']=_GetNavigatorToken();
	woopra_request['platformtoken']=navigator.platform;
	woopra_request['language']=_GetLanguageToken();
	woopra_request['pagetitle']=document.title;
	woopra_request['referer']=document.referrer;
	woopra_request['screen']=screen.width + 'x' + screen.height;
	woopra_request['localtime']=woopra_date.getHours()+':'+woopra_date.getMinutes();

      if(typeof woopra_array!='undefined'){
		for (var woo_key in woopra_array){
			if(typeof woopra_array[woo_key]!='function'){
				woopra_request['cv_'+woo_key]=woopra_array[woo_key];
			}
		}
	}
	if(typeof woopra_visitor!='undefined'){
		for (var woo_key in woopra_visitor){
			if(typeof woopra_visitor[woo_key]!='function'){
				woopra_request['cv_'+woo_key]=woopra_visitor[woo_key];
			}
		}
	}
	if(typeof woopra_event!='undefined'){
       	for (var woo_key in woopra_event){
			if(typeof woopra_event[woo_key]!='function'){
				woopra_request['ev_'+woo_key]=woopra_event[woo_key];
			}
		}
	}
    	var woo_req_url="";
    	for (var woo_key in woopra_request){
		if(typeof woopra_request[woo_key]!='function'){
			woo_req_url=woo_req_url+"&"+woo_key+"="+encodeURIComponent(woopra_request[woo_key])+"\n";
		}
	}
	//secure : woopra_script.src = 'http://192.168.0.150:8740/SecureD/visit.jsp?'+'ra='+_GetRandomString()+woo_req_url ;
	var _url = woopra_engine_host + '/visit/'+'ra='+_GetRandomString()+woo_req_url ;
	_Request(_url);
}


function _InitListeners(){
	if(document.addEventListener){
	    document.addEventListener("mousedown",_TrackClick,false);
	    document.addEventListener("mousemove",_ResetIdle,false);
	}
	else{
	    document.attachEvent("onmousedown",_TrackClick);
	    document.attachEvent("onmousemove",_ResetIdle);
	}
}

_InitListeners();
setTimeout('_CheckIdle()',5 * 1000);
setTimeout('_ReqTrack()',50);


//etc
function _ShowConversation(){
	var url=woopra_engine_host + '/serverfiles/chatframe.html?cookie='+_GetCookie('wooTracker')+'&host='+window.location.host+"&ra="+_GetRandomString();
	var newwindow=window.open(url,'name','height=400,width=350,resizable=1,statusbar=0,location=0,scrollbars=0,toolbar=0,directories=0,menubar=0');
	if (!newwindow.opener) newwindow.opener = self;
	if (window.focus) {newwindow.focus()}
}
