var frameBorders = { 
	top: 0,
	left: 0,
	right: 0,
	bottom: 0
};

var libOptionsVisible = true;
var libOptionsCollapsed = false;

var libOptionsOriginalHeight; 

$(window).load(function(){
	setHeight(false);
	$('#lib-options-toggle').click(function(){
		toggleLibOptions();
		this.blur();
		return false;
	});
	$('a#toggle').click(function(){
		scriptToggleLibOptions();
		this.blur();
		return false;
	});
	libOptionsOriginalHeight = currentLibOptionsHeight();
})

$(window).resize(function(){
	setHeight(true);
	updateClientProperties();
});

// Calculate the needed margins for the ViewPoint area based on header and footer elements
function setHeight(refresh)
{
	// Remove unwanted table header and footer elements, there are only used for ie5.5 compatibility
	$('thead').remove();
	$('tfoot').remove();
	
	// Get the frame width from CSS
	//var framedArea = $('#planner-area').find('tr#table-center').find('td');
	var framedArea = $('#frame');
	if (refresh == false) {
		frameBorders.top = parseInt(framedArea.css('padding-top'));
		frameBorders.left = parseInt(framedArea.css('padding-left'));
		frameBorders.right = parseInt(framedArea.css('padding-right'));
		frameBorders.bottom = parseInt(framedArea.css('padding-bottom'));
		framedArea.css('padding',0);
	}
	
	var header = $('#header');
	var categories = $('#categories');
	var footer = $('#footer');
	var functions = $('#functions');
	var info = $('#info');
	var content = $('#ViewpointArea'); // We assume the flasharea will have exactly the same dimensions
	var library = $('#library');
	var libraryContents = $('#library-contents');
	var libraryOptions = $('#library-options');
	var floatingFrame = $('#planner-frame');

	// Some initial calculations: window height, frames, borders, etc
	var topHeight = header.height() + categories.height() + parseInt(categories.css('margin-top'));
	var bottomHeight = footer.height();
	var guiToolsHeight = functions.height() + info.height() + parseInt(info.css('margin-top')) + parseInt(info.css('margin-bottom'));
	var frameOffset = topHeight + bottomHeight;
	var windowHeight = $('body').height();

	// And calculations based on the above calculations
	var calculatedFrameHeight = windowHeight - frameOffset;
	var floatingFrameHeight = calculatedFrameHeight - frameBorders.top - frameBorders.bottom;
	var calculatedVmpHeight = floatingFrameHeight - guiToolsHeight;
	var libraryHeight = floatingFrameHeight - parseInt(library.css('margin-bottom')) - parseInt(library.css('padding-top'));
	var flashWidth = framedArea.width() - frameBorders.left - frameBorders.left - library.width();

	// Size and position the ViewPoint area correctly
	framedArea.height(calculatedFrameHeight);
	framedArea.css('top',topHeight);
	content.height(calculatedVmpHeight);
	$('#FlashArea').height(calculatedVmpHeight);
	$('#FlashArea').width(flashWidth);
	library.height(libraryHeight);
	
	var contentsHeight = library.height() - currentLibOptionsHeight();
	if ( libOptionsVisible )
	{ 
		libraryContents.height(contentsHeight);
	}
	else 
	{
		libraryContents.height(library.height());
	}
}

function currentLibOptionsHeight()
{
	var totalLOHeight = $('#library-options').height() + parseInt($('#library-options').css('margin-top')) + parseInt($('#library-options').css('margin-bottom')) + parseInt($('#library-options').css('padding-top')) + parseInt($('#library-options').css('padding-bottom')) + parseInt($('#library-options').css('border-top-width')) + parseInt($('#library-options').css('border-bottom-width'));
	return totalLOHeight;
}

function scriptToggleLibOptions()
{
	$('#library-options').toggle(100, function(){ setHeight(true); });
	libOptionsVisible = !libOptionsVisible
}

function showLibraryOptions()
{
	$('#library-options').show(100, function(){ setHeight(true); });
	libOptionsVisible = true;
}

function hideLibraryOptions()
{
	$('#library-options').hide(100, function(){ setHeight(true); });
	libOptionsVisible = false;
}

function toggleLibOptions()
{
	var hiddenHeight = $('a#lib-options-toggle').height() - (parseInt($('#library-options').css('margin-top')) + parseInt($('#library-options').css('margin-bottom')) + parseInt($('#library-options').css('padding-top')) + parseInt($('#library-options').css('padding-bottom')) + parseInt($('#library-options').css('border-top-width')) + parseInt($('#library-options').css('border-bottom-width')));
	var shownHeight = libOptionsOriginalHeight - (parseInt($('#library-options').css('margin-top')) + parseInt($('#library-options').css('margin-bottom')) + parseInt($('#library-options').css('padding-top')) + parseInt($('#library-options').css('padding-bottom')) + parseInt($('#library-options').css('border-top-width')) + parseInt($('#library-options').css('border-bottom-width')));
	
	if ( !libOptionsCollapsed )
	{
		$('#library-options').animate({ 
			height: hiddenHeight
		}, 400, null, function() { setHeight(true); });
	}
	else
	{
		$('#library-options').animate({ 
			height: shownHeight
		}, 400, null, function() { setHeight(true); });
	}
	
	libOptionsCollapsed = !libOptionsCollapsed
}

/*function adjustLibContentsHeight(difference) 
{
	alert(difference);
	$('#library-contents').animate({ 
		height: $('#library-contents').height() + difference
	}, 100 );
	setHeight(true);
}*/