function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

var clusterPanelMoving = false;

$(".closeCluster").live("click",function(event)
{
	event.preventDefault();
	clusterPanelMoving = true;
	$(this).siblings(".inlineCluster").animate({left:0},500,function()
	{
		clusterPanelMoving = false;
		var normalLI = $(this).closest(".clusterDetails").parent();
		$(this).closest(".clusterDetails").remove();
		normalLI.find(".advanceLeft,.advanceRight,.leftShadow,.rightShadow,img.expandCluster").remove();
		normalLI.css({border:0,padding:"10px"});
		normalLI.find("*").show();
	});
});

$(".advanceLeft").live("click",function()
{
	if(!clusterPanelMoving)
	{
		
		var clusterUL = $(this).closest("li").find(".inlineCluster");
		var pos = clusterUL.position().left + 600;
		var currentNum = parseInt(clusterUL.parent().find(".count .current").text());
		currentNum--;
		if(pos<=0)
		{
			clusterPanelMoving = true;
			if(currentNum==1) $(this).addClass("disabled");
			$(this).siblings(".advanceRight").removeClass("disabled");
			clusterUL.animate({left: pos+"px"}, 1000,function()
			{
				clusterUL.parent().find(".count .current").text(currentNum);
				clusterPanelMoving=false;
			});
		}
	}
});

$(".advanceRight").live("click",function()
{
	if(!clusterPanelMoving)
	{
		var clusterUL = $(this).closest("li").find(".inlineCluster");
		var pos = clusterUL.position().left - 600;
		var currentNum = parseInt(clusterUL.parent().find(".count .current").text());
		currentNum++;
		if(pos>=(clusterUL.find("li").length-1)*-600)
		{
			if(currentNum==clusterUL.find("li").length) $(this).addClass("disabled");
			$(this).siblings(".advanceLeft").removeClass("disabled");
			clusterPanelMoving = true;
			clusterUL.animate({left: pos+"px"}, 1000,function()
			{
				clusterUL.parent().find(".count .current").text(currentNum);
				clusterPanelMoving=false;
			});
		}
	}
});

$("div.clusterDetails div.help a").live("click",function(event)
{
	event.preventDefault();
	clusterDetailsDiv = $(this).closest(".clusterDetails");
	clusterDetailsDiv.append("<div class='clusterHelp'>A Research Cluster is a collection of reports and other research within a common theme that spans multiple Research Services.  Clusters are an ideal way to augment the coverage of our traditional Research Services.  Browse all research in the Cluster using this tool.  <a href='/inforequest.jsp'>Email</a> or <a href='/contactus.jsp'>call</a> an ABI sales representative for pricing information.<a href='#' class='closeHelp'>X</a></div>");
	clusterDetailsDiv.find(".clusterHelp").animate({height:"78px",paddingTop:"20px",paddingBottom:"20px"});
});

$("div.clusterDetails div.clusterHelp a.closeHelp").live("click",function(event)
{
	event.preventDefault();
	$(this).parent().animate({height:0,paddingTop:0,paddingBottom:0},500,function(){$(this).remove();});
});

$(document).ready(function()
{
	$(".displayAdditionalProducts").click(function(e)
	{
		e.preventDefault();
		$(this).hide();
		$(this).next(".hideAdditionalProducts").show();
		$(this).next(".hideAdditionalProducts").data("numToShow", $(this).prev("ul").find("li:visible").length);
		$(this).prev("ul").find("li").show();
	});
	
	$(".hideAdditionalProducts").click(function(e)
	{
		e.preventDefault();
		$(this).hide();
		$(this).prev(".displayAdditionalProducts").show();
		var n = 0;
		var toShow = $(this).data("numToShow");
		$(this).prev().prev("ul").find("li").each(function()
		{
			if(n++ >= toShow)
				$(this).hide()
		});
	});
	
	$("#sectionTabs li a").click(function()
	{
		parent_li = $(this).parent();
		currentlySelected = "c"+$("li.active a").attr("id").substring(1);
		cID = "c"+$(this).attr("id").substring(1);
		if(!parent_li.hasClass("active"))
		{
			parent_li.siblings("li.active").removeClass("active");
			parent_li.addClass("active");
			$("#"+currentlySelected).slideUp(250,function(){$("#"+cID).slideDown(250);});
			setCookie("lastServiceTab"+$("#pvwrapper h1:first").text(),"s"+$("li.active a").attr("id").substring(1));
		}
	});
	
	$("a.productLink").click(function(event)
	{
		event.preventDefault();
		bookmark = $(this).attr("href");
		currentlySelected = "c"+$("li.active a").attr("id").substring(1);
		$("li.active").removeClass("active");
		$("#s1").parent().addClass("active");
		$("#"+currentlySelected).slideUp(250,function(){$("#c1").slideDown(250,function(){location.hash=bookmark;});});
	});
	
	$("#notifyMe").click(function(event)
	{
		event.preventDefault();
		var serviceID = $(this).children(".serviceID").text();
		$.ajax(
		{
			type: "POST",
			url: "/account/add_notification.jsp",
			data: "service="+encodeURIComponent(serviceID),
			success: function(message)
			{
				createMessage(message);
			}
		});
	});
	
	var lastServiceTab = getCookie("lastServiceTab"+$("#pvwrapper h1:first").text());
	if(lastServiceTab!="")
	{
		$("#"+lastServiceTab).click();
	}
	
	$("a.expandCluster").click(function(event)
	{
		event.preventDefault();
		var thisLI = $(this).closest("li");
		var clusterCode = $(this).find("img").attr("class");
		$(this).hide();
		$(this).after("<img class='expandCluster' src='/image/ui/circle-loader.gif' alt='Please Wait...' />");
		var currentHeight = thisLI.height();
		
		$.ajax(
		{
			type: "POST",
			url: "/cluster_inline.jsp",
			data: "code="+clusterCode+"&firstProduct="+thisLI.attr("id"),
			success: function(content)
			{
				thisLI.css({border:"1px solid #666",position:"relative"},1000);
				thisLI.animate({marginBottom:"10px",paddingTop:(119-currentHeight)/2+"px",paddingBottom:(119-currentHeight)/2+"px"},500,function()
				{
					thisLI.find("*").hide();
					thisLI.append(content);
					thisLI.css({padding:0});
					thisLI.find(".inlineCluster li").each(function()
					{
						var currentHeight = $(this).height();
						$(this).css({paddingTop:(119-currentHeight)/2+"px",paddingBottom:(119-currentHeight)/2+"px"});
					});
					var numItemsInCluster = thisLI.find(".inlineCluster li").length;
					thisLI.find(".inlineCluster").css({width:600*numItemsInCluster+"px"});
					thisLI.append('<div class="advanceLeft disabled"></div><div class="advanceRight"></div>');
					thisLI.append('<img alt="" src="/image/tech_invest/left_shadow.png" class="leftShadow">');
					thisLI.append('<img alt="" src="/image/tech_invest/right_shadow.png" class="rightShadow">');
					thisLI.find(".advanceLeft,.advanceRight").animate({width:"20px"},500);
					thisLI.find(".leftShadow,.rightShadow").fadeIn(500);
					thisLI.find("h2, .count, .help").fadeIn(500);
				});
			}
		});
	});
});


