// DOM id prefixes
var descriptionID = "group-list_";
var productLinkID = "REL_";

// stored id for current product - blank if none was selected
var currentProductID = "";
var allProductsVisible = false;
var path = window.location.href;
if (path.indexOf("#REL_") >=0) {
	currentProductID = path.substring((path.indexOf("#REL_")+5), path.length);
}

$(document).ready(function() {
	// event handler for 
	$("div.info-pop").hide().addClass("bubble");
	$("span.info-trigger").hover(
		function(e) {
			$("div#info-" + this.id).css({top:(e.pageY+10)+"px",left:(e.pageX-30)+"px"}).show();
		},
		function(e) {
			$("div#info-" + this.id).hide();
	});
						   
						   
	// hide all product descriptions and turn off tabs, unless URL has anchor, then use show that one.
	$("ul.group-list").hide();
	$("h4.group-title a").toggleClass("productLink_off");
	
	if (currentProductID) {
		$("ul#"+descriptionID+currentProductID).slideDown("fast");
		$("h4#"+productLinkID+currentProductID+" a").toggleClass("productLink_off");
	}
	
	// add event handler to all tabs.
	// handler hides current open tab (if there is one) and then shows clicked one.  Toggles tab if user clicks current tab.
	$("h4.group-title a").click(function() {
		if (allProductsVisible)
			hideAllProducts($("li#show_all")[0]);
		var productID = this.parentNode.id;
		productID = productID.substring(productID.indexOf("_")+1, productID.length);
		var productDescriptionNode = descriptionID + productID;
		$("ul#"+productDescriptionNode).slideDown("fast");
		
		// swap open tabs
		if (currentProductID) {
			$("ul#"+descriptionID+currentProductID).slideUp("fast");
		}
		
		
		// store which tab is current
		$("h4#"+productLinkID+productID+" a").toggleClass("productLink_off");
		if (currentProductID == productID) {currentProductID = ""; }
		else {
			$("h4#"+productLinkID+currentProductID+" a").toggleClass("productLink_off");
			currentProductID = productID;
			
			/* set location for bookmarking 
			if (path.indexOf("#REL_") >=0)
				location.href=path.substring(0,(path.indexOf("#REL_")+9))+productID;
			else
				location.href=path+"#REL_"+productID
			*/
		}
		
		return false;
	});
	
	// since print is javascript only - only show it with javascript
	$("ul#print").show();
	$("ul#show-all").show();
	
	// only show "show all products" if there are more than one
	var productDetails = $("dl.productDetail");
	if (productDetails.length > 1) $("li#show_all").css("display", "inline");
	
	// add event handlers
	$("li#print_page").click(function(){window.print();});
	$("li#show_all").click(function(){
									if (this.innerHTML.indexOf("Show") >= 0)
										showAllProducts(this);
									else
										hideAllProducts(this);
									});
});

function showAllProducts(linkObj) {
	$("ul.group-list").each(function() { $(this).show(); });
	$("h4.group-title a").each(function() { $(this).removeClass('productLink_off'); });;
	linkObj.innerHTML = linkObj.innerHTML.replace("Show","Hide");
	$(linkObj).toggleClass("contract");
	
	allProductsVisible = true;
}

function hideAllProducts(linkObj) {
	$("ul.group-list").each(function() { $(this).hide(); });
	$("h4.group-title a").each(function() { $(this).addClass('productLink_off'); });;
	linkObj.innerHTML = linkObj.innerHTML.replace("Hide","Show");	
	$(linkObj).toggleClass("contract");
	
	$("h4#"+productLinkID+currentProductID+" a").toggleClass("productLink_off");

	var productDescriptionNode = descriptionID + currentProductID;
	$("ul#"+productDescriptionNode).show();
	
	allProductsVisible = false;
}