//////////////////////////////////////////////////////////////
//															//
//	© 2005 - 2007 Vereyon									//
//  http://www.vereyon.nl									//
//	All rights reserved										//
//															//
//////////////////////////////////////////////////////////////

Progress.prototype.__type = "Progress";

Progress.prototype.hLoadingDiv = null;
Progress.prototype.hProgressDiv = null;
Progress.prototype.hToolBar = null;
Progress.prototype.hProgressContentParent = null;
Progress.prototype.hResultDetailsDiv = null;

Progress.prototype.hProgressTable = null;
Progress.prototype.hProgressTableGraphDiv = null;

Progress.prototype.arrResults = null;
Progress.prototype.arrGraphBarContainers = null;
Progress.prototype.iDisplayStart = -1;
Progress.prototype.iDisplayLength = 10;
Progress.prototype.iSelectedIndex = -1;
Progress.prototype.iViewIndex = -1;
Progress.prototype.bPeekViewing = false;

Progress.prototype.hFowardButton = null;
Progress.prototype.hBackButton = null;

function Progress() {
	
	this.Initialize = function() {
	
		this.arrResults = new Array();
		this.arrGraphBarContainers = new Array();
	
		// Bind loading div
		this.hLoadingDiv = document.getElementById("LoadingDiv");
		this.hToolBar = document.getElementById("ToolBar");
		
		// Bind content parent div
		this.hProgressContentParent = document.getElementById("contentDivParent");
		
		// Create progress table
		this.InitializeToolBar();
		this.InitializeProgressTable();
		
		this.SetMode("progress");
	}
	
	this.InitializeProgressTable = function() {
	
		// Declare variables
		var newRow, newCell, newP;
	
		// Create progress table div
		this.hProgressDiv = document.createElement("DIV");
		this.hProgressContentParent.appendChild(this.hProgressDiv);
		
		this.hProgressTableGraphDiv = document.createElement("DIV");
		this.hProgressTableGraphDiv.className = "progressGraphTableCellDiv";
		this.hProgressDiv.appendChild(this.hProgressTableGraphDiv);
		
		// Create the details div
		this.hResultDetailsDiv = document.createElement("DIV");
		this.hResultDetailsDiv.className = "progressDetailsDiv";
		this.hProgressDiv.appendChild(this.hResultDetailsDiv);
		
		
		
	}
	
	this.InitializeToolBar = function() {
	
		this.hBackButton = new UI.ToolBarButton(this.hToolBar);
		this.hBackButton.SetImage("images/icon-goback.png", "images/icon-goback-grey.png");
		this.hBackButton.SetToolTip("Vorige overhoring");
		this.hBackButton.onClick.Suscribe(this.OnBackButtonClick, this);
	
		this.hFowardButton = new UI.ToolBarButton(this.hToolBar);
		this.hFowardButton.SetImage("images/icon-gofoward.png", "images/icon-gofoward-grey.png");
		this.hFowardButton.SetToolTip("Volgende overhoring");
		this.hFowardButton.onClick.Suscribe(this.OnFowardButtonClick, this);
	}
	
	this.SetMode = function(mode) {
		
		switch(mode) {
			case "progress":
			
				this.InvalidateToolBar();
				
				break;
		}
		
		this.ShowDiv(mode);
	}
	
	this.ShowDiv = function(div) {
	
		this.hProgressDiv.style.visibility = "hidden";
		this.hProgressDiv.style.display = "none";
		this.hLoadingDiv.style.visibility = "hidden";
		this.hLoadingDiv.style.display = "none";
		
		switch(div) {
			case "progress":
				this.hProgressDiv.style.visibility = "visible";
				this.hProgressDiv.style.display = "block";
				break;
			case "loading":
				this.hLoadingDiv.style.visibility = "visible";
				this.hLoadingDiv.style.display = "block";
				break;
		}
	}
	
	this.OnGraphBarClick = function(e) {
	
		// Declare variables
		var srcClass, oEvent;
		
		srcClass = this.owningClass;
		
		if(window.event) {
		
			// Windows IE and DOM level 2
			// Bug: using window.event seems to slowdown event handling in IE. Perhaps IE takes a lot of time setting the window.event object.
			oEvent = window.event;
		} else {
		
			// Firefox
			oEvent = e;
		}
	
		srcClass.SelectResult(this.resultIndex);
	}
	
	this.OnGraphBarMouseOver = function(e) {
	
		// Declare variables
		var srcClass, oEvent;
		var hBar;
		
		srcClass = this.owningClass;
		
		if(window.event) {
		
			// Windows IE and DOM level 2
			// Bug: using window.event seems to slowdown event handling in IE. Perhaps IE takes a lot of time setting the window.event object.
			oEvent = window.event;
		} else {
		
			// Firefox
			oEvent = e;
		}
		
		
		// Hide the selected bar
		hBar = srcClass.arrGraphBarContainers[srcClass.iSelectedIndex];
		hBar.parentContainer.className = "graphBarContainer";
		hBar.style.left = "25px";
		hBar.style.top = (parseInt(hBar.style.top) + 1) + "px";
		
		// Highlight the mouse over bar
		this.parentContainer.className = "graphBarContainerHighlight";
		this.style.left = "24px";
		this.style.top = (parseInt(this.style.top) - 1) + "px";
		
		this.owningClass.bPeekViewing = true;
		this.owningClass.iViewIndex = this.resultIndex;
		this.owningClass.UpdateDetailsDiv();
	}
	
	this.OnGraphBarMouseOut = function(e) {
	
		// Declare variables
		var srcClass, oEvent;
		
		srcClass = this.owningClass;
		
		if(window.event) {
		
			// Windows IE and DOM level 2
			// Bug: using window.event seems to slowdown event handling in IE. Perhaps IE takes a lot of time setting the window.event object.
			oEvent = window.event;
		} else {
		
			// Firefox
			oEvent = e;
		}
		
		// Hide the mouse over bar
		this.parentContainer.className = "graphBarContainer";
		this.style.left = "25px";
		this.style.top = (parseInt(this.style.top) + 1) + "px";
		
		// Show the selected bar
		hBar = srcClass.arrGraphBarContainers[srcClass.iSelectedIndex];
		hBar.parentContainer.className = "graphBarContainerHighlight";
		hBar.style.left = "24px";
		hBar.style.top = (parseInt(hBar.style.top) - 1) + "px";
		
		this.owningClass.bPeekViewing = false;
		this.owningClass.UpdateDetailsDiv();
	}
	
	this.OnBackButtonClick = function(e) {
	
		this.iSelectedIndex--;
		
		// Check if selected index is visible
		if(this.iSelectedIndex <= this.iDisplayStart) {
			this.iDisplayStart = this.iSelectedIndex - 1;
			if(this.iDisplayStart < 0)
				this.iDisplayStart = 0;
		}
		
		// Update
		this.UpdateDetailsDiv();
		this.InvalidateToolBar();
		this.ClearGraphTable();
		this.PopulateGraphTable();
	}
	
	this.OnFowardButtonClick = function(e) {
	
		this.iSelectedIndex++;
		
		// Check if selected index is visible
		if(this.iSelectedIndex >= this.iDisplayStart + this.iDisplayLength - 1) {
			this.iDisplayStart = this.iSelectedIndex - this.iDisplayLength + 2;
			if(this.iDisplayStart < 0)
				this.iDisplayStart = 0;
		}
		
		
		// Update
		this.UpdateDetailsDiv();
		this.InvalidateToolBar();
		this.ClearGraphTable();
		this.PopulateGraphTable();
	}
	
	this.AddResult = function(testResult) {
	
		// Type check
		if(testResult.__type != "TestResult")
			throw "Type mismatch!";
		
		// Store the test result
		this.arrResults.push(testResult);
		
		if(this.iSelectedIndex == -1)
			this.iSelectedIndex = 0;
		
		if(this.iDisplayStart == -1)
			this.iDisplayStart = 0;
		
		// Refresh the graph table
		this.ClearGraphTable();
		this.PopulateGraphTable();
		this.InvalidateToolBar();
		this.UpdateDetailsDiv();
	}
	
	this.ClearGraphTable = function() {
	
		// Clear the graph table
		while(this.hProgressTableGraphDiv.childNodes.length > 0)
			this.hProgressTableGraphDiv.removeChild(this.hProgressTableGraphDiv.firstChild);
	}
	
	this.PopulateGraphTable = function() {
	
		// Declare variables
		var newDiv, newBar;
		var iHeight;
		var iScore, iCorrectAnswers;
		
		for(i = this.iDisplayStart; i < this.arrResults.length && i < this.iDisplayStart + this.iDisplayLength; i++) {
		
			// Calculate score
			iCorrectAnswers = this.arrResults[i].iQuestionCount - this.arrResults[i].iErrorCount;
			iScore = Math.round(iCorrectAnswers / this.arrResults[i].iQuestionCount * 100);
			
			if(iScore < 10)
				iScore = 10;
			
			// Calculate height
			iHeight = iScore * 2;
			
			if(i == this.iSelectedIndex) {
				newDiv = document.createElement("DIV");
				newDiv.className = "graphBarContainerHighlight";
				newDiv.style.top = "10px";
				newDiv.style.left = "5px";
				this.hProgressTableGraphDiv.appendChild(newDiv);
				
				newBar = document.createElement("DIV");
				newBar.className = "graphBarDiv";
				newBar.style.height = iHeight + "px";
				newBar.style.top = (209 - iHeight) + "px";
				newBar.style.left = "24px";
				newBar.parentContainer = newDiv;
				newBar.resultIndex = i;
				newBar.owningClass = this;
				//newBar.onmouseover = this.OnGraphBarMouseOver;
				//newBar.onmouseout = this.OnGraphBarMouseOut;
				newBar.onclick = this.OnGraphBarClick;
				newDiv.appendChild(newBar);
			} else {
				newDiv = document.createElement("DIV");
				newDiv.className = "graphBarContainer";
				newDiv.style.top = "10px";
				newDiv.style.left = "5px";
				this.hProgressTableGraphDiv.appendChild(newDiv);
				
				newBar = document.createElement("DIV");
				newBar.className = "graphBarDiv";
				newBar.style.height = iHeight + "px";
				newBar.style.top = (210 - iHeight) + "px";
				newBar.style.left = "25px";
				newBar.parentContainer = newDiv;
				newBar.resultIndex = i;
				newBar.owningClass = this;
				//newBar.onmouseover = this.OnGraphBarMouseOver;
				//newBar.onmouseout = this.OnGraphBarMouseOut;
				newBar.onclick = this.OnGraphBarClick;
				newDiv.appendChild(newBar);
			}
			
			this.arrGraphBarContainers[i] = newBar;
		}
	}
	
	this.UpdateDetailsDiv = function() {
	
		// Declare variables
		var newHeader;
		var newP, newB;
		var hTestResult;
		var iCorrectAnswers, iScore, iPercent
		var strScore, strDuration
		var dTime;

		// Clear the graph table
		while(this.hResultDetailsDiv.childNodes.length > 0)
			this.hResultDetailsDiv.removeChild(this.hResultDetailsDiv.firstChild);
			
		// Load test results
		if(this.bPeekViewing) {
			hTestResult = this.arrResults[this.iViewIndex];
		} else {
			hTestResult = this.arrResults[this.iSelectedIndex];
		}
		
		// Calculate score
		iCorrectAnswers = hTestResult.iQuestionCount - hTestResult.iErrorCount;
		iScore = Math.round(iCorrectAnswers / hTestResult.iQuestionCount * 10);
		iPercent = Math.round(iCorrectAnswers / hTestResult.iQuestionCount * 100);
		strScore = iScore + " - " + iCorrectAnswers + "/" + hTestResult.iQuestionCount + " goed (" + iPercent + "%)";
		
		// Calculate duration
		dTime = new Date();
		dTime.setTime(hTestResult.iDuration * 1000);
		strTime = (dTime.getHours() - 1) + ":";
		if(dTime.getMinutes() < 10) {
			strTime += "0" + dTime.getMinutes() + ":";
		} else {
			strTime += dTime.getMinutes() + ":";
		}
		if(dTime.getSeconds() < 10) {
			strTime += "0" + dTime.getSeconds();
		} else {
			strTime += dTime.getSeconds();
		}
		
		// Header
		newHeader = document.createElement("H2");
		newHeader.appendChild(document.createTextNode("Details resultaten van overhoring"));
		this.hResultDetailsDiv.appendChild(newHeader);
		
		// Score
		newP = document.createElement("P");
		newB = document.createElement("B");
		newB.appendChild(document.createTextNode("Score: "));
		newP.appendChild(newB);
		newP.appendChild(document.createTextNode(strScore));
		this.hResultDetailsDiv.appendChild(newP);
		
		// Date
		newP = document.createElement("P");
		newB = document.createElement("B");
		newB.appendChild(document.createTextNode("Datum: "));
		newP.appendChild(newB);
		newP.appendChild(document.createTextNode(hTestResult.dateTime.toLocaleString()));
		this.hResultDetailsDiv.appendChild(newP);
		
		// Duration
		newP = document.createElement("P");
		newB = document.createElement("B");
		newB.appendChild(document.createTextNode("Duur: "));
		newP.appendChild(newB);
		newP.appendChild(document.createTextNode(strTime));
		this.hResultDetailsDiv.appendChild(newP);
	}
	
	this.SelectResult = function(index) {
	
		// Check if index is in range
		if(index < 0 || index > this.arrResults.length - 1)
			throw "Index out of range";
		
		this.iSelectedIndex = index;
		
		this.UpdateDetailsDiv();
		this.InvalidateToolBar();
		this.ClearGraphTable();
		this.PopulateGraphTable();
	}
	
	this.InvalidateToolBar = function() {
		
		if(this.arrResults.length > 0) {
			if(this.iSelectedIndex <= 0) {
				this.hBackButton.Disable();
			} else {
				this.hBackButton.Enable();
			}
			
			if(this.iSelectedIndex >= this.arrResults.length - 1) {
				this.hFowardButton.Disable();
			} else {
				this.hFowardButton.Enable();
			}
		} else {
			this.hBackButton.Disable();
			this.hFowardButton.Disable();
		}
		
	}
	
	this.Initialize();
}

