var bookPage = 1;
var pages = new Array();
var currentLoadingPage = '';
var loopCount = 0;

var idleIntervalHandler;

$(document).ready(function() {
	var firstPageID = $('#mybook1').attr('firstpage');
	currentLoadingPage = firstPageID;
	$('#mybook1').append('<div class="bookPage" id="mybook1_'+currentLoadingPage+'"></div>');
	$('#mybook1_'+currentLoadingPage).hide();
	$('#mybook1_'+currentLoadingPage).load('slider.html #'+firstPageID,requestCascdingLoad());

	$('#slideshowBtnBack').click(animteBookRight);
	$('#slideshowBtnNext').click(animteBookLeft);

	$('#slideshowPagesBtn1').click(function() {
			gotoBookPage(1);
	});


	$('#slideshowPagesBtn2').click(function() {
			gotoBookPage(2);
	});


	$('#slideshowPagesBtn3').click(function() {
			gotoBookPage(3);
	});

});


function gotoBookPage(pageID) {
	/*
	startAutoSlideShow();
	bookPage = pageID;
	var gotoCord = -968*(pageID-1);
	$('#mybook1').animate({left:gotoCord},1000);
	resetSlideshowBalls();
	var slideshowPagesBallName = 'slideshowPages'+bookPage;
	document.images[slideshowPagesBallName].src = './images/slideshow/dot_blue.png';
	*/
	startAutoSlideShow();
	gotoBookPageFadeOutIn(pageID);
}

function gotoBookPageFadeOutIn(pageID) {
	// startAutoSlideShow();
	window.clearInterval(idleIntervalHandler);

	$('#mybook1').fadeOut(200,function() {
		bookPage = pageID;
		var gotoCord = -767*(pageID-1);
		$('#mybook1').animate({left:gotoCord},0);
		resetSlideshowBalls();
		var slideshowPagesBallName = 'slideshowPages'+bookPage;
		document.images[slideshowPagesBallName].src = './images/slideshow/dot_blue.png';
		$('#mybook1').fadeIn(1000,function() {
			startAutoSlideShow();
		});
	});
}

function animteBookRight() {
	startAutoSlideShow();
	if(bookPage>1) {
		bookPage--;
		/*
		$('#mybook1').animate({left:'+=968'},1500);
		resetSlideshowBalls();
		var slideshowPagesBallName = 'slideshowPages'+bookPage;
		document.images[slideshowPagesBallName].src = './images/slideshow/dot_blue.png';
		*/
		gotoBookPageFadeOutIn(bookPage);
	} else {
		gotoBookPageFadeOutIn(3);
	}
}

function animteBookLeft() {
	startAutoSlideShow();
	if(bookPage<3) {
		bookPage++;
		/*
		$('#mybook1').animate({left:'-=968'},1500);
		resetSlideshowBalls();
		var slideshowPagesBallName = 'slideshowPages'+bookPage;
		document.images[slideshowPagesBallName].src = './images/slideshow/dot_blue.png';
		*/
		gotoBookPageFadeOutIn(bookPage);
	} else {
		gotoBookPageFadeOutIn(1);
	}
}



function startAutoSlideShow() {
	window.clearInterval(idleIntervalHandler);
	idleIntervalHandler = window.setInterval("animteBookLeft()", 8000);
}


function resetSlideshowBalls() {
	document.images['slideshowPages1'].src = './images/slideshow/dot_grey.png';
	document.images['slideshowPages2'].src = './images/slideshow/dot_grey.png';
	document.images['slideshowPages3'].src = './images/slideshow/dot_grey.png';
}

function requestCascdingLoad() {
	setTimeout("cascdingLoad()",50);
}
function cascdingLoad() {
	$('#mybook1_'+currentLoadingPage).show();
	var currentPageID = String('#mybook1_'+currentLoadingPage);
	var nextPageID = $('#'+currentLoadingPage).attr('nextpage');

	currentLoadingPage = '';

	if(nextPageID!='' && nextPageID!=undefined && loopCount<50) {
		loopCount++;
		currentLoadingPage = nextPageID;
		$('#mybook1').append('<div class="bookPage" id="mybook1_'+nextPageID+'"></div>');
		$('#mybook1_'+nextPageID).hide();
		$('#mybook1_'+nextPageID).load('slider.html #'+nextPageID,requestCascdingLoad());
	} else {
		startAutoSlideShow();
		document.images['slideshowPages1'].src = './images/slideshow/dot_blue.png';
	}
}
