jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
        return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback).css('filter', '');
};

function fadeOut(href) {
	// stop link running so we can fade out page then run it
	$('h1').animate({foo: 1}, 200).fadeOut();
//	$('#site').animate({height: 660});
	$('.article, #home-article').animate({foo: 1}, 200).slideFadeToggle(200, 'linear', function() {
		window.location = href;
	});
	$('.largeimage').animate({foo: 1}, 200).fadeOut();

	// close menu if clicked page not in active menu
	// $('#nav ul ul.active').slideUp();

//	$('#brand-grad img').animate({left: 0}, 100);
}

function fadeIn() {
	$('h1').hide().animate({foo: 1}, 400).fadeIn();
	//$('.article, #home-intro, .standfirst').hide().animate({opacity: 1}, 600).slideFadeToggle();
	$('.article, #home-article').hide().animate({foo: 1}, 400).slideFadeToggle(200);

	$('.standfirst').hide().animate({foo: 1}, 180).show('slide', {direction: 'left'}, 200);


	$('.largeimage').hide().animate({foo: 1}, 200).fadeIn();

	//if ($.browser.msie && $.browser.version.substr(0,1)<7) {
	//} else {
		$('.brandgrad').animate({left: 0}, 1000).animate({left: 1000}, 6000);
	//}
	// make website height same as article height


	$('.active').parents('ul, li').addClass('active');
	$('#nav ul ul').not('.active').hide();

	$('#largeimage2').fadeOut(2000);
}

function formResponse(responseText, statusText)  {
	// email did not send
	if (responseText == 'false') {
		$('#formError').slideDown().animate({opacity: 1}, 8000).slideUp();
	}
	// email did send
	if (responseText == 'true') {
		$('#formSent').slideDown().animate({opacity: 1}, 8000).slideUp();
		// clear form
		$('#contact').clearForm();
	}

	// re-enable submit
	$('#contact .button').val('SEND').attr('disabled', '');
}

$(function() {

	$('#smallimage').cycle();
	$('#profile-image').hide();

	// place footer

	// work out distance from top
	/*if ($('.article').height()) {
		if ($('.article').height() < 230) {
			$('#footer').css('top', 480);
		} else {
			$('#footer').css('top', $('.article').height() + 185);
		}
		
	} else {
		$('#footer').css('top', 480);
	}

	// move it again if the window is tall
	if ($('#footer').css('top') < $(window).height()) {

	}*/

	$('html, body').height($(document).height());

	$(window).resize(function() {
		$('html, body').height($(document).height());
	});


	$('#footer').css('top', $(document).height() - 110);

	fadeIn();

	$('#nav a').click(function(event) {
		if (event.target.href) {
			fadeOut(event.target.href);
		}
		return false;
	});
/*
	$('.article a, #home-article a').click(function(event) {
		// make sure it's not an external link
		if (event.target.target != '_blank') {
			if (event.target.href) {
				fadeOut(event.target.href);
			}
			return false;
		}
	});
*/
	$('.profile strong').click(function(event) {
		image = $(this).parent('p').prev().attr('src').replace('-thumb', '');
		$('#largeimage').fadeOut();
		if ($('#profile-image img').attr('src') != image) {
			$('#profile-image').fadeOut('medium', function() {
				$(this).html('<img src='+image+' />').fadeIn();
			});
		}
	}).hover(function() {
		$(this).css('color', '#e1062b');
	}, function() {
		$(this).css('color', '#333333');
	});

	$('.profile img').click(function(event) {
		image = $(this).attr('src').replace('-thumb', '');
		$('#largeimage').fadeOut();
		if ($('#profile-image img').attr('src') != image) {
			$('#profile-image').fadeOut('medium', function() {
				$(this).html('<img src='+image+' />').fadeIn();
			});
		}
	}).hover(function() {
		$(this).fadeTo('fast', 0.5);
	}, function() {
		$(this).fadeTo('fast', 1);
	});

	$('#logo').click(function() {
		fadeOut('./');
		return false;
	});

	$('#nav ul ul').parent().click(function(event) {
		// close last menu
		$('#nav ul ul.active').slideUp();

		// remove all active ul
		$('#nav ul ul.active').removeClass('active');

		$(this).children('ul').slideToggle(400).addClass('active').click(function(event) {
			// stop the parent closing when the child is clicked
			// see if target is a link or not and only cancel non-links
			if (!event.target.href) {
				return false;
			} else {
				return true;
			}
		});
	});
});

$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};