

// Create closed namespace for jQuery code.
(function($) {
	$(document).ready(function() {

		var $years = new Array();

		// Check to see if we have already expanded a year
		var $the_cookie = $.cookies.get('year');
		if (null != $the_cookie) {
			$selected_year = $the_cookie;
		} else {
			$selected_year = 0;
		}

		$('#news-archive ul').each(function(i){

			var $this = $(this);
			var $the_link = $this.parent().find('a:first');
			var $the_expander_id = "ae-" + i;
			var $the_expander = $.A({href:"#", id:$the_expander_id, title:"Expand this year"},"[+]");

			// Add this link to the page
			$this.before($the_expander);

			// Update display classes according to which year is currently expanded
			if(i == $selected_year) {
				$('#' + $the_expander_id).addClass('archive-expander hide');
			} else {
				$this.hide();
				$('#' + $the_expander_id).addClass('archive-expander')
			}

			$years.push($this);
		});


		// Add the onclick event to each year
		$('#news-archive a.archive-expander').click(function() {

			var $this = $(this);
			var $this_id = $this.attr("id");
			var $this_loop = $this_id.substring(3,$this_id.length);

			// Set a cookie so we can remember which year we have expanded
			$.cookies.set('year',$this_loop);

			for ($i=0; $i<$years.length; $i++)
			{
				if($i == $this_loop){
					$years[$i].show();
					$this.addClass('hide');
				} else {
					$years[$i].hide();
					$('#ae-'+$i).removeClass('hide');
				}
			}
			return false;
		});



		$('#news-subjects').each(function(i) {

			// Check to see if we have already opened the subject list
			var $subject_cookie = $.cookies.get('subject_list');
			if (null != $subject_cookie) {
				$list_status = $subject_cookie;
			} else {
				$list_status = "closed";
			}

			var $this = $(this);
			var $the_list = $this.find('ul:first');

			if($list_status == "closed") {
				var $the_control = $.P({id:"ns-0"},$.A({href:"#", title:"Expand subject list"},"Show subjects [+]"));
				$the_list.hide();
			} else {
				var $the_control = $.P({id:"ns-0"},$.A({href:"#", title:"Expand subject list"},"Hide subjects [-]"));
			}

			// Add this link to the page
			$the_list.after($the_control);
			$('#ns-0').addClass('alignright');
			$('#ns-0 a').addClass('archive-expander');

		});

		// Add the onclick event to subject control
		$('#ns-0 a').click(function() {
			var $this = $(this);
			if($this.html() == "Show subjects [+]") {
				$.cookies.set('subject_list','open');
				$('#news-subjects ul:first').show();
				$this.html("Hide subjects [-]");
			} else {
				$.cookies.set('subject_list','closed');
				$('#news-subjects ul:first').hide();
				$this.html("Show subjects [+]");
			}
			return false;
		});


		// Add bottom curved border to asides:
		$(".quote-right").append('<span class="shim"></span>');
		$(".editorial").append('<span class="main-shim"></span>');
		$("#three-cols").append('<span class="three-shim"></span>');

		$("#selector div:nth-child(2)").addClass('centre');
		$("#selector div:nth-child(5)").addClass('centre');
		$("#selector div:nth-child(8)").addClass('centre');

		$("#selector-two-cols div:nth-child(2)").addClass('right-side');
		$("#selector-two-cols div:nth-child(4)").addClass('right-side');
		$("#selector-two-cols div:nth-child(6)").addClass('right-side');

	});
})(jQuery);



