// AGEA Viewer Application -- atlasplates.js
// Copyright (c) 2007-2008 Allen Institute for Brain Science. All Rights Reserved.

coronalPlates = new Array(1, 6, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63, 67, 70, 75, 79, 83, 86, 91, 95, 99, 102, 107, 111, 115, 119, 123, 126, 131, 135, 139, 142, 146, 151, 154, 158, 162, 166, 170, 174, 179, 182, 186, 190, 194, 198, 202, 206, 211, 214, 219, 223, 227, 231, 235, 239, 243, 247, 251, 255, 259, 263, 267, 271, 275, 278, 283, 287, 291, 293, 299, 302, 306, 310, 314, 319, 323, 327, 331, 335, 338, 343, 347, 351, 355, 359, 363, 367, 370, 375, 379, 383, 387, 391, 395, 399, 403, 407, 411, 415, 419, 423, 427, 431, 435, 439, 442, 447, 451, 455, 459, 463, 467, 471, 475, 479, 483, 487, 491, 495, 499, 503, 507, 511, 515, 519, 523, 527);


function findCoronalAtlasLevel(x)
{
	sectionLocation = 528 - x - 1;
	
	index = coronalPlates.lower_bound(sectionLocation);
	if ((coronalPlates[index] != sectionLocation) && (index != coronalPlates.length - 1))
		if ((coronalPlates[index + 1] - sectionLocation) < (sectionLocation - coronalPlates[index]))
			index = index + 1;
	
	return index;
}


function coronalAtlasPlateId(index)
{
	return 130879 + index;
}


function coronalAtlasPlateNum(index)
{
	return coronalPlates[index];
}


function getAtlasLevel(atlasIndex)
{
	return (132 - Math.round(coronalAtlasPlateNum(atlasIndex) / 4) + 1);
}


function coronalAtlasPlatePath(atlasPlateHost, index)
{
	index = coronalPlates[index];
	
	if (index >= 100)
		strIndex = index;
	else if (index >= 10)
		strIndex = "0" + index;
	else
		strIndex = "00" + index;
	
	return atlasPlateHost + "/pyramid/production1/SRGEReferenceAtlas/Coronal/Zoomify/Atlas-Coronal-" + strIndex + ".aff/TileGroup0/";
}


Array.prototype.lower_bound = function(item)
{
	var left = -1, right = this.length, mid;
		
	while (right - left > 1)
	{
		mid = (left + right) >>> 1;
		if (this[mid] < item)
			left = mid;
		else
			right = mid;
	}
	
	if (left >= this.length)
		left = this.length - 1;
	if (left < 0)
		left = 0;
	
	return left;
}
