///////
// gaAddons.js
// @Author Stphane Hamel - shamel@immeria.net - http://immeria.net
// See http://code.google.com/p/gaddons/people/list for contributors
//
// Google Groups forum: http://groups.google.com/group/gaaddons
// Google Code repository: http://code.google.com/p/gaddons/
//
///////
// License: Version: MPL 1.1
// The contents of this file are subject to the Mozilla Public License Version
// 1.1 (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// Changelog:
// v1.0 - feb. 24 2010 - delete code download and mailto because not use in pex
///////
/*
* @fileoverview Wrapper for gaAddons
*/
var gaA_pageTracker = pageTracker;

// record data type 
var gaA_dataType = 'url';
if( "gaAddonDataType" in window) {
  gaA_dataType = gaAddonDataType;
}

// get overture link data
var gaA_sponserHost     = 'rc20.overture.com';
// YST is need this param YSP is not need
var gaA_sponserHrefName = 'ss_name';
var gaA_hostType        = 'external';
if( "gaAddonHostType" in window) {
  gaA_hostType = gaAddonHostType;
}

/**
* @class ga Addons component.
* This class encapsulates all logic for the Google Analytics addons
* @constructor
*/
var gaAddons = function() {
   var getMetaKeyword = function() {
      var nodes = document.getElementsByTagName ('meta');
      for (var i = 0, I = nodes.length; i < I; i++) {
         if( nodes[i].name == 'keywords')
         {
            return nodes[i].content;
         }
      }
      return '';
   }
   /**
   * startListening: add a new listner for onclick event, handle Mozilla or IE methods
   * @param {Object} obj HREF object to listen to
   * @param {String} evnt event type (usually "click")
   * @param {Object} func Function to call when evnt is triggered
   */
   var startListening = function(obj, evnt, func) {
      if (obj.addEventListener) obj.addEventListener(evnt, func, false);
      else if (obj.attachEvent) obj.attachEvent("on" + evnt, func);
      }
   /**
   * trackLink: make GA call when clicking an outbound link, use trackEvent() or trackPageView() methods
   * @param {Object} evnt Object where the event happened
   */
   var trackLink = function(evnt) {
      evnt = evnt || event;
      var elmnt = evnt.srcElement || evnt.target;
      if (elmnt) {
         while (elmnt.tagName != "A") elmnt = elmnt.parentNode;
         if (/http/.test(elmnt.protocol)) {
            if( gaA_dataType == 'url' ){
               url = elmnt.href.substr(elmnt.href.indexOf('//')+2,Infinity);
               gaA_pageTracker._trackEvent( '/' + gaAddonName + '/' + gaA_dataType, "click", url );
            }else if( gaA_dataType == 'keyword') {
               data = getMetaKeyword();
               gaA_pageTracker._trackEvent( '/' + gaAddonName + '/' + gaA_dataType, "click", data );
            }
         }
      }
      else {
         if (/http/.test(this.protocol)) {
            if( gaA_dataType == 'url' ){
               url = this.href.substr(this.href.indexOf('//')+2,Infinity);
               gaA_pageTracker._trackEvent( '/' + gaAddonName + '/' + gaA_dataType, "click", url);
            }else if( gaA_dataType == 'keyword') {
               data = getMetaKeyword();
               gaA_pageTracker._trackEvent( '/' + gaAddonName + '/' + gaA_dataType, "click", data );
            }
         }
      }
   }
   /**
   * Initialize gaAddons
   */
   if (document.getElementsByTagName && typeof gaA_pageTracker == "object") {
      var hrefs = document.getElementsByTagName('a');
      for (var l = 0, m = hrefs.length; l < m; l++){
         if ( gaA_hostType == 'sponser' )
         {
            if ( hrefs[l].hostname == gaA_sponserHost ){
               if(hrefs[l].name == gaA_sponserHrefName){
                  startListening(hrefs[l], "click", trackLink);
               }
            }
         }else if (  gaA_hostType == 'external' )
         {
            if (hrefs[l].hostname != location.hostname) startListening(hrefs[l], "click", trackLink);
         }else if (  gaA_hostType == 'internal' )
         {
            if (hrefs[l].hostname == location.hostname) startListening(hrefs[l], "click", trackLink);
         }else if (  gaA_hostType == 'all' )
         {
            startListening(hrefs[l], "click", trackLink);
         }
      }
   }
}

if( "gaAddonName" in window) {
  if (window.addEventListener) // Standard
    window.addEventListener('load', gaAddons, false);
  else if (window.attachEvent) // old IE
    window.attachEvent('onload', gaAddons);
}
/// EOF ///
