/**
 * Eperfect remote tracker
 *
 * @author Nick Nettleton, plumdigitalmedia.com
 * @copyright Plum Digital Media, 2005
 * @version 0.1
 * @package Eperfect
 */

var _ep = {} ;

// tracking data

_ep.cc		= '' ;			// campaign code		string
_ep.mc		= '' ;			// member code		string
_ep.click	= 1 ;			// this is a click			bool
_ep.event	= '' ;			// name of event to record	string
_ep.value	= 0 ;			// value of this event		double
_ep.url		= location.href ;	// url				string
_ep.title	= document.title ;	// title of the page		string
_ep.clientId = 0 ;
_ep.client_code= '' ;


// settings

_ep.timeout	= 15768000 ;		// timeout for tracking in seconds (6 months)	int
_ep.debug	= 0 ;			// whether to show debug output	bool
_ep.type	= 'gif' ;			// output type (gif|js)

// constants

_ep.TRACKERURL	= 'www.eperfectdata.com/tracker' ;	// url of tracker, without schema

// track method

_ep.track = function()
{
	// clear cc and mc

	this.cc = '' ;
	this.mc = '' ;

	// cc and mc in querystring?

	if(location.search){
		var qp = location.search.substr(1).split('&') ;
		var qs = [] ;
		for(var i=0, l=qp.length; i<l; i++){
			var p = qp[i].split('=') ;
			qs[p[0]] = p[1] ;
		}
		if(qs['_ep.cc'] && qs['_ep.mc']){ // yes - set and save to cookie
			this.cc	= qs['_ep.cc'] ;
			this.mc	= qs['_ep.mc'] ;
			// (2008-10-15) PT: cc and mc now set from the server at eperfectdata.com
			//var dt	= new Date() ;
			//dt.setTime(dt.getTime()+(this.timeout*1000));
			//var exp = dt.toGMTString() ;
			//document.cookie = escape('_ep.cc') + '=' + escape(this.cc) + '; expires=' + exp + '; path=/;' ;
			//document.cookie = escape('_ep.mc') + '=' + escape(this.mc) + '; expires=' + exp + '; path=/;' ;
		}
		qp = '' ; qs = '' ;
	}
 

	// otherwise, cc and mc in cookie?
    // (2008-10-15) PT: cc and mc now set from the server at eperfectdata.com
	/*
	if(!this.cc || !this.mc){
		if(document.cookie){
			var cp = document.cookie.split('; ') ;
			var ck = [] ;
			for(var i=0, l=cp.length; i<l; i++){
				var p = cp[i].split('=') ;
				ck[p[0]] = p[1] ;
			}
			if(ck['_ep.cc'] && ck['_ep.mc']){ // yes - set
				this.cc = ck['_ep.cc'] ;
				this.mc = ck['_ep.mc'] ;
			}
			cp = '' ; ck = '' ;
		}
	}
	*/
	
	// cc and mc not set? return
	// (2008-10-15) PT: cc and mc now set from the server at eperfectdata.com
	//if(!this.cc || !this.mc) return false ;
	
	if(!this.click && !this.value && !this.event) return true ;

	// call it
	// if we're in debug mode, always use js

	if(this.debug) this.type = 'js' ;

	// build tracking url

	var url = ((location.protocol == 'https:') ? 'https://' : 'http://')
		+ this.TRACKERURL + '?'
		+ 'cc=' + escape(this.cc) + '&'
		+ 'mc=' + escape(this.mc) + '&'
		+ 'click=' + parseInt(this.click) + '&'
		+ 'event=' + escape(this.event) + '&'
		+ 'clientId=' + parseInt(this.clientId) + '&'
		+ 'client_code=' + escape(this.client_code) + '&' 
		+ 'value= ' + parseFloat(this.value).toFixed(2) + '&'
		+ 'url=' + escape(this.url) + '&'
		+ 'title=' + escape(this.title) + '&'
		+ 'type=' + ((this.debug) ? 'js' : 'gif') + '&'
		+ 'debug=' + ((this.debug) ? 1 : 0) + '&'
		;
	
	switch(this.type){
	
		// handle js with script tag, for debugging
		case 'js' :
			var h ='<script type="text/javascript" src="' + url + '"></scr' + 'ipt>' ;
			document.write(h) ;
			break ;
			
		// handle all others with new Image()
		case 'gif' :
		case 'xml' :
		case 'txt' :
		default :
			var i = new Image(1, 1) ;
			i.src = url ;			
			i.onload = function(){ return ; }

	}

	return true ;
}
