(function($) {
	$(function(){
		mainInit();
	
		$('div.active').makeTabs();

		//==Advanced search==
		//=== show more or less ===
		var showMoreList = $('#rightcolumn .advancedSearch ul.filter');
		var showMoreList2 = $('#rightcolumn .advancedSearch ul.filter_date');
		var showMoreText = $(showMoreList).siblings('p').children('a');
		var showMoreText2 = $(showMoreList2).siblings('p').children('a');

		//works out whether selected filter is inside the dropdown. If it is then open the dropdown on page load.
		$(showMoreList).siblings('p.opener').addClass('showmore').children('a').text('More sections');
		$(showMoreList).children('li').each(function(count){
			if($(this).hasClass('selected')){
				$(this).parent().siblings('p.opener');
				if(count>5)
					$(this).parent().siblings('p.opener').removeClass('showmore').addClass('showless').children('a').text('Fewer sections');
			}
		});
		$(showMoreList2).siblings('p.opener').addClass('showmore').children('a').text('More dates');
		$(showMoreList2).children('li').each(function(count){
			if($(this).hasClass('selected')){
				$(this).parent().siblings('p.opener');
				if(count>5)
					$(this).parent().siblings('p.opener').removeClass('showmore').addClass('showless').children('a').text('Fewer dates');
			}
		});

		//hides more than 6 results
		$('#rightcolumn .advancedSearch ul.filter_date li:gt(5)').wrapAll('<div class="hidden"></div>');
		$('#rightcolumn .advancedSearch ul.filter li:gt(5)').wrapAll('<div class="hidden"></div>');
		if ($(showMoreText).parent().hasClass('showmore')) { $(showMoreList).children('div.hidden').hide(); }
		if ($(showMoreText2).parent().hasClass('showmore')) { $(showMoreList2).children('div.hidden').hide(); }

		$(showMoreText).click(function(){
		//if there's a showmore class, hide the more than 6 results in a new div
			if ($(this).text('Fewer sections').parent().hasClass('showmore')) {$(this).parent().removeClass('showmore').addClass('showless').siblings('ul.filter').children('div.hidden').slideDown('fast');}
		//else remove class and allow hiding of extra content
			else if ($(this).text('More sections').parent().hasClass('showless')) {$(this).parent().removeClass('showless').addClass('showmore').siblings('ul.filter').children('div:visible').slideUp('fast');}
			return false;
		});
		$(showMoreText2).click(function(){
		//if there's a showmore class, hide the more than 6 results in a new div
			if ($(this).text('Fewer dates').parent().hasClass('showmore')) {$(this).parent().removeClass('showmore').addClass('showless').siblings('ul.filter_date').children('div.hidden').slideDown('fast');}
		//else remove class and allow hiding of extra content
			else if ($(this).text('More dates').parent().hasClass('showless')) {$(this).parent().removeClass('showless').addClass('showmore').siblings('ul.filter_date').children('div:visible').slideUp('fast');}
			return false;
		});

		// Apply PNG fix to the following selectors
		if ($.browser.msie && $.browser.version<7) {
			$.ifixpng.pixel = '/magazine/graphics/pixel.gif';
			//$('img[@src$=.png],input[@src$=.png]').not('.nofix').ifixpng();
			$('.bespoke_block .block_image').not('.nofix').ifixpng();
		}

		/* setup focus states on form fields */
		$('#addlinks, #addtags, #mp_direcory_home #content input[type=text], #mp_direcory_home #content select, #content div.formbox input[type=text], #content div.formbox input[type=password], #content div.formbox input[type=file], #content div.formbox textarea, #rightcolumn .formbox .text, #rightcolumn .formbox select, #inform fieldset.informcontent input[type=text], #inform fieldset.informcontent input[type=file], #inform fieldset.informcontent select, #inform fieldset.informcontent textarea, #emailsignup input[type=text], .informcontent table input').css({backgroundColor:'#FFF'}).focus(function() { $(this).css({backgroundColor:'#FFFFB7', color:'#000'}); }).blur(function() { $(this).css({backgroundColor:'#FFF'}); });
	
		//inform text input style
		$('.informcontent table input[type="text"]').css('border','1px solid #999');

		/* Page Tools */
		// Write out print button for users with JS enabled
		$('.page_options ul').prepend('<li><a href="javascript:window.print()" class="po_print">Print</a></li>');
		$('.paging_results').append('<span class="print"><a href="javascript:window.print()">Print</a></span>');

		$('.refine_search a.refine').click(function() {
			$(this).toggleClass('up');
			$('.refinesearch').toggleClass('hide');
			return false;
		});

		/*this hides the filter issues block*/
		$('.advancedSearch #filterByIssues').hide();
		$('.advancedSearch :input#filterDateRange').click(function(){
			if($(this).is(':checked')) {($(this).parents('.filterBy').siblings('#filterByDate').show().siblings('#filterByIssues').hide());}
		});

		$('.advancedSearch :input#filterIssues').click(function(){
			if($(this).is(':checked')) {($(this).parents('.filterBy').siblings('#filterByIssues').show().siblings('#filterByDate').hide());}
		});

		// Initially disable buttons
		var submitButton = $('#story #comments_form .button, #create_thread .addTags .button').attr('disabled','disabled').css({cursor:'default', opacity:.5});

		// Toggle button style depending on checkbox state
		$('#story #comments_form .note :checkbox, #create_thread .addTags .note :checkbox').click(function(){
			if(this.checked) {$(submitButton).attr('disabled','').css({cursor:'pointer', opacity:1});}
			else {$(submitButton).attr('disabled','disabled').css({cursor:'default', opacity:.5});}
		});

		//Center the navigation
		var footernav_ul_width = 0;
		$('#footernav .sleeve>ul>li').each(function(){
			footernav_ul_width += $(this).outerWidth();
		});
		$('#footernav .sleeve>ul').css('width',footernav_ul_width+1);

		// Custom blocks for The Lawyer at bottom of page
		var tallestBespokeBlock = 0;

		$('#bespoke_footer_blocks .bespoke_block').each(function() {
			var blockHeight = $(this).height();
			if(blockHeight > tallestBespokeBlock) 
				tallestBespokeBlock = blockHeight;
		});

		$('#bespoke_footer_blocks .bespoke_block').css('height', tallestBespokeBlock);
		$('input.button,img.button').hover(buttonOver,buttonOut);

		//feedback button
		$('<div class="feedback_close"><a href="#">Close form overlay</a></div>').appendTo('.feedback_box .inner_sleeve');
		$('.feedback_box .feedback_close').click(function(){ $(this).parents('.feedback_box').remove(); return false;});

		$('.tab_block li.first+li.last' && '.tab_block li.first+li.last a').css('width','110px');
	
		//Add the bg onto the body
		var b_w = $(document.body).width();
		$('#wrapper').css('width', b_w);
		$('#wrapper').css('background', '#FFF url(/magazine/graphics/site_bg.gif) repeat-y center top');
		$(window).resize(function(){
			$('#wrapper').css('width', $(document.body).width());
		});
	});

function buttonOver() {
	if ($(this).is(':not(.over)')) {
		this.src.match(/_over\..../) ? null : this.src = this.src.replace(/\.(...)$/,'_over.$1');
		$(this).addClass('over');
	}
}
function buttonOut() {
	if ($(this).is('.over')) {
		this.src = this.src.replace(/_over\.(...)$/,'.$1')
		$(this).removeClass('over');
	}
}
function mainInit() {
	$('#mainnav').SetupMenu();
}
})(jQuery);