buildShowLink = function (id, url, itemType)
{
	return '<a href="' + url + '?action=show&item=' + itemType + '&id=' + id + '">Show</a>' ;
}
//
buildAdminLink = function (id, url, itemType)
{
	return '<a href="' + url + '?action=form&item=' + itemType + '&id=' + id + '">Edit</a>&nbsp;'
	+ '<a href="' + url + '?action=post&do=delete&item=' + itemType + '&id=' + id + '">Delete</a>';
}
function NavigateContent (contentType, contentList, contentListUrl , divList , prevButs , nextButs, baseEditUrl, isAdmin)
{
	var list;
	var listSpan;
	var divs;
	var header;
	var adminBar;
	var footer;
	var currentItem = 0;
	var totalPage ;
	function showResponse (req)
	{
		list = $A (contentList.getElementsByTagName ('a')).reverse ();
		listSpan = $A (contentList.getElementsByTagName ('span'));
		divs = document.getElementsByClassName ('itemContent');
		header = document.getElementsByClassName ('itemHeader');
		adminBar = document.getElementsByClassName ('itemAdmin');
		footer = document.getElementsByClassName ('itemFooter');
		currentItem = 0;
		totalPage = parseInt ((list.length - 1) / divs.length);
		if (totalPage > 0)
		{
			showAllNext ();
		}
		buildlinks ();
	}
	
	resetAll = function (){
		Element.hide ('divList');
		for (var i ; i < divs.length ; i ++)
		{
			divs [i].innerHTML = "";
			header [i].innerHTML = "";
			adminBar [i].innerHTML = "";
			footer [i].innerHTML = "";
		}
		Element.show ('divList');
	}
	function populateHeader ()
	{
		
		var index;
		for (var i = 0 ; i < divs.length ; i ++)
		{
			index = i + (currentItem * divs.length);
			if (index < list.length )
			{
				
				header [i].innerHTML = '<hr>';
			} else
			{
				header [i].innerHTML = "";
			}
		}
	}
	function populateFooter ()
	{
		var index;
		for (var i = 0 ; i < divs.length ; i ++)
		{
			index = i + (currentItem * divs.length);
			if (index < list.length)
			{
				footer [i].innerHTML = buildShowLink (list [index].id, baseEditUrl, contentType);
			} else
			{
				footer [i].innerHTML = "";
			}
		}
	}
	function populateEditBar ()
	{
		if (isAdmin != "True" || ! isAdmin )
		{
			return;
		}
		var index;
		for (var i = 0 ; i < divs.length ; i ++)
		{
			index = i + (currentItem * divs.length);
			if (index < list.length )
			{
				adminBar [i].innerHTML = buildAdminLink (list [index].id, baseEditUrl, contentType);
			} else
			{
				adminBar [i].innerHTML = "";
			}
		}
	}
	function populateDiv ()
	{
		var index ;
		for (var i = 0 ; i < divs.length ; i ++)
		{
			divs [i].id = 'D' + i;
			index = i + (currentItem * divs.length);
			if (index < list.length )
			{
				new Ajax.Updater (divs [i].id, list [index] ,
				{
					method : 'get'
				});
			} else
			{
				divs [i].innerHTML = "";
			}
		}
	}
	goNext = function ()
	{
		resetAll();
		currentItem = currentItem + 1;
		if (currentItem >= totalPage )
		{
			currentItem = totalPage ;
		}
		if (totalPage > 0)
		{
			if (currentItem == totalPage)
			{
				hideAllNext ();
			} else
			{
				showAllNext ();
			}
			if (currentItem != 0)
			{
				showAllBack ();
			} else
			{
				hideAllBack ();
			}
		}
		buildlinks ();
	}
	goBack = function ()
	{
		resetAll();
		
		currentItem = currentItem - 1;
		if (currentItem < 0 )
		{
			currentItem = 0;
		}
		if (totalPage > 0)
		{
			if (currentItem == totalPage)
			{
				hideAllNext ();
			} else
			{
				showAllNext ();
			}
			if (currentItem != 0)
			{
				showAllBack ();
			} else
			{
				hideAllBack ();
			}
		}
		buildlinks ();
	}
	function hideAllNext ()
	{
		nextButs.each (function (node)
		{
			if (node != null) Element.hide (node);
		});
	}
	function showAllNext ()
	{
		nextButs.each (function (node)
		{
			if (node != null) Element.show (node);
		});
	}
	function hideAllBack ()
	{
		prevButs.each (function (node)
		{
			if (node != null) Element.hide (node);
		});
	}
	function showAllBack ()
	{
		prevButs.each (function (node)
		{
			if (node != null) Element.show (node);
		});
	}
	function setAllButs ()
	{
		prevButs.each (function (node)
		{
			if (node != null) Event.observe (node, 'click', goBack, true);
		});
		nextButs.each (function (node)
		{
			if (node != null) Event.observe (node, 'click', goNext, true);
		});
	}
	setAllButs ();
	hideAllNext ();
	hideAllBack ();
	this.build = function ()
	{
		new Ajax.Updater (contentList.id, contentListUrl,
		{
			method : 'get', onComplete : showResponse
		});
	}
	buildlinks = function ()
	{
		populateDiv ();
		populateEditBar ();
		populateHeader ();
		populateFooter ();
		if (totalPage >= 1){
			$('pageNumberTop').innerHTML = (currentItem + 1)+ "/" + (totalPage + 1);
			$('pageNumberBot').innerHTML = (currentItem + 1)+ "/" + (totalPage + 1);
		}
	}
	
	this.build ();
}

