window.addEvent('domready', function()	{
	var theDropdown;
	$$('.options_dropdown').addEvent('mouseover', function() {
		theDropdown = this;
		theDropdown.addClass('open');
	});
	$$('.options_dropdown div').addEvent('mouseout', function() {
		theDropdown.removeClass('open');
	});
	$$('.options_button').addEvent('click', function(e) {e.stop();});
});
(function($) {
	$(function(){
		
		//Append JS Querystring Param. Required in several places including making signin.aspx work as thickbox.
		$('a.thickbox').each(function()	{
			href = $(this).attr('href');
			$(this).attr('href', href.replace(/\?/, "?js=yes&"));
		});
		
		mainInit();

		$('div.active').makeTabs({
			//resize: 'biggest'
		});

		//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.hide_true').css({display:'none'});

			$('.filter_box').each(function() {
				$('.more_items a', this).click(function() {
					$(this).parents('.filter_box').children('ul').each(function(){
						$('li.hide_true, li.show_true', this).toggleClass('hide_true').toggleClass('show_true');
						$('li.hide_true').css({display:'none'});
						$('li.show_true').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 */
		$('#content div.formbox input[type=text], #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', borderColor:'#000'}); }).blur(function() { $(this).css({backgroundColor:'#FFF', color:'#666', borderColor:'#999'}); });
	
		//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>');

		/* button hover */
		//$('input.button,img.button').hover(buttonOver,buttonOut);
		$('p.closeme img').hover(closeButtonToggle,closeButtonToggle);

		$('.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, #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");});
		
	});

	function mainInit() {
		// Setup my account drop down
		$('#mastnav .myaccount a').bind('click', myAccountBoxOpenClose);
	}

	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');
		}
	}

	function closeButtonToggle() {
		($(this).is(':not(.over)')) ? $(this).attr('src', '/magazine/graphics/closehover.gif') : $(this).attr('src', '/magazine/graphics/close.gif');
		$(this).toggleClass('over');
	}
})(jQuery);
