var FlippingBook;
(function($){
	FlippingBook = function(){
		this.pages = [];
		this.enlargedImages = [];
		this.pageLinks = [];
		this.printPages = [];
		this.contents = [];
		this.stageWidth = '100%';
		this.stageHeight = '700';
		this.settings = {
			allowPagesUnload:true,
			alwaysOpened:false,
			autoFlipSize:50,
			backgroundColor:0xFFFFFF,
			backgroundImage:'',
			backgroundImagePlacement:'fit', //  'top left', 'center', 'fit'
			bookHeight:480,
			bookWidth:640,
			centerBook:true,		
			centerContent:true,
			closeSpeed:3,
			darkPages:false,
			downloadComplete:'Complete',
			downloadSize:'Size:4.7 Mb',
			downloadTitle:'Download PDF',
			downloadURL:'',
			dropShadowEnabled:true,
			dropShadowHideWhenFlipping:true,
			dynamicShadowsDarkColor:0x000000,
			dynamicShadowsDepth:1,
			dynamicShadowsLightColor:0xFFFFFF, // works for 'dark' pages only
			enlargedImagesSet:this.enlargedImages,
			extXML:'',
			firstLastButtons:false,
			firstPageNumber:1,
			flipCornerAmount:50,
			flipCornerAngle:20,
			flipCornerPlaySound:false,
			flipCornerPosition:'bottom-right',// 'bottom-right','top-right','bottom-left','top-left'
			flipCornerRelease:true,
			flipCornerStyle:'manually',// 'first page only', 'each page', 'manually'
			flipCornerVibrate:true,
			flipOnClick:true,
			flipSound:'',
			frameAlpha:100,
			frameColor:0xFFFFFF,
			frameWidth:0,
			freezeOnFlip:false,
			fullscreenEnabled:true,
			fullscreenHint:'',
			goToPageField:true,
			gotoSpeed:3,
			handOverCorner:true,
			handOverPage:true,
			hardcover:false,
			hardcoverEdgeColor:0xFFFFFF,
			hardcoverSound:'',
			hardcoverThickness:3,
			highlightHardcover:true,
			loadOnDemand:true,
			Ioader:true,
			moveSpeed:2,
			navigationBar:defaultPath+'swf/flipbook/navigation.swf',
			navigationBarPlacement:'bottom',
			navigationFlipOffset:30,
			pageBackgroundColor:0x99CCFF,
			pageLinksSet: this.pageLinks,
			pagesSet:this.pages,
			playOnDemand:true,
			preloaderType:'Thin', // 'Progress Bar', 'Round', 'Thin', 'Dots', 'Gradient Wheel', 'Gear Wheel', 'Line', 'Animated Book', 'None'
			preserveProportions:false,
			printEnabled:false,
			printPagesSet:this.printPages,
			printTitle:'Print Pages',
			rigidPageSpeed:5,
			rigidPages:false,
			scaleContent:true,
			showUnderlyingPages:false,
			slideshowButton:false,
			smoothPages:false,
			soundControlButton:false,
			staticShadowsDarkColor:0x000000,
			staticShadowsDepth:1,
			staticShadowsLightColor:0xFFFFFF, // works for 'Symmetric' shadows only
			staticShadowsType:'Asymmetric', // Asymmetric, Symmetric, Default
			useCustomCursors:true,
			zoomEnabled:true,
			zoomHint:'Double click for zooming.',
			zoomHintEnabled:false,
			zoomImageHeight:1165,
			zoomImageWidth:900,
			zoomOnClick:true,
			zoomPath:'pages/large/',
			zoomUIColor:0x8f9ea6,
			zoomingMethod:'flash'
		};	
		this.containerId = 'fbContainer';
		/*
			this.forwardButtonId = 'fbForwardButton';
			this.backButtonId = 'fbBackButton';
			this.zoomButtonId = 'fbZoomButton';
			this.printButtonId = 'fbPrintButton';
			this.downloadButtonId = 'fbDownloadButton';
			this.currentPagesId = 'fbCurrentPages';
			this.totalPagesId = 'fbTotalPages';
			this.contentsMenuId = 'fbContentsMenu';
		*/
	};
	FlippingBook.prototype.create = function(swfPath){
		var o = this;
		if(!swfPath)swfPath = 'FlippingBook.swf';
		this.settings.downloadURL = '';
		this.settings.pagesSet = $.map(this.pages,function(v,i){return v+(i<o.pages.length-1?'|':'')});
		this.settings.enlargedImagesSet = $.map(this.pages,function(v,i){return v.replace('/pag/','/pag/big/')+(i<o.pages.length-1?'|':'')});;
		this.settings.pageLinksSet = this.pageLinks;
		//this.settings.printPagesSet = this.printPages;
		if(location.hash.substr(1)!='')this.settings.firstPageNumber = location.hash.substr(1);
		//this.addLoadEvent(this.onWindowLoad);
		swfobject.embedSWF(swfPath,this.containerId,this.stageWidth,this.stageHeight,'8.0.0','js/expressInstall.swf',this.settings, {allowFullscreen:'true',allowScriptAccess:'always',bgcolor:'#'+this.settings.backgroundColor.toString(16),wmode:'opaque'});
	};
	/*
		FlippingBook.prototype.getFlippingBookReference = function() {
			return this.getObjectReference(this.containerId);
		};
		FlippingBook.prototype.getObjectReference = function(id) {
			return document.getElementById(id);
		};
		FlippingBook.prototype.flipForward = function() {
			flippingBook.getFlippingBookReference().flipForward();
		};
		FlippingBook.prototype.flipBack = function() {
			flippingBook.getFlippingBookReference().flipBack();
		};
		FlippingBook.prototype.zoomButtonClick = function() {
			if(flippingBook.getFlippingBookReference().isZoomedIn())flippingBook.zoomOut();
			else flippingBook.zoomIn();
		};
		FlippingBook.prototype.zoomIn = function() {
			this.getFlippingBookReference().zoomIn();
		};
		FlippingBook.prototype.zoomOut = function() {
			this.getFlippingBookReference().zoomOut();
		};
		FlippingBook.prototype.print = function() {	
			flippingBook.getFlippingBookReference().print();
		};
		FlippingBook.prototype.downloadFile = function() {	
			if(flippingBook.settings.downloadURL)flippingBook.getFlippingBookReference().downloadFile();
		};
		FlippingBook.prototype.onWindowLoad = function(){	
			var forwardButton = flippingBook.getObjectReference(flippingBook.forwardButtonId);
			if(forwardButton){
				forwardButton.style.cursor = 'pointer';
				forwardButton.onclick = flippingBook.flipForward;
			}
			var backButton = flippingBook.getObjectReference(flippingBook.backButtonId);
			if(backButton){
				backButton.style.cursor = 'pointer';
				backButton.onclick = flippingBook.flipBack;
			}
			var zoomButton = flippingBook.getObjectReference(flippingBook.zoomButtonId);
			if(zoomButton){
				zoomButton.style.cursor = 'pointer';
				zoomButton.onclick = flippingBook.zoomButtonClick;
			}
			var printButton = flippingBook.getObjectReference(flippingBook.printButtonId);
			if(printButton){
				printButton.style.cursor = 'pointer';
				printButton.onclick = flippingBook.print;
			}
			var downloadButton = flippingBook.getObjectReference(flippingBook.downloadButtonId);
			if(downloadButton){
				downloadButton.style.cursor = 'pointer';
				downloadButton.onclick = flippingBook.downloadFile;
			}
			flippingBook.buildContentsMenu();
		};
		FlippingBook.prototype.onPutPage = function(leftPageNumber, rightPageNumber){
			this.updatePagination(leftPageNumber, rightPageNumber);
			this.updateContentsMenu(leftPageNumber, rightPageNumber);
		};
		FlippingBook.prototype.updatePagination = function(leftPageNumber, rightPageNumber){
			var leftPageExists = (leftPageNumber != undefined);
			var rightPageExists = (rightPageNumber != undefined);
			var pageNumberString = leftPageNumber + '-' + rightPageNumber;
			if(!leftPageExists)pageNumberString = rightPageNumber;	
			if(!rightPageExists)pageNumberString = leftPageNumber;
			this.getObjectReference(this.currentPagesId).innerHTML = pageNumberString;
			this.getObjectReference(this.totalPagesId).innerHTML = ' / ' + this.getFlippingBookReference().totalPages();	
		};
		FlippingBook.prototype.buildContentsMenu = function(){
			var contentsSelect = this.getObjectReference(this.contentsMenuId);
			if(contentsSelect){
				for(var i=0;i<this.contents.length;i++)contentsSelect.options[i] = new Option(this.contents[i][0], this.contents[i][1]);
				contentsSelect.onchange = this.onContentsChange;
			}
		};
		FlippingBook.prototype.onContentsChange = function(){
			var contentsSelect = flippingBook.getObjectReference(flippingBook.contentsMenuId);
			var pageNumber = contentsSelect.options[contentsSelect.selectedIndex].value;
			if(pageNumber)flippingBook.getFlippingBookReference().flipGotoPage(pageNumber);
		};
		FlippingBook.prototype.updateContentsMenu = function(leftPageNumber, rightPageNumber){
			var contentsSelect = flippingBook.getObjectReference(flippingBook.contentsMenuId);
			if(contentsSelect){
				for(var i = 0; i < this.contents.length - 1; i++){
					var minPage = contentsSelect.options[i].value;
					var maxPage = contentsSelect.options[i+1].value;
					var leftOK = false;
					var rightOK = false;
					if(leftPageNumber)leftOK = (Number(leftPageNumber) >=  minPage && Number(leftPageNumber) <= maxPage);
					else leftOK = true;
					if(rightPageNumber)rightOK = (Number(rightPageNumber) >=  minPage && Number(rightPageNumber) <= maxPage);
					else rightOK = true;
					if(leftOK&&rightOK)break;
				}	
				contentsSelect.selectedIndex = i;
			}
		};
		FlippingBook.prototype.getWindowHeight = function() {
			var windowHeight = 0;
			if (typeof(window.innerHeight)=='number')windowHeight=window.innerHeight;
			else{
				if (document.documentElement &&	document.documentElement.clientHeight)windowHeight = document.documentElement.clientHeight;
				else if(document.body&&document.body.clientHeight)windowHeight=document.body.clientHeight;
			}
			return windowHeight;
		};
		FlippingBook.prototype.addLoadEvent = function (fn) {
			if(typeof window.addEventListener!='undefined')window.addEventListener('load',fn,false);
			else if(typeof document.addEventListener!='undefined')document.addEventListener('load',fn,false);
			else if(typeof window.attachEvent!='undefined')window.attachEvent('onload',fn);
			else if(typeof window.onload=='function'){
				var fnOld = window.onload;
				window.onload = function(){
					fnOld();
					fn();
				};
			}else window.onload = fn;
		}
		FlippingBook.prototype.handleWheel = function(delta){
			this.getFlippingBookReference().onWheelScroll(delta);
		};
		FlippingBook.prototype.goToPage = function(n){
			flippingBook.getFlippingBookReference().flipGotoPage(n);
		};
		$(document).scroll(function(event){
			var delta = 0;
			if(!event) event = window.event;
			if(event.wheelDelta){
				delta = event.wheelDelta/120; 
				if (window.opera) delta = -delta;
			}else if(event.detail) delta = -event.detail/3;
			if(delta)flippingBook.handleWheel(delta);
			if (event.preventDefault)event.preventDefault();
			event.returnValue = false;
		});
	*/
	flippingBook = new FlippingBook();
})(jQuery);
