// Create closed namespace for jQuery code.
(function($) {

	$(document).ready(function() {

		$("#nav_sub li:first").addClass("first");
		$("#nav_sub").after("<div id=\"sub-nav-shadow\"></div>");
		$(".box-flat").after("<div class=\"box-shadow\"></div>");
		$("#add-close-button").append("<ul><li class=\"no-border\"><a href=\"#\" onclick=\"window.close();\">Close [x]</a></li></ul>");
		$("#contact-email").after("<li class=\"print\"><a href=\"#\" onclick=\"window.print();\">Print</a></li>");
		$("#page-email").after("<li class=\"print\"><a href=\"#\" onclick=\"window.print();\">Print</a></li>");


		var $tabs = new Array();
		$('.tabs li a').each(function(i)
		{
			var $this = $(this);
			$tabs.push($this);
		});


		$('.tabs li a').click(function(i)
		{
			var $this = $(this);
			var $this_target = $this.attr('href');
			$.each($tabs,function(i,obj)
			{
				$obj_target = obj.attr('href');
				if ($obj_target == $this_target)
				{
					$($this_target).show();
					$this.parent().addClass('selected')
				}
				else
				{
					$($obj_target).hide();
					obj.parent().removeClass('selected')
				}
			});
			return false;
		});


		$('#special-message').each(function(i)
		{
			var $this = $(this);
			var $kids = $this.children();
			var $message = jQuery.trim($kids.html());
			$message = $message.replace("\n","");
			$message = $message.replace("\r","");
			if($message == "Your email message has been sent.")
			{
				$('#special-link').html("<a href=\"#\" onclick=\"window.close();\">Close [x]</a>");
			}
		});


		// Test for 'object' element inside 'media' elements
		$('.media object').each(function(i)
		{
			var $this = $(this);
			var $objWidth = $this.attr('width');
			var $objHeight = $this.attr('height');
			if($objWidth > 428)
			{
				$multiplier = 428 / $objWidth;
				$newHeight = parseInt($objHeight * $multiplier);
				$this.attr('width', '428');
				$this.attr('height',$newHeight);
			}
		});

		// Test for 'embed' element inside 'media' elements
		$('.media embed').each(function(i)
		{
			var $this = $(this);
			var $objWidth = $this.attr('width');
			var $objHeight = $this.attr('height');
			if($objWidth > 428)
			{
				$multiplier = 428 / $objWidth;
				$newHeight = parseInt($objHeight * $multiplier);
				$this.attr('width', '428');
				$this.attr('height',$newHeight);
			}
		});

		// Help window show on reading plan form
		$('.help_window').hide();

		$('.help_icon').mouseover(function()
		{
			// Close all open help blocks
			$('.help_icon').each(function()
			{
				$(this).removeClass('open');
				$(this).parent().children('.help_window').hide();
			});

			$(this).addClass('open');
			$(this).parent().children('.help_window').show();
		});

		$('.form_help h3 a').click(function()
		{
			$(this).parents('.form_help').children('.help_window').hide();
			$(this).parents('.form_help').children('.help_icon').removeClass('open');
			return false;
		});


		//$('.help_icon').mouseout(function() {
		//	var parent = $(this).parent();
		//	parent.children('.help_window').hide();
		//});


		// Form row highlighting for mouseover
		$('.form_row').mouseover(function() {
			$(this).addClass('active');
		}).mouseout(function() {
			$(this).removeClass('active');
		});


		// Form row highlighting for focus
		$('.form_row :input').focus(function() {
			var parent = $(this).parents('.form_row');
			parent.addClass('active');
		}).blur(function() {
			var parent = $(this).parents('.form_row');
			parent.removeClass('active');
		});


		$('p.message').delay(5000, function(){$('p.message').fadeOut()})


	});

})(jQuery);


( function popup() {
	$( document ).ready( function() {
		$("a[rel='pop-up']").click(function () {
      		    var features = "height=600,width=450,scrollTo,resizable=1,scrollbars=1,location=0";
      		    newwindow=window.open(this.href, 'Popup', features);
      		    return false;
 		});
	} );
}() );


jQuery.fn.delay = function(time,func){
    return this.each(function(){
        setTimeout(func,time);
    });
};