/**
 * File: apme.content.js
 * Author: João Fonseca (j.fonseca@netcabo.pt)
 */
var apme = apme || {};

apme.content = {
	section : 1,
	page : 1,
	language : 1,
	filter : '',
	clientPagination : false,
	pageItems : null,
	pageSize : 0,
	pager: null,
	updatePager: function () {
		var totalPages = Math.ceil($(this.pageItems).length / this.pageSize);
		var that = this;
		$(this.pager).html(
			"<div id=\"news_bg\" align=\"center\">"
		  + (that.page > 1 ? "<a class=\"previousPage\">&lt;&lt;&nbsp;</a>" : "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
		  + that.page + " / "
 		  + totalPages
		  + (totalPages > that.page ? "<a class=\"nextPage\">&nbsp;&gt;&gt;</a>" : "")
		  + "</div>");

	},
	paginate : function(pageItems, pager, size) {
		this.pageItems = pageItems;
		this.page = 1;
		this.clientPagination = true;
		this.pageSize = size;
		this.pager = pager;
		this.update();
	},
	titles : [
			[ 'Not&iacute;cias', 'A APME', 'Neg&oacute;cios',
					'Servi&ccedil;os', 'Gabinete de Apoio T&eacute;cnico',
					'Parcerias', 'Associadas', 'Actividades', 'Contactos',
					'Arquivo', 'Links &Uacute;teis', 'Documentos' ],
			[ 'Noticias', 'Qui&eacute;nes Somos', 'Mercado Virtual',
					'Asesoramiento', 'Oficina de Apoyo Tecnico', 'Alianzas',
					'Asociadas', 'Actividades', 'Contacto', 'Archivo',
					'La APME en la Web' ],
			[ 'News', 'About Us', 'Virtual Market', 'Advisory',
					'Technical Support Office', 'Partners', 'Members',
					'Activities', 'Contacts', 'Archive', 'APME on the Web' ] ],
	setTitle : function() {
		var that = this;
		$("#right_container").html(
				"<h1>" + that.titles[that.language - 1][that.section - 1]
						+ "</h1>" + $("#right_container").html());
	},
	update : function() {
		var that = this;
		if (this.clientPagination) {
			$(this.pageItems).each(function(idx, elem){
				if (idx >= (that.page - 1) * that.pageSize &&
				idx < that.page * that.pageSize) {
					$(elem).show();
				}
				else {
					$(elem).hide();
				}
			});
			this.updatePager();
			return;
		}
		else {
			$.ajax({
				url: '/?section=' + that.section + '&language=' + that.language + '&page=' + that.page + '&filter=' + that.filter,
				success: function(data){
					$("#right_container").html(data);
					that.setTitle();
				}
			});
		}
	},
	nextPage : function() {
		this.page++;
		this.update();
	},
	prevPage : function() {
		this.page--;
		this.update();
	},
	init : function() {
		var that = this;
		$('.nextPage').live('click', function(e) {
			e.preventDefault();
			that.nextPage();
		})
		$('.previousPage').live('click', function(e) {
			e.preventDefault();
			that.prevPage();
		})
		//that.update();
	}
};

