//Variables for page operation
var currentLatitude = 3;  //Set Camera to default latitude (0-6)
var currentLongitude = 0; //Set Camera to default longitude (0-7)
var currentColor = "white";
var pyloriPath = "./bacteria/pylori/wcl/h.pylori-0200-white%20copyright%20logo%20";
var pyloriBigPath = "./bacteria/pylori/wclw/h.pylori-0800-white%20copyright%20logo%20watermark%20";
var yeastPath = "./bacteria/yeast/wcl/s.cereviceae-0200-white%20copyright%20logo%20";
var yeastBigPath = "./bacteria/yeast/wclw/s.cereviceae-0800-white%20copyright%20logo%20watermark%20";
var yeastgroupPath = "./bacteria/yeastgroup/wcl/s.cereviceae%20group-0200-white%20copyright%20logo%20";
var yeastgroupBigPath = "./bacteria/yeastgroup/wclw/s.cereviceae%20group-0800-white%20copyright%20logo%20watermark%20";

var descriptionPath = "./bacteria/pylori/description.jpg";

var currentBacteria = "pylori";
var currentPath = pyloriPath;
var currentBigPath = pyloriBigPath;
var imageExtension = ".jpg";

//Establish Users Browser
var isIE = navigator.appVersion.indexOf("MSIE")>0;
var isNav = (navigator.appName == "Netscape");
var bigImageWindow;
var purchaseWindow;

preloadImages();

//If Netscape browser, Setup Keypress Capturing
if (isNav) 
{
	window.captureEvents(Event.KEYPRESS);
	window.onKeyPress = NNhandlePress;
}

function showBiggerImage()
{
	var bigImagePath = currentBigPath + makeArbitraryNumber(currentLatitude,currentLongitude)+imageExtension;
	bigImageWindow = window.open('#',makeArbitraryNumber(currentLatitude,currentLongitude)+currentColor,'height=600,width=800,toolbar=0,scrollbars=yes,resizable=yes');
	
	bigImageWindow.document.writeln('<CENTER><IMG src="');
	bigImageWindow.document.writeln(bigImagePath);
	bigImageWindow.document.writeln('"></CENTER>');
	bigImageWindow.document.close();

	bigImageWindow.window.moveTo(0,0);
	if (document.all) 
	{
		bigImageWindow.resizeTo(screen.availWidth,screen.availHeight);
	}
	else 
	if (document.layers||document.getElementById) 
	{
		if (bigImageWindow.outerHeight<screen.availHeight||bigImageWindow.outerWidth<screen.availWidth)
		{
			bigImageWindow.outerHeight = screen.availHeight;
			bigImageWindow.outerWidth = screen.availWidth;
		}
	}
	bigImageWindow.focus();
}

function preloadImages()
{
	MM_preloadImages('./img/ArrowBall_Up.jpg','./img/ArrowBall_Right.jpg','./img/ArrowBall_Left.jpg','./img/ArrowBall_Down.jpg');
}

//Test function for browser-type checking.
function indicateBrowser()
{
	if (isIE)
	{
		alert("IE here");
	}				 
	if (isNav)
	{
		alert("NAV here");
	}
}

function indicateBrowserAppName()
{
	alert(navigator.appName);
}

//IE compatible key handler
function IEonKeyDownHandler(event)
{
	switch (event.keyCode)
	{
	case 37:
		turnLeft();
		break;
	case 38:
		tiltUp();
		break;
	case 39:
		turnRight();
		break;
	case 40:
		tiltDown();
		break;
	case 52:
		turnLeft();
		break;					
	case 54:
		turnRight();
		break;
	}
}

//Netscape compatible key handler.
function NNhandlePress(evnt)
{
	
	switch (evnt.which)
	{
	case 52:
		turnLeft();
		break;					
	case 54:
		turnRight();
		break;
	case 56:
		tiltUp();
		break;					
	case 50:
		tiltDown();
		break;
	}
	
	return true;
}

function getPath(bacteria,color)
{
	var thePath = "./bacteria/" + bacteria + "/";
	if (color == "white")
	{
		thePath = thePath + "wcl/";					
	}
	if (color == "black")
	{
		thePath = thePath + "bcl/";
	}
	if (color == "art")
	{
		thePath = thePath + "acl/";
	}
	if (bacteria == "pylori")
	{
		thePath = thePath + "h.pylori-0200-";
	}
	if (bacteria == "yeast")
	{
		thePath = thePath + "s.cereviceae-0200-";
	}
	if (bacteria == "yeastgroup")
	{
		thePath = thePath + "s.cereviceae%20group-0200-";
	}
	thePath = thePath + color + "%20copyright%20logo%20";
	return thePath;
}

function getBiggerPicturePath(bacteria,color)
{
	var thePath = "./bacteria/" + bacteria + "/";
	if (color == "white")
	{
		thePath = thePath + "wclw/";					
	}
	if (color == "black")
	{
		thePath = thePath + "bclw/";
	}
	if (color == "art")
	{
		thePath = thePath + "aclw/";
	}
	if (bacteria == "pylori")
	{
		thePath = thePath + "h.pylori-0800-";
	}
	if (bacteria == "yeast")
	{
		thePath = thePath + "s.cereviceae-0800-";
	}
	if (bacteria == "yeastgroup")
	{
		thePath = thePath + "s.cereviceae%20group-0800-";
	}
	thePath = thePath + color + "%20copyright%20logo%20watermark%20";
	return thePath;
}

function turnLeft()
{
	if (currentLongitude == 0)
	{
		currentLongitude = 7;	
	}
	else
	{
		currentLongitude--;	
	}
	updateImages();
}

function turnRight()
{
	if (currentLongitude == 7)
	{
		currentLongitude = 0;	
	}
	else
	{
		currentLongitude++;	
	}
	updateImages();
	
}

function tiltUp()
{
	if (currentLatitude > 0)
	{
		currentLatitude--;
		changeLatitude(currentLatitude);
	}
	else
	{
		currentLatitude = 6;
		changeLatitude(currentLatitude);
	}
}

function tiltDown()
{
	if (currentLatitude < 6)
	{
		currentLatitude++;
		changeLatitude(currentLatitude);
	}
	else
	{
		currentLatitude = 0;
		changeLatitude(currentLatitude);
	}
}

function changeLatitude(latitude)
{
	currentLatitude = latitude;
	var nextImagePath = currentPath + makeArbitraryNumber(currentLatitude,currentLongitude) +imageExtension;
	
	MM_swapImage('bacteriaImage','',nextImagePath,1);


	document.numberForm.numberSelect.selectedIndex = getNumberSelectIndex(makeArbitraryNumber(currentLatitude,currentLongitude)+"");
	
}

function getNumberSelectIndex(arbNum)
{
	if (arbNum.charAt(0) == '0')
	{
		arbNum = parseInt(arbNum.charAt('1'));
	}
	else
	{
		arbNum = parseInt(arbNum);
	}
	return arbNum;
}

function makeArbitraryNumber(curLat,curLong)
{
	var arbitraryNumber = ((curLat*8) + (curLong));
	if (arbitraryNumber < 10)
	{
		arbitraryNumber = "0" + arbitraryNumber;
	}
	return arbitraryNumber;
}

function updateImages()
{
	
	MM_swapImage('bacteriaImage','',currentPath + makeArbitraryNumber(currentLatitude,currentLongitude) +imageExtension,1);
	MM_swapImage('thumb0','',currentPath+makeArbitraryNumber(0,currentLongitude)+imageExtension,1);
	MM_swapImage('thumb1','',currentPath+makeArbitraryNumber(1,currentLongitude)+imageExtension,1);
	MM_swapImage('thumb2','',currentPath+makeArbitraryNumber(2,currentLongitude)+imageExtension,1);
	MM_swapImage('thumb3','',currentPath+makeArbitraryNumber(3,currentLongitude)+imageExtension,1);
	MM_swapImage('thumb4','',currentPath+makeArbitraryNumber(4,currentLongitude)+imageExtension,1);	MM_swapImage('thumb5','',currentPath+makeArbitraryNumber(5,currentLongitude)+imageExtension,1);
	MM_swapImage('thumb6','',currentPath+makeArbitraryNumber(6,currentLongitude)+imageExtension,1);

	document.numberForm.numberSelect.selectedIndex = getNumberSelectIndex(makeArbitraryNumber(currentLatitude,currentLongitude)+"");
}

function newBacteriaSelected(form)
{
	
	for (var i=0; i < form.bacteriaSelect.options.length; i++)
	{
		if (form.bacteriaSelect.options[i].selected)
		{
			if (currentBacteria != form.bacteriaSelect.options[i].value)
			{
				currentBacteria = form.bacteriaSelect.options[i].value;

				modifyAvailableColors(form);
				switch (currentBacteria)
				{
					case "pylori":
						currentPath = pyloriPath;
						currentBigPath = pyloriBigPath;
						descriptionPath = "./bacteria/pylori/description.jpg";
						titlePath = "./bacteria/pylori/title.jpg";
						break;
					case "yeast":
						currentPath = yeastPath;
						currentBigPath = yeastBigPath;
						descriptionPath = "./bacteria/yeast/description.jpg";
						titlePath = "./bacteria/yeast/title.jpg";
						break;
					case "yeastgroup":
						currentPath = yeastgroupPath;
						currentBigPath = yeastgroupBigPath;
						descriptionPath = "./bacteria/yeastgroup/description.jpg";
						titlePath = "./bacteria/yeastgroup/title.jpg";
						break;
				}
				currentLatitude = 3;
				currentLongitude = 0;
				changeColor(document.selector.colorSelect.options[document.selector.colorSelect.selectedIndex].value);
				updateImages();
				updateDescription();
			}
		}
	}
	var selectedBacteria = form.bacteriaSelect
}

function updateDescription()
{
		MM_swapImage('description','',descriptionPath,1);
		MM_swapImage('title','',titlePath,1);
}

function changeColor(color)
{
	currentPath = getPath(currentBacteria,color);
	currentColor = color;
	currentBigPath = getBiggerPicturePath(currentBacteria,color);
	updateImages();
}

function getDimensions()
{
	alert("The current available area is HEIGHT> "+screen.availHeight+" WIDTH>"+screen.availWidth);
}

function getItHere()
{
	var informationString = "?imageNumber="+document.numberForm.numberSelect.options[document.numberForm.numberSelect.selectedIndex].value + "&bacteriaType="+document.selector.bacteriaSelect.options[document.selector.bacteriaSelect.selectedIndex].value + "&color="+document.selector.colorSelect.options[document.selector.colorSelect.selectedIndex].value;
	
	var bigImagePath = currentBigPath + makeArbitraryNumber(currentLatitude,currentLongitude)+imageExtension;
	
	if (purchaseWindow != null)
	{
		purchaseWindow.close();	
	}
		
	purchaseWindow = window.open("../cgi-bin/getPicture.pl"+informationString,"purchase",'resizable=yes,height=500,width=600,toolbar=0,scrollbars=yes');
	purchaseWindow.focus();
}

function viewSelection()
{
	if (purchaseWindow != null)
	{
		purchaseWindow.close();	
	}
	purchaseWindow = window.open("../cgi-bin/processSelection.pl","purchase",'resizable=yes,height=500,width=600,toolbar=0,scrollbars=yes');
	purchaseWindow.focus();
}

function orderSelection()
{
	if (purchaseWindow != null)
	{
		purchaseWindow.close();	
	}
	purchaseWindow = window.open("../cgi-bin/orderSentry.pl","purchase",'resizable=yes,height=500,width=600,toolbar=0,scrollbars=yes');
	purchaseWindow.focus();
}

function selectImageByNumber(theArbitraryNumber)
{
	currentLatitude = getCurLatFromArbNum(theArbitraryNumber);
	currentLongitude = getCurLongFromArbNum(theArbitraryNumber);
	updateImages();
}

function getCurLatFromArbNum(arbNum)
{
	if (arbNum.charAt(0) == '0')
	{
		arbNum = parseInt(arbNum.charAt('1'));
	}
	else
	{
		arbNum = parseInt(arbNum);
	}
	return parseInt(Math.floor(arbNum / 8));
}

function getCurLongFromArbNum(arbNum)
{
	if (arbNum.charAt(0) == '0')
	{
		arbNum = parseInt(arbNum.charAt('1'));
	}
	else
	{
		arbNum = parseInt(arbNum);
	}
	return parseInt(arbNum % 8);
}

function modifyAvailableColors(form)
{
	var selectLength;
	var newOption;
	switch (currentBacteria)
	{
		case "pylori":
			newOption = new Option("Art","art");
			selectLength = form.colorSelect.options.length;
			form.colorSelect.options[2] = newOption;
			break;
		case "yeast":	
			form.colorSelect.options[2] = null;
			break;
		case "yeastgroup":
			form.colorSelect.options[2] = null;
			break;
	}
}