/**
 * @desc	This script is used on wildblue.com's site to send us visit information
 * @author 	rcurrington
 * @license	RedVentures
 * 
 */


/**
 * @desc	Object for RVTracker
 * 
 */
function RVTracker() {
	this._initialize();
}

/**
 * @desc	Initializes the object, reads in the rv cookie and stores it in a query string
 */
RVTracker.prototype._initialize = function() {
	 
	 if(document.location.href.indexOf("staging.") >= 0 || document.location.href.indexOf("test.") >= 0) {
		 this.src = 'http://staging.track.wildblue.redventures.com/track.php';
	 } else {
		 this.src = 'http://track.wildblue.com/track.php';
	 }
	
	this.tracked	= '';
	
	var rvCookieArr;
	var cookieArr	= document.cookie.split(';');

	for(var i=0;i < cookieArr.length; i++) {

		var cookie = cookieArr[i];
		// Trim cookie to get rid of left hand spaces
		cookie = cookie.replace(/^\s+/,"");
		
		keyValArr = cookie.split('=');
		key = keyValArr[0];
		val = keyValArr[1];

		if(key == "RVCookie") {
			
			val = unescape(cookie.substring(9));
			
			//alert(val);
			//val = unescape(val);
			
			rvCookieArr = val.split("|");
			for(var j=0;j < rvCookieArr.length; j++) {
				var rvcookie = rvCookieArr[j];
				// Trim cookie to get rid of left hand spaces
				rvcookie = rvcookie.replace(/^\s+/,"");
				keyValArr = rvcookie.split('=');
			
				key = keyValArr[0];
				val = keyValArr[1];
				this._setVar(key, val);
			}
		} else {
			this._setVar(key, val);
		}
	}
	
	this._setVar('url', location.href);
	
}

/**
 * @desc	Adds a key value pair to the query string
 */
RVTracker.prototype._setVar = function(key, value) {
	 //alert(key + val);
	 if( !( key && value )) {
		 return false;
	 }
  
	 this.tracked = this.tracked + '&' + escape(key) + '=' + escape(value); 

}

/**
 * @desc	Renders pixel out to page
 */
RVTracker.prototype._renderPixel = function() {
	 
	var pixel = new Image();
	pixel.src = this.src + '?' + this.tracked;
	pixel.height = "1";
	pixel.width = "1";
	document.body.appendChild(pixel);
}


//Execution code
if(document.cookie) {
	var RVTracker = new RVTracker();
	RVTracker._renderPixel();
}
