var oxTracker = {
    
    url: null,
    cat1: '-',
    cat2: '-',
    target: '-',
    context: '-',
    value: 1,
    counter: 1,
    counter_types: {'default': 1, 'click': 2},
    
    _getOptions: function(options) {
        if (typeof(options) != 'object')
            options = {};
        if (typeof(options.url) == "undefined") 
            options.url = this.url;
        if (typeof(options.cat1) == "undefined") 
            options.cat1 = this.cat1;
        if (typeof(options.cat2) == "undefined") 
            options.cat2 = this.cat2;
        if (typeof(options.target) == "undefined") 
            options.target = this.target;
        if (typeof(options.context) == "undefined") 
            options.context = this.context;
        if (typeof(options.value) != "number") 
            options.value = this.value;
        if (typeof(options.counter) != "number") 
            options.counter = this.counter;
        return options
    },
    
    _trackingUrl: function(options) {
        options = this._getOptions(options);
        return options.url
		+'?cat1='+options.cat1
		+'&cat2='+options.cat2
		+'&target='+ options.target
		+'&context='+ options.context
		+'&value='+ options.value
        +'&counter'+ options.counter+'=1'
        ;
        
    },
    
    log: function(options) {
        
        this._writeTracker(this._trackingUrl(options))
    },
    _joinContext: function(idce,idet,idds) {
      if (typeof(idet)=='undefined'||idet == null||typeof(idds)=='undefined'||idds==null) {
          return idce;
      } else {
          return idce+'|'+idet+'|'+idds;
      }
    },
    _pageIdentity: function() {
        return this._joinContext(document.id_content_external,document.id_extend_type,document.id_data_source);
    },
    pageView: function() {
        this.log({
            target : this._pageIdentity(),
            cat1: 'pageTracking',
            cat2: 'show'
        })
    },
    
    addRelatedTracker: function(element, widget, idce, idet, idds) {
        element.oxtrack_options = {
            target: this._joinContext(idce, idet, idds),
            context : this._pageIdentity(),
            cat1: 'relatedTracking',
            cat2: widget || '',
            counter: this.counter_types['click']
        }
        this._addEvent(element, 'mousedown', this._onClickEvent, false);
    },
    
    relatedShown: function(widget,contents) {
        var targets = []
        for (i=0;i<contents.length;i++) {
            targets.push(this._joinContext(contents[i][0],contents[i][1],contents[i][2]));
        }
        this.log({
            target: targets.join(","),
            context : this._pageIdentity(),
            cat1: 'relatedTracking',
            cat2: widget || ''
        })
    },
    
    _writeTracker: function(url) {
        var trackerDiv = document.createElement('div');
		trackerDiv.innerHTML = '<p style="visibility:hidden;display:inline;width:0;height:0;"><img alt="oxTracker img" src="'+url+'"/></p>';
		document.body.appendChild(trackerDiv.firstChild);
    },
    
    /**
	 * Called on click event, to track clicked urls
	 *
	 * Always return true not to stop event propagation and default behaviour of click event
	 * @param Event e
	 * @return true
	 */
	_onClickEvent: function(e)
	{
		var source;
		if (typeof e == 'undefined') var e = window.event;
		if (typeof e.target != 'undefined') source = e.target;
		else if (typeof e.srcElement != 'undefined') source = e.srcElement;
		else return true;

		while( source.className.indexOf('oxtrack') == -1 )  
            source = source.parentNode;
			
        
        this.track(source.oxtrack_options);
		
		return true;
	},
    
    /**
	 * Called to track click
	 */
	track: function(options)
	{
		var image = new Image()
		image.onLoad = function() {};
		image.src = this._trackingUrl(options)+ '&rand=' + Math.random()
		this._pause( 500 );
	},
    
    /**
	 * Function used to generate a pause
	 *
	 * Do not use setTimeout() (does not work to prevent link from being followed) or preventDefault on click event (would interfeer too much with user's js event listeners if any)
	 * @param int time_msec
	 */
	_pause: function(time_msec)
	{
		var now = new Date();
		var expire = now.getTime() + time_msec;
		while(now.getTime() < expire)
			now = new Date();
	},
	/**
	 * Add event listener, browser independent
	 *
	 * @param Element el
	 * @param string event_type
	 * @param function fn
	 * @param bool use_capture
	 */
	_addEvent: function(el, event_type, fn, use_capture)
	{
        if(el.className.indexOf('oxtrack') == -1) 
            el.className += ' oxtrack';
		var bind = this, binded_fn = function() {return fn.apply(bind, arguments);};
		if (el.addEventListener) { 
			el.addEventListener(event_type, binded_fn, use_capture); 
		} else if (el.attachEvent) { 
			el.attachEvent('on' + event_type, binded_fn);
		} else {
			el['on' + event_type] = binded_fn;
		}
	}
}