	var chapterdata = textmassob = null;
	var chaptertitle = null;
	function displayPage(page,skipfx){
		
		location.hash='#'+page;
		pagenum=page;
		pageLink();
		
		var myFx = new Fx.Tween( textmassob, {
			'onComplete': function(){
				textmassob.set('html',(page==1?'<h1>'+chaptertitle+'</h1>':'<div class="pagespacing"></div>')+chapterdata[page-1]);	
				if(undefined !== window.repopulateVariables) {
					repopulateVariables();
					recalculateBmHeight();
					$$("div#rightpane ul#bookmarklist li").each(function(ob){
						var idsplit = ob.id.split(':');
						var chpid = idsplit[2],
							pgnum = idsplit[3],
							ycoord = idsplit[5];
							
						if(chpid == chapterid && pgnum == pagenum){
							marginPosY=ycoord;
							newListItemID=ob.id;
							var FadeInBM = new Fx.Tween(bm, {duration: OpacityWait, onComplete: function() {
								bmContainer.setStyle("background-position",'0px -999px');
								bmContainer.setStyle("opacity",'1');
								
								bmVisiBool = true;
								
								bm.setStyle("margin-top", ycoord.toInt());
								if(skipfx){
									var myFx = new Fx.Scroll(window, {
										offset: {
											'x': 0,
											'y': bm.getCoordinates().top.toInt()-50
										}
									}).toTop();
								}
							}}).start("opacity", 1);
						} else 	bmVisiBool = false;

					});

				}
				if(!Browser.Engine.trident) textmassob.setStyle('background-color', '#fff').fade('in');
			},
			'duration': 'short'
			
		});
		if(!skipfx && !Browser.Engine.trident)
			myFx.start('opacity', '1', '0');
		else
			myFx.onComplete();
		
		
		if(!skipfx){
			var myFx = new Fx.Scroll(window, {
				offset: {
					'x': 0,
					'y': 0
				}
			}).toTop();
		}
	}
	
	function chapterLoad(){
		
		textmassob = $('leftpane').getElement('div.body div.textmass');
		new Request ({
			url: chapterurl
			,onSuccess: function(req){
				var ret = JSON.decode(req);
				chapterdata = ret.pages;
				chaptertitle = ret.chaptertitle;
				
				displayPage(pagenum,true);
				pageLink();
				if(bmList != null){
					var printout='<h1>'+$('chapter').get('text')+'</h1>';
					for(var i=0;i<chapterdata.length;i++){
						printout += chapterdata[i];
					}
					$('printchapter').set('html',printout);
				}
				
			}
		}).send();

		
	}
	
	function pageLink(){
		$$('ul.listpages').each(function (ulob) {
			ulob.getElements('li').each(function (liob){
				if(! ( liob.hasClass('nolink') ) ) {
					liob.dispose();
				}
			});
				
			var liob = new Element('li', {
				'class': 'previous',
				'styles': {
					'display': (pagenum==1?'none':'block')
				}

			}).inject(ulob);
			
			var aob = new Element('a', {
				'href': 'javascript:;',
				'events': {
					'click': function(){
						displayPage(pagenum-1);
					}
				}
			}).inject(liob);
			aob.set('text','Förra');

			for(var a=(pagenum-4);a<(pagenum+4);a++) {
				if(a < 1 || a > chapterdata.length ) continue;
				var liob = new Element('li').inject(ulob);
				var aob = new Element('a', {
					'href': 'javascript:;',
					'class': (pagenum == a?'active':''),
					'events': {
						'click': function(){
							displayPage(this.retrieve('page'));
						}
					}
				}).inject(liob);
				aob.store('page',a);
				aob.set('text',a);
			}
			if(pagenum == chapterdata.length && nextchapterurl == ''){
				if(pagenum == chapterdata.length && blList){
					new Request({
						'url': baseurl + 'medlem/bookread',
						'data': 'bid=' + bookid
					}).send();
				}
			}
			else {
				var liob = new Element('li', {
					'class': 'next'
				}).inject(ulob);
				var aob = new Element('a', {
					'href': 'javascript:;',
					'events': {
						'click': function(){
							if(pagenum !== chapterdata.length)
								displayPage(pagenum+1);
							else{
								window.location.href=nextchapterurl;
								return false;
							}
						}
					}
				}).inject(liob).set('html',(pagenum !== chapterdata.length ? 'N&auml;sta sida'  :'N&auml;sta kapitel'));
			}
			
		} );
	}
	
	var ctr = 0, kareqinprog = false;

	function keepAlive(){
		kareqinprog = true;
		if(blList) new Request({
			"url": baseurl + "medlem/keepalive",
			"data": "ctr=" + ctr + "&bid=" + bookid,
			"onComplete": function() {
				ctr = 0;
				kareqinprog = false;
			}
		}).send();
	}

	window.addEvent("domready", function() {
		window.setInterval(function() {
			if(!kareqinprog && ctr++ > 39) {
				keepAlive();
				ctr = 0;
			}
		}, 1000);
		$$("div.chapternavigation select").each(function(ob) {
			ob.addEvent("change", function(){
				window.location = bookurl + "/" + this.value;
			});
		});
		$$("li.chapternavigation select").each(function(ob) {
			ob.addEvent("change", function(){
				window.location = bookurl + "/" + this.value;
			});
		});
	});

	window.addEvent("unload", function() {
		keepAlive();
	});

