// Corpse Additional Tracking code by Plamen Arnaudov
// event tracking based on WebTrends SmartSource Data Collector Tag Version: 8.5.0     
// final tracking call performed by Google Analytics
// Created: 12/10/2008 11:59:00
// Last modified: 12/10/2008 12:15:00

function corpseTrack(){
  var that=this;
  // begin: user modifiable
  this.onsitedoms="corpse.org,exquisitecorse.org";
  this.downloadtypes="xls,doc,pdf,txt,csv,zip";
  // identify clicks on download types based on query parameters
  this.paramtypes="pdf,emailform";
  this.trackevents=true;
  this.enabled=true;
  this.i18n=false;
  this.exre=(function(){return(window.RegExp?new RegExp("dcs(uri)|(ref)|(aut)|(met)|(sta)|(sip)|(pro)|(byt)|(dat)|(p3p)|(cfg)|(redirect)|(cip)","i"):"");})();
  this.re=(function(){return(window.RegExp?(that.i18n?{"%25":/\%/g}:{"%09":/\t/g,"%20":/ /g,"%23":/\#/g,"%26":/\&/g,"%2B":/\+/g,"%3F":/\?/g,"%5C":/\\/g,"%22":/\"/g,"%7F":/\x7F/g,"%A0":/\xA0/g}):"");})();
}

corpseTrack.prototype.dcsIsOnsite=function(host){
  if (host.length>0){
      host=host.toLowerCase();
      if (host==window.location.hostname.toLowerCase()){
        return true;
      }
      if (typeof(this.onsitedoms.test)=="function"){
        return this.onsitedoms.test(host);
      }
      else if (this.onsitedoms.length>0){
        var doms=this.dcsSplit(this.onsitedoms);
        var len=doms.length;
        for (var i=0;i<len;i++){
          if (host==doms[i]){
              return true;
          }
        }
      }
  }
  return false;
}
corpseTrack.prototype.dcsTypeMatch=function(pth, qry, typelist){
  var type=pth.substring(pth.lastIndexOf(".")+1,pth.length);
  var types=this.dcsSplit(typelist);
  var tlen=types.length;  
  for (var i=0;i<tlen;i++){
    if (type==types[i]){
      return types[i];
    }
  }
  var types=this.dcsSplit(this.paramtypes);
  var tlen=types.length;  
  for (var i=0;i<tlen;i++){
    if (qry.indexOf(types[i]) != -1) {
	return types[i];
    }
  }
  return false;
}
corpseTrack.prototype.dcsEvt=function(evt,tag){
  var e=evt.target||evt.srcElement;
  while (e.tagName&&(e.tagName!=tag)){
    e=e.parentElement||e.parentNode;
  }
  return e;
}
corpseTrack.prototype.dcsBind=function(event,func){
  if ((typeof(func)=="function")&&document.body){
    if (document.body.addEventListener){
      document.body.addEventListener(event, func.wtbind(this), true);
    }
    else if(document.body.attachEvent){
      document.body.attachEvent("on"+event, func.wtbind(this));
    }
  }
}
corpseTrack.prototype.dcsET=function(){
  var e=(navigator.appVersion.indexOf("MSIE")!=-1)?"click":"mousedown";
  this.dcsBind(e,this.dcsDownload);
  this.dcsBind(e,this.dcsOffsite);
}

corpseTrack.prototype.dcsSplit=function(list){
  var items=list.toLowerCase().split(",");
  var len=items.length;
  for (var i=0;i<len;i++){
    items[i]=items[i].replace(/^\s*/,"").replace(/\s*$/,"");
  }
  return items;
}
// Code section for Track clicks to download links.
corpseTrack.prototype.dcsDownload=function(evt){
  evt=evt||(window.event||"");
  if (evt&&((typeof(evt.which)!="number")||(evt.which==1))){
    var e=this.dcsEvt(evt,"A");
    if (e.href){
        var hn=e.hostname?(e.hostname.split(":")[0]):"";
        var qry=e.search?e.search.substring(e.search.indexOf("?")+1,e.search.length):"";
	 var dtype=this.dcsTypeMatch(e.pathname,qry,this.downloadtypes);
        if (this.dcsIsOnsite(hn)&&dtype){
            var pth=e.pathname?((e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname):"/";
            var ttl="";
            var text=document.all?e.innerText:e.text;
            var img=this.dcsEvt(evt,"IMG");
            if (img.alt){
              ttl=img.alt;
            }
            else if (text){
              ttl=text;
            }
            else if (e.innerHTML){
              ttl=e.innerHTML;
            }
	     //for now, set event category to download type
	       ttl=dtype;
		var id=getParam("id", qry);
		id?id="id="+id:id="id=unknown";

			var t = document.title; document.title=document.title.replace(/Exquisite Corpse - Journal of Letters and Life - /, "");
		pageTracker._trackEvent(ttl, "click", id, 1);
			document.title=t;
        }
    }
  }
}
// Code section for Track clicks to links leading offsite.
corpseTrack.prototype.dcsOffsite=function(evt){
  evt=evt||(window.event||"");
  if (evt&&((typeof(evt.which)!="number")||(evt.which==1))){
    var e=this.dcsEvt(evt,"A");
    if (e.href){
        var hn=e.hostname?(e.hostname.split(":")[0]):"";
        var pr=e.protocol||"";
        if ((hn.length>0)&&(pr.indexOf("http")==0)&&!this.dcsIsOnsite(hn)){
          var qry=e.search?e.search.substring(e.search.indexOf("?")+1,e.search.length):"";
          var pth=e.pathname?((e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname):"/";
          var trim=true;
	//alert("Offsite link: "+hn+pth+qry);
        }
    }
  }
}

corpseTrack.prototype.dcsAdv=function(){
  if (this.trackevents&&(typeof(this.dcsET)=="function")){
    if (window.addEventListener){
      window.addEventListener("load",this.dcsET.wtbind(this),false);
    }
    else if (window.attachEvent){
      window.attachEvent("onload",this.dcsET.wtbind(this));
    }
  }
  //this.dcsFunc(this.dcsFPC());
}
corpseTrack.prototype.dcsEscape=function(S, REL){
  if (REL!=""){
    S=S.toString();
    for (var R in REL){
      if (REL[R] instanceof RegExp){
        S=S.replace(REL[R],R);
      }
    }
    return S;
  }
  else{
    return escape(S);
  }
}
dcsA=function(N,V){
  if (i18n&&(exre!="")&&!exre.test(N)){
    if (N=="dcsqry"){
      var newV="";
      var params=V.substring(1).split("&");
      for (var i=0;i<params.length;i++){
        var pair=params[i];
        var pos=pair.indexOf("=");
        if (pos!=-1){
          var key=pair.substring(0,pos);
          var val=pair.substring(pos+1);
          if (i!=0){
            newV+="&";
          }
          newV+=key+"="+dcsEncode(val);
        }
      }
      V=V.substring(0,1)+newV;
    }
    else{
      V=dcsEncode(V);
    }
  }
  return "&"+N+"="+this.dcsEscape(V, this.re);
}
dcsEncode=function(S){
  return (typeof(encodeURIComponent)=="function")?encodeURIComponent(S):escape(S);
}
Function.prototype.wtbind = function(obj){
  var method=this;
  var temp=function(){
    return method.apply(obj,arguments);
  };
  return temp;
}
// -- Custom functions - Plamen

function getParam (name, source) {  
//returns an arbitrary query parameter's value
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
var regexS = "[\\?&]"+name+"=([^&#]*)";  
var regex = new RegExp( regexS );  
var results = regex.exec(source); 
 
  if( results == null )    
    return "";  
  else    
    return results[1];
}

function isSearchEngine (uri_stem) { 
//detects search engine. if recognized,
//returns its search phrase query parameter

knownSE=[["yahoo","p"],["search.msn","q"],["search.aol","query"],[".abcsok.","q"],["lycos","query"],["ask","q"],["altavista","q"],["altavista","r"],["netscape","query"],["cnn","query"],["looksmart","qt"],["about","terms"],["mamma","query"],["alltheweb","q"],["gigablast","q"],["voila","rdata"],["virgilio","qs"],["live","q"],["baidu","wd"],["alice","qs"],["yandex","text"],["najdi","q"],["aol","q"],["club-internet","query"],["mama","query"],["seznam","q"],["search","q"],["wp","szukaj"],["onet","qt"],["netsprint","q"],["google","q"],["szukacz","q"],["yam","k"],["pchome","q"],["kvasir","searchExpr"],["sesam","q"],["ozu","q"],["terra","query"],["nostrum","query"],["mynet","q"],["ekolay","q"],["search.ilse","search_for"],["naver","query"],["netfind","query"],["overture.com","Keywords"],["excite.com","q"]];

  for ( i=0; i < knownSE.length;i++) {
      if (uri_stem.indexOf(knownSE[i][0]) != -1)
    result = getParam(knownSE[i][1], _tag.DCS.dcsref);
          else 
    result = "";

      if (result) return result; 
    //complete match only if both se and its parameters
    //have been recognized
  }
  return result;
}

// end custom functions

var _tag = new corpseTrack();
_tag.dcsAdv();

//on-load Google Analytics call
try {
var pageTracker = _gat._getTracker("UA-6307833-1");
if (location.search.indexOf("searchword=")!= -1) 
  pageTracker._trackPageview("/sitesearch?keywords="+getParam("searchword",location.search));
else {
var ti=document.title.match(/^.+-.+-(.*)$/);
if(ti) ti="/"+ti[1].replace(/ /g, "_");
var t = document.title; document.title=document.title.replace(/Exquisite Corpse - Journal of Letters and Life - /, "");
ti?pageTracker._trackPageview(ti):pageTracker._trackPageview();
document.title=t;
}
} catch(err) {}