//anything member inside the object with the "var " scope is private, anthing in the "that" scope is public. Example below.

function bitmap () {
	that = this; //public
	var current_bitmap = []; //private
	var historic_bitmap = [];
	var viewer_id;
	var presentation_id;
	var session_id;
	
	/* This part of the code initializes the bitmap. This is using a for loop to set the index values of current_bitmap to 0.*/
	that.initBitmap = function (seconds) {
		for (var i=0; i <= seconds; i++) {
			current_bitmap[i] = 0;
		}
	};
	
	/*Gets the historical array from the server. It checks to see if the historical array has anything inside of it.
	 * It will send an error message if it is null. For now, it will be.*/
	that.initHistoricalBitmap = function (historical_array) {
		if (historical_array != null) {
			historic_bitmap = historical_array;
		} else {
			alert("ERROR: Nothing inside the historical array.");
		}
	
	};
	
	/* It uses the seconds from the video and makes the current_bitmap array to the number of seconds. For
	 *  example, if the video is 45 seconds long, the current_bitmap array would also be 45 indexes long.*/
	that.setBit = function (second) {
		if (second != null) {
			current_bitmap[second] = 1;
		} else {
			alert("ERROR: No bit set.");
		}
		
	};
	
	//Sets the Viewer ID.
	that.setViewerID = function (view_id) {
		if (view_id != null) {
			viewer_id = view_id;
		} else {
			alert("ERROR: No viewer ID");
		}
	};
	
	that.getViewerID = function () {
		return viewer_id;
	};
	
	//Sets the Session ID.
	that.setSessionID = function (ses_id) {
		if (ses_id != null) {
			session_id = ses_id;
		} else {
			alert("ERROR: No session ID");
		}
	};
	
	that.getSessionID = function () {
		return session_id;
	};
	
	//Set Presentation ID.
	that.setPresentationID = function (pres_id) {
		presentation_id = pres_id;
	
	};
	
	that.getPresentationID = function () {
		return presentation_id;
	};
	
	//Gets the Current Usage. Returns Current_bitmap.
	that.getCurrentUsageSeconds = function () {
		return current_bitmap;
	};
	
	//Gets historic bitmap from the server. Historic bitmap == old data.
	that.getHistoricUsageSeconds = function () {
		return historic_bitmap;
		
	};
	
	//Combines the two arrays to see which ones was previously viewed. Optional function.b     
	that.getCombinedUsageSeconds = function () {
		combined = [];
		for (i=0; i< current_bitmap; i++) {
			combined = historic_bitmap[i] & current_bitmap[i];
		}
		return combined;
	};
		
	//SetInterval is measured in miliseconds.
	that.scheduleDiffToServer = function () {
		storeBitmap();
		setInterval(function () {
			storeBitmap();
		}, 30000);
	};
	
	return that;
}

storeBitmap = function () {
	var reRoute = $('body').attr('data-reroute');
	if (reRoute == null) {
		reRoute = '';
	}

	if (myBitmap['ready'] === true) {
		$.ajax({
			type: "POST",
			url: reRoute + "/presentations/storebitmap/index.php",
			data: {
				presentation_id: myBitmap.getPresentationID(),
				session_id: myBitmap.getSessionID(), 
				viewer_id: myBitmap.getViewerID(),
				current_bitmap: myBitmap.getCurrentUsageSeconds().join("") 
			},
			dataType: "json",
			success: function(response) {
				if (response == null) {
					return;
				}
				$("body").attr('data-percent_watched', response['percent_watched']);
				if (typeof videoProgressCallback == 'function') { 
					videoProgressCallback(response['percent_watched']); 
				}
			}
		});
	}
};
