function NewsSidebarTabs(nav_item){
	/* find higher */
	$('#top_stories').addClass('tabbed_items');
	var	top_stories_height = 100; /* min-height */
	$('#top_stories ol').each(function(){
		if ($(this).innerHeight()>top_stories_height) {
			top_stories_height = $(this).innerHeight();
		}
	});
	$('#top_stories').css('height',top_stories_height);
	$(nav_item).addClass('tabbed_items').find('ol').eq(1).hide().addClass('hidden_tab');
	$('.hidden_tab').prev('h4').click(function(){
		$(this).next('ol').show().removeClass('hidden_tab');
		$('.tabbed_items h4').removeClass('active');
		$(this).addClass('active');
		$('ol').hide().addClass('hidden_tab');
		$(this).next('ol').removeClass('hidden_tab').show();
	});
	$('ol').not('.hidden_tab').prev('h4').click(function(){
		$(this).next('ol').hide().addClass('hidden_tab');
		$('.tabbed_items h4').removeClass('active');
		$(this).addClass('active');
		$('ol').hide();
		$(this).next('ol').show();
	});
}

function newsSidebar(){
	$('.overflow').hide();
	$('.overflow').each(function(){
		$(this).parent().append('<a href="#" class="show_overflow">more</a>');
	});
	$('.show_overflow').click(function(){
		$(this).toggleClass('show_overflow_open').prev().slideToggle();
		if ($(this).hasClass('show_overflow_open')) {$(this).html('close')}
		else {$(this).html('more')}
		return false;
	})
}

function checkForm() {

	var self = this;
	var valid = false;
	
	var formFail = function(field, problem) {
		$('.'+field, self).addClass('invalid').parent().find('label .error_alert_icn').show();
		valid=false;
		return false;
	}
	
	function validate(field){
		$(field).removeClass('invalid');
		$(field).parent().find('label .error_alert_icn').hide();
		$(field).parent().find('.validation_message').hide();

	}
	
			if ($(".reviewer_name", this).val()=='' || $("#reviewer_name", this).val()==' ' || !emailPattern.test($(".email", this).val()) || $(".comment", this).val()=='' || $(".comment",this).val()==' ' || $(".comment", this).val().length < 20) {
		valid = false;
		}
		else {valid=true}

	
	if ($(".reviewer_name", this).val()==''||$("#reviewer_name", this).val()==' ') {
		formFail('reviewer_name','Please enter your name');
	}
	else {
		validate($(".reviewer_name", this));
	}
	
	if (!emailPattern.test($(".email", this).val())) {
		formFail('email', 'Please enter a valid email address');
	}
	else {
		validate($(".email", this));
	}
	
	if ($(".comment", this).val()=='' || $(".comment",this).val()==' ') {
		formFail('comment', 'Please enter a comment');
	}
	else if ($(".comment", this).val().length < 20) {
		formFail('comment', 'Please write a longer comment');
		$('.validation_message').show();
	}
	else {
		validate($(".comment", this));
	}
	
	if (valid) {
		$("#comment_form fieldset").hide();
		$("#comment_form_status").clone().insertAfter('#comment_form').removeClass('error_message').html('<img src="/_img/broadband/brands/loading.gif" alt="Loading..." title="Sending review" />').show();
		$("#comment_form_status").remove();
		
		$.ajax({
			type: "POST",
			url: "/_ajax/rating.php",
			data: $("#comment_form").serialize(),
			success: function(response){
				if (response=='success') {
					message = '<p>Your comment has been submitted for approval. Once we\'ve given it the green light (usually within 1 hour) we\'ll email you to let you know when it can be seen here.</p>';
					$('#comment_form_status').removeClass('error_message');
					$('#comment_form_section h3').html('<span class="green_tick">Thanks for getting involved!</span>');
					Cufon.replace('#comment_form_section h3');
				}
				else {
					message = '<p><strong>There\'s been a problem saving your comment.</strong></p><p>Our technical team have been informed so please try again later.</p>';	
					$('#comment_form_status').addClass('error_message');
				}
				$("#comment_form_status").html(message);
			}
		});
	}
	return false;
}

if (!$.browser.msie || $.browser.version > 6) {
	$(document).ready(function(){
	
		$("#social_networking a").hover(function() {
	
			$(this).after('<em></em>').removeAttr('title').next().html($(this).html()).append('<span></span>').fadeIn('fast');
			$('#social_networking').addClass($(this).attr('id'));
			}, function() {
				$(this).next('em').fadeOut('fast').remove();
				$('#social_networking').removeClass($(this).attr('id'));
		});
	
	});
}

$(document).ready(function() {
	NewsSidebarTabs($('#top_stories'));
	newsSidebar();
	
	$('#comment_form').submit(checkForm);


	$('.js_only').show();
	
	$('.scroll_here').click(function(){
		$(window).scrollTo('#comment_form_section', 250);
	});
	
	$('.text_content a.reply').click(function() {
		$('#fs_comments textarea#comment').focus().text('@'+$(this).parent().find('.comment_name').html()+' ');
	});
	
	$('.email_me .show_consent').click(function() {
		$('#consent div').slideToggle();
	});
	
	$('#latest_comments div').click(function() {
		window.location=$(this).find('.article_title a').attr('href');
	});
	
	// media gallery
	$('.media_gallery').createPopup({
		mode: 'gallery',
		title: ' Media Gallery'
	});
	
});