function ContentChanger()
{	
};

ContentChanger.isLastPage = false;
ContentChanger.sectionIsLastPage = new Array();

ContentChanger.changeContent =
	function(sectionName, offset, limit, objectFrom, objectTo, filter, numPages, callback, effect)
	{
		if (objectTo.getAttribute("pagingLoaded")=="true")
		{
			var responseText = objectTo.innerHTML;
			ContentChanger.handleContent(objectFrom, objectTo, responseText, false, sectionName, effect);
			callback(sectionName, numPages, filter, effect);
		}
		else
		{
			var ajaxUrl = "/ajax/sectiondocs/"+sectionName+"/"+offset+"/"+limit + (filter==null?"":"/"+filter);
		
			var xmlHttp;
			try {
				// Firefox, Opera 8.0+, Safari
				xmlHttp=new XMLHttpRequest();
			}
			catch (e) {
				// Internet Explorer
				try {
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e) {
					try {
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e) {
						alert("Your browser does not support AJAX!");
						return false;
					}
				}
			}
			xmlHttp.onreadystatechange =
				function()
				{
					if(xmlHttp.readyState==4)
					{
						objectTo.setAttribute("pagingLoaded", "true");
						ContentChanger.handleContent(objectFrom, objectTo, xmlHttp.responseText, false, sectionName, effect);
						callback(sectionName, numPages, filter, effect);
					}
				}
			xmlHttp.open("GET", ajaxUrl, true);
			xmlHttp.send(null);	
		}
	}

ContentChanger.loadContent =
	function(sectionName, offset, limit, object, filter, effect)
	{
		if (object.getAttribute("pagingLoaded")=="true")
		{
			var responseText = object.innerHTML;
			ContentChanger.handleContent(object, object, responseText, false, sectionName, effect);
		}
		else
		{
			var ajaxUrl = "/ajax/sectiondocs/"+sectionName+"/"+offset+"/"+limit + (filter==null?"":"/"+filter);
		
			var xmlHttp;
			try {
				// Firefox, Opera 8.0+, Safari
				xmlHttp=new XMLHttpRequest();
			}
			catch (e) {
				// Internet Explorer
				try {
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e) {
					try {
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e) {
						alert("Your browser does not support AJAX!");
						return false;
					}
				}
			}
			xmlHttp.onreadystatechange =
				function()
				{
					if(xmlHttp.readyState==4)
					{
						object.setAttribute("pagingLoaded", "true");
						ContentChanger.handleContent(object, object, xmlHttp.responseText, false, sectionName, effect);
					}
				}
			xmlHttp.open("GET", ajaxUrl, true);
			xmlHttp.send(null);	
		}
	}


ContentChanger.preloadContent =
	function(sectionName, offset, limit, object, filter, effect)
	{
		if (object.getAttribute("pagingLoaded")=="true")
		{
			var responseText = object.innerHTML;
			ContentChanger.handleContent(object, object, responseText, true, sectionName, effect);
		}
		else
		{
			var ajaxUrl = "/ajax/sectiondocs/"+sectionName+"/"+offset+"/"+limit + (filter==null?"":"/"+filter);
		
			var xmlHttp;
			try {
				// Firefox, Opera 8.0+, Safari
				xmlHttp=new XMLHttpRequest();
			}
			catch (e) {
				// Internet Explorer
				try {
					xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch (e) {
					try {
						xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch (e) {
						alert("Your browser does not support AJAX!");
						return false;
					}
				}
			}
			xmlHttp.onreadystatechange =
				function()
				{
					if(xmlHttp.readyState==4)
					{
						object.setAttribute("pagingLoaded", "true");
						ContentChanger.handleContent(object, object, xmlHttp.responseText, true, sectionName, effect);
					}
				}
			xmlHttp.open("GET", ajaxUrl, true);
			xmlHttp.send(null);	
		}
	}



ContentChanger.handleContent =
	function(objectFrom, objectTo, content, isPreloading, sectionName, effect)
	{
//		var isLastPage = false;
	
//		alert("ContentChanger.handleContent");
		
		if (isPreloading!=true)
			isPreloading=false;
		
		var p0 = content.indexOf("<!--LP:");
		var p1 = content.indexOf("-->");
		
		//alert("content = "+content);
		
		if (p0!=-1)
		{
			var strLp = content.substring(p0+7, p1)
			ContentChanger.isLastPage = (strLp=="1");
			
			if (sectionName!=null)
			{
				//alert("ContentChanger setting lastPage to "+(strLp=="1")+" for section "+sectionName+", preloading = "+isPreloading);
				ContentChanger.sectionIsLastPage[sectionName] = (strLp=="1");
			}
		}
		
		p0 = content.indexOf("<script type=\"text/javascript\">");
		p1 = content.indexOf("</script>");

		var fnname="";
		if (p0!=-1)
		{
			fnname = content.substring(p0+31, p1);
			setTimeout(fnname, 100);
		}
	
		//alert("fnname = "+fnname);
		
		
		objectTo.innerHTML = content;

		if (!isPreloading)
		{
			if (objectFrom!=objectTo)
			{
				if (effect=="slide.left")
				{
					$(objectFrom).style.left="0px";
//					$(objectFrom).style.right="0px";
					$(objectFrom).style.marginLeft="0px";
					$(objectFrom).style.display="block";
					$(objectFrom).morph("margin-left: -724px;", {duration: 0.6, queue: {position:"end", scope:"contentChanger.out"}});
				}
				else if (effect=="slide.right")
				{
					$(objectFrom).style.left="0px";
//					$(objectFrom).style.right="0px";
					$(objectFrom).style.marginLeft="0px";
					$(objectFrom).style.display="block";
					$(objectFrom).morph("margin-left: 724px;", {duration: 0.6, queue: {position:"end", scope:"contentChanger.out"}});
				}
				else if (effect=="slide.longleft")
				{
					$(objectFrom).style.left="0px";
//					$(objectFrom).style.right="0px";
					$(objectFrom).style.marginLeft="0px";
					$(objectFrom).style.display="block";
					$(objectFrom).morph("margin-left: -990px;", {duration: 0.6, queue: {position:"end", scope:"contentChanger.out"}});
				}
				else if (effect=="slide.longright")
				{
					$(objectFrom).style.left="0px";
//					$(objectFrom).style.right="0px";
					$(objectFrom).style.marginLeft="0px";
					$(objectFrom).style.display="block";
					$(objectFrom).morph("margin-left: 990px;", {duration: 0.6, queue: {position:"end", scope:"contentChanger.out"}});
				}				
				else
				{
					$(objectFrom).style.left="0px";
//					$(objectFrom).style.right="0px";
					$(objectFrom).style.marginLeft="0px";
					$(objectFrom).style.display="block";
					$(objectFrom).fade({duration: 0.6, queue: {position:"end", scope:"contentChanger.out"}});
				}
			}

			if (effect=="slide.left")
			{
				$(objectTo).style.left="0px";
//				$(objectTo).style.right="0px";
				$(objectTo).style.marginLeft="724px";
				$(objectTo).style.display="block";
				$(objectTo).morph("margin-left: 0px;", {duration: 0.6, queue: {position:"end", scope:"contentChanger.in"}});
			}
			else if (effect=="slide.right")
			{
				$(objectTo).style.left="0px";
//				$(objectTo).style.right="0px";
				$(objectTo).style.marginLeft="-724px";
				$(objectTo).style.display="block";
				$(objectTo).morph("margin-left: 0px;", {duration: 0.6, queue: {position:"end", scope:"contentChanger.in"}});
			}
			else if (effect=="slide.longleft")
			{
				$(objectTo).style.left="0px";
//				$(objectTo).style.right="0px";
				$(objectTo).style.marginLeft="990px";
				$(objectTo).style.display="block";
				$(objectTo).morph("margin-left: 0px;", {duration: 0.6, queue: {position:"end", scope:"contentChanger.in"}});
			}
			else if (effect=="slide.longright")
			{
				$(objectTo).style.left="0px";
//				$(objectTo).style.right="0px";
				$(objectTo).style.marginLeft="-990px";
				$(objectTo).style.display="block";
				$(objectTo).morph("margin-left: 0px;", {duration: 0.6, queue: {position:"end", scope:"contentChanger.in"}});
			}
			else
			{
				$(objectTo).style.left="0px";
//				$(objectTo).style.right="0px";
				$(objectTo).style.marginLeft="0px";
				$(objectTo).style.display="none";
				$(objectTo).appear({duration: 0.6, queue: {position:"end", scope:"contentChanger.in"}});
			}
		}
	}

