// This file demonstrates the different options of the pagination plugin
// It also demonstrates how to use a JavaScript data structure to 
// generate the paginated content and how to display more than one 
// item per page with items_per_page.
        
/**
 * Callback function that displays the content.
 *
 * Gets called every time the user clicks on a pagination link.
 *
 * @param {int}page_index New Page index
 * @param {jQuery} jq the container with the pagination links as a jQuery object
 */
function pageselectCallback(page_index, jq){
    // Get number of elements per pagionation page from form
    var items_per_page = 20;
    var max_elem = Math.min((page_index+1) * items_per_page, contos.length);
    var newcontent = '';

    // Iterate through a selection of the content and build an HTML string
    for(var i=page_index*items_per_page;i<max_elem;i++)
    {	
		//na nossa estrutura:				
    	//contos[i][0] = Categoria
		//contos[i][1] = Url da pagina do video
		//contos[i][2] = DataHora
		//contos[i][3] = Titulo
		//contos[i][4] = Descricao
		
		newcontent += "<div class='mostraConto'>";
			
		newcontent += "<ul>";
		newcontent += "<li class='categoriaConto'>" + contos[i][0] + "</li>";
		//newcontent += "<li class='data'>" + contos[i][2] + "</li>";
		newcontent += "<li class='titulo'>";
		newcontent += "<a href='" + contos[i][1] + "'>" + contos[i][3] + "</a>";
		newcontent += "</li>";
		newcontent += "<li class='descricaoConto'>" + contos[i][4] + "</li>";
		newcontent += "<li class='leiamais'><a href='" + contos[i][1] + "'>Leia o conto completo</a></li>";
		newcontent += "</ul>";
		newcontent += "</div>";
    }
    
    // Replace old content with new content
    jQuery('div.mostraContos').html(newcontent);
    
    // Prevent click eventpropagation
    return false;
}

// The form contains fields for many pagiantion optiosn so you can 
// quickly see the resuluts of the different options.
// This function creates an option object for the pagination function.
// This will be be unnecessary in your application where you just set
// the options once.
function getOptions(){
    var opt = {callback: pageselectCallback};
    opt['items_per_page'] = 20;
    opt['num_display_entries'] = 3;
    opt['num_edge_entries'] = 2;
    opt['prev_text'] = '<< anterior';
    opt['next_text'] = 'próxima >>';
    // Collect options from the text fields - the fields are named like their option counterparts
    /*
    $("input:text").each(function(){
        opt[this.name] = this.className.match(/numeric/) ? parseInt(this.value) : this.value;
    }); */
    // Avoid html injections in this demo
    var htmlspecialchars ={ "&":"&amp;", "<":"&lt;", ">":"&gt;", '"':"&quot;"}
    jQuery.each(htmlspecialchars, function(k,v){
        opt.prev_text = opt.prev_text.replace(k,v);
        opt.next_text = opt.next_text.replace(k,v);
    })
    return opt;
}