var intCount = 0;
var arrCategories = new Array();
var strCategories = new String();
var strCurCategory = "";
var intCurPage = 0;
var intPageCount = 0;
var intRecordCount = 0;
var intPerPage = 10;
var objCurPageAnchor = "";
$(document).ready(function() {
	$(".product-container .categories").each(function() {
		var strTempCategories = $(this).html();
		var arrTempCategories = strTempCategories.split(",");
		for (var i=0; i<arrTempCategories.length;i++) {
			if (strCategories.indexOf(arrTempCategories[i]) == -1) {
				strCategories += (strCategories.length < 1) ? arrTempCategories[i] : "," + arrTempCategories[i];
				//alert(strCategories);
			}
		}
	});
	if (strCategories.length > 0) {
		arrCategories = strCategories.split(",");
		strTabNavigation = "<td class=\"active\"><a href=\"javascript:void(0);\">View All Products</a></td>";
		var arrCategoryOrdered = $("#ordered-tabs").html().split(",");
		
		for (i=0;i<arrCategoryOrdered.length;i++) {
			if (jQuery.inArray(arrCategoryOrdered[i],arrCategories)>-1) {
				strTabNavigation += "<td><a href=\"javascript:void(0);\">" + arrCategoryOrdered[i] + "</a></td>";
			}
		}
		$("#tab-instructions").show();
	} else {
		strTabNavigation = "";
		$(".product-tabs").hide();
	}
	$(".product-tabs tr").html(strTabNavigation);
	
	$(".product-tabs td").hover(function() {
		$(this).addClass("product-tabs-hover");
		}, 
		function() {
		$(this).removeClass("product-tabs-hover");
	});
	
	//set featured for All tab	
	strFeaturedCategory = "All";
	$(".product-container .featured-categories:contains('[" + strFeaturedCategory + "]')").parent().parent().hide().clone().addClass("featured").insertBefore(".product-container:first").show();
	
	$(".product-tabs a").click(function() {
		$(".product-tabs td").removeClass("active");
		//$("td").removeClass("active");
		$(this).parent().addClass("active");
		if ($(this).text() == "View All Products") {
			strCurCategory = "";
			//$("#subtabs").hide();
		} else {
			strCurCategory = $(this).text();
		}
		$(".product-container").hide();
		if ($("#divPager").length>0) {
			$(".product-container .categories:contains('" + strCurCategory + "')").parent().parent().slice(0,intPerPage).show();
			populatePager();
		}
		else 
		{
			$(".product-container .categories:contains('" + strCurCategory + "')").parent().parent().show();
		}
		
		//handle featured
		$(".product-container.featured").remove();
		strFeaturedCategory = (strCurCategory.length>0)?strCurCategory:"All";
		$(".product-container .featured-categories:contains('[" + strFeaturedCategory + "]')").parent().parent().hide().clone().addClass("featured").insertBefore(".product-container:first").show();		
	});
	
	//hide all
	$(".product-container").hide();
	
	//don't display pages on CA
	if ($("#divPager").length>0) {
		$(".product-container .categories:contains('" + strCurCategory + "')").parent().parent().slice(0,intPerPage).show();
		populatePager();
	} else {
		$(".product-container .categories:contains('" + strCurCategory + "')").parent().parent().show();
	}
	
	//author A-Z cleanup
	$(".product-container.noborder.topborder:first").removeClass("topborder");
	$(".author-az:empty").parents(".product-container").removeClass("topborder").addClass("author-multilist");
		
	function pagerSetPage(intPage) {
		if ((intPage <= intPageCount) && (intPage >= 1)) {
			$(".page-nav a.pager-link").removeClass("active");
			$(".page-nav a.pager-link[href*=location.href+'#showpage" + intPage + "']").addClass("active");
			intCurPage = intPage - 1;
			$(".product-container").hide();
			if ((intCurPage + 1) * intPerPage > intRecordCount) {
				strPagerContent = "<strong>" + ((intCurPage * intPerPage) + 1) + " to " + intRecordCount;
				$(".product-container .categories:contains('" + strCurCategory + "')").parent().parent().slice((intCurPage * intPerPage),intRecordCount).show();
			} else {
				strPagerContent = "<strong>" + ((intCurPage * intPerPage) + 1) + " to " + ((intCurPage + 1) * intPerPage);
				$(".product-container .categories:contains('" + strCurCategory + "')").parent().parent().slice((intCurPage * intPerPage),((intCurPage + 1) * intPerPage)).show();
			}
			strPagerContent += " of " + intRecordCount + "</strong>";
			$(".page-status").html(strPagerContent);
		}
	}
	function populatePager() {
		intCurPage = 0;
		intRecordCount = $(".product-container .categories:contains('" + strCurCategory + "')").length;
		intPageCount = Math.ceil(intRecordCount / intPerPage);
		if ((intCurPage + 1) * intPerPage > intRecordCount) {
			strPagerContent = "<strong>" + ((intCurPage * intPerPage) + 1) + " to " + intRecordCount + "</strong>";
		} else {
			strPagerContent = "<strong>" + ((intCurPage * intPerPage) + 1) + " to " + ((intCurPage + 1) * intPerPage);
		}
		strPagerContent += " of " + intRecordCount + "</strong>";
		$(".page-status").html(strPagerContent);
		strPageNav = "";
		if (intPageCount > 1) {
			strPageNav = "<a href=\""+location.href+"#showprevious\"><img src=\"/hai/images/pai/products/product-list-pager-left.gif\" width=\"10\" height=\"10\" alt=\"Previous\" /></a> ";		
			for (i=0;i<intPageCount;i++) {
				if (i==0) {
					strPageNav += "&nbsp;<a class=\"pager-link\" href=\""+location.href+"#showpage" + (i + 1) + "\">" + (i + 1) + "</a>&nbsp;";
				} else {
					strPageNav += "&nbsp;<a class=\"pager-link\" href=\""+location.href+"#showpage" + (i + 1) + "\">" + (i + 1) + "</a>&nbsp;";
				}
			}
			strPageNav += " <a href=\""+location.href+"#shownext\"><img src=\"/hai/images/pai/products/product-list-pager-right.gif\" width=\"10\" height=\"10\" alt=\"Next\" /></a>";
		}
		$(".page-nav").html(strPageNav);
		objCurPageAnchor = $(".page-nav a.pager-link[href*=location.href+'#showpage1']");
		objCurPageAnchor.addClass("active");		
		$(".page-nav a.pager-link").click(function() {
			pagerSetPage($(this).html());
		});
		$(".page-nav a[href*='#showprevious']").click(function() {
			pagerSetPage(intCurPage);
		});
		$(".page-nav a[href*='#shownext']").click(function() {
			pagerSetPage(intCurPage+2);
		});
	}
});