(function($) {

	$(function(){
		if($('#mastad .ad').length<1) {
			$('#mastad').css('display','none');
			$('#wrapper_sleeve').css('background-position', 'center -100px');
		}
		if ($('#mainnav'))
			$('#mainnav').SetupMenu();
	
		$('div.active').makeTabs();
	
		// apply PNG fix to the following selectors
		if ($.browser.msie && $.browser.version<7) {
			$.ifixpng.pixel = '/magazine/graphics/pixel.gif';
			$('#logo img[@src$=.png]').ifixpng();
		}
	
		// focus states on form fields
		$('#columns :text, #columns :password, #columns textarea').css({backgroundColor:'#FFF', color:'#333'})
			.focus(function() {
				$(this).css({backgroundColor:'#FFFFB7', color:'#000', borderColor:'#000'}); })
			.blur(function() { 
				$(this).css({backgroundColor:'#FFF', color:'#333', borderColor:'#89B1D1'});
		});
	
		// write out print button for users with JS enabled
		$('.page_options li').removeClass('first');
		$('.page_options ul').prepend('<li class="first"><a href="javascript:window.print()" class="po_print">Print</a></li>');
		$('.page_options_min .po_print').addClass('wvTip').attr('rel','Print');
		$('.paging_results').append('<span class="print"><a href="javascript:window.print()">Print</a></span>');
		$('.pageOptions ul').append('<li class="last"><a class="poPrint" href="javascript:window.print()">Print this page</a></li>');
		
		//Change the Classifieds back button link to javascript back if javascript is present.
		$('a.poBack').attr('href', 'javascript: history.go(-1)');
		
		// disables buttons
		$('#register .termsandconditions input.button, .myNewsletters .button, #blogPost #comments_form .button').attr('disabled','disabled').css({cursor:'default', opacity:.5});
	
		// toggle button style depending on checkbox state
		$('.termsandconditions .formOptions :checkbox, .newsletterformSleeve .formOptions :checkbox, #story #comments_form .note :checkbox, .uploadAgreement :checkbox, #blogPost #comments_form :checkbox').click(function(){
			if(this.checked) {$('#register .termsandconditions input.button, .myNewsletters .button, #story #comments_form .button, .uploadAgreement .button, #blogPost #comments_form .button').attr('disabled','').css({cursor:'pointer', opacity:'1'});}
			else {$('#register .termsandconditions input.button, .myNewsletters .button, #story #comments_form .button, .uploadAgreement .button, #blogPost #comments_form .button').attr('disabled','disabled').css({cursor:'default', opacity:.5});}
		});
	
		// newsletters box, clear text
		$('#newsletter_block #news_email').focus(function() { if($(this).val() == "Enter email address") $(this).val(''); })
										  .blur(function() { if($(this).val() == "") $(this).val("Enter email address");});
		
		//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;});
	
		$('#story .relatedimages_block a.thickbox_image').each(function(){
			href = $(this).attr('href');
			// *************
			if(href.match('js=yes'))	alert('js=yes still hardcoded');
			// *************
			$(this).attr('href', href.replace(/\.aspx$/g, ".aspx?js=yes&"));
		});
		
		//==Advanced search==
		//=== show more or less ===
		var showMoreList = $('#searchResults .section_column2 .advancedSearch ul.filter');
		var showMoreList2 = $('#searchResults .section_column2 .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
		$('#searchResults .section_column2 .advancedSearch ul.filter_date li:gt(5)').wrapAll('<div class="hidden"></div>');
		$('#searchResults .section_column2 .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;
		});
		
		//********** PAGING CENTERING **********//
		$( ".paging_numbers" ).each(function(){
				var pager = $(this).children(":first") 
				var pagingWidth = pager.width() - 123;
				var calculatedWidth = 0;
				var pagingCnt = 0;
				$( $(pager).children('li') ).each(function(){
					calculatedWidth += $(this).width();
					pagingCnt +=1;
				});
				pagingCnt-=2;
				calculatedWidth -=123;
				calculatedWidth += pagingCnt*11; 
				var paddingTotal = ((pagingWidth - calculatedWidth)/2)-3;
				$($(pager).children('.first')).css('margin-right', paddingTotal+'px');
			});
		});

})(jQuery);


