// Think Ambient
// Global Javascript functions
// Powered by Prego

$(function()
{
	// Set up news marquee
	$("#news")
		.mouseover(stopNewsMarquee)
		.mouseout(animateNewsMarquee)
		.addClass("js");
		
	animateNewsMarquee();
	
	// Set up menu hover behaviour
	$("#navigation > ul > li")
		.mouseover(function() 
		{
			var $t = $(this);
			
			if($t.hasClass("thinkstx"))
			{
				$t.addClass("thinkstx-hover");
				return;
			}
			
			if($t.hasClass("thinkcling"))
			{
				$t.addClass("thinkcling-hover");
				return;
			}
			
			if($t.hasClass("clingssr"))
			{
				$t.addClass("clingssr-hover");
				return;
			}
			
			if($t.hasClass("news"))
			{
				$t.addClass("news-hover");
				return;
			}
			
			$t.addClass("hover");
		})
		.mouseout(function() 
		{
			$(this)
				.removeClass("hover")
				.removeClass("thinkstx-hover")
				.removeClass("thinkcling-hover")
				.removeClass("clingssr-hover")
				.removeClass("news-hover");
		});
	
	// Set up show/hide forms on subscribe page
	var hash = window.location.hash.replace(/^#/, '');
	$(".form-inner:not(."+hash+")").each(function()
	{
		$("form", this).hide();
	
		$(this.parentNode).addClass("contracted");
	
		$(this).append(
			$("<div/>")
				.addClass("buttons")
				.append(
					$("<button/>")
						.addClass("expand")
						.append(
							"<span class=\"accessibility\">Click to expand form</span>"
						)
						.mousedown(function()
						{
							var $parent = $(this).parents(".form-inner");
							
							$parent.parent().removeClass("contracted");
							
							stopNewsMarquee();
							if(jQuery.browser.msie && jQuery.browser.version == 6)
								$("#copyright, a.top").hide();
							
							$("form", $parent).slideDown("slow", function()
							{
								animateNewsMarquee();
								$("#content").height();
								if(jQuery.browser.msie && jQuery.browser.version == 6)
									$("#copyright, a.top").show();
							});
							$("> .buttons", $parent).fadeOut();
							
							return false;
						})
				)
		);
	});
	
	$("#content").height();  // Required by MSIE6, makes it redraw the absolutely positioned elements correctly
});


function animateNewsMarquee()
{
	$("#news li:first")
		.each(function()
		{
			var target = $(this).innerWidth();
			var distance = target + parseInt(this.style.marginLeft ? this.style.marginLeft : 0);
			
			$(this).stop().animate({marginLeft:"-"+target+"px"}, distance * 40, "linear", function()
			{
				$(this)
					.appendTo("#news ul:first")
					.css("margin-left", "0")
					.show();
				
				animateNewsMarquee();
			});
		});
}

function stopNewsMarquee()
{
	$("#news li:first").stop();
}