// ShillerMath 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: 01/08/2008 11:59:00
// Last modified: 01/08/2009 12:15:00

function smTrack(){
  var that=this;
  // begin: user modifiable
  this.onsitedoms="shillermath.com";
  this.downloadtypes="pdf,wmv,swf,rss,mp3,xml,jpg"; //not used at the moment
  this.paramtypes="";
  // identify clicks on download types based on query parameters
  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}):"");})();
}

smTrack.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;
}
smTrack.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;
}
smTrack.prototype.dcsEvt=function(evt,tag){
  var e=evt.target||evt.srcElement;
  while (e.tagName&&(e.tagName!=tag)){
    e=e.parentElement||e.parentNode;
  }
  return e;
}
smTrack.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));
    }
  }
}
smTrack.prototype.dcsET=function(){
  var e=(navigator.appVersion.indexOf("MSIE")!=-1)?"click":"mousedown";
  this.dcsBind(e,this.dcsWire);
//  this.dcsBind(e,this.dcsOffsite);
}

smTrack.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.
smTrack.prototype.dcsWire=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 gati="";
	   var el = e.href;		

	       //attach tracking to print links
	       if (el.indexOf("window.print")!=-1) {
		  gati="/print/"+document.title;
		    pageTracker._trackPageview(gati);
			return;
		}

		if(el.indexOf("javascript:popUp")!=-1) {
		var re = "[\"\']([^\"\']+)";
		var regex = new RegExp(re);
		var result = regex.exec(el);
		if (result)
		   el=result[1];
		}

	  var type=0;

	     if (el.indexOf("http")==-1)
		 type = 1; //relative onsite link
	     if (el.indexOf("shillermath.com")!=-1)
		 type = 2; //absolute onsite link
	     if (el.indexOf("shillermath.com")==-1
		  && el.indexOf("http")!=-1)
		 type = 3; //offsite link

		if (type==1||type==2) {
    		if ( 
			el.indexOf(".pdf")!=-1 || el.indexOf(".mp3")!=-1 
		    || el.indexOf(".wmv")!=-1 || el.indexOf(".swf")!=-1 
		    || el.indexOf(".jpg")!=-1 || el.indexOf(".rss")!=-1
		    || el.indexOf(".xml")!=-1 
	       );
		else
		  return;

		//prepend backslash to onsite relative
		if (type==1) {
		if (el.indexOf("/")!=0)
		   gati = el.substr(0);
		}

		//grab extension from onsite links
		var re = "^.*\.(...)$";
		var regex = new RegExp(re);
		var result = regex.exec(gati);
		if (result)
		   gati = "/"+result[1]+"/"+el.replace(/\//g, "("); //strip slashes

		} //end spoof onsite link url
	
		if (type==3) { //offsite link
			var re = "\/\/(.+)$";
			var regex = new RegExp(re);
			var result = regex.exec(el);

		  if (result) {
		     gati = result[1].replace(/\//g, "("); //strip slashes
		     gati="/offsite/"+gati;
		     gati=gati.substr(0);
		  }

		} //end offsite link

             //register virtual pageview
              if (gati)
 	   	   pageTracker._trackPageview(gati);

    } //end if e.href
  }
}

// Code section for Track clicks to links leading offsite.
smTrack.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){
		if(e.href.indexOf("javascript:popUp")!=-1) {
		var re = "[\"\']([^\"\']+)";
		var regex = new RegExp(re);
		var result = regex.exec(e.href);
		if (result)
		var gati="Offsite: "+result[1];	   	
	}
	else {
        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;
	   var gati="/offsite/"+hn+pth;
	}
		if (gati) pageTracker._trackPageview(gati);
        }
    }
  }
}

smTrack.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());
}
smTrack.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;
}

