// JavaScript Document


var talkativeness = 0; 
var showScienceTalk = true;
var showHighlighter = false;
var works_item = "dissertation";


function setTalkativeness(value) {
	talkativeness = value; 
	renderView(); 
}

function setShowScienceTalk(value) {
	showScienceTalk = value; 
	renderView(); 
}

function setShowHighlighter(value) {
	showHighlighter = value; 
	renderView(); 
}


function renderView() {
	renderText(); 
	renderControl(); 
}


function renderControl() {
	
	var hashString = "life-" + talkativeness; 

	if (talkativeness == 0) {

		$("#control-a-0 a").addClass("selected"); 
		$("#control-a-1 a").removeClass("selected"); 
		$("#control-a-2 a").removeClass("selected"); 
		
	} else if (talkativeness == 1) {
		
		$("#control-a-0 a").addClass("selected"); 
		$("#control-a-1 a").addClass("selected"); 
		$("#control-a-2 a").removeClass("selected"); 
		
	} else if (talkativeness == 2) {
		
		$("#control-a-0 a").addClass("selected"); 
		$("#control-a-1 a").addClass("selected"); 
		$("#control-a-2 a").addClass("selected"); 
		
	}
	
	if (showScienceTalk) {
		
		hashString = hashString  + "1"; 
		
		$("#control-b-0 a").removeClass("selected"); 
		$("#control-b-1 a").addClass("selected"); 
	
	} else {
		
		hashString = hashString  + "0"; 

		$("#control-b-0 a").addClass("selected"); 
		$("#control-b-1 a").removeClass("selected"); 
	}
	
	
	if (showHighlighter) {
	
		hashString = hashString  + "1"; 
	
		$("#control-c-0 a").removeClass("selected"); 
		$("#control-c-1 a").addClass("selected"); 
	
	} else {
	
		hashString = hashString  + "0"; 

		$("#control-c-0 a").addClass("selected"); 
		$("#control-c-1 a").removeClass("selected"); 
	}
	

	document.location.hash = hashString;

}


function renderText() {
	
	$(".tfk-b-0").show(); 

	if (talkativeness == 0) {
		
		$(".tfk-a-0").show(); 
		$(".tfk-a-1").hide(); 
		$(".tfk-a-2").hide(); 	
	
	} else if (talkativeness == 1) {
	
		$(".tfk-a-0").show(); 
		$(".tfk-a-1").show(); 
		$(".tfk-a-2").hide(); 
	
	} else if (talkativeness == 2) {
	
		$(".tfk-a-0").show(); 
		$(".tfk-a-1").show(); 
		$(".tfk-a-2").show(); 
	
	}
	
	if (showScienceTalk) {

	} else {
	
			$(".tfk-b-0").hide(); 

	}
	
	if (showHighlighter) {
		
			$(".tfk-a-0").addClass("tfk-a-0-highlighted tfk-highlighted"); 
			$(".tfk-a-1").addClass("tfk-a-1-highlighted tfk-highlighted"); 
			$(".tfk-a-2").addClass("tfk-a-2-highlighted tfk-highlighted"); 
			$(".tfk-b-0").addClass("tfk-b-0-highlighted tfk-highlighted"); 
			
	
	} else {
	
			$(".tfk-a-0").removeClass("tfk-a-0-highlighted"); 
			$(".tfk-a-0").removeClass("tfk-highlighted"); 
			$(".tfk-a-1").removeClass("tfk-a-1-highlighted"); 
			$(".tfk-a-1").removeClass("tfk-highlighted"); 
			$(".tfk-a-2").removeClass("tfk-a-2-highlighted"); 
			$(".tfk-a-2").removeClass("tfk-highlighted"); 
			$(".tfk-b-0").removeClass("tfk-b-0-highlighted"); 
			$(".tfk-b-0").removeClass("tfk-highlighted"); 
	}

}

$(document).ready(function() {
	
	
	$("#works-toc tr td").mouseover(function() {
		showWorksItem($("a", $(this).parent("")).attr("href").substring(1));
	}); 	

	
	var hashString = window.location.hash.substr(1);
	if (hashString == "") {
		hashString = "life-000";
		window.location.hash = hashString; 	
	}
	
	if (hashString.substring(0,4) == "life") {
		
		showLifeSection(); 
	
		talkativeness = hashString.substring(5,6);
	
		if (hashString.substring(6,7) == "1")
			showScienceTalk = true;
		else
			showScienceTalk = false;

		if (hashString.substring(7,8) == "1")
			showHighlighter = true;
		else
			showHighlighter = false;
	
		renderText(); ;
		renderControl()
		
		
	} else if (hashString.substring(0,5) == "works") {
		works_item = hashString.substring(6);		
		showWorksSection(); 
		showWorksItem(works_item); 
	}

					   
	$(".section-heading").html(""); 	
	
	$("#life-button").mouseover(function() {
		showLifeSection(); 	
		
	});
	
	$("#works-button").mouseover(function() {
		showWorksSection(); 
		showWorksItem(works_item);
		window.location.hash = "works-" + works_item;
	
	});
	
}); 


function showLifeSection() {
	$("#life-button").removeClass("life-button-inactive"); 
	$("#life-button").addClass("life-button-active"); 
	
	//$("#life-button").animate({"padding-top": "+=210px" "margin-top": "-=120px"}, "slow");
	
	$("#life-section").show(); 

	$("#works-button").removeClass("works-button-active"); 
	$("#works-button").addClass("works-button-inactive"); 
	$("#works-section").hide(); 
	renderControl(); 
	renderText(); 
}
function showWorksSection() {
	$("#life-button").removeClass("life-button-active"); 
	$("#life-button").addClass("life-button-inactive");
	$("#life-section").hide(); 
		
	$("#works-button").removeClass("works-button-inactive"); 
	$("#works-button").addClass("works-button-active"); 
	$("#works-section").show(); 
}
function showWorksItem(newWorksItem) {
	
	
	$("#works-toc td").removeClass("highlighted");
	$("td", $("a[href=#" + newWorksItem + "]").parent("").parent("")).addClass("highlighted");
	$(".works-body").hide();
		
	//alert("#" + newWorksItem); 
	$("#" + newWorksItem).show(); 
		
	hashString = "works-" + newWorksItem; 
	window.location.hash = hashString;
	works_item = newWorksItem; 
}


var d = 15;
var steps_x = 59;
var steps_y = 14;
var border = 5; 

var matrix0 = Array(); 
var matrix1 = Array();


var matrix2 = [[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.145, 0.192, 0.165, 0.145, 0.129, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.263, 0.2, 0.161, 0.322, 0.388, 0.286, 0.286, 0.235, 0.141, 0.122, 0.122, 0.122],
[0.451, 0.439, 0.4, 0.4, 0.482, 0.408, 0.325, 0.259, 0.255, 0.133, 0.125, 0.122],
[0.596, 0.584, 0.447, 0.318, 0.4, 0.561, 0.561, 0.498, 0.431, 0.353, 0.275, 0.2],
[0.694, 0.627, 0.373, 0.408, 0.478, 0.58, 0.612, 0.592, 0.565, 0.525, 0.475, 0.416],
[0.714, 0.616, 0.243, 0.369, 0.569, 0.643, 0.647, 0.647, 0.616, 0.588, 0.537, 0.482],
[0.729, 0.569, 0.239, 0.529, 0.58, 0.655, 0.678, 0.651, 0.6, 0.588, 0.557, 0.506],
[0.761, 0.588, 0.333, 0.38, 0.502, 0.561, 0.561, 0.561, 0.439, 0.478, 0.518, 0.333],
[0.769, 0.631, 0.678, 0.69, 0.682, 0.694, 0.753, 0.714, 0.392, 0.4, 0.545, 0.31],
[0.765, 0.643, 0.722, 0.824, 0.843, 0.851, 0.882, 0.863, 0.525, 0.506, 0.643, 0.498],
[0.757, 0.569, 0.451, 0.675, 0.78, 0.8, 0.816, 0.788, 0.537, 0.698, 0.667, 0.576],
[0.769, 0.424, 0.455, 0.584, 0.765, 0.827, 0.792, 0.769, 0.647, 0.784, 0.647, 0.569],
[0.753, 0.384, 0.604, 0.608, 0.753, 0.863, 0.859, 0.827, 0.859, 0.804, 0.663, 0.565],
[0.769, 0.471, 0.424, 0.663, 0.792, 0.831, 0.878, 0.831, 0.812, 0.784, 0.604, 0.647],
[0.773, 0.561, 0.525, 0.718, 0.808, 0.804, 0.847, 0.847, 0.788, 0.761, 0.698, 0.663],
[0.722, 0.596, 0.533, 0.761, 0.816, 0.78, 0.788, 0.796, 0.78, 0.737, 0.706, 0.682],
[0.655, 0.671, 0.698, 0.773, 0.773, 0.733, 0.737, 0.749, 0.745, 0.71, 0.678, 0.635],
[0.639, 0.694, 0.722, 0.718, 0.694, 0.671, 0.663, 0.651, 0.643, 0.62, 0.569, 0.533],
[0.584, 0.639, 0.69, 0.643, 0.651, 0.643, 0.639, 0.584, 0.345, 0.329, 0.365, 0.38],
[0.427, 0.651, 0.804, 0.6, 0.675, 0.8, 0.761, 0.29, 0.125, 0.122, 0.125, 0.129],
[0.094, 0.702, 0.596, 0.647, 0.529, 0.376, 0.204, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.078, 0.161, 0.153, 0.11, 0.122, 0.122, 0.118, 0.118, 0.122, 0.122, 0.122, 0.122],
[0.106, 0.09, 0.09, 0.11, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.09, 0.09, 0.102, 0.118, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.118, 0.118, 0.118, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122],
[0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122, 0.122]
];


function initializeMatrix(m, k, l) {
	
	for (i = 0; i < k; i++) {
	
		m[i] = new Array(); 

	
		for (j = 0; j < l; j++) {
		
			m[i][j] = Math.abs(Math.sin(i*j)*Math.sin(i*j)) * Math.abs((Math.random() * 10) - 9)/10;
			m[i][j] = Math.random();

		}
	}
}



function renderMatrix(m) {
	
	for (i in m) {
		
		for (j in m[i]) {
			
			x0 = i * d; 
			y0 = j * d; 
			height = d; //m[i][j] * d; 
			
			
			$("#grid_" + i + "_" + j).css("left", x0 + "px"); 
			$("#grid_" + i + "_" + j).css("top", y0 + "px"); 
			//$("#grid_" + i + "_" + j).css("width", height + "px"); 
			//$("#grid_" + i + "_" + j).css("height", height + "px"); 
		
			color = m[i][j] * 10;
			color = Math.round(color*1.3) + 0;
			
	/*		if (color < 10) border = 6;
			if (color < 7) border = 7;
			if (color < 5) border = 8;
		*/	
			$("#grid_" + i + "_" + j).css("width", 10 + "px"); 
			$("#grid_" + i + "_" + j).css("height", 10 + "px"); 

			
			if (color == 0 || color ==1) color = 2;
			if (color == 10) color = "A"; 
			if (color == 11) color = "B"; 
			if (color == 12) color = "C"; 
			if (color == 13) color = "D"; 
			if (color == 14) color = "E"; 
			if (color == 15) color = "F"; 
			
				$("#grid_" + i + "_" + j).css("background-color", "#" + color + "" + color + "" + color + "" + color + "" + color + "" + color); 
	
	
	}
	}		 
}


function renderMatrix2(m) {
	
	for (i in m) {
		
		for (j in m[i]) {
			
			x0 = 5 +  (2*i + 1/2 - m[i][j]) * d / 2; 
			y0 = 5 + (2*j + 1/2 - m[i][j]) * d / 2; 
			height = m[i][j] * d; 
			
			
			$("#grid_" + i + "_" + j).css("left", x0 + "px"); 
			$("#grid_" + i + "_" + j).css("top", y0 + "px"); 
			//$("#grid_" + i + "_" + j).css("width", height + "px"); 
			//$("#grid_" + i + "_" + j).css("height", height + "px"); 
			$("#grid_" + i + "_" + j).css("width", d + "px"); 
			$("#grid_" + i + "_" + j).css("height", d + "px"); 
	
		}
	}		 
}

var steps = 2;


function transition(step) {
	
	$("#stepLog").html(step);
	
	if (step < steps) {
		
		// calculate intermediate matrix
		var m = matrix0;
		
		for (i in m) {
		
			for (j in m[i]) {
			
				m[i][j] = matrix0[i][j]  + step * (matrix1[i][j] - matrix0[i][j]) / steps;

			}
		}
		
		
		// render intermediate matrix
		
		renderMatrix(m);
		
		
		var nextStep = step + 1; 									
		var t = setTimeout("transition(" + nextStep + ");", 100);
		
	} else {
		
		for (i in m) {
		
			for (j in m[i]) {
			
				matrix0[i][j] = matrix1[i][j];

			}
		}
		
		//initializeMatrix(matrix1, steps_x,steps_y);
		var t = setTimeout("transition(0);", 1000);
		
	}

	
}


function createGrid(k, l) {
	
for (i = 0; i < k; i++) {
	
		for (j = 0; j < l; j++) {

		var div = jQuery("<div id='grid_" + i + "_" + j +"' class='gridElement'></div>");
		div.appendTo($("#grid"));
		
		}
	}
	
}

$(document).ready(function() {
												 
												 createGrid(steps_x,steps_y);
												 initializeMatrix(matrix0, steps_x,steps_y);
												 //initializeMatrix(matrix1, steps_x,steps_y);
												 renderMatrix(matrix2); 
												 //transition( 0); 
						   });
