var hash = location.hash;

$(function () {
	$("param[name=movie][value^=http://www.youtube.com]").parents('object').hide().each (function () {
		$(this).wrap('<div class="movie_hider" style="position:relative;width:470px;height:350px" />');
	});
	$("embed[src^=http://video.google.com]").hide().each(function () {
		$(this).wrap('<div class="movie_hider" style="position:relative;width:470px;height:379px" />');
	});
	$(".movie_hider").append ('<div style="background-color: #000; cursor: pointer;background-image: url(/_assets/images/common/video-intro-frame.gif); background-repeat: no-repeat; width: 470px; height: 350px; color: blue; position: absolute; top: 0; left: 0;" class="movie_shower"></div>');
	$(".movie_shower").click(function () {
		$(this).hide();
		$(this).siblings("object,embed").show();
	});
	$("#about-video .movie_shower").css("background-image", "url(/_assets/images/common/SWF-since-2004.gif)");
});

$(function() {
	$("#about-video")
		.css("position", "relative")
		.append('<div class="clicker" style="width: '+$(this).width()+'; height: '+$(this).height()+'; position: absolute; top: 0; left: 0;" />')
		.find("embed").hide()
		.end().find(".clicker").click (function () {
			$(this).hide();
			$(this).parent().find("embed").show();
		});
});

$(function () {
	$("#container").removeClass("no-js").addClass("with-js");
	if (document.getElementById("home"))
	{
		$("#content li").clickableBlocks ();
	}
	if ($(".three_col .half_col").length)
	{
		$(".three_col .half_col").clickableBlocks ();
	}
	if ($(".tactical-box").length)
	{
		$(".tactical-box").clickableBlocks ();
	}
	if ($(".question + .answer").length)
	{
		collapseAnswers();
	}
	if ($(".search_result").length)
	{
		$(".search_result").clickableBlocks();
	}
	if ($(".individual-speaker").length)
	{
		$(".individual-speaker").clickableBlocks();
	}
	setupSifrLinks ();
});

function setupSifrLinks ()
{
	$('.sIFR-replaced').click(function(event) {
		if($(this).children('span').children('a').length)
		{
			var url = $(this).children('span').children('a').attr('href');
			document.location.href = url;
		}
	});
}

function collapseAnswers ()
{
	$(".question + .answer").slideUp(1);
	$(".question").css ("cursor", "pointer").click (function () {
		var $answer = $(this).next();
		if ($answer.is(":visible"))
		{
			$answer.stop().slideUp();
		}
		else
		{
			$(".answer").stop().slideUp();
			$answer.stop().slideDown();
		}
	}).hover(function () {
		$(this).css("color", "#FF7E12");
	}, function () {
		$(this).css("color", "#333");
	});
	if (hash && $(".question" + hash).length)
	{
		$(hash).next().slideDown(1);
//		$.scrollTo(hash, 1000);
		$(hash).next().andSelf().effect("highlight", {}, 3000);
	}
}

// stop the nasty question/answer 'jumping' bug
$("head").append('<style type="text/css">.answer{display: none;}</style>');

/***************************************************************************************************
 * CM: Cool little utility plugin to turn any element wrapping an anchor into a clickable block.
 *     It also adds in the CSS cursor:pointer to let the user know that they can click the entire
 *     item. 
 *
 *     EXAMPLE HTML
 *     ------------
 *
 *     <div class="clickable_block">
 *       <a href="#">Anchor</a>
 *       <p>Clicking this will also work</p>
 *     </div>
 *
 *     EXAMPLE jQUERY USAGE
 *     --------------------
 *     $(".clickable_block").clickableBlocks();
 **************************************************************************************************/
$.fn.clickableBlocks = function ()
{
	return this.each (function () {
		var $this = $(this);
		var $anchor = $this.find("a:nth-of-type(1)");
		if ($anchor.length)
		{
			$this.css("cursor", "pointer").click (function ()
			{
				location.href = $anchor[0].href;
			});
		}
	});
}