/**
 * 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;
    	}
    },
    parseProducts: function(elem) {
    	if (elem.getAttribute('products')) {
    		return elem.getAttribute('products');
    	} else {
    		return false;
    	}
    },
    createSvals: function(elem) {
    	$(elem).observe('mousedown', function(event){
    		elem = Event.element(event);
    		var _s = s_gi(this.rsid);
        	_evars = this.parseEvars(elem);
        	_sprops = this.parseSprops(elem);
        	this.products = this.parseProducts(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(',');
    		if (elem.getAttribute('name')=='cartAdds') {
    			eval('_s.linkTrackVars="events,eVar7,products";');	
    		} else {
    			eval('_s.linkTrackVars="'+_sps+',events";');
    		}
    		eval('_s.linkTrackEvents="'+_events+'";');
    		eval('_s.events="'+_events+'";');
    		if (elem.getAttribute('name')=='cartAdds') {
    			sizeOption='';
    			productAddToCartForm.form.getElements().each(
					function(_e){ 
						if(_e.id.lastIndexOf('attribute')==0) { 
							if ($('size_'+_e.value)) {
								sizeOption = ($('size_'+_e.value).innerHTML);
							}
						}
					}
    			);
    			eval('_s.products=";'+_prods+';;;;evar7='+_prods+'-'+sizeOption+'";');
    		} else {
	       		if (_prods != null) {
	       			if (_evars) {
	       				eval('_s.products=";'+_prods+';;;;'+elem.getAttribute('evars').replace(/;/g,'|').replace(/eVar/g,'evar')+'";');
	       				_evars = false;
	       			} else {
	       				eval('_s.products=";'+_prods+'";');
	       			}
	       		}
    		}
   			if (_evars) {
       			this.regVars(_evars);
       		}	
       		if (_sprops) {
       			this.regVars(_sprops);
       		}
       		_s.tl(elem,'o', elem.getAttribute('name'));
       		eval(elem.onclick);
		 }.bind(this));
    },
    regVars: function(arr) {
    	for(i=0;i<arr.length;i++) {
    		eval('_s.'+arr[i]+';');
    	}
    }
}
