$(document).ready(function () {

	window.eventstream_verbose = false;
	debug = {}
	debug.log = function(msg) {
		if (window.eventstream_verbose)
			console.log(msg)
	}

	function getXPath( element )
	{
		var xpath = '';
		for ( ; element && element.nodeType == 1; element = element.parentNode )
		{
			var id = $(element.parentNode).children(element.tagName).index(element) + 1;
			id > 1 ? (id = '[' + id + ']') : (id = '');
			xpath = '/' + element.tagName.toLowerCase() + id + xpath;
		}
		return xpath;
	}


	/**
	 * Log the activity for later analysis to improve course structure.
	 */
	push_event = function (type, d) {
		return;
		e = {}
		e['data'] = d
		e['type'] = type
		json_data = JSON.stringify(e)
		debug.log(json_data)

		$.ajax({
			method: "POST",
			url: "/api/rest/event",
			contentType: 'application/json',
			data: json_data
		})
		.done(function( msg ) {
			debug.log("Event logged:");
			debug.log(msg );
		});
	}

	window.push_event = push_event;



	// Why take the start time?, jquery as there is IE8 issues
	now = $.now()


	/**
	 * MOUSE MOVEMENTS
	 *
	 * For very detailed analysis tracking mouse movements can be enabled.
	 * Note however that this will create vast amounts of events which may
	 * hold little value for didactic improvements.
	 *
	 * Alternative: push mouse trace with click, to see what lead to the click?
	 */
	/* */
	mousemove_buffer = [];
	mousemove_buffer_size = 100;
	mousemove_counter = mousemove_buffer_size;

	$(document).mousemove(function( event ) {
		var pageCoords = "( " + event.pageX + ", " + event.pageY + " )";
	  	var clientCoords = "( " + event.clientX + ", " + event.clientY + " )";
	  	var screenCoords = "( " + event.screenX + ", " + event.screenY + " )";

		//console.log("---");
		//console.log( pageCoords );
		//console.log( clientCoords );

		mousemove_buffer.push([now+event.timeStamp, event.pageX, event.pageY])
		mousemove_counter--;

		if (mousemove_counter < 0)
		{
			push_event("mousemove", mousemove_buffer)
			mousemove_buffer = []
			mousemove_counter = mousemove_buffer_size;
		}

	});



	$(window).resize(function(event) {
		var w = $(window).width();
		var h = $(window).height();

		e = {'width': w, 'height': h};

		mousemove_buffer.push([now+event.timeStamp, "resize", w, h])
		push_event("resize", e)
	});



	$(window).scroll(function(event) {
		var t = $(window).scrollTop();
		var l = $(window).scrollLeft();

		mousemove_buffer.push(["scroll", t, l])

		e = {'top': t, 'left': l};
		push_event("scroll", e)
	});



	/**
	 * CLICK EVENTS (window)
	 *
	 * Similar to the mouse movement it may be useful to collect click data,
	 * though click data maybe somewhat more meaningful as it stands for a
	 * concrete user interaction
	 * */
	$(document).click(function(event) {
		//debug.log(event)
  		//debug.log(event.target)

		//var pageCoords = "( " + event.pageX + ", " + event.pageY + " )";
	  	//var clientCoords = "( " + event.clientX + ", " + event.clientY + " )";
	  	//var screenCoords = "( " + event.screenX + ", " + event.screenY + " )";

		var w = $(window).width();
		var h = $(window).height();

		var t = $(window).scrollTop();
		var l = $(window).scrollLeft();

		mousemove_buffer.push([now+event.timeStamp, "click", event.pageX, event.pageY])

		x = getXPath(event.target);


		// get editor line, char
		editor = "none";
		if ( window.cEditor != null ) {
			editor = cEditor.getCursor();
		}

		e = {"xpath": x, "pos": [event.pageX, event.pageX], "viewport": [w, h], "scroll": [t, l], "editor": editor}

		push_event("click", e)
	});






	// TODO
	// Start/Stop/Jump Video => may hint at problems, convienient when continueing


	/**
	 * YOUTUBE VIDEO EVENTS
	 *
	 * Understanding how users consume the learning videos seems to have great
	 * potential in improving the videos. E.g. if many people pause or rewatch
	 * a part of the video this may indeicate a problem.
	 */




});
