var sortNpage = Class.create();

sortNpage.prototype =
{
	initialize: function(table, rowsPerPage, sortColumn)
	{
		if (!$(table)) return;

		this.tbody = $(table).getElementsByTagName('tbody')[0];
		this.rows = $A(this.tbody.getElementsByTagName('tr'));

		this.rowCount = this.rows.length;

		this.thead = $(table).getElementsByTagName('thead')[0];
		this.columnCount = this.thead.getElementsByTagName('th').length;


		if (sortColumn && sortColumn <= this.columnCount)
		{
			this.sortRowsBy(sortColumn);
		}

		this.rowsPerPage = rowsPerPage;
		this.lastPage = Math.ceil(this.rowCount / this.rowsPerPage);

		this.maxVisiblePageNumbers = 5;

		this.gotoPage(1);
	},


	sortRowsBy: function(column, columnType)
	{
		var rows = this.rows;


		if (!columnType)
		{
			var firstCell = rows[0].getElementsByTagName('td')[column - 1].innerHTML.stripTags().toLowerCase();

			// type bubbling
			var type = 'string';
	    if (firstCell.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/) || firstCell.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) type = 'date';
	    if (firstCell.match(/^-?[£$€]?\s*[0-9\.\,]+\s*[£$€]?/)) type = 'currency';
	    if (firstCell.match(/^[\d\.]+$/)) type = 'numeric';
		}
		else
		{
			var type = columnType;
		}

    //alert(type);


    var sortMethod = (this.sortMethod == 'asc' && column == this.column)? 'desc' : 'asc';

    this.sortMethod = sortMethod;
    this.column = column;



		function sortFx(a, b)
		{
			var aVal = a.getElementsByTagName('td')[column - 1].innerHTML;
	    var bVal = b.getElementsByTagName('td')[column - 1].innerHTML;



	    if (type == '') return;

	    if (type == 'string')
	    {
	    	aVal = aVal.stripTags().toLowerCase();
	    	bVal = bVal.stripTags().toLowerCase();

	    	if (aVal == bVal) return 0;
		    if (sortMethod == 'asc')
		    {
		    	return (aVal < bVal)? -1 : 1;
		    }
		    // desc
		    return (aVal < bVal)? 1 : -1;
	    }


	    if (type == 'date')
	    {
	    	aVal = aVal.stripTags().toLowerCase();
	    	bVal = bVal.stripTags().toLowerCase();

	    	if (aVal.length == 10) {
		        aDate = aVal.substr(6, 4) + aVal.substr(3, 2) + aVal.substr(0, 2);
		    } else {
		        year = aVal.substr(6, 2);
		        if (parseInt(year) < 50) { year = '20' + year; } else { year = '19' + year; }

		        aDate = year + aVal.substr(3, 2) + aVal.substr(0, 2);
		    }

		    if (bVal.length == 10) {
		        bDate = bVal.substr(6, 4) + bVal.substr(3, 2) + bVal.substr(0, 2);
		    } else {
		        year = bVal.substr(6, 2);
		        if (parseInt(year) < 50) { year = '20' + year; } else { year = '19' + year; }

		        bDate = year + bVal.substr(3, 2) + bVal.substr(0, 2);
		    }

		    if (aDate == bDate) return 0;

		    if (sortMethod == 'asc')
		    {
		    	if (aDate < bDate) return -1;
		    	return 1;
		    }
		    // desc
		    if (aDate < bDate) return 1;
		    return -1;
	    }


	    if (type == 'currency')
	    {
	    	// remove <strike> elements and their content
	    	aVal = aVal.replace(/<strike>.*<\/strike>/, '');
	    	bVal = bVal.replace(/<strike>.*<\/strike>/, '');

	    	aVal = aVal.stripTags();
	    	bVal = bVal.stripTags();

	    	aVal = aVal.replace(/[^0-9]/g, '');
	    	bVal = bVal.replace(/[^0-9]/g, '');

	    	//alert(parseFloat(aVal) + ' - ' + parseFloat(bVal));

	    	if (sortMethod == 'asc') return parseFloat(aVal) - parseFloat(bVal);
	    	// desc
	    	return parseFloat(bVal) - parseFloat(aVal);
	    }


	    if (type == 'numeric')
	    {
	    	aVal = aVal.stripTags();
	    	bVal = bVal.stripTags();

	    	aVal = parseFloat(aVal);
		    bVal = parseFloat(bVal);



			  if (sortMethod == 'asc') return aVal - bVal;
			  // desc
			  return bVal - aVal;
	    }
		}


		rows.sort(sortFx);



		rows.each((function(row) {
      this.tbody.appendChild(row);
    }).bind(this));

    this.gotoPage(1);


    css.zebra();


		this.rows = rows;
	},



	gotoPage: function(pageNr)
	{
		var rows = this.rows;

		// hide all rows
		for (i=0, len=rows.length; i<len; i++)
		{
			if (rows[i]) $(rows[i]).style.display = 'none';
		}


		// unhide rows within page
		var startRow = this.rowsPerPage * (pageNr - 1);
		for(i=startRow, count=(startRow+this.rowsPerPage); i<count; i++)
		{
			if (rows[i]) $(rows[i]).style.display = '';
		}


		// hide paginationlinks if appropriate
		if ($('snpFirstPage') && $('snpLastPage') && $('snpPageNumbers'))
		{
			(this.lastPage > 1)? Element.show('snpFirstPage', 'snpLastPage', 'snpPageNumbers') : Element.hide('snpFirstPage', 'snpLastPage', 'snpPageNumbers');
		}

		// hide next & previous link if appropriate
		if ($('snpPreviousPageLink')) $('snpPreviousPageLink').style.visibility = (pageNr <= 1)? 'hidden' : '';
		if ($('snpNextPageLink')) $('snpNextPageLink').style.visibility = (pageNr >= this.lastPage)? 'hidden' : '';


		//if (document.recalc) document.recalc();


		this.currentPage = pageNr;

		this.redrawPageNumbers();

	},

	redrawPageNumbers: function()
	{
		if (this.lastPage <= 1 && $('snpPagination')) Element.hide('snpPagination');


		var numberLinks = '';

		var side = this.currentPage - Math.floor(this.maxVisiblePageNumbers / 2);
		var begin = (side > 0) ? side : 1;

		var rightMargin = this.lastPage - this.maxVisiblePageNumbers;
		if (begin > rightMargin && rightMargin > 0) begin = this.lastPage - this.maxVisiblePageNumbers + 1;


		var i = begin;
		while (i<(begin + this.maxVisiblePageNumbers) && i<=this.lastPage)
		{
			//alert('begin: ' + begin + ', i: ' + i);

			if (i == this.currentPage)
			{
				numberLinks += i + ' ';
			}
			else
			{
				numberLinks += '<a href="#" onclick="snp.gotoPage(' + i + ')">' + i + '</a> ';
			}

			i++;

		}

		if ($('snpPageNumbers')) $('snpPageNumbers').innerHTML = numberLinks;
	},

	gotoFirstPage: function()
	{
		this.gotoPage(1);
	},

	gotoLastPage: function()
	{
		this.gotoPage(this.lastPage);
	},

	gotoNextPage: function()
	{
		if (this.currentPage >= 1 && this.currentPage < this.lastPage)
		{
			this.gotoPage(this.currentPage + 1);
		}

	},

	gotoPreviousPage: function()
	{
		if (this.currentPage > 1 && this.currentPage <= this.lastPage)
		{
			this.gotoPage(this.currentPage - 1);
		}
	}


}






