/**
 * @fileoverview 
 * <p>This library is designed to encapsulate the details of hit reporting.</p>
 * @author Robert D. Rice
 */

// vc_id = "$Id: hitReport.js 10220 2007-01-12 18:34:45Z robert $"

/**
 * target the hit report server
 * @param path to track, optional
 */
function captureHitStats( path ) {
    // captureHitStatsCT( );
    captureHitStatsG( path );
}

/**
 * target the ct hit report server
 */
function captureHitStatsCT( ) {
  var loc = escape( document.location );
  var ref = escape( document.referrer );
  var src = document.location.protocol + 
        '//ct.windermere.com/cgi-bin/ctasp-server.cgi?i=b5jSZ6hcQ2YGvIci&amp;g=1&amp;r=' +
        loc + '&amp;f=' + ref;
  var image = new Image( );
  image.src = src;
}

var G_ACCTS = new Object( );
G_ACCTS[ 'www.windermere.com' ] = "UA-384279-1";
var G_INITIALIZED = false;
var G_QUEUE = new Array( );

/**
 * method to retrieve the correct account key.
 * this method should contain business logic, to include or disclude domains.
 * @return the key or null if not defined
 */
function retrieveAccountG( ) {
    // var key =  G_ACCTS[ document.location.hostname ];
    // for now, include everything and filter on the stats side
    var key = G_ACCTS[ 'www.windermere.com' ];
    return key;
}

/**
 * target the g hit report server
 * @param path to track, optional, assumes urchin will get initialized
 *  this path should start with a forward slash, typically everything after domain
 */
function captureHitStatsG( path ) {
    if ( retrieveAccountG( ) && !G_INITIALIZED && !path ) {
	var e = document.createElement("script");
	e.src = "http://www.google-analytics.com/urchin.js";
	e.type = "text/javascript";
	e.onload = fireGTracker; // common event handling
	// IE event handling
	e.onreadystatechange = 
	    function ( ) { if ( this.readyState == 'complete' || this.readyState == 'loaded' ) { fireGTracker( ); } };
	document.getElementsByTagName("head")[0].appendChild(e); 
    } 
    if ( path ) {
        if ( !G_INITIALIZED ) {
	    // queue it up for later tracking
	    G_QUEUE[ G_QUEUE.length ] = path;
        } else {
	    path = resolveTrackerPathG( path );
	    if ( path != null ) {
		urchinTracker( path );
	    }
        }
    }
}

/**
 * Resolve url for tracking
 * @param path, unresolved
 * @return path, resolved
 */
function resolveTrackerPathG( path ) {
    var resolved = null;

    if ( path.indexOf( "/" ) == 0 ) {
	resolved = path;
    } else if ( path.indexOf( "://" ) != -1 ) {
	resolved = path;
	resolved = resolved.slice( resolved.indexOf( "://" ) + 3 );
	resolved = resolved.slice( resolved.indexOf( "/" ) );
    } else {
	resolved = path;
	base = document.location.pathname;
	base = base.substr( 0, base.lastIndexOf( "/" ) + 1 );
	while ( resolved.indexOf( "../" ) == 0 ) {
	    resolved = resolved.slice( 3 );
            base = base.substr( 0, base.length - 1 );
	    base = base.substr( 0, base.lastIndexOf( "/" ) + 1 );
	}

	resolved = base + resolved;
    }
    return resolved;
}

/**
 * callback method after G is loaded
 */
function fireGTracker(  ) {
    _uacct = retrieveAccountG( );
    urchinTracker( );
    G_INITIALIZED = true;
    // track anything that has been q'd up.
    for ( var i = 0; i < G_QUEUE.length; i++ ) {
	captureHitStatsG( G_QUEUE[ i ] );
    }
}

