/**
 * ClickParser - Omniture Click Parser Class
 * @author makammer - Matt Kammersell
 * @copyright Basic Research
 */
ClickParser = Class.create();
ClickParser.prototype = {
    initialize: function(rsid,options) {
		this.rsid = rsid;
		if (options) {
			if (options.products) {
				this.products = options.products;
			}
		}
		$$('.o').each(this.createSvals.bind(this));
    },
    parseEvars: function(elem) {
    	if (elem.getAttribute('evars')) {
    		return elem.getAttribute('evars').split(';');
    	} else {
    		return false;
    	}
    },
    parseSprops: function(elem) {
    	if (elem.getAttribute('sprops')) {
    		return elem.getAttribute('sprops').split(';');
    	} else {
    		return false;
    	}
    },
    createSvals: function(elem) {
    	$(elem).observe('click', function(event){
    		elem = Event.element(event);
    		var _s = s_gi(this.rsid);
        	_evars = this.parseEvars(elem);
        	_sprops = this.parseSprops(elem);
        	_prods = this.products;
        	_events = elem.getAttribute('events');
    		
    		_tvl = Array();
    		if (_evars) {
	    		_evars.each(function(_e){
	    			_tvl.push(_e.substring(0,_e.lastIndexOf('=')));
	    		});
    		}
    		if (_sprops) {
	    		_sprops.each(function(_e) {
	    			_tvl.push(_e.substring(0,_e.lastIndexOf('=')));
	    		});
    		}
    		if (_prods) {
    			_tvl.push('products');
    		}
    		var _sps = _tvl.join(',');
    		
    		eval('_s.linkTrackVars="'+_sps+',events";');
    		eval('_s.linkTrackEvents="'+_events+'";');
    		
       		eval('_s.events="'+_events+'";');
       		if (_prods != null) {
       			eval('s.products="'+_prods+'";');
       		}
       		
       		if (_evars) {
       			this.regVars(_evars).each(function(_e){ eval(_e); });
       		}
       		if (_sprops) {
       			this.regVars(_sprops).each(function(_e){ eval(_e); });
       		}
       		_s.tl(elem,'o', s.prop1 + '/' + elem.getAttribute('name'));
       		eval(elem.onclick);
		 }.bind(this));
    },
    regVars: function(arr) {
    	var _rarr = Array();
    	arr.each(function(_e){
    		if (_e != '') { 
	    		var reg = _e.split('=');
	    		_rarr.push('_s.'+reg[0]+'=s.prop1+"/'+reg[1]+'";');
    		}
    	});
    	return _rarr;
    }
    
}
