// JavaScript Document

$(document).ready(function() {
	SetPageImages();
	$('#btnPrev').click(function() {
		Prev();
	});
	$('#btnNext').click(function() {
		Next();
	});
	$('#catalogue-start').click(function() {
		Next();
	});
	
	$('#catalogueLink').click(function() {
		document.getElementById('catalogue').style.display = 'block';
	});
});
function SetPageImages() {
	//preload images
	if(arImages[intCurPage][0]=='')	imgFrontLeft	= null;
	else							imgFrontLeft	= strImagesFolder + arImages[intCurPage][0];
	if(arImages[intCurPage][1]=='')	imgFrontRight	= null;
	else							imgFrontRight	= strImagesFolder + arImages[intCurPage][1];
	if(intCurPage>0) {
		imgPrevLeft		= strImagesFolder + arImages[(intCurPage-1)][0];
		imgPrevRight	= strImagesFolder + arImages[(intCurPage-1)][1];
	} else {
		imgPrevLeft		= null;
		imgPrevRight	= null;
	}
	if(intCurPage<(arImages.length-1)) {
		imgNextLeft		= strImagesFolder + arImages[(intCurPage+1)][0];
		imgNextRight	= strImagesFolder + arImages[(intCurPage+1)][1];
	} else {
		imgNextLeft		= null;
		imgNextRight	= null;
	}
	//assign images
	if(!blClicked){
		AssignImage(imgFrontRight, 'imgFrontRight');
		AssignImage(imgFrontLeft, 'imgFrontLeft');
	}
	
	//show buttons if applicable
	if(intCurPage>0)					ShowButton('Prev');
	if(intCurPage<(arImages.length-1))	ShowButton('Next');
	//disable the left page for the first page, so the text and link can be accessed
	if(intCurPage==0) {
		//document.getElementById('left-page').style.display = 'none';
		document.getElementById('imgFrontRight').style.width = intInitialWidth+'px';
		document.getElementById('imgFrontRight').style.height= intInitialHeight+'px';
		document.getElementById('imgBackRight').style.width = intInitialWidth+'px';
		document.getElementById('imgBackRight').style.height= intInitialHeight+'px';
		document.getElementById('divBackToStart').style.display = 'none';
	}
	if(imgFrontLeft==null) {
		document.getElementById('left-page').style.display = 'none';
	}
	if(imgFrontRight==null) {
		document.getElementById('right-page').style.display = 'none';
	}
}
function AssignImage(imgPreloaded, strElementID) {
	//put relevant preloaded image into the img element
	var elImage = document.getElementById(strElementID);
	if(imgPreloaded==null)	elImage.style.display = 'none';
	else {
		elImage.src = imgPreloaded;
		elImage.style.width = intWidth+'px';
		elImage.style.left = 0;
		elImage.style.display = 'block';
	}
}

function Next() {
	document.getElementById('left-page').style.display = 'block';
	if(intCurPage==0) FirstUse();
	HideButtons();
	AssignImage(imgFrontRight, 'imgFrontRight');
	AssignImage(imgNextRight, 'imgBackRight');
	//turn right page
	$('#imgFrontRight').animate({
		width: '0px'
	}, intInterval, function() {
		// Animation complete. show and expand next left page
		AssignImage(imgFrontLeft, 'imgBackLeft');
		
		AssignImage(imgNextLeft, 'imgFrontLeft');
		document.getElementById('imgFrontLeft').style.width = '0px';
		document.getElementById('imgFrontLeft').style.left = intWidth+'px';
		
		//expand next left page
		$('#imgFrontLeft').animate({
			width: intWidth+'px',
			left: '0px'
		}, intInterval, function() {
			//preload next images
			intCurPage++;
			SetPageImages();
		});
	});
}
function Prev() {
	document.getElementById('right-page').style.display = 'block';
	HideButtons();
	AssignImage(imgFrontLeft, 'imgFrontLeft');
	AssignImage(imgPrevLeft, 'imgBackLeft');
	//turn right page
	$('#imgFrontLeft').animate({
		width: '0px',
		left: intWidth+'px'
	}, intInterval, function() {
		// Animation complete. show and expand Prev right page
		AssignImage(imgFrontRight, 'imgBackRight');
		
		AssignImage(imgPrevRight, 'imgFrontRight');
		document.getElementById('imgFrontRight').style.width = '0px';
		//document.getElementById('imgFrontRight').style.left = intWidth+'px';
		
		//expand Prev right page
		$('#imgFrontRight').animate({
			width: intWidth+'px'
		}, intInterval, function() {
			//preload next images
			intCurPage--;
			SetPageImages();
		});
	});
}
function FirstUse() {
	//actions on first use of the catalogue
	if(!blClicked) document.getElementById('catalogue-start').style.display = 'none';
	
	blClicked = true;
	document.getElementById('imgFrontRight').style.width = intWidth+'px';
	document.getElementById('imgFrontRight').style.height= intHeight+'px';
	document.getElementById('imgBackRight').style.width = intWidth+'px';
	document.getElementById('imgBackRight').style.height= intHeight+'px';
	document.getElementById('divBackToStart').style.display = 'block';
}

function HideButtons() {
	document.getElementById('btnPrev').style.display = 'none';
	document.getElementById('btnNext').style.display = 'none';
}
function ShowButton(strType) {
	document.getElementById('btn'+strType).style.display = 'inline';
}
