/*
Copyright AG2 Agência de Inteligência Digital S.A.
Criado por Cássio Almeida
Versão: 4 - 11/08/2008
MULTISITES - REMOTO e SID
Registro de informações adicionais
*/
var qssl = (("https:" == document.location.protocol) ? "https:" : "http:");

if(trk_onload){
	function WindowOnload(oNewOnloadFunction) {
		var oPreviousOnloadFunction = window.onload;
		window.onload = function() {
			if (oPreviousOnloadFunction) { oPreviousOnloadFunction(); }
			oNewOnloadFunction();
		}
	}	
	function trk_init(){
		faz_tracking();
	} 
	WindowOnload(trk_init);
}
if(typeof prm_flw=="undefined"){
	prm_flw=trk_url;
}

trk_url=qssl+"//iv30.popstats.com.br/"+trk_url+"/";

var trk_url_dir=""
var trk_fct=""
var sessaotp=1800;
document.write( '<div id="trk" style="position:absolute; left:0px; top:0px; width:1px; height:1px; z-index:1"></div>' );
function include(script_filename){
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}
include(trk_url+"paginas.js");
function setVisitorID(semail, semail_ant, suserID,resx,resy,versao) { 
	if(trk_pid>0){
		var today = new Date();
		var zero_date = new Date(0,0,0); 
		today.setTime(today.getTime() - zero_date.getTime()); 
		var cookie_expire_date = new Date(today.getTime()); 
		cookie_expire_date = new Date(); 
		cookie_expire_date.setTime(cookie_expire_date.getTime()+(90*24*60*60*1000));
		Set_Cookie('obtxEMAIL',semail,cookie_expire_date,"/"); 
		Set_Cookie('obtxuserID',suserID,cookie_expire_date,"/"); 
		cookie_expire_date = new Date();
		cookie_expire_date.setTime(cookie_expire_date.getTime()+(0.5*60*60*1000));
		setSessionID()
		trk_tp_atu=new Date();
		if(Math.ceil((trk_tp_atu.getTime()-millennium.getTime())/1000-trk_tp_ant)>sessaotp){
			sessao=sessao+'x';
		}
		Set_Cookie('trk_tp_ant',Math.ceil((trk_tp_atu.getTime()-millennium.getTime())/1000),'',"/");
		Set_Cookie('SessionID',sessao,cookie_expire_date,"/"); 
		email_ant=semail_ant;
		userId=suserID;
		trk_email=semail;
		pic1= new Image(1,1); 
		if( typeof trk_fct!="undefined" && trk_fct!=""){
			pic1.onload=eval(trk_fct);
		}else{
			pic1.onload="";
		}		
		
		trk_fct=""
		
		url_dest=trk_url+'trk_api.asp?em=' + trk_email + '&ema=' + email_ant + '&ss=' + sessao + '&uId=' + userId + '&mid=' + smid + '&pid=' + trk_pid + '&siteid=' + trk_siteid + '&resx=' + resx + '&resy=' + resy + '&ver=' + versao + "&utm_source=" + trk_source + "&utm_medium=" + trk_medium + "&utm_term=" + trk_term + "&utm_content=" + trk_content + "&utm_campaign=" + trk_campaign + "&referer=" + escape(document.referrer)
		trk_campaign='';
		trk_source='';
		trk_medium='';
		trk_term='';
		trk_content='';

		if( typeof trk_info=="number" || typeof trk_info=="string" ){
			for(_i=1;_i<100;_i++){
				if( _i<=trk_info){
					if(_i>1) url_dest+='||'; 
					else url_dest+='&info=';
					url_dest+=eval('trk_info'+_i);
				}else
					_i=1000
			}
			trk_info=0;
		}else if(typeof trk_info=="object"){
			for (_x in trk_info){
				if(_x>0) url_dest+='||';
				else url_dest+='&info=';
				url_dest+=trk_info[_x]; 
			}
			trk_info=null;
		}
			
		if(prm_flw=='debug' || userId=='0855710255'){
			window.open(url_dest + "&cache=" + parseInt(Math.random()*1000000000,10))
		}else{
			if(trk_url_dir==""){
				trk_ga(trk_pid)
				if(trk_popstats) {
					pic1.src= url_dest + "&cache=" + parseInt(Math.random()*1000000000,10);
				}
			}else{
				click_pop(trk_pid, trk_url_dir);
			}
		}
	}
} 

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_setTextOfLayer(objName,x,newText) { //v4.01
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (document.layers) {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}
function setSessionID() { 
	sessao=Get_Cookie('SessionID');
	if (!sessao) {
		sessao=parseInt(Math.random()*1000000000,10);
		cookie_expire_date = new Date();
		cookie_expire_date.setTime(cookie_expire_date.getTime()+(0.5*60*60*1000));
		Set_Cookie('SessionID',sessao,cookie_expire_date,"/"); 
	}
	Set_Cookie('obtxSMID',smid,'',"/"); 
} 
function novoUserId() { 
	return(parseInt(Math.random()*1000000000,10));
} 
function Get_Cookie(name) { 
   var start = document.cookie.indexOf(name+"="); 
   var len = start+name.length+1; 
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
   if (start == -1) return null; 
   var end = document.cookie.indexOf(";",len); 
   if (end == -1) end = document.cookie.length; 
   return unescape(document.cookie.substring(len,end)); 
} 
function Set_Cookie(name,value,expires,path,domain,secure) { 
    var cookieString = name + "=" +escape(value) + 
       ( (expires) ? ";expires=" + expires.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : ""); 
    document.cookie = cookieString; 
} 
function FlashCookie(semail,semail_ant,modo,sessao,suserID){
	var fm=getFlashMovieObject("trk_fl");
	fm.trk(semail, semail_ant, sessao, suserID, prm_flw);
	semail=fm.GetVariable("/:email");
	semail_ant=fm.GetVariable("/:email_ant");
	suserID=fm.GetVariable("/:userID");
	resx=fm.GetVariable("/:resx");
	resy=fm.GetVariable("/:resy");
	versao=fm.GetVariable("/:versao");
	setVisitorID(semail, semail_ant, suserID,resx,resy,versao)
}
function detectFlash(){
	var flashinstalled = 0;
	if (navigator.plugins && navigator.plugins.length){
		x = navigator.plugins["Shockwave Flash"];
		if (x){
			return true;
		}else{
			return false;
		}
		if (navigator.plugins["Shockwave Flash 2.0"]){
			return true;
		}
	}else if (navigator.mimeTypes && navigator.mimeTypes.length){
		x = navigator.mimeTypes['application/x-shockwave-flash'];
		if (x && x.enabledPlugin)
			return true;
		else
			return false;
	}
	return true;
}
function getFlashMovieObject(movieName){
  if (window.document[movieName]){
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1){
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }else{ // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    return document.getElementById(movieName);
  }
}
function getVar(name){
	get_string = document.location.search;         
	return_value = '';
	name_index = get_string.indexOf('?' + name + '=');
	if(name_index == -1){
		name_index = get_string.indexOf('&' + name + '=');
	}
	if(name_index != -1){
		get_string = get_string.substr(name_index + name.length + 2, get_string.length - name_index);
		end_of_value = get_string.indexOf('&');
		if(end_of_value != -1) value = get_string.substr(0, end_of_value);                
		else value = get_string;                
		return_value = value;
	}
	space = return_value.indexOf('+');
	while(space != -1){ 
		return_value = return_value.substr(0, space) + ' ' + 
		return_value.substr(space + 1, return_value.length);
		space = return_value.indexOf('+');
	}
	return(return_value);        
}
var trk_tp_ant=Get_Cookie('trk_tp_ant')
var millennium=new Date(2000, 0, 1)
if(var_nula(trk_tp_ant)){
	trk_tp_ant=new Date();
	Set_Cookie('trk_tp_ant',Math.ceil((trk_tp_ant.getTime()-millennium.getTime())/1000),'',"/");
}
var trk_pid_temp=getVar("pid");
if(!var_nula(trk_pid_temp)){
	var trk_pid=trk_pid_temp;
}
if(var_nula(trk_pid)){
	var trk_pid=pid;
}
if(typeof trk_siteid=="undefined"){
	var trk_siteid=siteid;
}
if(typeof trk_email=="undefined"){
	if(typeof email=="undefined"){
		var trk_email='';
	}else{
		var trk_email=email;
	}
}
if(typeof trk_popstats=="undefined"){
	var trk_popstats=true;
}

if(var_nula(trk_siteid))
	trk_siteid=0;
if(!var_nula(trk_pid)){
	var smid=getVar("mid");
	if(var_nula(smid))
		smid=Get_Cookie('obtxSMID');
	if(var_nula(smid)){
		smid=0;
	}
	var trk_campaign=getVar('utm_campaign');
	var trk_source=getVar('utm_source');
	var trk_medium=getVar('utm_medium');
	var trk_term=getVar('utm_term');
	var trk_content=getVar('utm_content');
	if(trk_campaign=='') trk_campaign=getVar('s_cid');
	if(trk_campaign=='') trk_campaign=getVar('s_kwcid');
	if(trk_campaign>'' && smid==0)
		smid=1;

	var email_ant=Get_Cookie('obtxEMAIL');
	if(var_nula(email_ant))
		email_ant='tracking@anonimo.com.br';
		
	if(var_nula(trk_email)){
		var sid=getVar("sid");
		if(var_nula(sid) || sid=="0"){
			sid=0;
			var trk_email=getVar("em");
		}else
			var trk_email=sid;
		if(var_nula(trk_email))
			trk_email=Get_Cookie('obtxEMAIL');
		if(var_nula(trk_email))
			trk_email="tracking@anonimo.com.br";
	}
		
	var sessao="";
	var	userId="";
	userId=Get_Cookie('obtxuserID');
	if(var_nula(userId))
		userId=novoUserId();
	setSessionID();
}
temp='<object id="trk_fl" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=""'+qssl+'//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="1" height="1">';
temp+='  <param name="movie" value="'+qssl+'//iv30.popstats.com.br/lib/fltrk4.swf?email=' + trk_email + '&email_ant=' + email_ant + '&modo=0&sessao=' + sessao + '&userID=' + userId + '&trk_url=' + prm_flw + '">';
temp+='  <param name="quality" value="high">';
temp+='  <param name="allowScriptAccess" value="always"/>';
temp+='  <embed swliveconnect="true" name="trk_fl" src="'+qssl+'//iv30.popstats.com.br/lib/fltrk4.swf?email=' + trk_email + '&email_ant=' + email_ant + '&modo=0&sessao=' + sessao + '&userID=' + userId + '&trk_url=' + prm_flw + '" quality="high" pluginspage=""'+qssl+'//www.macromedia.com/go/getflashplayer" allowScriptAccess="always" type="application/x-shockwave-flash" width="1" height="1"></embed>';
temp+='</object>';
MM_setTextOfLayer('trk','',temp)
function var_nula(qual){
	if(typeof qual=="undefined")
		return true;
	qual=qual+'-';
	if( qual=='-' || qual=='undefined-' || qual=='null-')
		return true;
	else
		return false;
}
function faz_tracking(){
	if(!var_nula(trk_pid)){	
		if(detectFlash()){
			if(trk_email!=email_ant){
				FlashCookie(trk_email,email_ant,1,sessao,userId); 
			} else {
				FlashCookie(trk_email,email_ant,0,sessao,userId);
			}
		}else{
			setVisitorID(trk_email, email_ant, userId);
		}
	}
}
function click_pop(qp, url){
	trk_pid=qp;
	trk_fct="self.location='" + url + "'";
	FlashCookie(trk_email,email_ant,0,sessao,userId);
}
function click_pop_window(qp, url, janela, prm){
	trk_pid=qp;
	trk_fct="nwin1 = window.open('" + url + "','" + janela + "','"  + prm + "')";
	FlashCookie(trk_email,email_ant,0,sessao,userId);
}
function click_flash(qp){
	trk_pid=qp;
	FlashCookie(trk_email,email_ant,0,sessao,userId);
}
function click_funcao(qp, fct){
	trk_pid=qp;
	trk_fct=fct;
	FlashCookie(trk_email,email_ant,0,sessao,userId);
}
function click_flash_email(qp,tem){
	trk_pid=qp;
	trk_email=tem;
	email_ant=Get_Cookie('obtxEMAIL');
	if(var_nula(email_ant))
		email_ant='';
	FlashCookie(trk_email,email_ant,0,sessao,userId);
}
function click_info(idx, qinfo){
	if(idx==0)
		trk_info=new Array();
	trk_info[idx]=qinfo; 
}
function paginas(qp){
	if(var_nula(qp))
		tt='';
	else
		if(typeof eval("window.pp_" + qp)!="undefined")
			tt=eval("window.pp_" + qp);
		else
			tt='';
	return tt;
}
function trk_ga(qp){
	if(!var_nula(getVar("trk_ip"))) {
		self.status=qp+' - ' + paginas(qp);
		alert(qp+' - ' + paginas(qp));
	}
	if(typeof urchinTracker!="undefined"){
		if(typeof trk_loaded!="undefined"){
			urchinTracker(paginas(qp));
		}		else{
			setInterval('trk_ga(' + qp + ')', 1000);
		}
	}
}

