/**
* Google Analytics for Preston Bailey
* $Id$
*/

var Metrics = {
init: function() {
  if (typeof(pageTracker) != 'undefined') {
    this.on = true;
  this.findExit();
  } else {
    this.on = false;
  }
},
findExit: function() {
  if ($pbj('.generic-page-container').hasClass('generic-page-container') && $pbj('#generic-post').html() != null) {
  var linkObj = $pbj('#generic-post').find('a');
    for (k=0;k<linkObj.length;k++) {
      if (linkObj[k].href.indexOf(document.location.hostname) < 1 && linkObj[k].href.length > 3) {
        $pbj(linkObj[k]).bind('click',
          function() {
            Metrics.exit(Metrics.replaceURL(this.href));
          } // end bind function
        ); // end bind
        $pbj(linkObj[k]).attr('target','_blank');
      } // end if link is exit
    } // end for k
  } // end if
    if ($pbj('.site-content').hasClass('site-content') && $pbj('.site-content').html() != null) {
  linkObj = $pbj('.site-content').find('a');
    for (k=0;k<linkObj.length;k++) {
      if (linkObj[k].href.indexOf(document.location.hostname) < 1 && linkObj[k].href.length > 3) {
        $pbj(linkObj[k]).bind('click',
          function() {
            Metrics.exit(Metrics.replaceURL(this.href));
          } // end bind function
        ); // end bind
        $pbj(linkObj[k]).attr('target','_blank');
      } // end if link is exit
    } // end for k
  } // end if
},
exit: function(URL) {
  var exit_url = this.replaceURL(URL);
  this.trackPageView('/exit/' + exit_url);
},
sliderShrink: function(section) {
  this.trackEvent('flash slide','shrink',section);
},
sliderExpand: function(section) {
  this.trackEvent('flash slide','expand',section);
},
bookmark: function() {
  this.trackEvent('bookmark','click',document.location.pathname);
},
subscribeRSS: function() {
  this.trackEvent('subscribe','RSS',document.location.pathname);
},
favorite: function(section) {
  this.trackEvent('favorite',section,document.location.pathname);
},
replaceURL: function(URL) {
  var string = URL.replace(/^http:\/\//,'');
  return string;
},
trackNewsletterSignup: function() {
  this.trackVar(1,'Newsletter Signup','true',2);
  this.trackEvent('form','submit','newsletter signup');
},
removeHostname: function(URL) {
  // note, this is after the 'http://' is removed
  var test = URL.split('/',1);
  var final_url = URL.replace(test.toString(),'');
  if (final_url.length > 1) {
    return final_url;
  } else {
    return '';
  }
},
trackPageView: function(URL) {
  if (this.on == true) {
    pageTracker._trackPageview(URL);
  }
},
trackEvent: function(category,action,label) {
  if (this.on == true) {
    if (category && action && label) {
      pageTracker._trackEvent(category,action,label);
    }
    else if (category && action) {
      pageTracker._trackEvent(category,action);
    }
    else {
      pageTracker._trackEvent(category);
    }
  }
},
trackVar: function(index,name,value,opt_scope) {
  if (this.on == true) {
    if (index && name && value && opt_scope) {
      pageTracker._setCustomVar(index,name,value,opt_scope);
    }
    else if (index && name && value) {
      pageTracker._setCustomVar(index,name,value);
    }
    else if (index && name) {
      pageTracker._setCustomVar(index,name,'true');
    }
    else {
    }
  }
},
zzz: function() {}
};

