/*
  parseUri 1.2.1
  (c) 2007 Steven Levithan <stevenlevithan.com>
  MIT License
*/
function parseUri (str) {
  var o   = parseUri.options,
    m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
    uri = {},
    i   = 14;

  while (i--) uri[o.key[i]] = m[i] || "";

  uri[o.q.name] = {};
  uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
    if ($1) uri[o.q.name][$1] = $2;
  });

  return uri;
};

parseUri.options = {
  strictMode: false,
  key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
  q:   {
    name:   "queryKey",
    parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  },
  parser: {
    strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
    loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
  }
};

function Track(param) {
  param = param || {};
  var me = this;
  this.url  = param.url  || parseUri(window.location);
  this.host = param.host || this.url.host;
  this.host = this.host.match(/dev\./) ? 'search.dev.resumebucket.com' : 'search.resumebucket.com';
  this.path = param.path || 'track';
  this.id   = param.id   || 'id_' + Math.ceil(100*Math.random());
  this.fields = {};
  this.fields['event'] = param.event || 'impression';
  this.createImage = function() {
    var img = document.createElement("img");
    img.id = this.id;
    img.style.width = '1px';
    img.style.height = '1px';
    document.body.appendChild(img);
    me.image = img;
    return img;
  };
  this.getImageSrc = function() {
    var src = me.url.protocol + '://' + me.host + '/' + me.path + '?';
    for( var p in me.fields ) {
      if(me.fields.hasOwnProperty(p)) {
        src += p + '=' + encodeURIComponent(me.fields[p]) + '&';
      }
    }
    return src;
  };
  this.createQuery = function() {
    var f = {};
    f['advertiser'] = me.url.queryKey['adsource'] || me.url.queryKey['referrer'] || 'organic';
    f['keywords'] = me.url.queryKey['keyword'] || '';
    f['sub_id1'] = me.url.host;
    f['sub_id2'] = me.url.queryKey['camp'] || '';
    f['sub_id3'] = me.url.queryKey['group'] || '';
    f['sub_id4'] = me.url.queryKey['creativeid'] || '';
    f['sub_id8'] = me.url.path || '';
    me.fields = f;
  };
  this.setEvent = function(i) {
    i = parseInt(i);
    me.fields['event'] = (i==1) ? 'impression' : 'click';
  };
  this.sendRequest = function(e) {
    me.createImage();
    me.image.src = me.getImageSrc();
  };
  this.setClickKeyword = function(kwd) {
    me.fields['sub_id7'] = kwd || '';
  };
  this.setClickedAd = function(id) {
    me.fields['sub_id6'] = id || '';
  };
  this.setClickedDiv = function(div) {
    me.fields['sub_id5'] = div || '';
  };
  this.trackImpression = function(params) {
    me.setEvent(1);
    me.setClickKeyword( params.keyword );
    me.setClickedAd();
    me.setClickedDiv();
    me.sendRequest();
  };
  this.trackClick = function(params) {
    me.setEvent(2);
    me.setClickKeyword( params.keyword );
    me.setClickedAd( params.id );
    me.setClickedDiv( params.div );
    me.sendRequest();
  };
  this.createQuery();
  return this;
};
