if (YAHOO) {
	/**
	*	The <em>com.digitas.global.tracking.omniture</em> namespace contains objects that support the DIGITAS
	*	Omniture Framework.
	*	@module com.digitas.global.tracking.omniture
	*	@title DIGITAS Omniture Framework
	*/
	var omniture = YAHOO.namespace('com.digitas.global.tracking.omniture');
	var code;

	omniture.DataContainerCache = null;

	/**
	*	The <em>com.digitas.global.tracking.omniture.Beacon</em> class represents 1 beacon to be fired by Omniture
	*	@class com.digitas.global.tracking.omniture.Beacon
	*	@param <Object> d the data retrieved from BeaconData
	*/
	omniture.Beacon = function(d) {
		this.properties = {};
		this.reservedProperties = {};

		for (var itm in d) {
			if (typeof(d[itm]) == 'string') {
				if (this.RESERVED_WORDS.find(itm))
					this.reservedProperties[itm] = d[itm];
				else
					this.properties[itm] = d[itm];
			}
		}
	}

	omniture.Beacon.prototype.RESERVED_WORDS = ['type'];

	/**
	*	Returns the set of keys represented within the object based on the format specified
	*	@class	com.digitas.global.tracking.omniture.Beacon
	*	@method	getKeys
	*	@param	<String>	frmt 	the format of one of the keys
	*	@param	<String>	key		the part of the format that should be replace with the key
	*	@return	<String>	the keys represented in the format specified.  If data isn't present, it will return an empty string
	*/
	omniture.Beacon.prototype.getKeys = function(frmt,key) {
		retVal = '';

		if (this.data) {
			for( var itm in this.data )
				retVal += frmt.replace(key,itm);
		}

		return retVal;
	}

	/**
	*	<em>com.digitas.global.tracking.omniture.DataContainer</em> is container to store data loaded from a <em>BeaconData</em> object
	*	@class com.digitas.global.tracking.omniture.DataContainer
	*/
	omniture.DataContainer = function() {}
	omniture.DataContainer.prototype.data = [];

	/**
	*	Sets the data of the object
	*	@class com.digitas.global.tracking.omniture.DataContainer
	*	@method setData
	*	@param <Array> the array to store
	*/
	omniture.DataContainer.prototype.setData = function(d) {
		this.data = d;
	}
	/**
	*	Gets the data of the object
	*	@class com.digitas.global.tracking.omniture.DataContainer
	*	@method setData
	*	@param <Array> the array to store
	*/
	omniture.DataContainer.prototype.getData = function() {
		return this.data;
	}

	/**
	*	<em>com.digitas.global.tracking.omniture.AbstractEventHandler</em> is intended to be an abstract class that
	*	encapsulates the core functionality of an event handler, but should never be instantiated itself.
	*	@class com.digitas.global.tracking.omniture.AbstractEventHandler
	*	@param {String} evt The event type
	*	@param {String} id Unique identifier of the event
	*/
	omniture.AbstractEventHandler = function(evt,id) {
		//alert("1 in abstracteventhandler");
		this.beacon = null;

		YAHOO.log('AbstractEventHandler created','debug','com.digitas.global.tracking.omniture.AbstractEventHandler');
		YAHOO.log('AbstractEventHandler event : '+evt,'debug','com.digitas.global.tracking.omniture.AbstractEventHandler');
		YAHOO.log('AbstractEventHandler id : '+id,'debug','com.digitas.global.tracking.omniture.AbstractEventHandler');

		this.setBeacon(evt,id);
	}


	/**
	*	Intended to be an abstract method, <em>com.digitas.global.tracking.omniture.AbstractEventHandler.track</em> is the default
	*	action of any <strong>EventHandler</strong> that inherits from <em>com.digitas.global.tracking.omniture.AbstractEventHandler</em>
	*	and doesn't override the method.
	*	@class com.digitas.global.tracking.omniture.AbstractEventHandler
	*	@method track
	*	@return boolean Whether beacon was fired correctly
	*/
	omniture.AbstractEventHandler.prototype.track = function(){
		YAHOO.log('AbstractEventHandler fired','info','com.digitas.global.tracking.omniture.AbstractEventHandler');
		return true;
	};

	/**
	*	Set the beacon of the event handler.
	*
	*	@class com.digitas.global.tracking.omniture.AbstractEventHandler
	*	@method setBeacon
	*/
	omniture.AbstractEventHandler.prototype.setBeacon = function(evt,id){
		//alert("2 in setBeacon");
		//get data for beacon if available
		var bContainer = new omniture.DataContainer();

		YAHOO.log("Locating beaconData."+evt+"['"+id+"']",'debug','com.digitas.global.tracking.omniture.AbstractEventHandler');

		if (omniture.DataContainerCache == null) {
			YAHOO.com.digitas.isLoaded('BeaconData','com.digitas',bContainer);
			YAHOO.log('beaconData object found','debug','com.digitas.global.tracking.omniture.AbstractEventHandler');
			omniture.DataContainerCache = bContainer.getData();
		}

		var beaconData = omniture.DataContainerCache;
		//alert("3" + beaconData);
		//alert("4" + evt);
		//alert("5" + id);
		//evt=evt.replace(/_/, "");
		//id=id.replace(/_/, "");
		var data = eval('beaconData.'+evt+"['"+id+"']");

		if (typeof(data) == 'object') {
			YAHOO.log('Beacon data found','debug','com.digitas.global.tracking.omniture.AbstractEventHandler');
			this.beacon = new omniture.Beacon(data);
		} else {
			//alert("5.5 " + typeof(data));
			YAHOO.log("Unable to locate beaconData."+evt+"['"+id+"']",'error','com.digitas.global.tracking.omniture.AbstractEventHandler');
			throw new Exception("Unable to locate beaconData."+evt+"['"+id+"']");
		}
	};

	/**
	*	<em>com.digitas.global.tracking.omniture.LoadEventHandler</em> is a subclass of <em>com.digitas.global.tracking.omniture.AbstractEventHandler</em>
	*	and is meant to handle the handling of a beacon fired on load of an HTML page.
	*	@class com.digitas.global.tracking.omniture.LoadEventHandler
	*	@param {String} id Unique identifier of the event
	*/
	omniture.LoadEventHandler = function(id) {
		try {
			omniture.LoadEventHandler.superclass.constructor.call(this,'load',id);
		} catch (e) {
			YAHOO.log('Unable to create LoadEventHandler','error','com.digitas.global.tracking.omniture.LoadEventHandler');
		}
	}

	/**
	*	Override of the <em>com.digitas.global.tracking.omniture.AbstractEventHandler.track</em> method.
	*	@class com.digitas.global.tracking.omniture.LoadEventHandler
	*	@method track
	*	@return boolean Whether beacon was fired correctly
	*/
	omniture.LoadEventHandler.track = function(){
		YAHOO.log('in LoadEventHandler','debug','com.digitas.global.tracking.omniture.LoadEventHandler');

		if (document.omnitureFire && s){
			//set each property into Omniture object
			for (var itm in this.beacon.properties) {
				s[itm] = this.beacon.properties[itm];
			}

			//call Omniture page load beacon generation function
			code = s.t();


		} else {
			if (document.omnitureFire)
				throw Exception('Unable to find Omniture library');
		}
	}
	YAHOO.lang.extend(omniture.LoadEventHandler,omniture.AbstractEventHandler,{track : omniture.LoadEventHandler.track});

	/**
	*	<em>com.digitas.global.tracking.omniture.ClickEventHandler</em> is a subclass of <em>com.digitas.global.tracking.omniture.AbstractEventHandler</em>
	*	and is meant to handle the handling of a beacon fired on load of an HTML page.
	*	@class com.digitas.global.tracking.omniture.ClickEventHandler
	*	@param {String} id Unique identifier of the event
	*/
	omniture.ClickEventHandler = function(lnk) {
		//alert("6 In click event handler");
		this.link = lnk;
		//alert("6.1 In click event handler" + this.link);
		//alert("6.2 In click event handler" + lnk);

		try {
			omniture.ClickEventHandler.superclass.constructor.call(this,'click',lnk.id);
		} catch (e) {
			//see if there are any classes set up within the omniture data
			if (lnk.className) {
				var classes = lnk.className.split(' ');
				//alert("6.5" + classes.length);

				for (var i=0; i<classes.length; i++) {
					try {
						this.setBeacon('click',classes[i]);

						//if data is found, move on
						if (this.beacon != null)
							break;
					} catch (e2) {
						//do nothing so loop moves on
					}
				}
			}
		}
		YAHOO.util.Event.addListener(lnk,'click',this.track,this,true);
	}

	/**
	*	Override of the <em>com.digitas.global.tracking.omniture.AbstractEventHandler.track</em> method.
	*	@class com.digitas.global.tracking.omniture.ClickEventHandler
	*	@method track
	*	@return boolean Whether beacon was fired correctly
	*/
	omniture.ClickEventHandler.track = function() {
		//alert("7 In clickeventhandler.track");
		var retVal = true;

		YAHOO.log('in ClickEventHandler','debug','com.digitas.global.tracking.omniture.ClickEventHandler');

		if (document.omnitureFire && s) {
			YAHOO.log('ClickEventHandler fired','debug','com.digitas.global.tracking.omniture.ClickEventHandler');
			//alert("7.1 In clickeventhandler.track");
			//initialize
			s.linkTrackVars = '';
			s.linkTrackEvents = 'None';
			s.events = '';

			//see if object has a omnituredata attribute
			if (this.link.getAttribute && (this.link.getAttribute('omnituredata') != 'undefined')) {
				//alert("7.2 In clickeventhandler.track");
				var dData = eval('('+this.link.getAttribute('omnituredata')+')');
				//alert("7.3 In clickeventhandler.track" + this.link.getAttribute('omnituredata'));
				//merge data with beacon
				for (var itm in dData) {
					var rexp = new RegExp("\\["+itm+"\\]", "gi");
					for (var bData in this.beacon.properties) {
						this.beacon.properties[bData] = this.beacon.properties[bData].replace(rexp,dData[itm]);
					}
				}
			}
			//alert("7.4 In clickeventhandler.track");

			//set each property into Omniture object
			for (var itm in this.beacon.properties) {
				s[itm] = this.beacon.properties[itm];

				//add item to linkTrackVars if it doesn't already exist
				if (s.linkTrackVars.indexOf(itm) < 0) {
					s.linkTrackVars += itm + ',';
				}

				//if an event, set it into linkTrackEvents
				if (itm.toLowerCase() == 'events') {
					if (s.linkTrackEvents == 'None')
						s.linkTrackEvents = '';

					s.linkTrackEvents += this.beacon.properties[itm] + ',';
				}
			}
			//alert("7.5 In clickeventhandler.track");

			//clean of linkTrackVars and linkTrackEvents
			s.linkTrackVars = s.linkTrackVars.substr(0,s.linkTrackVars-1);
			if (s.linkTrackEvents.length > 0)
				s.linkTrackEvents = s.linkTrackEvents.substr(0,s.linkTrackEvents-1);

			//call Omniture click action beacon generation function
			var type = ((this.beacon.reservedProperties['type']) ? this.beacon.reservedProperties['type'] : 'o');
			var action = ((this.beacon.properties['prop1']) ? this.beacon.properties['prop1'] : 'HTML Action');
			//alert("7.6 In clickeventhandler.track" + this.link + type + action);

			code = s.tl(this.link,type,action);

		} else {
			//alert("7.7 In clickeventhandler.track");
			if (document.omnitureFire)
				throw Exception('Unable to find Omniture library');
		}
		//alert("7.8 In clickeventhandler.track" + retVal);

		return retVal;
	}
	YAHOO.lang.extend(omniture.ClickEventHandler,omniture.AbstractEventHandler,{track : omniture.ClickEventHandler.track});

    omniture.SendBeacon = function(beaconName, properties) {
    	//alert("8 in sendBeacon");
        var beaconData = omniture.DataContainerCache.click[beaconName];
        if(beaconData == null) {
            //alert("9 Beacon Not Found");
            return;
        }
        var beacon = new omniture.Beacon(beaconData);

        // Dynamic Properties
        for (var p in properties)
            beacon.properties[p] = properties[p];

        if (document.omnitureFire && s) {
			//initialize
			s.linkTrackVars = '';
			s.linkTrackEvents = 'None';
			s.events = '';

			//set each property into Omniture object
			for (var itm in beacon.properties) {
				s[itm] = beacon.properties[itm];

				//add item to linkTrackVars if it doesn't already exist
				if (s.linkTrackVars.indexOf(itm) < 0) {
					s.linkTrackVars += itm + ',';
				}

				//if an event, set it into linkTrackEvents
				if (itm.toLowerCase() == 'events') {
					if (s.linkTrackEvents == 'None')
						s.linkTrackEvents = '';

					s.linkTrackEvents += beacon.properties[itm] + ',';
				}
			}

            //clean of linkTrackVars and linkTrackEvents
			s.linkTrackVars = s.linkTrackVars.substr(0,s.linkTrackVars-1);
			if (s.linkTrackEvents.length > 0)
				s.linkTrackEvents = s.linkTrackEvents.substr(0,s.linkTrackEvents-1);

			//call Omniture click action beacon generation function
			var type = ((beacon.reservedProperties['type']) ? beacon.reservedProperties['type'] : 'o');
			var action = ((beacon.properties['prop1']) ? beacon.properties['prop1'] : 'HTML Action');

            code = s.tl(null,type,action);

		} else {
			if (document.omnitureFire)
				throw Exception('Unable to find Omniture library');
		}
    }

    /**
	*	This function fires the appropriate Omniture page load beacon, as well as, sets up all the
	*	link beacons
	*	@method Initialize
	*/
	omniture.Initialize = function(){
		var qs = new Querystring();

		document.omnitureFire = qs.get('fire',true);

		var debug = qs.get('debug','false');
		if (debug.toLowerCase() == 'true')
			document.debug();

		//get URI
		var page = window.location.pathname;
		YAHOO.log('Original page path: ' + page,'debug','com.digitas.global.tracking.omniture.Initialize');
		//make sure it always ends with an actual filename -- index.html is the default
		if (/(^\/|\/$)/.test(page))
			page += 'index.html';

		YAHOO.log('Tested page path: ' + page,'debug','com.digitas.global.tracking.omniture.Initialize');
		//remove leading /, filename extension if present, and any / left in name
		page = page.replace(/^\/(.*)/,'$1');
		page = page.replace(/([^\.*])\..*$/,'$1');
		page = page.replace(/\//g,'');

		YAHOO.log('Final page path: ' + page,'debug','com.digitas.global.tracking.omniture.Initialize');

		//create and fire page load beacon
		var loadBeacon = new omniture.LoadEventHandler(page);
		loadBeacon.track();

        //attach objects to click events
        omniture.attachClickHandlers(null);
    }

    omniture.attachClickHandlers = function(rootElement) {
        var omnitureLinks;
        if (rootElement == null) {
            omnitureLinks = YAHOO.util.Dom.getElementsByClassName('omniture');
        } else {
            omnitureLinks = YAHOO.util.Dom.getElementsByClassName('omniture', "*", rootElement);
        }
        //alert("10" + omnitureLinks.length);
        
        //create new omniture click handler(s)
	for(var i=0;i<omnitureLinks.length;i++) {
		//alert("11 Ses" + omnitureLinks[i].id + "Ses");
		new omniture.ClickEventHandler(omnitureLinks[i]);
	}
    }

    YAHOO.util.Event.onDOMReady(omniture.Initialize);
}