function FlipThrough(container,images) {
	var thisObj = this;
	
	this.info = document.createElement("div");
	this.info.innerHTML = "Click on pages to flip through the book. Hold mouse down to keep turning pages";
	this.info.style.position = "absolute";
	this.info.style.display = "none";
	
	this.image = document.createElement("img");
	this.image.style.position = "absolute";
	this.image.style.left = "0px";
	this.image.style.right = "0px";
	this.currentImage = 0;
	
	this.container = container;
	this.container.style.position = "relative";
	this.container.appendChild(this.image);
	
	this.container.appendChild(this.info);
	
	this.images = new Array();
	
	for (var i=0; i<images.length; i++) {
		var img = new Image();
		img.src = images[i];
		this.images.push(img);
	}
	
	document.body.onmouseup = function() {
		thisObj.onStop.call(thisObj);
	}
	
	this.interval = -1;
	this.image.onload = function() {
		thisObj.onImageLoad.call(thisObj);
	}
	this.loadImage();
}

FlipThrough.prototype.onImageLoad = function() {
	var thisObj = this;
	this.image.onload = null;
	
	var infoMargin = 40;
	
	this.info.style.width = (Math.round(this.image.width/2)-(infoMargin*2))+"px";
	this.info.style.height = (this.image.height-(infoMargin*2))+"px";
	this.info.style.left = infoMargin+"px";
	this.info.style.top = infoMargin+"px";

	this.leftArea = document.createElement("div");
	this.leftArea.style.position = "absolute";
	this.leftArea.style.cursor = "pointer";
	this.leftArea.style.left = "0px";
	this.leftArea.style.top = "0px";
	this.leftArea.style.width = Math.round(this.image.width/2)+"px";
	this.leftArea.style.height = this.image.height+"px";
	this.leftArea.onmousedown = function() {
		thisObj.onPrevBtn.call(thisObj);
	}
	this.rightArea = document.createElement("div");	
	this.rightArea.style.position = "absolute";
	this.rightArea.style.cursor = "pointer";
	this.rightArea.style.left = Math.round(this.image.width/2)+"px";
	this.rightArea.style.top = "0px";
	this.rightArea.style.width = Math.round(this.image.width/2)+"px";
	this.rightArea.style.height = this.image.height+"px";
	this.rightArea.onmousedown = function() {
		thisObj.onNextBtn.call(thisObj);
	}
	this.container.style.height = this.image.height+"px";
	this.container.appendChild(this.leftArea);
	this.container.appendChild(this.rightArea);
}

FlipThrough.prototype.onNextBtn = function() {
	var thisObj = this;
	function next() {
		thisObj.next();
	}
	clearInterval(this.interval);
	this.next();
	this.interval = setInterval(next,500);
}

FlipThrough.prototype.onPrevBtn = function() {
	var thisObj = this;
	function prev() {
		thisObj.previous();
	}
	clearInterval(this.interval);
	this.previous();
	this.interval = setInterval(prev,500);
}

FlipThrough.prototype.onStop = function() {
	clearInterval(this.interval);
	
}

FlipThrough.prototype.next = function() {
	this.currentImage++;
	if (this.currentImage >= this.images.length) {
		this.currentImage = this.images.length-1;
		this.onStop();
	}
	this.loadImage();
}

FlipThrough.prototype.previous = function() {
	this.currentImage--;
	if (this.currentImage < 0) {
		this.currentImage = 0;
		this.onStop();
	}
	this.loadImage();
}

FlipThrough.prototype.loadImage = function() {
	this.info.style.display = this.currentImage > 0 ? "none" : "block";
	this.image.src = this.images[this.currentImage].src;
}