// MOOTOOLS
window.addEvent('domready', function() {
	$$('.change.into.tabs').customChangeIntoTabs();
});

// JQUERY
(function($) {
	$(function(){
	
		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
		$('#columns :text, #columns :password, #columns textarea').css({backgroundColor:'#FFF'})
			.focus(function() {
				$(this).css({backgroundColor:'#FFFFB7'}); })
			.blur(function() { 
				$(this).css({backgroundColor:'#FFF'});
		});
	
		// 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>');
	
		// disables buttons
		$('#register .termsandconditions input.button, #blogPost #comments_form .button, #FreeTrialConfirmForm .button').attr('disabled','disabled').css({cursor:'default', opacity:.5});
	
		// toggle button style depending on checkbox state
		$('.termsandconditions .formOptions :checkbox, #story #comments_form .note :checkbox, .uploadAgreement :checkbox, #blogPost #comments_form :checkbox, #FreeTrialConfirmForm :checkbox').click(function(){
			if(this.checked) {$('#register .termsandconditions input.button, #story #comments_form .button, .uploadAgreement .button, #blogPost #comments_form .button, #FreeTrialConfirmForm .button').attr('disabled','').css({cursor:'pointer', opacity:'1'});}
			else {$('#register .termsandconditions input.button, #story #comments_form .button, .uploadAgreement .button, #blogPost #comments_form .button, #FreeTrialConfirmForm .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&"));
		});
	
		// 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();
		}

		$('.filter .filter_block').each(function() {
			// check more than 4 items
			if($('li', this).length > 6) {
				// hide list elements > 6 items
				$('li:gt(3)', this).addClass('hide_true');
				$(this).append('<p class="show_more"><a href="#" title="Show more item above">Show more</a></p>');

				// bind event to more link
				$('.show_more a', this).bind("click", function() {
					if($(this).text() == "Show more") {
						$(this).text('Show less');
						$(this).parents('.filter_block').find('li').removeClass('hide_true');
						$(this).parent().removeClass('show_more').addClass('show_fewer');
					} else {
						$(this).text('Show more');
						$(this).parents('.filter_block').find('li:gt(3)').addClass('hide_true');
						$(this).parent().removeClass('show_fewer').addClass('show_more');
					}
					return false;
				});
			}
		});

		//$('.filter .filter_block p').previous('ul').css('border-bottom','0')
		
		// modify rendered page depending on whether a user is logged in (identified via META data)
		var userID = $('head meta[name="WT.seg_1"]').attr('content'); 		// logged in userID
		var userType = $('head meta[name="Status"]').attr('content'); 		// is user registered or subscriber?
		var startDate = $('head meta[name="StartDate"]').attr('content');	// start date if subscribed
		var qStr = '';
	
		if (userID) { // logged in
			$('#columns .userid_show').css('display', 'block');
	
			// function to append user META data to link's HREF
			$('#columns a.userid_link, #columns .userid_show a').click(function() {
				var newHref = $(this).attr('href');
	
				// subscriber & "staff users"
				if(startDate || userType.toUpperCase() === 'X') {
					qStr = userID + '&status=' + userType + '&startdate=' + startDate
				}
				// registered & "free trial users"
				else {
					if(userType.toUpperCase() === 'F') {
						userType = 'R'										// Change free trial userType to registered
					}
					qStr = userID + '&status=' + userType
				}
	
				// only execute if user has not previously clicked a link
				if(!$(this).data('userMeta')) {
					$(this).data('userMeta', 'stored');
	
					// does HREF already include a QueryString
					if (newHref.indexOf('?') > 0) { $(this).attr('href', newHref + '&i=' + qStr); } 
					else { $(this).attr('href', newHref + '?i=' + qStr); }
				}
			});
		}

	});


})(jQuery);
