var Tracking = {
  cookiePrefix: '__tar_',
  
  getParam: function(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    
    if( results == null ) {
      return null;
    } else {
      return results[1];
    }
  },
  
  readURL: function() {
    var urlSource = this.getParam("source");
    var cookieSource = this.get("src");
    var source = "";

    /**
     * If there is a URL source and a cookie source, the URL source takes
     * precedence and wipes out whatever is in the src cookie.  Otherwise,
     * read from the cookie.
     **/
    if (urlSource != null) {
      source = urlSource;
      this.updateCookie("src", urlSource);
    } else if (cookieSource != null) {
      source = this.get("src");
    }

    /**
     * If there is a URL addsrc argument, prepend the source cookie with
     * whatever source code is given.
     **/
    var asource = this.getParam("addsrc");
    if (asource != null && asource != "") {
      if (source.indexOf(asource) == -1) {
	source = asource + "--" + source;
	this.updateCookie("src", source);
      }
    }

    /**
     * Inspiration keys are sources that specifically come from the invitation
     * of another user.  This is the method for assigning credit to users
     * for recruiting friends.
     **/
    var inspirationSource = this.getParam("inspiration");
    if (inspirationSource != null && inspirationSource != "") {
      this.updateCookie('insp', inspirationSource);
    }
    
  },
  
  init: function() {
    if (document.domain == 'localhost') {
      this.cookieDomain = '';
    } else {
      var dcs = document.domain.split('.');
      var sdomain = dcs[dcs.length - 1];
      if (dcs.length >= 2) {
	sdomain = dcs[dcs.length - 2] + '.' + sdomain;
      }
      this.superDomain = sdomain;
      if (dcs.length > 2) {
	this.cookieDomain = '.' + sdomain;
      } else {
	this.cookieDomain = sdomain;
      }
    }

    this.options = { expires: 2 * 365,
		     path: '/', 
		     domain: this.cookieDomain };
  },

  get: function(name) {
    var prefix = this.cookiePrefix + name;
    return jQuery.cookie(prefix);
  },
  
  setCookie: function(name, value, options)  {
    jQuery.cookie('' + name, '' + value, options);
  },
  
  updateCookie: function(name, value) {
    var prefix = this.cookiePrefix + name;
    var options = this.options;
    this.setCookie(prefix, '' + value, options);
  },

  recordPageView: function() {
    var prevCount = this.get('pv');
    if (prevCount != null && prevCount != "") {
      var count = parseInt(prevCount) + 1;
      this.updateCookie('pv', '' + count);
    } else {
      var count = 1;
      this.updateCookie('pv', '' + count);
    }
    },

  recordFormStep: function(step) {
        $.post("/formStep", { 
		user_description: this.get('uexp'),
		source: this.get('src'),
		inspiration: this.get('insp'),
		step: step },
	    function (xml) {});
    },

  recordCustomization: function(field, value) {
	customization_cookie = this.get('uexp');
	if (customization_cookie == null) {
	    customization_cookie = "";
	}
	subcookies = customization_cookie.split('|');
	
	result_cookie = "";
	replaced = false;
	for (var i = 0; i < subcookies.length; i++) {
	    if (subcookies[i] != null && subcookies[i] != "") {
		if (subcookies[i].indexOf(field) == 0) {
		    result_cookie = result_cookie + "|" + field + '=' + value;
		    replaced = true;
		} else {
		    result_cookie = result_cookie + '|' + subcookies[i];
		}
	    }
	}

	if (!replaced) {
	    result_cookie = result_cookie + '|' + field + '=' + value;
	}

	this.updateCookie('uexp', result_cookie);
    }
};
