var MILI_STEP = 10;

var currentSlide = 1;
var numSlide;
var recording = false;
var breakPoints = [];	
var miliCount = 0;
var intervalVar;


/*
  Renders and displays the page at specified index of the document.
  The page will be displayed inside the HTML canvas
*/
function displayPage(index) {
	PDFJS.getDocument('tester-slides.pdf').then(function(pdf) {
	 	numSlide = pdf.numPages;
	    pdf.getPage(index).then(function(page) {
	  		var scale = 2;
			var viewport = page.getViewport(scale);

			var canvas = document.getElementById('the-canvas');
			var context = canvas.getContext('2d');
			canvas.height = viewport.height;
			canvas.width = viewport.width;

			var renderContext = {
			  canvasContext: context,
			  viewport: viewport
			};
			
			page.render(renderContext);
		});
	});
}

/*
   Displays next slide. If reached the end, does nothing.
*/
function displayNextSlide() {
	var nextSlide = Math.min(currentSlide + 1, numSlide);
	clearDrawCanvas();
	if(currentSlide !== nextSlide) {
		currentSlide = nextSlide;
		displayPage(currentSlide);
		if(recording) {
			breakPoints.push(miliCount);
		}
	}
}

/*
   Displays previous slide. If reached the end, does nothing.
*/
function displayPreviousSlide() {
	var nextSlide = Math.max(currentSlide - 1 , 1);
	clearDrawCanvas();
	if(currentSlide !== nextSlide) {
		currentSlide = nextSlide;
		displayPage(currentSlide);
	}
}

/*
   Function used to bind spacebar with nextSlideFunction
*/
function nextIfSpace(e) {
    if (e.keyCode == "32") {
        displayNextSlide();
    }
}

/*
   Function called when record button is pushed. Sets boolean recording, 
   and updates the button layout. It also starts the interval call to 
   the incrementCount function which deals with time elapsed.
*/
function recordPush() {
	if(recording) {
		document.getElementById("record").innerHTML = "<i class=\"fa fa-video-camera\"></i>";
		recording = false;
		window.clearInterval(intervalVar);
		miliCount = 0;
		document.getElementById("refresh").style.display= "inline";
		document.getElementById("trash").style.display= "inline";
		document.getElementById("link").style.display= "inline-block";
	} else {
		document.getElementById("record").innerHTML = "<i class=\"fa fa-stop\"></i>";
		recording = true;
		intervalVar = window.setInterval(incrementCount, MILI_STEP);
	}
}

/*
   Function called at given interval to increment the timer.
*/
function incrementCount() {
	miliCount += MILI_STEP;
}

/*
   Function called when the trash button is pressed.
   Resets the "memory", and toggles a few buttons.
*/
function reset() {
	breakPoints = [];
	currentSlide = 1;
	displayPage(currentSlide);
	document.getElementById("refresh").style.display= "none";
	document.getElementById("trash").style.display= "none";
	document.getElementById("link").style.display= "none";
	clearDrawCanvas();
	clearDrawingMemory();
	fileOut = "";
}

/*
   Function calle when the replay button is pressed.
   Sets the timeout calls to displayNextSlide function
   based on the recorded breakpoints.
*/
function replay() {
	currentSlide = 1;
	displayPage(currentSlide);
	for (var i = 0; i < breakPoints.length; i++) {
		window.setTimeout(displayNextSlide, breakPoints[i]);
	};
	replayAnnotations();
}

/*
   Function used to make sure initial loading of assets
   is done in correct order. Withouth it, some elements 
   may be called before they are loaded
*/
function adjustFrame() {
	document.getElementById("daddy-canvas").height = document.getElementById("the-canvas").height;
	document.getElementById("daddy-canvas").width = document.getElementById("the-canvas").width;
}


// Initialization of functions and events
window.setTimeout(adjustFrame, 2300);
window.addEventListener("keydown", nextIfSpace, false);
window.addEventListener("load",displayPage(currentSlide), false);

