window.addEvent('domready', function()	{
	$$('.make.button').makeButton();
	$$('.make.labelled').setLabel();
	$$('.make.tabs').makeTabs();
	$$('.change.into.tabs').changeIntoTabs();
	$$('.make.linked').makeAllClickable();
	$$('.make.expandable').makeExpandable();
	$$('.give.hover').giveHoverState();
	$$('INPUT[type=text], INPUT[type=password], TEXTAREA, SELECT').giveFocusClassOnSelect();
	//Customised versions of the above functions for eVision (Events)
	$$('.change.into.customtabs').customChangeIntoTabs();
	$$('.make.customExpandable').customMakeExpandable();
	
	//MyLibrary page - deleteItem
	$$('.deleteItemDirectory').addEvent('click', function(el) {
		var del = confirm('Are you sure you want to delete this item?');
		if (del == true) {
			//this.getParent().setStyle('border', '1px solid red');
			//return true;
			
			var req = new Request({
		        method: 'get',
		        url: this.href
		    }).send();
		    this.getParent().dispose();
		    
		}
		return false;
	});	
	
});

(function($) {

	$(function(){	
		$('a.thickbox').each(function()	{
			href = $(this).attr('href');
			$(this).attr('href', href.replace(/\?/, "?js=yes&"));
		});		
		
		mainInit();
	
		$('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
		// Modified - do NOT style Directory form fields with "noFocus" class (KPB 6/11/09)
		$('#columns :text, #columns :password, #columns textarea').each(function() {
			if (!$(this).hasClass('noFocus')) {
				$(this).css({backgroundColor:'#FFF', color:'#333'})
					.focus(function() {
							$(this).css({backgroundColor:'#FFFFB7', color:'#000', borderColor:'#000'}); }) 
					.blur(function() { 
						$(this).css({backgroundColor:'#FFF', color:'#333', borderColor:'#BAB9BF'});
				});
			}
		});
		/*$('#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:'#BAB9BF'});
		});*/
	
		// 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 .po_print').addClass('wvTip');
		$('.paging_results').append('<span class="print"><a href="javascript:window.print()">Print</a></span>');
	
		// 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});}
		});
		
		/* Show button when you click terms and conditions link */
		if ($('#AgreeDisagree').is(':checked')){
            		$('.myNewsletters .button').css({cursor:'pointer', opacity:'1'});
        	}
	
		// 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;});

        // 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;
				});
			});
		}

		//My library page - highlight correct tab depending on "?type=" in querystring		
		page = location.href;
		libraryPage = page.indexOf('?type=');
		if (libraryPage > 0) {
			var tab = (page.substr(libraryPage+6,1)).toUpperCase();
			switch(tab) {
				case 'T':
				  	$('.tab_block a:contains("Discussions")').click();
				  	break;
				case 'B':
				  	$('.tab_block a:contains("Blogs")').click();
				  	break;
				case 'D':
				  	$('.tab_block a:contains("Directory")').click();
				  	break;
			}
		}

		$('#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&"));
		});
	
		// Link form behaviour in footer promo
		$('#promo_container input.button').click(function() {
			var SiteRelocate = $('#siteName option:selected').attr('value');
	
			if(SiteRelocate != "") {
				window.parent.location.href = SiteRelocate;
			}
	
			return false;
		});
		function mainInit() {
			$('#mainnav').SetupMenu();
		}
		
		//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+3);
		
		//Center the seo navigation
		/*var footernav_ul_width = 0;
		$('#footer ul.seo_nav>li').each(function(){
			footernav_ul_width += $(this).outerWidth();
		});
		$('#footer ul.seo_nav').css('width',footernav_ul_width+3);*/
	
	});

})(jQuery);