/* Loaded in every page/entry/index */

var showhidemasthead = function showhidemasthead() {
	
	var mastheadheight, 
		masthead = $('#masthead'),
		frame = $('#frame');
	
	var starthidden = $('body').hasClass('content_page');
	
	var fitmasthead = function fitmasthead() {
		mastheadheight = masthead.outerHeight();
		if (frame.data('minimised')) {
			frame.css({'padding-top': mastheadheight, 'margin-top': -mastheadheight});
		}
		else {
			frame.css({'padding-top': mastheadheight});
		}
	};
	
	var moreorless = $('<div id="moreorless"><a href="#content">Less</a></div>');
	
	var foldup = function foldup(speed) {
		frame.animate({marginTop: -mastheadheight},speed,'swing',function(){
			$(this).data('minimised',true).addClass('folded');
			moreorless.trigger('switch',[true]);
		});
	};	
	
	var folddown = function folddown(speed) {
		frame.animate({marginTop:'0'},speed,'swing',function(){
			$(this).data('minimised',false).removeClass('folded');
			moreorless.trigger('switch',[false]);
		});
	}	
	
	var togglemasthead = function togglemasthead() {
		
		if($(this).is(':animated')) {
			return false;
		}

		if ($(this).data('minimised')) {
			folddown(250);
		}
		else {
			foldup(250);
		}
	}
	

	/* init */
	
	$(window).bind('resize',fitmasthead);
	$('#frame').bind( 'emchange', fitmasthead );
	

	frame.bind('togglemasthead',togglemasthead);
	$('#summary,#sitenav,#feed').delegate('a, input', 'focus', function() {
		if (frame.data('minimised')) {
			folddown(250);
		}
	});
	moreorless
		.bind('click',function(){
			$('#frame').trigger('togglemasthead');
			return false;
		}).bind('switch',function(event,flag) {
			if (flag) {
				$(this).find('a').text('More').attr('href','#masthead');
			}
			else {
				$(this).find('a').text('Less').attr('href','#content');
			}
		})
	.appendTo('#masthead');
	
	frame.data('minimised',starthidden);
	fitmasthead();
	if (starthidden) {
		foldup(0);
	}
	else {
		folddown(0);
	}
	
};



$(function(){
	if (!$('body').hasClass('home')) {
		showhidemasthead();
	}
	$('#searchform label').inFieldLabels();
});