 var fileOut = "";


function fileGen() {
	 document.getElementById('link').onclick = function(code) {
	 	
	 	var strokeIndex = 0;
	 	var currStroke;
	    
	    for(var slideIndex = 0; slideIndex < breakPoints.length; ++slideIndex) {
		    fileOut += openSlideXML(slideIndex, breakPoints[slideIndex]);
		    
		    while(strokeIndex < annotations.length && annotations[strokeIndex].timestamp < breakPoints[slideIndex]) {
		    	currStroke = annotations[strokeIndex];
		    	fileOut += strokeXML(currStroke.a.x, currStroke.a.y, currStroke.b.x, currStroke.b.y, currStroke.timestamp);
		    	++strokeIndex;
		    }

		    fileOut += closeSlideXML();
	   }
	   
	   this.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(fileOut);
	 };
};

window.addEventListener("load", fileGen, false);



// Helper functions for xml generation

function openSlideXML(number, breakpoint) {
	var result = "<slide " + "num=\"" +  number +  "\"" + " break=\"" + breakpoint +  "\"" + ">\n";
	return result;
}

function closeSlideXML() {
	var result = "</slide>\n";
	return result;
}

function strokeXML(a_x, a_y, b_x, b_y, timestamp) {
	var result = "\t<stroke " + "a_x=\"" +  a_x +  "\" " + "a_y=\"" +  a_y +  "\" " + "b_x=\"" +  b_x +  "\" " + "b_y=\"" +  b_y +  "\" " + "timestamp=\"" +  timestamp +  "\" "  + "/>\n";
	return result;
}