$(function(){
	mainInit();

	$('div.active').makeTabs();

	//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;});

	// more link behaviour on search results page
	if($('body').is('#searchResults')) {
		$('li.hideme').css({display:'none'});

		$('.sub_section').each(function() {
			$('.more_items a', this).click(function() {
				$(this).parents('.sub_section').children('ul').each(function(){
					$('li.hideme, li.showme', this).toggleClass('hideme').toggleClass('showme');
					$('li.hideme').css({display:'none'});
					$('li.showme').css({display:'list-item'});
				});

				($(this).text() == "Show more ...") ? $(this).text('Show less ...') : $(this).text('Show more ...');
				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();
	}

	/* setup focus states on form fields */
	$('#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').focus(function() { $(this).css({backgroundColor:'#FFFFB7', color:'#000'}); }).blur(function() { $(this).css({backgroundColor:'#FFF', color:'#8C8C8C'}); });
	
	//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;
	});

	/* Clear on click function */
	//$('.clearme').one('focus', function() { $(this).val(''); });

	//stops inheriting the height for the auto height
	$('#rightcolumn .active.searches .active_block ul, .searches .filter ul').css('height','auto');
	
	/*==Advanced search==*/
	//stops inheriting the height for the auto height
	$('#rightcolumn .active.advancedSearch .active_block ul').css('height','auto');

	/*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
	$('#register .termsandconditions input.button, .myNewsletters .button, #story #comments_form .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});}
	});

	// Sniffs for ie6 and removes left border. IE6 won't support two class on one element
	$('#mainnav_sleeve li.first.current a').each(function() {
		if ($.browser.msie && $.browser.version<7) {
			$(this).css('borderLeft','0')
		}
	});
	
	$('#mp_directory_home #featured_brands').css('display','none');
	//tabs on directory home page
	$('.mp_companysearch .tab_block a').each(function(index){
		$(this).click(function(){
			$('#featured_blocks>div').css('display','none');
			$('#featured_blocks>div').eq(index).css('display','block');
		});
	});

	$('.ad_strip li:empty').remove();

});

function mainInit() {
	// Setup my account drop down
	$('#mastnav .myaccount a').bind('click', myAccountBoxOpenClose);
	$('#mainnav li.current').prev().addClass('prev');
}

function myAccountBoxOpenClose() {
	var myAccountBox = $('#myaccount_box');

	// Ensure top lines up if no / bigger ads
	var accountBoxTop = $('#mastnav .myaccount a').offset().top + $('#mastnav .myaccount a').height();
	myAccountBox.css('top', accountBoxTop + 11);

	// set/remove a class of open on myAccountBox and then fadeIn/Out and manipulate the body click event
	// so we can close the box if the user clicks outside of it to mimick common functionality.
	$(myAccountBox).toggleClass('open');

	if($(myAccountBox).is('.open')) {
		$('#mastnav .myaccount img').unbind('mouseout').unbind('mouseover').unbind('mouseleave').unbind('mouseenter');
		$(myAccountBox).fadeIn(400).find('.closeme img').bind('click', function() { 
			var imgSrc;

			myAccountBoxOpenClose(); 
			$('#mastnav .myaccount img').removeClass('over');

			if($.browser.msie && $.browser.version<7) $('#mastnav .myaccount img').ifixpng(); 
			return false; 
		});
	} else {
		$(myAccountBox).fadeOut(400).find('.closeme img').unbind('click');
	}

	return false;
}

function buttonOver() {
	if ($(this).is(':not(.over)')) {
		if ($.browser.msie && $.browser.version<7) $(this).iunfixpng();
		this.src.match(/_over\..../) ? null : this.src = this.src.replace(/\.(...)$/,'_over.$1');

		if ($.browser.msie && $.browser.version<7) $(this).ifixpng();
		$(this).addClass('over');
	}
}

function buttonOut() {
	if ($(this).is('.over')) {
		if ($.browser.msie && $.browser.version<7) $(this).iunfixpng();
		this.src = this.src.replace(/_over\.(...)$/,'.$1')

		if ($.browser.msie && $.browser.version<7) $(this).ifixpng();
		$(this).removeClass('over');
	}
}
