window.onload = init;

function transition(background_frame, next_image)
{
  $('#'+background_frame).animate({
    marginLeft: '0'
  },'slow',
  function()
  {
    //Determine which image follows
    if(next_image == 3)
    {
      next_image = 1;
    }
    else
    {
      next_image++;
    }

    //Determine next frame to use as background/slider
    next_frame = "";
    if(background_frame == 'bframe')
    {
      next_frame = 'fframe';
    }
    else
    {
      next_frame = 'bframe';
    }

    $('#'+background_frame).css('z-index', '1');
    $('#'+next_frame).css('margin-left', '-710px');
    $('#'+next_frame).css('z-index', '2');
    $('#'+next_frame).attr('src', '/system/media/img/banner.png');
    setTimeout("transition('"+next_frame+"',"+next_image+")",3000);
  });
};


function init()
{
	setTimeout("transition('bframe',2)", 3000);

	$('#search select').selectmenu({style:'dropdown'});	

	$('#menu li').each(function(){
		$(this).hover(
		function(){
			$(this).addClass('selected');
		},
		function()
		{
			$(this).removeClass('selected');
		});
	});	

	$('#libros #panel li a').each(function(){
		$(this).hover(
		function(){
			$(this).addClass('selected');
		},
		function()
		{
			$(this).removeClass('selected');
		});
	});

	$('#menu a:contains("Nosotros")').click(function()
	{
		$('#main').load('nosotros.html');
	});

	$('#menu a:contains("Servicios")').click(function()
	{
		$('#main').load('servicios.html');
	});

	$('#menu a:contains("Libros")').click(function()
	{
		$('#main').load('libros.html');
    
	});

	$('#menu a:contains("Bibliotecas")').click(function()
	{
		$('#main').load('bibliotecas.html');
	});

	$('#menu a:contains("Bibliotecas")').click();


	$('#categories a').hover(
		function()
		{
			$(this).addClass('selected');
		},
		function()
		{
			$(this).removeClass('selected');
		}
	);


	$('#wishlist #update').click(function()
	{
		var books = {};
    var error = false;		

		$('#wishlist #results .book').each(function()
		{
			var id = $(this).attr('isbn');
			var quantity = $(this).children('.quantity').val();

      var pattern = /^[0-9]+$/;
      error = (!pattern.test(quantity)) || error;

			books[id] = quantity;
		});

    if(!error)
    {
		  $.post('/index.php/page/quantities', books, function()
		  {
			  alert('Cantidades actualizadas exitosamente');	
		  });
    }
    else
    {
      alert('Las cantidades deben ser número enteros mayores a cero');
    }

	});

/*PAGER*/

function next_page_set()
{
  var page_set_size = 10*10; /*Page size * number of pages*/
  var current_page_set = parseInt($('ul.pages').attr('page_set'));
  var quantity = parseInt($('span.count #quantity').html());
  var quantity_page_sets = Math.ceil(quantity / page_set_size); 

  if((current_page_set + 1) < quantity_page_sets) //Next page_set still in range?
  {
    var next_page_set_limit = page_set_size * (current_page_set + 2);
    //add 2, the limit of the next is the beginning of the next after next
    var quantity_next_page_set = page_set_size;
    if(next_page_set_limit > quantity)
    {
      var exceeding = next_page_set_limit - quantity;
      quantity_next_page_set = page_set_size - exceeding;
    }

    var pages_next_page_set = Math.ceil(quantity_next_page_set / 10);
    var start_page = ((current_page_set + 1) * 10);

    //Add CATEGORY filter, if any
    var category = $('span#categoria').attr('clave');
    if(category != null && typeof category != 'undefined')
    {
      category = '&categoria='+category;
    }
    else
    {
      category = '';
    }

    //Add EDITORIAL filter, if any
    var editorial = $('span#editorial').attr('clave');
    if(editorial != null && typeof editorial != 'undefined')
    {
      editorial = '&editorial='+editorial;
    }
    else
    {
      editorial = '';
    }

    //Add TITULO filter, if any
    var titulo = $('span#titulo').attr('query');
    if(titulo != null && typeof titulo != 'undefined')
    {
      titulo = '&search_query='+titulo+'&search_category=titulo';
    }
    else
    {
      titulo = '';
    }

    //Add AUTOR filter, if any
    var autor = $('span#autor').attr('query');
    if(autor != null && typeof autor != 'undefined')
    {
      autor = '&search_query='+autor+'&search_category=autor';
    }
    else
    {
      autor = '';
    }

    //Add ISBN filter, if any
    var isbn = $('span#isbn').attr('query');
    if(isbn != null && typeof isbn != 'undefined')
    {
      isbn = '&search_query='+isbn+'&search_category=isbn';
    }
    else
    {
      isbn = '';
    }

    var pages = '<li><a id="previous_page_set" href="#"><</a></li>';
    for(var i = 0; i < pages_next_page_set; i++)
    {
      pages += '<li>'+
               '<a href="/index.php/book/'+(start_page+i)+
               '?page_set='+(current_page_set+1)+
               category+
               editorial+
               titulo+
               autor+
               isbn+
               '">'+
               (start_page+i+1)+
               '</a>'+
               '</li>';
    }
    pages += '<li><a id="next_page_set" href="#">></a></li>';

    $('ul.pages').html(pages);
    $('ul.pages').attr('page_set', (current_page_set + 1));

    init_pager_events();
  }
};

function previous_page_set()
{
  var current_page_set = parseInt($('ul.pages').attr('page_set'));

  if(current_page_set > 0) //Not first page set?
  {
    var start_page = ((current_page_set - 1) * 10);

    var category = $('span#categoria').attr('clave');
    if(category != null && typeof category != 'undefined')
    {
      category = '&categoria='+category.attr('clave');
    }
    else
    {
      category = '';
    }

    var editorial = $('span#editorial').attr('clave');
    if(editorial != null && typeof editorial != 'undefined')
    {
      editorial = '&editorial='+editorial;
    }
    else
    {
      editorial = '';
    }

    //Add TITULO filter, if any
    var titulo = $('span#titulo').attr('query');
    if(titulo != null && typeof titulo != 'undefined')
    {
      titulo = '&search_query='+titulo+'&search_category=titulo';
    }
    else
    {
      titulo = '';
    }

    //Add AUTOR filter, if any
    var autor = $('span#autor').attr('query');
    if(autor != null && typeof autor != 'undefined')
    {
      autor = '&search_query='+autor+'&search_category=autor';
    }
    else
    {
      autor = '';
    }

    //Add ISBN filter, if any
    var isbn = $('span#isbn').attr('query');
    if(isbn != null && typeof isbn != 'undefined')
    {
      isbn = '&search_query='+isbn+'&search_category=isbn';
    }
    else
    {
      isbn = '';
    }

    var pages = '<li><a id="previous_page_set" href="#"><</a></li>';
    for(var i = 0; i < 10; i++)
    {
      pages += '<li>'+
               '<a href="/index.php/book/'+(start_page+i)+
               '?page_set='+(current_page_set-1)+
               category+
               editorial+
               titulo+
               autor+
               isbn+
               '">'+
               (start_page+i+1)+
               '</a>'+
               '</li>';
    }
    pages += '<li><a id="next_page_set" href="#">></a></li>';

    $('ul.pages').html(pages);
    $('ul.pages').attr('page_set', (current_page_set - 1));

    init_pager_events();
  }
};

function init_pager_events()
{
  $('#next_page_set').click(next_page_set);
  $('#previous_page_set').click(previous_page_set);
};

init_pager_events();


/*VIEW DETAIL EVENTS*/
$('#libros .book .ver_detalle').click(function()
{
  var book_id = $(this).attr('book_id');
 
  $.get('/index.php/detail/index', {id: book_id}, function(data)
  {
    $(data).dialog({
       modal: true, 
       position: 'center', 
       width: '500px',
       title: 'Detalle del Libro'
    });
  });  
  
  return false;
});

};

