ScrollGAEvents = {

	qElements : [],
	elements : [],
	initialized: false,

	add : function(el, label){
		if (this.initialized)
			this.addWatch($(el).offset()["top"], label);
		else
			this.qElements.push([el, label]);
	},

	addWatch : function(scrollY, label) {
		this.elements.push({
			y: scrollY,
			label: label,
			viewed: false
		});
	},

	init : function(){
		// Take all queued elements, add them to the watched elements
		for (var i = 0; i < this.qElements.length; i++) {
			var el = $(this.qElements[i][0]);
			var label = this.qElements[i][1];

			if (el.length > 0) {
				this.addWatch(el.offset()["top"], label);
			}
		}

		// Clear the queue
		this.qElements = [];

		// Setup the watching interval
		var self = this;
		window.setInterval(function(){
			var attentionWindowTop = $(document).scrollTop() - 50;	// Tolerate 50px 
			var attentionWindowBottom = attentionWindowTop + $(window).height() * 0.5;

			// Go through all elements and see if some of them are in the attention
			// window, if they are "mark them as viewed" and send an event
			for (var i = 0; i < self.elements.length; i++) {
				var el = self.elements[i];
				if (!el.viewed && (attentionWindowTop < el.y) && (el.y < attentionWindowBottom)) {
					el.viewed = true;
					trackScrollEvent(el.label);
				}
			}
		}, 2000);

		// And we are initialized
		this.initialized = true;
	}

}
// Initialize, when the window loads
$(window).load(function(){
	ScrollGAEvents.init();
});
