function FlippingBook() {
    this.pages = [];
    this.pagesNum = [];
	this.zoomPages = [];
	this.printPages = [];
	this.contents = [];
	this.currentPage = -1;
	this.message = null;
	this.pageChanged = null;

	this.stageWidth = "800";
	this.stageHeight = "620px";

	this.settings = {
	        pageFlipSwfPath: "/PageFlip",
			bookWidth: 640,
			bookHeight: 480,
			pagesSet: this.pages,
			zoomPagesSet: this.zoomPages,
			printPagesSet: this.printPages,
			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: "Symmetric", // Asymmetric, Symmetric, Default
			staticShadowsDepth: 0.1,
			staticShadowsLightColor: 0xFFFFFF, // works for "Symmetric" shadows only
			staticShadowsDarkColor: 0x000000,
			dynamicShadowsDepth: 0.4,
			dynamicShadowsLightColor: 0xFFFFFF, // works for "dark" pages only
			dynamicShadowsDarkColor: 0x000000,
			moveSpeed: 2,
			closeSpeed: 3,
			gotoSpeed: 3,
			rigidPageSpeed: 5,
			flipSound: "/PageFlip/Sounds/01.mp3",
			hardcoverSound: "",
			preloaderType: "Thin", // "Progress Bar", "Round", "Thin", "Dots", "Gradient Wheel", "Gear Wheel", "Line", "Animated Book", "None"
			pageBackgroundColor: 0x333333,
			loadOnDemand: true,
			allowPagesUnload: false,
			showUnderlyingPages: false,
			playOnDemand: true,
			freezeOnFlip: false,
			darkPages: false,
			smoothPages: false,
			rigidPages: false,
			flipCornerStyle: "first page only", // "first page only", "each page", "manually"
			flipCornerPosition: "bottom-right",// "bottom-right","top-right","bottom-left","top-left"
			flipCornerAmount: 50,
			flipCornerAngle: 20,
			flipCornerRelease: true,
			flipCornerVibrate: true,
			flipCornerPlaySound: false,
			pagesPathBase: "",
			zoomEnabled: true,
			zoomPathRel: "Zoom/",
			zoomPath: "pages/large/",
			zoomImageWidth: 900,
			zoomImageHeight: 1165,
			zoomOnClick: true,
			zoomUIColor: 0xCBC8C1,
			zoomHint: "Duplo-clique para ampliar",
			zoomHintEnabled: false,
			centerBook: true,		
			useCustomCursors: true,
			dropShadowEnabled: true,
			dropShadowHideWhenFlipping: false,
			backgroundColor: 0xCCCCCC,
			backgroundImage: "",
			backgroundImagePlacement: "fit", //  "top left", "center", "fit"
			printEnabled: true,
			printTitle: "Imprimir P%C3%A1ginas",
			downloadURL: "",
			downloadTitle: "Download PDF",
			downloadSize: "Size: 4.7 Mb",
			downloadComplete: "Complete",
			extXML: "",
			backgroundTransparent: true,
			cssClass: ""
		};
		
		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() {
    if (this.pages.length == 0 && this.settings.pagesPathBase != "" && this.pagesNum.length > 0) {
        var imgPg;
        for (var pg = 0; pg < this.pagesNum.length; pg++) {
            imgPg = (this.pagesNum[pg] == 0 ? this.settings.pagesPathBase + "/blank.png" : this.settings.pagesPathBase + "/" + this.pagesNum[pg] + ".jpg");

            this.pages.push(imgPg);
        }

        this.settings.zoomPath = this.settings.pagesPathBase + "/" + this.settings.zoomPathRel;
    }

    this.settings.pagesSet = this.pages;

    this.settings.zoomPagesSet = this.zoomPages;
    this.settings.printPagesSet = this.printPages;
    var attrObjs = {};

    if (this.settings.stageWidth != undefined) this.stageWidth = this.settings.stageWidth;
    if (this.settings.stageHeight != undefined) this.stageHeight = this.settings.stageHeight;
    if (this.settings.cssClass != "") attrObjs = { "class": this.settings.cssClass };

    //if (location.hash.substr(1) != "")
    //    this.settings.firstPageNumber = location.hash.substr(1);

    //this.addLoadEvent(this.onWindowLoad);

    if (this.message == null && this.settings.pagesSet.length > 0) {
        var wmodeStr = (this.settings.backgroundTransparent ? "transparent" : "window");
        swfobject.embedSWFVis(this.settings.pageFlipSwfPath + "/FlippingBook.swf", this.containerId, this.stageWidth, this.stageHeight, "8.0.0", this.settings.pageFlipSwfPath + "/expressInstall.swf", this.settings, { allowScriptAccess: "always", wmode: wmodeStr, menu: "false", quality: "medium", bgcolor: "#" + this.settings.backgroundColor.toString(16) }, attrObjs);
    }
}

FlippingBook.prototype.getFlippingBookReference = function() {
	return this.getObjectReference( this.containerId );
}

FlippingBook.prototype.getObjectReference = function( id ) {
	return document.getElementById( id );
}

FlippingBook.prototype.flipForward = function() {
	this.getFlippingBookReference().flipForward();
}

FlippingBook.prototype.flipBack = function() {
	this.getFlippingBookReference().flipBack();
}

FlippingBook.prototype.flipFirst = function() {
    this.getFlippingBookReference().flipGotoPage(1);
}

FlippingBook.prototype.flipLast = function() {
    this.getFlippingBookReference().flipGotoPage(this.pages.length);
}

FlippingBook.prototype.goPage = function(nPage) {
    this.getFlippingBookReference().flipGotoPage(nPage);
}

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() {	
	this.getFlippingBookReference().print();
}

FlippingBook.prototype.downloadFile = function() {	
	if( this.settings.downloadURL )
		this.getFlippingBookReference().downloadFile();
}

FlippingBook.prototype.onWindowLoad = function() {
    var forwardButton = this.getObjectReference(this.forwardButtonId);
    if (forwardButton) {
        forwardButton.style.cursor = "pointer";
        forwardButton.onclick = this.flipForward;
    }

    var backButton = this.getObjectReference(this.backButtonId);
    if (backButton) {
        backButton.style.cursor = "pointer";
        backButton.onclick = this.flipBack;
    }

    var zoomButton = this.getObjectReference(this.zoomButtonId);
    if (zoomButton) {
        zoomButton.style.cursor = "pointer";
        zoomButton.onclick = this.zoomButtonClick;
    }

    var printButton = this.getObjectReference(this.printButtonId);
    if (printButton) {
        printButton.style.cursor = "pointer";
        printButton.onclick = this.print;
    }

    var downloadButton = this.getObjectReference(this.downloadButtonId);
    if (downloadButton) {
        downloadButton.style.cursor = "pointer";
        downloadButton.onclick = this.downloadFile;
    }

    this.buildContentsMenu();
}

FlippingBook.prototype.onPutPage = function(leftPageNumber, rightPageNumber) {
    this.currentPage = (leftPageNumber != undefined ? leftPageNumber : rightPageNumber);
    this.updatePagination(leftPageNumber, rightPageNumber);
    this.updateContentsMenu(leftPageNumber, rightPageNumber);
	if (typeof (this.pageChanged) == "function") this.pageChanged(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;

}

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;
	}
}