function Stats() {

    /** variable declarations */
	this.name;
    this.tags;
    this.current_environment;
    this.current_feature;
    this.settings;
	this.initialized = false;
	this.data = new Array();
	this.loadTriggers = new Array("featureToFeature","featureToHomeToFeature","homeToFeature","initialToFeature","initialToHome","featureToHome");

    /** function declarations */
	this.onLoadEvent = onLoadEvent;
	this.onClickEvent = onClickEvent;
    this.init = init;
    this.send = send;
    //this._doLoad = _doLoad;
    //this._doClick = _doClick;
    this.setGlobals = setGlobals;
    this._setOmnitureTag = _setOmnitureTag;
    //this._doOmniture = _doOmniture;
	this._clearOmniture = _clearOmniture;
	this.initialized = false;
	this._findNode = _findNode;
	this.populate = populate;
	this.debug = true;
	

    this.init();
	
	
	
    /** get xml */
    function init() {
		this.name = "Omniture Stats Instance";
		this.settings = {};
        jQuery.noConflict();
        jQuery.ajax({
            type: "GET",
            url: "./xml/tags.xml",
            dataType: "xml",
			error: function(request, textStatus, errorThrown) {

			},
            success: function(data, text_status) {
                Stats.tags = data;
                Stats.setGlobals();
                //send HOME load
                //Stats.send('load', 'home');		
            }
        });
    }
	
	function populate(arr){
		//if(this.debug) console.log("[populate] ");
		this.initialized = true;
		jQuery(this.tags).find("events tag").each(function() {
			var o = {};											   
			var f = jQuery(this).attr("feature");
			var trig = jQuery(this).attr("trigger");
			var type = jQuery(this).attr("type");
			var n = jQuery(this).attr("name");
			var v = jQuery(this).text();
			var e = jQuery(this).attr("environment");
			var pev = jQuery(this).attr("pev2");
			o.name = n;
			o.value = v;
			o.environment = e;
			o.trigger = trig;
			o.feature = f;
			o.type = type;
			o.pev2 = pev;
			
			arr.push(o);
		});
	}

    /** main work method: set info and trigger omniture call */
    function send(type, feature, trigger) {
		//void(this.traceSettings());
        //get tags via a method depending on type
		//if(this.debug) console.log("[Stats][send] type :  " + type + "    feature :    " + feature + "    trigger :    " + trigger);
        
		// object with node and type.
		var obj = this._findNode(feature,trigger);
		
		//if(this.debug) console.log("\tfound node: " + obj.feature + " trigger: " + obj.trigger + " type: " + obj.type);
		
		if(obj.type == "click"){
			this.onClickEvent(obj);
		} else if(obj.type == "load") {
			this.onLoadEvent(obj);
		} else {
			//if(this.debug) console.log("no omniture information found!!!");
		}
		
		this._clearOmniture();
    }
	
	/**
	* Convoluted function to deal with their convoluted xml
	* to find the correct node.
	*/
	function _findNode(feature,trigger){
		//if(this.debug) console.log("[findNode] feature: " + feature + " trigger: " + trigger);
		if(!this.initialized) {
			this.populate(this.data);
		}
		
		//if(this.debug) console.log("\tstart search");
		var resArr = new Array();
		
		// first test features
		//if(this.debug) console.log("\tsearch features");
		for(var i=0;i<this.data.length;i++){
			var o =this.data[i];
			//if(this.debug) console.log("\t" + o.feature + " : " + feature);
			if(o.feature == feature){
				resArr.push(o);
			} else if (o.feature == undefined){
				if(o.trigger == trigger){
					//if(this.debug) console.log("\ttigger found!");
					return o;
				}
			}
		}
		
		//if(this.debug) console.log("results after feature test: " + resArr.length);
		
		//if(this.debug) console.log("\n\tsearch hardcoded triggers first");
		
		// look through hardcoded triggers first
		for(var i=0;i<resArr.length;i++){
			var o = resArr[i];
			for(var j=0;j<this.loadTriggers.length;j++){
				////if(this.debug) console.log("\t" + this.loadTriggers[j] + " : " + trigger);
				if(trigger == this.loadTriggers[j]){
					//if(this.debug) console.log("\tfound!");
					return o;
				}
			}
		}
		
		
		//if(this.debug) console.log("\n\tsearch triggers");
		// now test triggers
		for(var p=0;p<resArr.length;p++){
			var o = resArr[p];
			
			if(o.trigger != undefined){
				var triggersArray = o.trigger.split(",");
				for(var m = 0;m<triggersArray.length;m++){
					var t = triggersArray[m];
					//if(this.debug) console.log("\ttrigger: " + o.trigger + " = " + t);
					if(trigger == t){
						//if(this.debug) console.log("\tfound!");
						return o;
					} 
				}
			}
		}
		
		return false;
	}
	
	function onLoadEvent(o){
		//if(this.debug) console.log("[onLoadEvent] " + typeof s + " settings: " + typeof this.settings);
		this._setOmnitureTag(o.name, o.value);
        
		Stats.current_environment = o.environment;
		Stats.current_feature = o.feature;
		s.visitorNamespace = this.settings['s.visitorNamespace'];
        s.eVar3 = this.settings['s.evar3'];
        s.prop2 = this.settings['s.prop2'];
        s.prop10 = this.settings['s.prop10'];
        s.prop11 = this.settings['s.prop11'];
        s.prop32 = this.settings['s.prop32'];
        s.prop46 = this.settings['s.prop46'];
        s.channel = this.settings['s.channel'];
        s.pageName = this.settings['s.pageName'];
		
		void(s.t());
		//if(this.debug) console.log("\t s.t() called");
		
	}
	
	function onClickEvent(o){
		this._setOmnitureTag(o.name, o.value);
		var u = escape(document.location.host);
		s.visitorNamespace = this.settings['s.visitorNamespace'];
		s.eVar3 = this.settings['s.evar3'];
		s.prop2 = this.settings['s.prop2'];
		s.prop10 = this.settings['s.prop10'];
		s.prop11 = this.settings['s.prop11'];
		s.prop32 = this.settings['s.prop32'];
		s.prop46 = this.settings['s.prop46'];
		s.channel = this.settings['s.channel'];
		s.pageName = this.settings['s.pageName'];
		
		//if(this.debug) console.log("[onClickEvent] settings: " + this.settings + "\n\tu: " + u + "\n\tpev2: " + o.pev2);	
		//if(this.debug) console.log("prop46: " + s.prop46);
		
		if(s.prop46 != "") 	{
			s.linkTrackVars = "prop46";
			//if(this.debug) console.log("s.prop46: " + s.prop46);
		}
		if(s.eVar3 != "") 	{
			s.linkTrackVars = "eVar3";
			//if(this.debug) console.log("s.eVar3: " + s.eVar3);
		}
		s.tl(u,"o",o.pev2);
		
		//if(this.debug) console.log("\t s.tl() called");
		
	}
	
    /** set global stats settings */
    function setGlobals() {
		//if(this.debug) console.log("[setGlobals] " + this.name + " settings: " + typeof this.settings);
        jQuery(this.tags).find('globals tag').each(function() {
			//if(this.debug) console.log("\tglobal found");
            Stats._setOmnitureTag(jQuery(this).attr('name'), jQuery(this).text());
        });
    }

    /** tell omniture code to set a setting */
    function _setOmnitureTag(name, value) {
        this.settings[name] = value;
		//if(this.debug) console.log("[Stats][_setOmnitureTag] " + name + " : " + this.settings[name] + " settings: " + typeof this.settings);
    }
	
	function _clearOmniture() {
		//if(this.debug) console.log("[Stats][_clearOmniture] settings:" + typeof this.settings );
		/*
		for each(var i in this.settings){
			s[i] = this.settings[i] = "";
			//if(this.debug) console.log(i + " : " + this.settings[i]);
		}
		*/
		
		/*
		for (var i = 1; i <= 50; i++) {
				s['prop' + i] = '';
				s['eVar' + i] = '';
		}
		
		s['events'] = "";
		s['pageName'] = "";
		s['channel'] = "";*/
		//s.visitorNamespace = this.settings['s.visitorNamespace'] = "";
        
		s.eVar3 = this.settings['s.evar3'] = "";
        s.prop2 = this.settings['s.prop2'] = "";
        s.prop10 = this.settings['s.prop10'] = "";
        s.prop11 = this.settings['s.prop11'] = "";
        s.prop32 = this.settings['s.prop32'] = "";
        s.prop46 = this.settings['s.prop46'] = "";
        s.channel = this.settings['s.channel'] = "";
        s.pageName = this.settings['s.pageName'] = "";
		s.pev2 = this.settings["pev2"] = "";
		s.pe = "";
		
		//if(this.debug) console.log("\settings.pageName: " + this.settings['s.pageName']);
	}
	
}
