﻿function FlippingBook(id, width, height, pages, contents) {
	this.baseUrl = 'http://media.makin-hey.com/';
	this.pageFlipSwf = this.baseUrl + 'pageFlip/1.0/pageFlip.swf';
	this.installSwf = this.baseUrl + 'swfobject/2.2/expressInstall.swf';
	this.contents = contents;
	this.settings = {
		bookWidth: width,
		bookHeight: height,
		pagesSet: pages,
		zoomPagesSet: [],
		printPagesSet: [],
		scaleContent: true,
		preserveProportions: false,
		centerContent: true,
		hardcover: false,
		hardcoverThickness: 3,
		hardcoverEdgeColor: 0xFFFFFF,
		highlightHardcover: true,
		frameWidth: 0,
		frameColor: 0xFFFFFF,
		frameAlpha: 100,
		firstPageNumber: 1,
		autoFlipSize: 50,
		navigationFlipOffset: 30,
		flipOnClick: true,
		handOverCorner: true,
		handOverPage: true,
		alwaysOpened: false,
		staticShadowsType: 'Asymmetric',
		staticShadowsDepth: 1,
		staticShadowsLightColor: 0xFFFFFF,
		staticShadowsDarkColor: 0x000000,
		dynamicShadowsDepth: 1,
		dynamicShadowsLightColor: 0xFFFFFF,
		dynamicShadowsDarkColor: 0x000000,
		moveSpeed: 2,
		closeSpeed: 3,
		gotoSpeed: 3,
		rigidPageSpeed: 5,
		flipSound: this.baseUrl + 'pageFlip/1.0/sounds/02.mp3',
		hardcoverSound: '',
		preloaderType: 'Thin',
		pageBackgroundColor: 0x99CCFF,
		loadOnDemand: true,
		allowPagesUnload: true,
		showUnderlyingPages: false,
		playOnDemand: true,
		freezeOnFlip: false,
		darkPages: false,
		smoothPages: false,
		rigidPages: false,
		flipCornerStyle: 'manually',
		flipCornerPosition: 'bottom-right',
		flipCornerAmount: 50,
		flipCornerAngle: 20,
		flipCornerRelease: true,
		flipCornerVibrate: true,
		flipCornerPlaySound: false,
		zoomEnabled: true,
		zoomPath: 'pages/large/',
		zoomImageWidth: 900,
		zoomImageHeight: 1165,
		zoomOnClick: true,
		zoomUIColor: 0x8f9ea6,
		zoomHint: 'Double click for zooming.',
		zoomHintEnabled: false,
		centerBook: true,
		useCustomCursors: true,
		dropShadowEnabled: true,
		dropShadowHideWhenFlipping: true,
		backgroundColor: 0xFFFFFF,
		backgroundImage: this.baseUrl + 'pageFlip/1.0/images/red/bgBook.jpg',
		backgroundImagePlacement: 'fit',
		printEnabled: true,
		printTitle: 'Print Pages',
		downloadURL: '',
		downloadTitle: '',
		downloadSize: '',
		downloadComplete: '',
		extXML: ''
	};
	this.cntBook = id;
	this.cntViewport = id + "_viewport";
	this.cntStage = id + "_stage";
	this.cntFooter = id + "_footer";
	this.ddlContents = id + "_contents";
	this.lblCurrent = id + "_current";
	this.lblTotal = id + "_total";
	this.btnDownload = id + "_download";
	this.btnNext = id + "_next";
	this.btnPlayPause = id + "_playPause";
	this.btnPrevious = id + "_previous";
	this.btnPrint = id + "_print";
	this.btnZoom = id + "_zoom";
}
FlippingBook.prototype.getFlippingBookReference = function() {
	return document.getElementById(this.cntBook);
};
FlippingBook.prototype.downloadFile = function() {
	if (this.settings.downloadURL) {
		this.getFlippingBookReference().downloadFile();
	}
};
FlippingBook.prototype.flipBack = function() {
	this.getFlippingBookReference().flipBack();
};
FlippingBook.prototype.flipForward = function() {
	this.getFlippingBookReference().flipForward();
};
FlippingBook.prototype.print = function() {
	this.getFlippingBookReference().print();
};
FlippingBook.prototype.zoomIn = function() {
	this.getFlippingBookReference().zoomIn();
};
FlippingBook.prototype.zoomOut = function() {
	this.getFlippingBookReference().zoomOut();
};
FlippingBook.prototype.zoomButtonClick = function() {
	if (this.getFlippingBookReference().isZoomedIn()) {
		this.zoomOut();
	}
	else {
		this.zoomIn();
	}
};
FlippingBook.prototype.playCallback = function() {
	var me = this;
	if (this.leftPage === this.settings.pagesSet.length || this.rightPage === this.settings.pagesSet.length) {
		this.getFlippingBookReference().flipGotoPage(1);
	}
	else {
		this.flipForward();
	}
	this.playTimer = setTimeout(function() { me.playCallback(); }, 5000);
};
FlippingBook.prototype.play = function() {
	var me = this, btn = document.getElementById(this.btnPlayPause);
	this.playCallback();
	if (btn) {
		btn.src = btn.src.replace('btnPlay.gif', 'btnPause.gif');
		btn.alt = "Pause";
		btn.onclick = function() { me.pause(); };
	}
};
FlippingBook.prototype.pause = function() {
	var me = this, btn = document.getElementById(this.btnPlayPause);
	if (this.playTimer) {
		clearTimeout(this.playTimer);
		this.playTimer = null;
	}
	if (btn) {
		btn.src = btn.src.replace('btnPause.gif', 'btnPlay.gif');
		btn.alt = "Play";
		btn.onclick = function() { me.play(); };
	}
};
FlippingBook.prototype.updateContentsMenu = function(left, right) {
	var s = document.getElementById(this.ddlContents), i, l, minPage, maxPage, leftOK, rightOK;
	if (s) {
		for (i = 0, l = this.contents.length - 1; i < l; i++) {
			minPage = s.options[i].value;
			maxPage = s.options[i + 1].value;
			leftOK = true;
			rightOK = true;

			if (left) {
				leftOK = (Number(left) >= minPage && Number(left) <= maxPage);
			}
			if (right) {
				rightOK = (Number(right) >= minPage && Number(right) <= maxPage);
			}
			if (leftOK && rightOK) {
				break;
			}
		}
		s.selectedIndex = i;
	}
};
FlippingBook.prototype.updatePagination = function(left, right) {
	var p = '',
	c = document.getElementById(this.lblCurrent),
	t = document.getElementById(this.lblTotal);
	if (c) {
		if (left) {
			p += left;
		}
		if (right) {
			if (left) {
				p += '-';
			}
			p += right;
		}
		c.innerHTML = p;
	}
	if (t) {
		t.innerHTML = this.settings.pagesSet.length;
	}
};
FlippingBook.prototype.onPutPage = function(left, right) {
	this.leftPage = left;
	this.rightPage = right;
	this.updatePagination(left, right);
	this.updateContentsMenu(left, right);
};
FlippingBook.prototype.onContentsChange = function() {
	var s = document.getElementById(this.ddlContents), n;
	if (s) {
		n = s.options[s.selectedIndex].value;
		if (n) {
			this.getFlippingBookReference().flipGotoPage(n);
		}
	}
};
FlippingBook.prototype.buildContentsMenu = function() {
	var me = this, s = document.getElementById(this.ddlContents), i, l;
	if (s) {
		s.options.length = 0;
		for (i = 0, l = this.contents.length; i < l; i++) {
			s.options[i] = new Option(this.contents[i][0], this.contents[i][1]);
		}
		s.onchange = function() { me.onContentsChange.call(me); };
	}
};
FlippingBook.prototype.addButtonClickHandler = function(id, fn) {
	var me = this, btn = document.getElementById(id);
	if (btn) {
		btn.onclick = function() { fn.call(me); };
		btn.style.cursor = 'pointer';
	}
};
FlippingBook.prototype.getWindowSize = function() {
	var h = 0, w = 0;
	if (typeof window.innerWidth === 'number') {
		h = window.innerHeight;
		w = window.innerWidth;
	}
	else if (document.documentElement && (document.documentElement.clientHeight || document.documentElement.clientWidth)) {
		h = document.documentElement.clientHeight;
		w = document.documentElement.clientWidth;
	}
	else if (document.body && (document.body.clientHeight || document.body.clientWidth)) {
		h = document.body.clientHeight;
		w = document.body.clientWidth;
	}
	return { height: h, width: w };
};
FlippingBook.prototype.sizeContent = function() {
	var dim = this.getWindowSize(),
	f = document.getElementById(this.cntFooter),
	s = document.getElementById(this.cntStage),
	v = document.getElementById(this.cntViewport),
	h = 0;
	if (s) {
		if (f) {
			h = dim.height - f.offsetHeight;
		}
		else {
			h = dim.height;
		}
		if (h < this.settings.bookHeight + 10) {
			h = this.settings.bookHeight + 10;
		}
		s.style.height = h + 'px';
	}
	if (v) {
		if (dim.width < this.settings.bookWidth + 10) {
			v.style.width = (this.settings.bookWidth + 10) + 'px';
		}
		else {
			v.style.width = 'auto';
		}
	}
};
FlippingBook.prototype.onWindowLoad = function() {
	var me = this;
	this.sizeContent();
	this.addButtonClickHandler(this.btnDownload, this.downloadFile);
	this.addButtonClickHandler(this.btnNext, this.flipForward);
	this.addButtonClickHandler(this.btnPlayPause, this.play);
	this.addButtonClickHandler(this.btnPrevious, this.flipBack);
	this.addButtonClickHandler(this.btnPrint, this.print);
	this.addButtonClickHandler(this.btnZoom, this.zoomButtonClick);
	this.buildContentsMenu();
	if (window.addEventListener) {
		window.addEventListener('resize', function() { me.sizeContent(); }, false);
	}
	else if (window.attachEvent) {
		window.attachEvent('onresize', function() { me.sizeContent(); });
	}
	else {
		window.onresize = function() { me.sizeContent(); };
	}
};
FlippingBook.prototype.create = function() {
	var me = this, params = {
		allowScriptAccess: "always",
		bgcolor: "#" + this.settings.backgroundColor.toString(16)
	};
	swfobject.addLoadEvent(function() { me.onWindowLoad(); });
	swfobject.embedSWF(this.pageFlipSwf, this.cntBook, '100%', '100%', '8.0.0', this.installSwf, this.settings, params);
};
