/*
 * (c) Copyright 2009 Hen's Teeth Network. All rights reserved. www.hens-teeth.net
 */


/*
 * (c) Copyright 2009-2010 Hen's Teeth Network. All rights reserved. www.hens-teeth.net
 */


/*
 * Change 3
 * << prev 1 2 [3] 4 5 next >>
 *
 */

/*
 * 
 * Start: < [1] 2 3 ... 50 >
 * 1-3: < 1 [2] 3 4 ... 50 >
 * 4 to N-3 < 1 ... 4 [5] 6 ... 50 >
 * N-3 < 1 ... N-3 [N-2] N-1 N >
 * N: < 1 ... N-2 N-1 N >
 *
 * N = 1: No search results
 * N = 2: < 1 2 >
 * N = 3: < 1 2 3 >
 * N = 4: < 1 2 3 4 > 
 * N = 5: < 1 2 3 4 5 > 
 * N = 6: < 1 2 3 4 5 6 >
 * N = 7: < 1 2 3 ... 7 > < 1 2 3 4 ... 7 > < 1 2 3 4 5 ... 7 > < 1 ... 3 4 5 6 7 > 
 * N < 8 = Show all number
 * N > 8 use N-3 forumula
 */

/* This function reconstructs the PDG Shopping Cart next or previous search result URL, without   */
/*    the searchstart data.  This data is later dynamically added for each search page link.      */

function reconstructSearchURL (oldURL)
{
	var arrayURL = oldURL.split("&");
	var newURL = "";

	for (var i=0; i < arrayURL.length; i++)
	{
		if (arrayURL[i].indexOf("page") == -1)
		{
			newURL += arrayURL[i];
			if (i != (arrayURL.length - 1))
			{
				newURL += "&";
			}
		}	
	}
	
	if( i > 2 ) 
	{
		newURL += "&";
	}

	return newURL;
}


function getPageLinks(iStart, iEnd, iCurrent, resultsPerPage, barSearchURL)
{
	var aLinks = new Array();
	var sLink = "";
	
	iStart = parseInt(iStart);
	iEnd = parseInt(iEnd);
	iCurrent = parseInt(iCurrent);
	
	for (var i=iStart; i <= iEnd; i++)
	{
		if (i == iCurrent)
		{
			sLink = "<span class='pagebar-current'>" + i + "</span>";
		}
		else
		{
			sLink = "<a href='" + barSearchURL + "page=" + (i-1) * resultsPerPage + "'>" +
					i + "</a>";
		}
		
		aLinks.push(sLink);
	}
	
	return aLinks;
}
 
 
function betterSearchPageBar(sTargetID, pageInfo, nextSearchURL, prevSearchURL, resultsPerPage)
{
	var N = 1; // Number of steps away before ellipsisizing 
	var sEllipsis = "";
	var aLinks = new Array();
	var barSearchURL;

	var prevText = "prev";
	var nextText = "next";

	var arrayPageInfo = pageInfo.split(" ");
	
	
	//var currentPage = Number(arrayPageInfo[0]);
	var currentPage = window.location.href.slice(window.location.href.indexOf('page'));
	currentPage = parseInt(currentPage.replace("page",''));
	if(isNaN(currentPage))
	{
		currentPage = Number(arrayPageInfo[0]);
	 }
	
	var maxPage = Number(arrayPageInfo[2]);
	
	var iBack = (currentPage <=5) ? 1 : currentPage - 5;
	var iAhead = (currentPage >= maxPage - 5) ? maxPage : currentPage + 5;
	
	if ('0 of 0' == pageInfo || '' == pageInfo)
	{
		document.getElementById(sTargetID).innerHTML = '';
		return;
	}

	if (nextSearchURL != "")
	{
		barSearchURL = reconstructSearchURL(nextSearchURL);
	}
	else if (prevSearchURL != "")
	{
		barSearchURL = reconstructSearchURL(prevSearchURL);
	}
	else if (prevSearchURL == "" && nextSearchURL == "")
	{
		barSearchURL = reconstructSearchURL(document.location.href);
	}	
	
	aLinks.push("<div id='pageinfo'>Page " + currentPage + " of " + maxPage + " </div>");

	if (maxPage <= 1)
	{
		// Only one page of results
		document.getElementById(sTargetID).innerHTML = aLinks.join('&nbsp;');
		return; 
	}

	if (prevSearchURL == "")
	{
		var iStart = 0;
		if (currentPage != 1)
		{
			iStart = (currentPage - 2) * resultsPerPage;
		}
		prevSearchURL = barSearchURL + "page" + iStart;
	}
	
	
	if (nextSearchURL == "")
	{
		var iStart = (currentPage -1) * resultsPerPage;
		nextSearchURL = barSearchURL + "page" + iStart;		
	}


	// This has the << link go back about 5 pages
	//aLinks.push("<a href='" + barSearchURL + "searchstart=" + (iBack-1) * resultsPerPage + "'>&lt;&lt;</a>");
	
	// This has the << link go back to the beginning
	aLinks.push("<a href='" + barSearchURL + "page0'>&lt;&lt;</a>");
	
	aLinks.push("<a href='" + prevSearchURL + "'>" + prevText + "</a>");
	
	if (maxPage <= 5)
	{
		// Build a page array of all the page links, display them all, no hiding of pages
		aLinks = aLinks.concat(getPageLinks(1, maxPage, currentPage, resultsPerPage, barSearchURL));
	}
	else
	{
		// Display 2 on either side of current if possible
		var iFromStart = currentPage;
		var iFromEnd = maxPage - currentPage;
		
		if (currentPage <= 3)
		{
			// We're at the beginning
			aLinks = aLinks.concat(getPageLinks(1, 5, currentPage, resultsPerPage, barSearchURL));
		}
		else if (currentPage > 3 && iFromStart <= (maxPage - 3))
		{
			// We're in the middle
			aLinks = aLinks.concat(getPageLinks(currentPage-2, currentPage+2, currentPage, resultsPerPage, barSearchURL));
		}
		else if (currentPage >= (maxPage - 3))
		{
			// We're at the end
			aLinks = aLinks.concat(getPageLinks(maxPage-4, maxPage, currentPage, resultsPerPage, barSearchURL));
		}

		
	}
	
	aLinks.push("<a href='" + nextSearchURL + "'>" + nextText + "</a>");
	// aLinks.push("<a href='" + barSearchURL + "searchstart=" + (iAhead-1) * resultsPerPage + "'>&gt;&gt;</a>");
	var iLastPage = (maxPage - 1) * resultsPerPage;
	aLinks.push("<a href='" + barSearchURL + "page" + iLastPage + "'>&gt;&gt;</a>");
	
	document.getElementById(sTargetID).innerHTML = aLinks.join('&nbsp;');
}

function reconstructCategoryURL (oldURL)
{
	var arrayURL = oldURL.split("/");
	var newURL = "";

	for (var i=0; i < arrayURL.length; i++)
	{
		if (arrayURL[i].indexOf("page") == -1)
		{
			newURL += arrayURL[i];
			if (i != (arrayURL.length - 1))
			{
				newURL += "/";
			}
		}	
	}
	
	if( i > 2 ) 
	{
		//newURL += "/";
	}

	return newURL;
}

function getCategoryPageLinks(iStart, iEnd, iCurrent, resultsPerPage, barSearchURL)
{
	var aLinks = new Array();
	var sLink = "";
	
	iStart = parseInt(iStart);
	iEnd = parseInt(iEnd);
	iCurrent = parseInt(iCurrent);
	
	for (var i=iStart; i <= iEnd; i++)
	{
		if (i == iCurrent)
		{
			sLink = "<span class='pagebar-current'>" + i + "</span>";
		}
		else
		{
			sLink = "<a href='" + barSearchURL + "page" + i + "'>" +
					i + "</a>";
		}
		
		aLinks.push(sLink);
	}
	
	return aLinks;
}


function betterCategoryPageBar(sTargetID, pageInfo, nextSearchURL, prevSearchURL, resultsPerPage)
{
	var N = 1; // Number of steps away before ellipsisizing 
	var sEllipsis = "";
	var aLinks = new Array();
	var barSearchURL;

	var prevText = "prev";
	var nextText = "next";

	var arrayPageInfo = pageInfo.split(" ");
	
	//var currentPage = Number(arrayPageInfo[0]);
	
	var currentPage = window.location.href.slice(window.location.href.indexOf('page'));
	currentPage = parseInt(currentPage.replace("page",''));
	
	if(isNaN(currentPage))
	{
		currentPage = Number(arrayPageInfo[0]);
	 }
	//alert(currentPage);
	
	var maxPage = Number(arrayPageInfo[2]);
	
	var iBack = (currentPage <=5) ? 1 : currentPage - 5;
	var iAhead = (currentPage >= maxPage - 5) ? maxPage : currentPage + 5;
	
	if ('0 of 0' == pageInfo || '' == pageInfo)
	{
		document.getElementById(sTargetID).innerHTML = '';
		return;
	}

	if (nextSearchURL != "")
	{
		barSearchURL = reconstructCategoryURL(nextSearchURL);
	}
	else if (prevSearchURL != "")
	{
		barSearchURL = reconstructCategoryURL(prevSearchURL);
	}
	else if (prevSearchURL == "" && nextSearchURL == "")
	{
		barSearchURL = reconstructCategoryURL(document.location.href);
	}	
	
	aLinks.push("<div id='pageinfo'>Page " + currentPage + " of " + maxPage + " </div>");

	if (maxPage <= 1)
	{
		// Only one page of results
		document.getElementById(sTargetID).innerHTML = aLinks.join('&nbsp;');
		return; 
	}

	if (prevSearchURL == "")
	{
		var iStart = 1;
		if (currentPage != 1)
		{
			iStart = (currentPage - 1);
		}
		prevSearchURL = barSearchURL + "page" + iStart;
	}
	
	
	if (nextSearchURL == "")
	{
		var iStart = (currentPage +1);
		nextSearchURL = barSearchURL + "page" + iStart;		
	}


	// This has the << link go back about 5 pages
	//aLinks.push("<a href='" + barSearchURL + "searchstart=" + (iBack-1) * resultsPerPage + "'>&lt;&lt;</a>");
	if(currentPage != 1)
	{
	// This has the << link go back to the beginning
		aLinks.push("<a href='" + barSearchURL + "page1'>&lt;&lt;</a>");
		aLinks.push("<a href='" + prevSearchURL + "'>" + prevText + "</a>");
	}
	
	if (maxPage <= 5)
	{
		// Build a page array of all the page links, display them all, no hiding of pages
		aLinks = aLinks.concat(getCategoryPageLinks(1, maxPage, currentPage, resultsPerPage, barSearchURL));
	}
	else
	{
		// Display 2 on either side of current if possible
		var iFromStart = currentPage;
		var iFromEnd = maxPage - currentPage;
		
		if (currentPage <= 3)
		{
			// We're at the beginning
			aLinks = aLinks.concat(getCategoryPageLinks(1, 5, currentPage, resultsPerPage, barSearchURL));
		}
		else if (currentPage > 3 && iFromStart <= (maxPage - 3))
		{
			// We're in the middle
			aLinks = aLinks.concat(getCategoryPageLinks(currentPage-2, currentPage+2, currentPage, resultsPerPage, barSearchURL));
		}
		else if (currentPage >= (maxPage - 3))
		{
			// We're at the end
			aLinks = aLinks.concat(getCategoryPageLinks(maxPage-4, maxPage, currentPage, resultsPerPage, barSearchURL));
		}

		
	}
	
	if(currentPage < maxPage)
	{
		aLinks.push("<a href='" + nextSearchURL + "'>" + nextText + "</a>");
	
	
	// aLinks.push("<a href='" + barSearchURL + "searchstart=" + (iAhead-1) * resultsPerPage + "'>&gt;&gt;</a>");
	var iLastPage = (maxPage);
	aLinks.push("<a href='" + barSearchURL + "page" + iLastPage + "'>&gt;&gt;</a>");
	}
	
	
	document.getElementById(sTargetID).innerHTML = aLinks.join('&nbsp;');
}

