function PageTurn() {

	this.content	= null;
	this.control	= null;
	
	this.realContent	= null;

	this.contentId	= 'pageTurn';

	this.count		= 0; // stores current position


	this.pages		= null;
	this.doublePages	= null;
	this.loading	=		null;
	this.loaded 	=		false;
		
	
	this.showLayerLoad	= function() {
		
		this.layerLoad.show();
		
	};
	
	this.hideLayerLoad	= function() {
		
		this.layerLoad.hide();
		
	};

	
	this.show	= function() {

		if ( _pageFocus && (_page > 0) && (_page <= this.doublePages) ) {
			
			this.content.show().booklet((_page - 1) * 2);
			this.count	= _page - 1;
			
			_pageFocus	= false;
			
		} else {
			
			this.content.show().booklet('next');
			++this.count;
			
		}

		window.location.hash	= _revueString + this.id + '/' + _pageString + (this.count + 1);
		
		//this.control.fadeIn(400);
		var selfObj	= this;
		setTimeout( function () { selfObj.control.show(); }, 1000);

		$('#pageTurnControl_cancel').fadeIn(100);
		$('#pageTurn_canvas').fadeIn(100);
		$('#canvas').css('display','none');		
	};

	this.hide	= function() {

		this.content.hide();
		
		$('#pageTurnControl_cancel').fadeOut(400);
		$('#pageTurn_canvas').fadeOut(100);
		$('#canvas').css('display','block');		
	};
	
	this.fadeIn	= function() {

		this.content.fadeIn(400);
		this.control.fadeIn(400);
		$('#pageTurnControl_cancel').fadeIn(100);
		
	};

	this.fadeOut	= function() {

		this.content.fadeOut(400);
		this.control.fadeOut(400);
		$('#pageTurnControl_cancel').fadeOut(100);

	};
	
	this.fadeControl	= function(control, direction, fade) {
		
		var time	= 400;
		
		if ('left' == direction) {
			
			/*switch(control) {
			
				case 'turn'		:	if ('in' == fade) this.controlTurnL.fadeIn(time); else this.controlTurnL.fadeOut(time); 
									break;
									
				case 'zoom'		:	if ('in' == fade) this.controlZoomL.fadeIn(time); else this.controlZoomL.fadeOut(time);
									break;
			
			
			}*/
			
			
			switch(control) {
			
			case 'turn'		:	if ('in' == fade) this.controlTurnL.show(); else this.controlTurnL.hide(); 
								break;
								
			case 'zoom'		:	if ('in' == fade) this.controlZoomL.show(); else this.controlZoomL.hide();
								break;
								
								}
			
		
			
		}
		
		if ('right' == direction) {
		
			/*switch(control) {
			
			case 'turn'		:	if ('in' == fade) this.controlTurnR.fadeIn(time); else this.controlTurnR.fadeOut(time);
								break;
								
			case 'zoom'		:	if ('in' == fade) this.controlZoomR.fadeIn(time); else this.controlZoomR.fadeOut(time);
								break;
		
		
			}*/
			
			switch(control) {
			
				case 'turn'		:	if ('in' == fade) this.controlTurnR.show(); else this.controlTurnR.hide();
									break;
								
				case 'zoom'		:	if ('in' == fade) this.controlZoomR.show(); else this.controlZoomR.hide();
									break;
		
		
			}
			
		}
		
	};
	
	this.preload =		function(id, count) {

		for (i = 1; i <= count; ++i) {

			var src	= ('content/' + id + '/preview/page_' + i + '.jpg');
			var img = new Image();

			var selfObj	= this;
			img.onload	= function() {

				
				var position	= $.inArray(this.src, selfObj.loading);
				
				if ( -1 != position ) {

					selfObj.loading.splice(position, 1);

					if (0 == selfObj.loading.length) {

						selfObj.loaded	= true;
						
					} else {

						selfObj.loaded	= false;

					}

				}
		
			};

			img.src		= src;
			this.pages.push(img);
			this.loading.push(img.src);

		}

},
	
	this.loadHelper	= function(id) {
	
		var html	= '<div id="pageTurn_realContent" class="b-load">';
		
		for (var i = 1, maxI = coverData.pages[id-1]; i <= maxI; ++i) {
			
			html	+= '<div style="background:#fff url(images/controls/preloading.gif) no-repeat center center; display: block; height: 300px;"><img src="content/' + id + '/preview/page_' + i + '.jpg" alt="" /></div>';
			
		}
		
		html		+= '</div>';
		
		/*
		 * 1 2/3 4/5		-> 3
		 * 1 2/3 4/5 6/7	-> 4
		 * 1 2/3 4/5 6		-> 4
		 * 
		 */
		this.doublePages	= 1 + Math.ceil( (coverData.pages[id-1] - 1) / 2);
		this.realPages		= coverData.pages[id-1];
		
		this.content.empty().html( html );
	
		this.content.booklet({
			
			width:    630,
			height:   495,
			speed:    800,
			closed:             true, 
					
			pagePadding:        0,
			pageNumbers:       false,
			
			overlays:           false,
			hovers:             false,
			shadows: 			false 
		
		});
		
		this.count	= 0;
		this.show();
		
		covers.hideFront();
	
	};

	this.load	= function(id) {
		
		id	= id.substr(6);
		
		/* preload cancelled */
		this.id	= id;
		
		//if (this.id != id) {
		if (true == false) {
		
			this.pages	= new Array();
			this.loading= new Array();
			this.loaded	= false;
				
			this.preload(id, coverData.pages[id-1]);
			var selfObj	= this;
			this.preloadTick	= setInterval( function() {
					
				if (selfObj.loaded) {
					
					clearInterval(selfObj.preloadTick);
					selfObj.preloadTick	= null;
					selfObj.loadHelper(id);
	
				}			
				
			}, 20);
			
			this.id	= id;
			
		} else {
			
			this.loadHelper(id);
			
		}
		
		
	};

	this.backflip = function() {

		this.content.booklet(1);
		
		this.control.fadeOut(500);
		
		
		if (this.count == 0) {
			
			this.hide();
			
			covers.pageTurn();
			
			myContents.fadeIn();
			
		} else {
		
			var selfObj	= this;
			setTimeout( function() {
			
				selfObj.hide();
			
				covers.pageTurn();
				
				myContents.fadeIn();
				
			}, 900);
			
		}
		
		this.count	= 0;
		
		window.location.hash	= _revueString + this.id;
		

	};

	this.flip = function (direction) {

		this.turn(direction);

	};
	
	this.turn = function (direction) {

		if ( ('next' == direction) && ( this.doublePages - 1 > this.count ) ) {
			 
			++this.count;
			
			this.content.booklet(direction);
						
		}
		
		if ( ('prev' == direction) && (0 < this.count) ) {
			
			--this.count;
			
			this.content.booklet(direction);
			
		}
		
		window.location.hash	= _revueString + this.id + '/' + _pageString + (this.count + 1);

	};
	
	this.getPage = function (direction) {

		var realPage	= null;
		
		if (this.count == 0) {
			
			realPage	= 1;
			
		} else if ( 'left' == direction ) {
			
			realPage	= ( this.count == 1 ? this.count + 1 : this.count * 2 );
			
		} else if ( 'right' == direction ) {
			
			realPage	= ( this.count == 1 ? this.count + 2 : this.count *2 + 1 );
			
		}
		
		this.showLayerLoad();	
		reader.load(this.id, coverData.pages[this.id-1],realPage);
		
	};
	
	this.init	= function() {

		/*
		 * select content element and set top offset
		 */
		this.content	= $('#' + this.contentId);
		this.control	= $('#' + this.contentId + 'Control');
		
		
		this.realContent	= $('#pageTurn_realContent');
		this.layerLoad		= $('#pageTurn_layerLoad');
		
		
		this.controlTurnL	= $('#pageTurnControlArrow_l');
		this.controlTurnR	= $('#pageTurnControlArrow_r');
		
		this.controlZoomL	= $('#pageTurnControlloupe_l');
		this.controlZoomR	= $('#pageTurnControlloupe_r');
		
		this.controlTurnL.css('opacity', 0.6);
		this.controlTurnR.css('opacity', 0.6);
		
		this.controlTurnL.hide();
		this.controlTurnR.hide();
		
		this.controlZoomL.hide();
		this.controlZoomR.hide();
		
		this.content.css('width', 2 * config.coverWidth).css('height', config.coverHeight);
		this.content.css('left', canvas.middleVertical - config.coverWidth).css('top', config.topMargin);
		
		this.control.css('width', 2 * config.coverWidth).css('height', config.coverHeight);
		this.control.css('left', canvas.middleVertical - config.coverWidth).css('top', config.topMargin);
		
		this.layerLoad.css('width', 2 * config.coverWidth).css('height', config.coverHeight);
		this.layerLoad.css('left', canvas.middleVertical - config.coverWidth).css('top', config.topMargin);
		
		
		
		var selfObj	= this;
		
		$("#pageTurnControl_l").hover(
				  function () { if (0 < selfObj.count) selfObj.fadeControl('turn', 'left', 'in'); }, 
				  function () { selfObj.fadeControl('turn', 'left', 'out'); }
			);
		
		$("#pageTurnControl_r").hover(
				  function () { if ( selfObj.doublePages - 1 > selfObj.count ) selfObj.fadeControl('turn', 'right', 'in'); }, 
				  function () { selfObj.fadeControl('turn', 'right', 'out'); }
			);
		
		$("#pageTurnControlZoom_l").hover(
				  function () { if (0 < selfObj.count) selfObj.fadeControl('zoom', 'left', 'in'); }, 
				  function () { selfObj.fadeControl('zoom', 'left', 'out'); }
			);
		
		$("#pageTurnControlZoom_r").hover(
				  function () { if ( 1 == selfObj.count % 2 || selfObj.doublePages - 1 > selfObj.count ) selfObj.fadeControl('zoom', 'right', 'in'); }, 
				  function () { selfObj.fadeControl('zoom', 'right', 'out'); }
			);
		
		this.controlTurnL.bind('click',function() { selfObj.turn('prev'); } ); // TODO ALS GLOB DEKLARIEREN
		this.controlTurnR.bind('click',function() { selfObj.turn('next'); } );
		
		var selfObj	= this;
		this.controlZoomL.bind('click',function() { selfObj.getPage('left') } ); // TODO ALS GLOB DEKLARIEREN
		this.controlZoomR.bind('click',function() { selfObj.getPage('right') } );
		
		
	};

	this.init();

}
