function GetBook(xBookID){
	var AjaxGetBook = new req('AjaxGetBook');
	var xShowBook = new ShowBook();
	AjaxGetBook.xmlhttpPost('ops.php', xShowBook,'op=EditBook&Book=' + xBookID);
	GetBookPics(xBookID);
}
function ShowBook(){
	this.callBackFunction = function(xTxt) 
	{
		HandleBooksData(xTxt);
		if(xXML.length > 0)
		{
			GlBooks = xXML.length;
			document.getElementById('booksscroll').innerHTML = '';
			for(x=0; x < GlBooks; x++)
			{
				xRow = document.createElement("div");
				xRow.setAttribute("id",'ScrollBook_' + GlArrBooks[x][0]);
				xRow = DefineClass(xRow,'ResultElement');
				xRow.innerHTML = '<img src="' + GlArrBooks[x][2] +'" width="100px" alt="' + GlArrBooks[x][1] + '"><br>' + GlArrBooks[x][4];
				document.getElementById('booksscroll').appendChild(xRow);
			}
		}
	}
}
function GetBookPics(xBookID){
	var AjaxGetBookPics = new req('AjaxGetBookPics');
	var xShowBooksPics = new ShowBooksPics();
	AjaxGetBookPics.xmlhttpPost('ops.php', xShowBooksPics,'op=GetBooksPhotos&Book=' + xBookID);
}
function ShowBooksPics (){
	this.callBackFunction = function(xTxt) 
	{
		HandlePhotosData(xTxt);
		if(GlPhotos > 0)
		{
			for(x=0;x<GlPhotos;x++)
			{
				xRatio = GetImageRatio(GlArrPhotos[x][6], GlArrPhotos[x][7]);
				xRow = document.createElement("div");
				xRow.setAttribute("id",'ShowPic_' + x);
				xRow = DefineClass(xRow,'ResultElement');
				xRow.innerHTML = '<img src="' + GlArrPhotos[x][4] + '" width="' + xRatio[0] + '" height="' + xRatio[1] +'" onclick=ShowImage(' + x + ')><br>' + GlArrPhotos[x][2];
				document.getElementById('pics').appendChild(xRow);
			}
			if(GlOrigen !=0);
			{
				for(x=0;x < GlPhotos;x++)
				{
					if(GlArrPhotos[x][0] == GlOrigen)
					{
						showPhoto(x);
					}
				}
				GlOrigen = 0;
			}
		}
	}
}

function EditBook(xBook){
	var AjaxEditBook = new req('AjaxEditBook');
	var xShowBooksSpects = new ShowBooksSpects();
	AjaxEditBook.xmlhttpPost('ops.php', xShowBooksSpects,'op=EditBook&book='+xBook);
}
function ShowBooksSpects(){
	this.callBackFunction = function(xTxt) 
	{
		HandleBooksData(xTxt);
		document.getElementById('SelGroups').value = GlArrBooks[0][3];
		document.getElementById('TxtBookName').value = GlArrBooks[0][4];
		document.getElementById('TxtBookDesc').value = GlArrBooks[0][1];
		EGetBooksPics(GlArrBooks[0][0]);
	}
}

function EGetBooksPics(xBookID){
	ChangeDivColor(xBookID);
	document.getElementById('BtnUploadImages').disabled = false;
	document.getElementById('SelGroups').disabled = false;
	document.getElementById('TxtBookName').disabled = false;
	document.getElementById('TxtBookDesc').disabled = false;
	document.getElementById('BtnAplicar').disabled = true;
	var AjaxEGetBooksPics = new req('AjaxEGetBooksPics');
	var xEShowBooksPics = new EShowBooksPics();
	AjaxEGetBooksPics.xmlhttpPost('ops.php', xEShowBooksPics,'op=GetBooksPhotos&Book='+xBookID);
}
function EShowBooksPics(){
	this.callBackFunction = function(xTxt) 
	{
		HandlePhotosData(xTxt);
		document.getElementById('booksPic').innerHTML = '';
		document.getElementById('ActionBookPnl').innerHTML = '';
		if(GlPhotos > 0)
		{
			for(x=0;x<GlPhotos;x++)
			{
				xRow = document.createElement("div");
				xRow.setAttribute("id",GlArrPhotos[x][0]);
				xRow.innerHTML = '<img src="' + GlArrPhotos[x][4] + '" onclick=ShowPicsSpects(' + x + ')>';
				document.getElementById('booksPic').appendChild(xRow);
			}
		}
	}
}
function GetUsersBooks(){
	var AjaxGetUsersBooks = new req('AjaxGetUsersBooks');
	var xRefreshUsersBooks = new RefreshUsersBooks();
	AjaxGetUsersBooks.xmlhttpPost('ops.php', xRefreshUsersBooks,'op=GetUsersBooks');
}
function CreateNewBook(){
	xString = 'Name=' + document.getElementById('NewBookName').value + '&Desc=' + document.getElementById('NewBookDesc').value;
	var AjaxCreateNewBook = new req('AjaxCreateNewBook');
	var xRefreshUsersBooks = new RefreshUsersBooks();
	AjaxCreateNewBook.xmlhttpPost('ops.php', xRefreshUsersBooks,'op=CreateBook&'+xString);
}

function RefreshUsersBooks(){
	this.callBackFunction = function(xTxt) 
	{
		HandleBooksData(xTxt);
		if(xXML.length > 0)
		{
			document.getElementById('books').innerHTML = '';
			for(x=0; x < GlBooks; x++)
			{
				xRow = document.createElement("div");
				xRow.setAttribute("id",'book_' + GlArrBooks[x][0]);
				xRow = DefineClass(xRow,'book');
				xRow.innerHTML = '<a href="#" onclick="EditBook(' + GlArrBooks[x][0] + ')">' + GlArrBooks[x][4] + '</a>';
				document.getElementById('books').appendChild(xRow);
			}
		}
		document.getElementById('ActionBookPnl').innerHTML = '';
		if(GlBooks > 0 && GlOrigen == 1) // Existen libros y el pedido viene desde OnLoad (GlOrigen == 1).
		{
			
			GlOrigen = 0;
			EditBook(GlArrBooks[0][0]);
		}
	}
}

function SaveBooksChanges(){
	xString = 'Name=' + document.getElementById('TxtBookName').value + '&Desc=' + document.getElementById('TxtBookDesc').value;
	xString = xString + '&Group=' + document.getElementById('SelGroups').value + '&ID=' + GlCurrentBook;
	var AjaxSaveBooksChanges = new req('AjaxSaveBooksChanges');
	var xUpdateBookStatus = new UpdateBookStatus();
	AjaxSaveBooksChanges.xmlhttpPost('ops.php', xUpdateBookStatus,'op=UpdateBook&'+xString);
}

function UpdateBookStatus(){
	this.callBackFunction = function(xTxt) 
	{
		if(xTxt == 'ok')
		{
			alert("Se aplicaron los cambios");
			EnableAplicar(0);
			GetUsersBooks();
		}
		else
		{
			alert("Error al aplicar los cambios");
		}
	}
}

function ShowPicsSpects(xPhotoArrID){ //Completar
	alert(GlArrPhotos[xPhotoArrID][2]);
}
function ShowImage(xPic){
	ChangeDivPicColor(xPic);
	GlCurrentPhoto = xPic;
	document.getElementById('images').innerHTML = ' <a href="' + GlArrPhotos[xPic][5] + '" target="new"><img src="' + GlArrPhotos[xPic][4] + '"/></a>';
}
function showPhoto(xPhoto) {
	document.getElementById('images').innerHTML = '<a href="' +GlArrPhotos[xPhoto][5]+ '" target="new"><img src="' + GlArrPhotos[xPhoto][4] + '" alt="' + GlArrPhotos[xPhoto][3] + '"/></a>';
	ChangeDivPicColor(xPhoto);
	GlCurrentPhoto = xPhoto;
}

function GetNextPhoto(){
	if (GlCurrentPhoto < GlPhotos - 1)
	{
		showPhoto(GlCurrentPhoto + 1 );
	}
	else
	{
		showPhoto(0);

	}
}

function GetPrevPhoto(){
	if (GlCurrentPhoto == 0)
	{
		showPhoto(GlPhotos - 1);
		
	}
	else
	{
		showPhoto(GlCurrentPhoto - 1);
	}
}
