// JavaScript Document


function fix_width(){
	
	if (totalImages==8||totalImages==4||totalImages==12||totalImages==16||totalImages==24){
		
		document.getElementById('menu_outer').style.width="184px";
	}
	else if (totalImages==9||totalImages==18||totalImages==21){
		document.getElementById('menu_outer').style.width="138px";
	}
}


function loadNextImage(){
	imageLoadedArray[currentLoadImage]=true;
	currentLoadImage++;
	//alert('loading next image ' + currentLoadImage);
	if (currentLoadImage<=totalImages ){
		if 	(!imageLoadedArray[currentLoadImage]){
			//alert("loading " + currentLoadImage);
			imageArray[currentLoadImage].src = imageSrcArray[currentLoadImage];
			imageArray[currentLoadImage].onLoad = loadNextImage();
		}
		else {
			loadNextImage();
		}
	}
	else if (currentLoadImage==totalImages+1 && notes){
		
		if 	(!imageLoadedArray[currentLoadImage]){
			//alert("loading " + currentLoadImage);
			imageArray[currentLoadImage].src = imageSrcArray[currentLoadImage];
			imageArray[currentLoadImage].onLoad = loadNextImage();
		}
		else {
			loadNextImage();
		}
	}
}


function fix_image_margins(mywidth, myheight){
	if (mywidth==0){mywidth=500;}
	if (myheight==0){myheight=500;}
	margin = (500-mywidth)/2;
	document.getElementById('main_img').style.paddingLeft=margin+"px";
	margin = (500-myheight)/2;
	//alert(mywidth + " " + myheight);
	document.getElementById('main_img').style.paddingTop=margin+"px";
}
function showImage(num){
	
	if (currentImage!=num ){
		
		$("#main_img").addClass('loader');
		thisID="#imageID"+currentImage;
		$(thisID).fadeOut();
		
		document.getElementById('trans_'+currentImage).style.display='block';
		document.getElementById('trans_'+num).style.display='none';
			
		currentImage=num;
		fixArrows();   
		
		
        $(imageDisplayArray[num]).load(function () {
            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
            $(this).hide();
			$("#main_img").empty();
			$("#main_img").removeClass('loader');
            $('#main_img').append(this);
			imageLoadedArray[currentImage]=true;
			fix_image_margins(this.width, this.height);
            $(this).fadeIn();
        }).error(function () {
            // notify the user that the image could not be loaded
        }).attr('src', imageSrcArray[num]);
		
		
		
		
	}

}

function fixArrows(){
	eImages=totalImages;
	if (notes){
			eImages=totalImages+1;
	}
	if (currentImage==eImages ){
		document.getElementById('right_arrow').style.display='none';
	}
	else {
		document.getElementById('right_arrow').style.display='block';
	}
	
	if (currentImage==1){
		document.getElementById('left_arrow').style.display='none';
	}
	else {
		document.getElementById('left_arrow').style.display='block';
	}
}

function switchImage(num){
	//alert ('switching' + num);
		
			document.getElementById('main').src=imageSrcArray[num];
			fix_image_margins(imageArray[num].width, imageArray[num].height);
			document.getElementById('trans_'+currentImage).style.display='block';
			document.getElementById('trans_'+num).style.display='none';
			currentImage=num;
	
}
