function MenuClick(ABookId, AIndex)
{
	var BookObj = $(ABookId);
	if(BookObj) BookObj = BookObj.obj;	
	if(!BookObj) return;
	var MenuObj = BookObj.menu;
	if(!MenuObj) return;
	
	MenuObj.getItemByIndex(AIndex).action();
}

function itBookMenuItem(ABook)
{
	this.book = ABook;
	this.index = -1;
	this.name = '';
	this.image = '';
	this.action = null;
}

function itBookMenuSeparator(ABook)
{
	this.book = ABook;
	this.index = -1;
}

function itBookMenu(ABook)
{
	this.book = ABook;
	this.menu = $(ABook.name+'menu');
	this.items = new Array();

	if(!this.menu)
	{
		this.menu = document.createElement('div');
		this.menu.id = ABook.name+'menu';
		this.menu.innerHTML = '';
		this.menu.className = 'menu';	
		this.menu.style.display = 'none';
		this.book.main.insertBefore(this.menu, null);
		
		Event.observe(this.book.main, 'mousedown', function()
		{
			var MenuObj = $(ABook.name+'menu');
			
			if(MenuObj.style.display == '')
			{
				MenuObj.style.display = 'none';
				return true;
			}
	
			return false;
		});
		
		this.book.box.oncontextmenu = function(e)
		{
			if(document.all) e = event;
			
			var MenuObj = $(ABook.name+'menu');
			
			MenuObj.style.left = e.clientX+document.body.scrollLeft+'px';
			MenuObj.style.top = e.clientY+document.body.scrollTop+'px';
			MenuObj.style.display = '';
			
			return false;
		}
	}
	
	this.hide = function()
	{
		var MenuObj = $(ABook.name+'menu');
		if(MenuObj) MenuObj.style.display = 'none';
	}
	
	this.add = function(AName, AImage, AAction)
	{
		AItem = new itBookMenuItem(this.book);
		AItem.name = AName;
		AItem.image = AImage;
		AItem.action = AAction;
		this.addItem(AItem);
	}
	
	this.addSeparator = function()
	{
		AItem = new itBookMenuSeparator(this.book);
		this.addItem(AItem);
	}
	
	this.addItem = function(AItem)
	{
		AItem.index = this.items.length;
		this.items.push(AItem);
	}
	
	this.count = function()
	{
		return this.items.length;
	}
	
	this.clear = function()
	{
		this.items.clear();
	}
	
	this.getItemByIndex = function(AIndex)
	{
		if(AIndex == -1 || AIndex >= this.count()) return null;
		
		return this.items[AIndex];
	}
	
	this.menuClick = function(AIndex)
	{
		alert(AIndex);
	}
	
	this.create = function()
	{
		this.menu.innerHTML = '';
		if(this.book.ie6) var MenuHeight = 4;
		else var MenuHeight = 0;
			
		var AMenuThis = this;
						
		for(var i=0; i<this.items.length; i++)
		{
			var _elem = this.items[i];
			var _name = this.book.name+'MenuItem'+i;
			
			if(_elem instanceof itBookMenuItem)
			{
				var item = '<span class="MenuImage" id="'+_name+'Img"><img src="/img/'+_elem.image+'" width="16" height="16" alt=""></span><span class="MenuText" id="'+_name+'" onmouseover="this.className=\'MenuTextFocused\'; $(\''+_name+'Img\').className=\'MenuImageFocused\';" onmouseout="this.className=\'MenuText\'; $(\''+_name+'Img\').className=\'MenuImage\';" onmousedown="MenuClick(\''+this.book.name+'\', '+i+');">'+_elem.name+'</span>';
				MenuHeight += 25;
			}
			else
			{
				var item = '<span class="MenuSeparatorImage"><img src="/img/temp.gif" width="1" height="1"></span><span class="MenuSeparatorText"><img src="/img/temp.gif" width="1" height="1"></span>';
				MenuHeight += 5;
			}
			if(i < this.items.length-1)
			{
				item += '<span class="MenuEndItem"></span>';
				MenuHeight += 1;
			}
				
			this.menu.innerHTML += item;									
		}
		
		this.menu.style.height = MenuHeight+'px';
	}
	
	return this;
}

function itBookPage(ABook)
{
	this.book = ABook;
	this.index = -1;
	this.id = 0;
	this.name = '';
	this.size = new itBookPageSize(ABook, this);
	this.toc_name = '';
	this.notes = '';
	this.loading_src = '';
	
	this.preLoad = function()
	{
		var SizeInfo = this.size.getSize();
		if(SizeInfo)
		{
			if(SizeInfo.loaded || SizeInfo.loading) return;
			this.book.pages.loadImage(this.index, SizeInfo, false);
		}
	}
	
	this.load = function()
	{		
		var SizeInfo = this.size.getSize();
		if(!SizeInfo)
		{
			alert(itBookMsg['no_page_data']);
			return;
		}
		
		this.ShowNotes();

		this.book.setPageSize(SizeInfo.width, SizeInfo.height);
		this.book.showPage();
		this.book.pages.loadImage(this.index, SizeInfo);
		
		this.book.showNotes(this.notes != '');
	}
	
	this.ShowNotes = function()
	{
		var ANotes = this.notes;		
		
		if(ANotes == '')
		{
			ANotes = itBookMsg['add_note'];
			$('TextNote').style.color = 'gray';
		}
		else
		{
			$('TextNote').style.color = 'black';
		}
		
		this.book.CancelNote();
		ANotes = ANotes.replace(/\n/g, '<br>');
		$('TextNote').innerHTML = ANotes;
	}
}

function itBookPageSizeItem(ABook, APage)
{
	this.book = ABook;
	this.page = APage;
	this.index = -1;
	this.width = '';
	this.height = '';	
	this.file = '';
	
	this.image = new Image();
	this.loading = false;
	this.loaded = false;
		
	this.showImage = function()
	{
		if(this.image.src == this.book.loading_src && new Number(this.book.page_width)*1 == new Number(this.width)*1 && new Number(this.book.page_height)*1 == new Number(this.height)*1)
		{
			this.book.page_img.style.backgroundImage = 'url('+this.image.src+')';
			
			if(this.book.load_page)
			{
				this.book.load_page = false;
				NewPosTop = 0;
				NewPosLeft = 0;
			}
			else
			{
				var OldHeight = this.page.size.getItemByIndex(this.book.view_old_zoom).height;
				var NewHeight = this.page.size.getItemByIndex(this.book.view_zoom).height;			
				var NewPosTop = Math.floor((this.book.box.scrollTop / OldHeight) * NewHeight);
				if(NewPosTop < 0) NewPosTop = 0;
				
				var OldWidth = this.page.size.getItemByIndex(this.book.view_old_zoom).width;
				var NewWidth = this.page.size.getItemByIndex(this.book.view_zoom).width;			
				var NewPosLeft = Math.floor((this.book.box.scrollLeft / OldWidth) * NewWidth);
				if(NewPosLeft < 0) NewPosLeft = 0;
			}
			
			this.book.box.scrollTop = NewPosTop;
			this.book.box.scrollLeft = NewPosLeft;	
			this.book.showLoadingBar(false);
		}
	}	
}

function itBookPageSize(ABook, APage)
{
	this.items = new Array();
	this.book = ABook;
	this.page = APage;
	
	this.add = function(AWidth, AHeight, AFile)
	{
		AItem = new itBookPageSizeItem(this.book, this.page);
		AItem.width = new Number(AWidth);
		AItem.height = new Number(AHeight);
		AItem.file = AFile;
		this.addItem(AItem);
	}
	
	this.addItem = function(AItem)
	{
		AItem.index = this.items.length;
		this.items.push(AItem);
	}
	
	this.count = function()
	{
		return this.items.length;
	}
	
	this.clear = function()
	{
		this.items.clear();
	}
	
	this.getItemByIndex = function(AIndex)
	{
		if(AIndex == -1 || AIndex >= this.count()) return null;
		
		return this.items[AIndex];
	}
	
	this.getSize = function()
	{
		if(this.items.length == 0) return;
		
		var ReturnIndex = 0;
		
		if(this.book.view_mode == 3)
		{
			if(this.book.view_zoom < 0) this.book.view_zoom = 0;
			if(this.book.view_zoom >= this.items.length) this.book.view_zoom = this.items.length-1;
			ReturnIndex = this.book.view_zoom;
						
			return this.getItemByIndex(ReturnIndex);
		}
		
		if(this.book.view_mode == 1)
		{
			var TestSize = this.items[0].width;
			var BoxSize = this.book.box_width-50;
			if(this.book.left_menu.style.display == '')
				BoxSize -= $(this.book.name+'left_menu').getWidth();
		}
		else if(this.book.view_mode == 2)
		{
			var TestSize = this.items[0].height;
			var BoxSize = this.book.box_height;
		}				
		
		for(var i=0; i<this.items.length; i++)
		{
			if(this.book.view_mode == 1) var NewSize = this.items[i].width;
			else if(this.book.view_mode == 2) var NewSize = this.items[i].height;
			else var NewSize = this.items[i].width;
			if(NewSize > TestSize && NewSize <= BoxSize)
			{
				TestSize = NewSize;
				ReturnIndex = i;
			}
		}
		
		this.book.view_zoom = ReturnIndex;
		return this.getItemByIndex(ReturnIndex);
	}
}

function itBookPages(ABook)
{
	this.items = new Array();
	this.book = ABook;
	
	this.add = function(AId, AName, ANotes)
	{
		AItem = new itBookPage(this.book);
		AItem.id = AId;
		AItem.name = AName;
		if(!ANotes) ANotes = '';
		AItem.notes = ANotes;
		this.addItem(AItem);
	}
	
	this.addItem = function(AItem)
	{
		AItem.index = this.items.length;
		this.items.push(AItem);
	}
	
	this.count = function()
	{
		return this.items.length;
	}
	
	this.clear = function()
	{
		this.items.clear();
	}
	
	this.getItemByIndex = function(AIndex)
	{
		if(AIndex == -1 || AIndex >= this.count()) return null;
		
		return this.items[AIndex];
	}
	
	this.getItemByName = function(AName)
	{
		for(var i=0; i<this.items.length; i++)
		{
			if(this.items[i].name == AName)
			{
				return this.items[i];
			}
		}
		return null;
	}
	
	this.getIndexByName = function(AName)
	{
		for(var i=0; i<this.items.length; i++)
		{
			if(this.items[i].name == AName)
			{
				return i;
			}
		}
		return 0;
	}
	
	this.getIndexById = function(AId)
	{
		for(var i=0; i<this.items.length; i++)
		{
			if(this.items[i].id == AId)
			{
				return i;
			}
		}
		return 0;
	}
	
	this.loadImage = function(AIndex, ASizeObj, AShowLoadedImage)
	{
		if(arguments.length < 3) AShowLoadedImage = true;
		if(AShowLoadedImage) this.book.loading_src = ASizeObj.file;
		
		if(!ASizeObj.loaded && !ASizeObj.loading || AShowLoadedImage)
		{
			ASizeObj.image.src = ASizeObj.file;			
			
			ASizeObj.loading = true;
			if(AShowLoadedImage)
				this.book.page_img.style.backgroundImage = '';
			
			//Event.observe(ASizeObj.image, 'load', function() {
				//ASizeObj.loading = false;
				//ASizeObj.loaded = true;				
			//} );
			//if(ASizeObj.image.complete) {
			//	ASizeObj.loading = false; ASizeObj.loaded = true;
			//}
		}
		if(!AShowLoadedImage) return true;
							
		var AThis = this;		

		new PeriodicalExecuter(function(Ev) {
			if(!ASizeObj.image.complete) return;
			Ev.stop();
			ASizeObj.showImage();
			AThis.preDownloadImages();	
		}, 0.1);
	}
	
	this.preDownloadPage = function(AIndex)
	{
		AIndex = new Number(this.book.page)+AIndex-1;

		var Item = this.getItemByIndex(AIndex);
		if(!Item) return true;
				
		Item.preLoad();
		return false;
	}
	
	this.preDownloadImages = function()
	{
		var AThis = this;
		
		new PeriodicalExecuter(function(Ev) {			
			var PreIndex = Array(1, -1);
			var PreOk = true;
			
			for(var i = 0; i < PreIndex.length; i++)
			{				
				var PreI = new Number(PreIndex[i]);
				if(!AThis.preDownloadPage(PreI))
				{
					PreOk = false;
					break;	
				}
			}
			
			if(PreOk) Ev.stop();
		}, 1);
	}
}

function ItBookInfo(ABook)
{
	this.book = ABook;
	
	this.title = '';
	this.author = '';
	this.year = '';
	this.pages = '';
	this.isbn = '';
	
	this.setInfo = function(xmlObj)
	{
		this.title = getXMLValue(xmlObj, 'title');
		this.author = getXMLValue(xmlObj, 'author');
		this.year = getXMLValue(xmlObj, 'year');
		this.pages = getXMLValue(xmlObj, 'pages');
		this.isbn = getXMLValue(xmlObj, 'isbn');
		
		this.book.refreshInfo();
	}
}

function itBook(ADivId, AWidth, AHeight)
{
	AThis = this;
	
	this.main = $(ADivId);
	if(!this.main) return;
	
	this.main.obj = this;
	
	this.ie6 = (BrowserDetect.browser == 'Explorer' && BrowserDetect.version == 6);
	if(this.ie6) this.scroll_width = 20;
	this.footer_height = 30;
								
	this.name = ADivId;
	this.page = 1;
	this.book_id = 0;
	this.getBookScript = '';
	this.getPageScript = '';
	this.view_mode = 1;
	this.view_zoom = 0;
	this.view_old_zoom = 0;
	
	// [1] - dopasuj do szerokosci
	// [2] - dppasuj do wysokosci
	// [3] - zoom
	
	this.img_preload = function(AImg)
	{
		var _img = new Image();
		_img.src = '/img/'+AImg;
		
		return _img;
	}
	
	this.loading_bar = '<img src="/img/loading_bar.gif" width="220" height="19" alt="">';
	this.img_preload('loading_bar.gif');
	
	this.btn_preload = function(AImg)
	{
		this.img_preload(AImg+'_btn.png');
		this.img_preload(AImg+'_btn_sel.png');
		this.img_preload(AImg+'_btn_dis.png');
	}
		
	this.btn_preload('first');
	this.btn_preload('next');
	this.btn_preload('prev');
	this.btn_preload('last');
	this.btn_preload('zoom_in');
	this.btn_preload('zoom_out');
	this.btn_preload('view_width');
	this.btn_preload('view_height');
	this.btn_preload('toc');
	this.btn_preload('notes');
	
	this.pages_id = this.name + '_nav';
	this.first_id = this.name + '_first';
	this.prev_id = this.name + '_prev';
	this.next_id = this.name + '_next';
	this.last_id = this.name + '_last';
	this.zoom_in_id = this.name + '_zoom_in';
	this.zoom_out_id = this.name + '_zoom_out';
	this.view_width_id = this.name + '_view_width';
	this.view_height_id = this.name + '_view_height';
	this.toc_id = this.name + '_toc';
	this.notes_id = this.name + '_notes';
	
	this.main.className = 'book';
	this.main.style.posiotion = 'relative';	
	
	this.wait = document.createElement('div');
	this.wait.innerHTML = '&nbsp;<img src="/img/loading.gif" width="32" height="32" alt="">&nbsp;';
	this.wait.className = 'wait';	
	this.wait.style.display = 'none';
	this.main.insertBefore(this.wait, null);		
	
	this.left_menu = document.createElement('div');
	this.left_menu.innerHTML = '';
	this.left_menu.className = 'left_menu';
	this.left_menu.id = this.name+'left_menu';
	this.main.insertBefore(this.left_menu, null);	
		
	this.box = document.createElement('div');
	this.box.innerHTML = '';
	this.box.className = 'box';
	this.box.id = this.name+'box';
	this.main.insertBefore(this.box, null);
	
	this.page_container = document.createElement('div');
	this.page_container.innerHTML = '';
	this.page_container.className = 'page_container';
	this.page_container.onselectstart = function() { return false; }
	this.page_container.ondragstart = function() { return false; }
	this.box.insertBefore(this.page_container, null);
	
	this.page_notes = document.createElement('div');
	this.page_notes.innerHTML = '<span id="TextNote"></span><textarea id="EditNote"></textarea><input type="button" value="Zapisz zmiany" id="SaveNote"><input type="button" value="Anuluj" id="CancelNote">';
	this.page_notes.className = 'page_notes';
	this.page_notes.id = this.name+'page_notes';	
	this.main.insertBefore(this.page_notes, null);
		
	this.page_box = document.createElement('div');
	this.page_box.innerHTML = '';
	this.page_box.className = 'page_box';
	this.page_container.insertBefore(this.page_box, null);
	
	this.page_obj = document.createElement('div');
	this.page_obj.innerHTML = '<div style="width: 50px; height: 50px;" id="'+this.name+'img"></div>';
	this.page_obj.className = 'page';
	this.page_box.insertBefore(this.page_obj, null);
	
	this.page_img = $(this.name+'img');
	
	this.page_right = document.createElement('div');
	this.page_right.innerHTML = '<span id="img_top"><img src="/img/shadow_rt.jpg" width="10" height="20"></span>';
	this.page_right.className = 'page_right';
	this.page_box.insertBefore(this.page_right, null);
	
	this.page_bottom = document.createElement('div');
	this.page_bottom.innerHTML = '<span id="img_left"><img src="/img/shadow_bl.jpg" width="20" height="10"></span><span id="img_right"><img src="/img/shadow_br.jpg" width="10" height="10"></span>';
	this.page_bottom.className = 'page_bottom';
	this.page_box.insertBefore(this.page_bottom, null);
	
	this.page_width = 0;
	this.page_height = 0;
	
	this.ClickNote = function()
	{
		if(this.page < 1) return;
		
		var APage = this.pages.getItemByIndex(this.page-1);
		if(!APage) return;
		
		var EditNote = $('EditNote');
		if(!EditNote) return;
		
		if(APage.notes == '') EditNote.value = '';			
		else EditNote.value = APage.notes;
		
		$('TextNote').style.display = 'none';
		$('SaveNote').style.display = '';
		$('CancelNote').style.display = '';
		$('EditNote').style.display = '';
		
		EditNote.focus();
	}
	
	this.SaveNote = function()
	{
		if(this.page < 1) return;
		
		var APage = this.pages.getItemByIndex(this.page-1);
		if(!APage) return;
		
		var EditNote = $('EditNote');
		if(!EditNote) return;
		
		APage.notes = EditNote.value; 
		APage.ShowNotes();
				
		advAJAX.get({
			url: '/xml/setNote.php',
			book_id: APage.book.book_id,
			page_id: APage.id,			
			notes: APage.notes
		});	
	}
	
	this.CancelNote = function()
	{
		$('TextNote').style.display = '';
		$('SaveNote').style.display = 'none';
		$('CancelNote').style.display = 'none';
		$('EditNote').style.display = 'none';
	}
	
	this.setPageSize = function(AWidth, AHeight)
	{
		var _w = new Number(AWidth);
		var _h = new Number(AHeight);
		
		this.page_width = _w;
		this.page_height = _h;
		
		if(AWidth < 10 || AHeight < 10) return;
		
		if(this.ie6)
		{
			this.page_box.style.width = _w+12+'px';
			this.page_box.style.height = _h+12+'px';
			this.page_obj.style.width = _w+2+'px';
			this.page_obj.style.height = _h+2+'px';
			this.page_right.style.height = _h+2+'px';
		}
		else
		{		
			this.page_box.style.width = _w+12+'px';
			this.page_box.style.height = _h+2+'px';			
			this.page_obj.style.width = _w+'px';
			this.page_obj.style.height = _h+'px';			
		}
		
		this.page_img.style.width = _w+'px';
		this.page_img.style.height = _h+'px';
		
		if(_h < this.height-this.footer_height) var NewMargin = new Number((this.height-this.footer_height-_h-30) / 2);
		else var NewMargin = 0;
		NewMargin = Math.floor(NewMargin);
		this.page_container.style.marginTop = NewMargin+'px';
	}
	
	this.showPage = function(AState)
	{
		if(arguments.length == 0) AState = true;
		
		if(AState)
			this.page_box.style.display = '';
		else
			this.page_box.style.display = 'none';
	}
	
	this.showToc = function(AState)
	{
		if(arguments.length < 1)
			AState = this.left_menu.style.display == 'none';
			
		if(!AState)
			this.left_menu.style.display = 'none';
		else
			this.left_menu.style.display = '';
			
		this.updateToc();
		if(this.book_id > 0) this.getPage();
	}
	
	this.updateToc = function()
	{
		AState = this.left_menu.style.display == 'none';
		
		var AWidth = 0;
		
		if(AState)
		{
			AWidth = $('Wnd').getWidth();
		}
		else
		{
			var AHeight = this.box_height;
			if(!this.ie6) AHeight -= 10;
			
			this.left_menu.style.height = AHeight+'px';			
			AWidth = $('Wnd').getWidth() - $(this.name+'left_menu').getWidth();			
		}
		
		this.box.style.width = AWidth+'px';
		
		if(!this.ie6) AWidth -= 14;
		this.page_notes.style.width = AWidth+'px';		
		if(this.ie6) AWidth -= 14;
		$('TextNote').style.width = AWidth + 'px';
		AWidth -= 5;		
		$('EditNote').style.width = AWidth + 'px';
		
		AState2 = this.page_notes.style.display == 'none';
		
		if(AState2)
		{
			var AHeight = $('Wnd').getHeight() - 32;
			this.box.style.height = AHeight + 'px';
		}
		else
		{			
			var AHeight = this.box_height;
			if(!AHeight) return;
			
			if(!this.ie6) AHeight -= 15;
			AHeight -= 120;
			this.box.style.height = AHeight+'px';						
		}	
	}
	
	this.showNotes = function(AState)
	{		
		if(this.disabled) return;
		
		this.CancelNote();
		
		if(arguments.length < 1)
			AState = this.page_notes.style.display == 'none';
			
		if(!AState)
			this.page_notes.style.display = 'none';
		else
			this.page_notes.style.display = '';
			
		this.updateToc();
	}
	
	this.showToc(false);
	this.showNotes(false);
	
	this.footer = document.createElement('div');
	this.footer.innerHTML = '<span id="Buttons">&nbsp;&nbsp;&nbsp;<img id="'+this.toc_id+'" src="/img/toc_btn.png" width="32" height="27" alt="'+itBookMsg['toc']+'" title="'+itBookMsg['toc']+'" />&nbsp;&nbsp;&nbsp;<img id="'+this.view_width_id+'" src="/img/view_width_btn.png" width="32" height="27" alt="'+itBookMsg['view_width']+'" title="'+itBookMsg['view_width']+'" /><img id="'+this.view_height_id+'" src="/img/view_height_btn.png" width="32" height="27" alt="'+itBookMsg['view_height']+'" title="'+itBookMsg['view_height']+'" />&nbsp;&nbsp;&nbsp;<img id="'+this.zoom_in_id+'" src="/img/zoom_in_btn.png" width="32" height="27" alt="'+itBookMsg['view_zoom_in']+'" title="'+itBookMsg['view_zoom_in']+'" /><img id="'+this.zoom_out_id+'" src="/img/zoom_out_btn.png" width="32" height="27" alt="'+itBookMsg['view_zoom_out']+'" title="'+itBookMsg['view_zoom_out']+'" />&nbsp;&nbsp;&nbsp;<img id="'+this.first_id+'" src="/img/first_btn.png" width="32" height="27" alt="'+itBookMsg['first']+'" title="'+itBookMsg['first']+'" /><img id="'+this.prev_id+'" src="/img/prev_btn.png" width="32" height="27" alt="'+itBookMsg['prev']+'" title="'+itBookMsg['prev']+'" />&nbsp;<select size="1" id="'+this.pages_id+'" class="book_select"></select>&nbsp;<img id="'+this.next_id+'" src="/img/next_btn.png" width="32" height="27" alt="'+itBookMsg['next']+'" title="'+itBookMsg['next']+'" /><img id="'+this.last_id+'" src="/img/last_btn.png" width="32" height="27" alt="'+itBookMsg['last']+'" title="'+itBookMsg['last']+'" />&nbsp;&nbsp;&nbsp;<img id="'+this.notes_id+'" src="/img/notes_btn.png" width="32" height="27" alt="'+itBookMsg['notes']+'" title="'+itBookMsg['notes']+'" /></span><span id="AppLogo"><a href="#" onclick="window.open(\'http://ipaper.itelix.pl\');" onfocus="blur();"><img src="/img/ipaper.png" width="59" height="25" alt="" border="0"></a></span><span id="LoadingBar"><img src="/img/loading_bar.gif" width="220" height="19" alt=""></span>';
	this.footer.className = 'footer';
	this.footer.style.height = this.footer_height+'px';
	this.footer.style.lineHeight = this.footer_height+'px';	
	this.main.insertBefore(this.footer, null);
	
	this.new_button = function(AId, AImg)
	{
		var NewBtn = $(AId);
		if(NewBtn)
		{		
			NewBtn.book = this;
			NewBtn.onmouseover = function() { if(this.disabled) this.src = '/img/'+AImg+'_btn_dis.png'; else this.src = '/img/'+AImg+'_btn_sel.png'; }
			NewBtn.onmousedown = function() { if(!this.disabled) this.src = '/img/'+AImg+'_btn_sel.png'; }
			NewBtn.onmouseout = function() { if(this.disabled) this.src = '/img/'+AImg+'_btn_dis.png'; else this.src = '/img/'+AImg+'_btn.png'; }
			NewBtn.setDisabled = function(AState)
			{
				NewBtn.disabled = AState;
				if(AState) this.src = '/img/'+AImg+'_btn_dis.png';
				else this.src = '/img/'+AImg+'_btn.png';
			}
		}	
		
		return NewBtn;
	}
	
	var _zoom_in = new Function('AThis.view_mode = 3; AThis.zoomIn(); AThis.getPage();');
	var _zoom_out = new Function('AThis.view_mode = 3; AThis.zoomOut(); AThis.getPage();');
	var _view_width = new Function('AThis.view_mode = 1; AThis.getPage();');
	var _view_height = new Function('AThis.view_mode = 2; AThis.getPage();');
	
	this.new_button(this.first_id, 'first').onclick = function() { if(!this.disabled) this.book.showFirstPage(); }
	this.new_button(this.prev_id, 'prev').onclick = function() { if(!this.disabled) this.book.showPrevPage(); }
	this.new_button(this.next_id, 'next').onclick = function() { if(!this.disabled) this.book.showNextPage(); }
	this.new_button(this.last_id, 'last').onclick = function() { if(!this.disabled) this.book.showLastPage(); }
	this.new_button(this.view_width_id, 'view_width').onclick = _view_width;
	this.new_button(this.view_height_id, 'view_height').onclick = _view_height;
	this.new_button(this.zoom_in_id, 'zoom_in').onclick = _zoom_in;
	this.new_button(this.zoom_out_id, 'zoom_out').onclick = _zoom_out;
	this.new_button(this.toc_id, 'toc').onclick = new Function('AThis.showToc();');
	this.new_button(this.notes_id, 'notes').onclick = new Function('AThis.showNotes();');
	
	this.menu = new itBookMenu(this);
	this.menu.add(itBookMsg['prev'], 'prev.png', $(this.prev_id).onclick);
	this.menu.add(itBookMsg['next'], 'next.png', $(this.next_id).onclick);
	this.menu.addSeparator();
	this.menu.add(itBookMsg['view_zoom_in'], 'zoom_in.png', _zoom_in);
	this.menu.add(itBookMsg['view_zoom_out'], 'zoom_out.png', _zoom_out);
	this.menu.addSeparator();
	this.menu.add(itBookMsg['view_width'], 'view_width.png', _view_width);
	this.menu.add(itBookMsg['view_height'], 'view_height.png', _view_height);	
	this.menu.addSeparator();
	this.menu.add(itBookMsg['notes'], 'notes.png', $(this.notes_id).onclick);
	this.menu.addSeparator();
	this.menu.add(itBookMsg['close'], 'temp.gif', new Function('window.close();'));
	this.menu.create();
	
	this.info = new ItBookInfo(this);
	this.pages = new itBookPages(this);
	
	this.setSize = function(ANewWidth, ANewHeight)
	{		
		if(this.width == ANewWidth && this.height == ANewHeight) return;
		if(ANewWidth <= 50 || ANewHeight <= 50) return;
		
		this.width = ANewWidth;
		this.height = ANewHeight;
		
		var BoxHeight = this.height;
		BoxHeight -= this.footer_height;
		this.box_height = BoxHeight;
		this.box_width = this.width;
			
		this.box.style.height = BoxHeight+'px';
			
		this.wait.style.height = BoxHeight+'px';
		this.wait.style.lineHeight = BoxHeight+'px';
			
		this.main.style.width = this.width+'px';
		this.main.style.height = this.height+1+'px';
		
		this.updateToc();
	}
		
	this.showWait = function(AVisible)
	{				
		if(AVisible) this.wait.style.display = '';
		else this.wait.style.display = 'none';
	}
	
	this.showBox = function(AVisible)
	{
		if(AVisible) this.box.style.display = '';
		else this.box.style.display = 'none';
	}
	
	this.showLoadingBar = function(AVisible)
	{
		if(AVisible) $('LoadingBar').style.display = '';// = this.loading_bar;
		else $('LoadingBar').style.display = 'none';//innerHTML = '';
	}
	
	this.showPageNum = function(APageNum, APartNum, AUpdateToc)
	{
		this.saveData();
		this.load_page = true;
		
		if(arguments.length < 3) AUpdateToc = true;
		
		APageNum = this.pages.getIndexByName(APageNum)+1;
		
		if(APageNum >= this.pages.count()) APageNum = this.pages.count();
		else if(APageNum < 1) APageNum = 1;
		
		this.page = APageNum;
		this.clearCache();

		this.getPage(APageNum, APartNum, AUpdateToc);		
	}
	
	this.showFirstPage = function()
	{
		this.saveData();
		this.load_page = true;
		
		this.page = 1;
		this.clearCache();

		this.getPage();
	}
	
	this.showPrevPage = function()
	{
		this.saveData();
		this.load_page = true;
		
		if(this.page <= 1) this.page = this.pages.count();
		else this.page--;
		this.clearCache();
		
		this.getPage();
	}
	
	this.showNextPage = function()
	{		
		this.saveData();
		this.load_page = true;

		if(this.page >= this.pages.count()) this.page = 1;
		else this.page++;
		this.clearCache();
		
		this.getPage();
	}
	
	this.showLastPage = function()
	{
		this.saveData();
		this.load_page = true;
		
		this.page = this.pages.count();
		this.clearCache();
		
		this.getPage();
	}
	
	this.zoomIn = function()
	{
		this.view_old_zoom = this.view_zoom;
		
		return ++this.view_zoom;
	}
	
	this.zoomOut = function()
	{
		this.view_old_zoom = this.view_zoom;
		
		return --this.view_zoom;
	}
	
	this.updatePages = function(AFullUpdate)
	{
		var Obj = $(this.name+'_nav');
		if(!Obj) return;
		if(arguments.length < 1) AFullUpdate = true;
		
		var AThis = this;
		
		setObjectDisabled(this.pages_id, false);
		
		$(this.first_id).setDisabled(this.pages.count() == 0 || this.page == 1);
		$(this.prev_id).setDisabled(this.pages.count() == 0 || this.page == 1);
		$(this.next_id).setDisabled(this.pages.count() == 0 || this.page ==	 this.pages.count());
		$(this.last_id).setDisabled(this.pages.count() == 0 || this.page ==	 this.pages.count());
		
		$(this.zoom_in_id).setDisabled(this.pages.count() == 0);
		$(this.zoom_out_id).setDisabled(this.pages.count() == 0);
		$(this.view_width_id).setDisabled(this.pages.count() == 0);
		$(this.view_height_id).setDisabled(this.pages.count() == 0);
		$(this.toc_id).setDisabled(this.pages.count() == 0);
		$(this.notes_id).setDisabled(this.pages.count() == 0);
		
		if(!AFullUpdate)
		{
			Obj.options[this.page-1].selected = true;
			if(this.ie6) Obj.options.selectedIndex = this.page-1;
			
			return true;
		}
		
		$(this.name+'_nav').book = this;
		
		Event.observe(this.name+'_nav', 'change', function()	
		{
			var Obj = $(AThis.name+'_nav');
			var APageNum = Obj.options[Obj.selectedIndex].value;
			if(APageNum > 0)
			{
				Obj.book.load_page = true;
				Obj.book.saveData();
				Obj.book.page = APageNum;
				Obj.book.clearCache();
				Obj.book.getPage();
			}			
		});
		
		while (Obj.firstChild) Obj.removeChild(Obj.firstChild);
		
		for(var i=0; i<this.pages.count(); i++)
		{
			var title = itBookMsg['page']+' '+this.pages.getItemByIndex(i).name;
			var item = new Option(title, (i+1), i+1 == this.page);
			if(i+1 == this.page) item.selected = true;
			Obj.options.add(item);
		}
		if(this.ie6) Obj.options.selectedIndex = this.page-1;
		
		if(this.pages.count() == 0)
		{
			var item = new Option(itBookMsg['no_pages'], 0, true);
			item.selected = true;			
			Obj.options.add(item);
		}		
	}
	
	this.refreshInfo = function()
	{
		window.document.title = this.info.title;
	}
	
	this.saveData = function()
	{
		
	}
	
	this.clearCache = function()
	{
		for(var i=0; i<this.pages.count(); i++)
			if(i+1 != this.page && i+1 != this.page+1 && i+1 != this.page-1)
			{
				for(var j=0; j<this.pages.items[i].size.count(); j++)
					this.pages.getItemByIndex(i).size.getItemByIndex(j).image = new Image;
			}
				
	}
	
	this.getBook = function(AId, ASetPage)
	{
		this.book_id = AId;
		
		if(this.getBookScript == '') return;
				
		$(this.first_id).setDisabled(true);
		$(this.prev_id).setDisabled(true);
		$(this.next_id).setDisabled(true);
		$(this.last_id).setDisabled(true);
		
		$(this.zoom_in_id).setDisabled(true);
		$(this.zoom_out_id).setDisabled(true);
		$(this.view_width_id).setDisabled(true);
		$(this.view_height_id).setDisabled(true);
		$(this.toc_id).setDisabled(true);
		$(this.notes_id).setDisabled(true);
		
		setObjectDisabled(this.pages_id, true);		
			
		var AThis = this;
		
		advAJAX.get({
		    url: '/xml/'+AThis.getBookScript+'.php',
		    id: AId,
		    
		    onInitialization : function() {
				AThis.showWait(true);		    	
		        AThis.showBox(false);					        
		    },
		    onSuccess : function(obj) {
  		        var xmldoc = obj.responseXML;
				
				var status = getXMLValue(xmldoc, 'status');
		        var msg = getXMLValue(xmldoc, 'message');
		        
		        if(status == 'ERROR')
		        {
					alert(msg);
					return;
				}
				
				AThis.info.setInfo(xmldoc);
				AThis.pages.clear();
				
				var info = xmldoc.getElementsByTagName('page');
		        if(info.length > 0) for(var i=0; i < info.length; i++)
				{
					AThis.pages.add(
						getXMLValue(info[i], 'id'),
						getXMLValue(info[i], 'name'),
						getXMLValue(info[i], 'notes')
					);
				}
				
				var info = xmldoc.getElementsByTagName('size');
				if(info.length > 0) for(var i=0; i < info.length; i++)
				{					
					var APage = AThis.pages.getItemByIndex(AThis.pages.getIndexById(info[i].getAttribute('page')));
					if(APage)
					{
						APage.size.add(
							info[i].getAttribute('width'),
							info[i].getAttribute('height'),
							info[i].getAttribute('file')
						)
					}
				}
				
				var arrItems = new Array();
				
				function getItems(_xml, ABook)
				{				
					var _len = _xml.childNodes.length;	
		        	if(_len > 0) 
		        	{		
						var mainArray = new Array();
										
						for(var i=0; i < _len; i++) if(_xml.childNodes[i].getAttribute('text'))
						{
							var _text = _xml.childNodes[i].getAttribute('text');
							var _page = _xml.childNodes[i].getAttribute('page');
							var _part = _xml.childNodes[i].getAttribute('part');
														
							var newItem = Array();
							newItem.push(_text);
							newItem.push(_page);
							newItem.push(_part);
							
							if(_page)
							{
								var PageObj = ABook.pages.getItemByName(_page);
								if(PageObj)
									PageObj.toc_name = _text;
							}
							var _sub = getItems(_xml.childNodes[i], ABook);
							if(!_sub) newItem.push(null);
							else
							{
								_sub.each(function(item) {
									newItem.push(item);
								});								
							}
							
							mainArray.push(newItem);
						}
						
						return mainArray;
					}
				}
				
				var toc = xmldoc.getElementsByTagName('toc');
		        if(toc.length > 0) arrItems = getItems(toc[0], AThis);

				AThis.toc_info = new tree(arrItems, AThis.left_menu, AThis.name);
		    },
		    onError : function(obj) {
				alert(itBookMsg['error']+obj.status);
			},
		    onFinalization : function() {		        		       		        			
		        AThis.updatePages();
				AThis.showWait(false);
				AThis.showBox(true);
								
				if(!ASetPage) ASetPage = 1;
				else ASetPage = AThis.pages.getItemByName(ASetPage).index+1;
				
				AThis.page = ASetPage;
				AThis.getPage();
		    }
		});
	}
	
	this.getPage = function(APageIndex, APartNum, AUpdateToc)
	{
		if(this.getPageScript == '') return;

		if(arguments.length < 1) var APageIndex = this.page;
		if(arguments.length < 2) var APartNum = 0;
		if(arguments.length < 3) AUpdateToc = true;
		if(APageIndex < 1 || APageIndex > this.pages.count()) return;		
						
		var APage = this.pages.getItemByIndex(APageIndex-1);
		if(!APage) return;

		if(APage.size.count() > 0)
		{
			this.updatePages(false);
			this.showLoadingBar(true);			
			this.menu.hide();
								
			APage.load();			
			
			var SelPageIndex = APageIndex-1;
			while(SelPageIndex >= 0 && this.pages.getItemByIndex(SelPageIndex).toc_name == '')
				SelPageIndex--;
			
			if(AUpdateToc)
			{
				var SelPage = this.pages.getItemByIndex(SelPageIndex);
				var PageSize = APage.size.getSize();
				
				if(SelPage)	this.toc_info.selectItem(this.book_id, APage.id, APageIndex == 1, PageSize.width, PageSize.height, SelPage.name);
				else this.toc_info.selectItem(this.book_id, APage.id, APageIndex == 1, PageSize.width, PageSize.height, null);
			}
			this.updatePages(false);
				
			return true;
		}
		
		//if(!APreLoad) AThis.updatePages(false);
		
		/*
		advAJAX.get({
		    url: '/xml/'+AThis.getPageScript+'.php',
		    book_id: this.book_id,
		    page_id: APage.id,
		    
		    onSuccess : function(obj) {
  		        var xmldoc = obj.responseXML;
				
				var status = getXMLValue(xmldoc, 'status');
		        var msg = getXMLValue(xmldoc, 'message');
		        
		        if(status == 'ERROR')
		        {
					if(!APreLoad) alert(msg);
					return;
				}				
				
				APage.size.clear();
				var info = xmldoc.getElementsByTagName('size');
				if(info.length > 0) for(var i=0; i < info.length; i++)
				{
					APage.size.add(
						info[i].getAttribute('width'),
						info[i].getAttribute('height'),
						info[i].getAttribute('file')
					);
				}
				if(APreLoad) APage.preLoad();
				else APage.load();
		    },
		    onError : function(obj) {
		    	if(!APreLoad) alert(itBookMsg['error']+obj.status);
			},
		    onFinalization : function() {			
		        if(!APreLoad) AThis.updatePages(false);
		    }
		});
		*/
	}
	
	Event.observe(window, 'resize', function() {
		var _w = $('bs').getWidth();
		var _h = $('bs').getHeight();
		
		AThis.setSize(_w, _h);
		AThis.getPage();
	});
		
	new DragScrollable(this.name+'box');
	
	this.setSize(AWidth, AHeight);
	this.setPageSize(50, 50);
	this.showPage(false);
	
	$('TextNote').onclick = new Function('AThis.ClickNote()');
	$('SaveNote').onclick = new Function('AThis.SaveNote()');
	$('CancelNote').onclick = new Function('AThis.CancelNote()');
	this.CancelNote();
}

var itBookMsg = new Array();
itBookMsg['title_loading'] = 'Loading...';
itBookMsg['no_data'] = '&laquo;No data to display&raquo;';
itBookMsg['error'] = 'Error: ';
itBookMsg['page'] = 'Page ';
itBookMsg['no_pages'] = 'No pages';
itBookMsg['first'] = 'First';
itBookMsg['prev'] = '&laquo;&nbsp;Prev';
itBookMsg['next'] = 'Next&nbsp;&raquo;';
itBookMsg['last'] = 'Last';
itBookMsg['no_page_data'] = 'No data';
itBookMsg['close'] = 'Close window';
itBookMsg['view_width'] = 'Width';
itBookMsg['view_height'] = 'Height';
itBookMsg['view_zoom_in'] = 'Zoom In';
itBookMsg['view_zoom_out'] = 'Zoom Out';
itBookMsg['notes'] = 'Notes';
itBookMsg['toc'] = 'TOC';
itBookMsg['add_note'] = 'Kliknij, aby dodać własną notatkę.';
