﻿$(document).ready(function() {
	resizeSidebar();
});

function resizeSidebar() {
	$('#create_quote').css('height', '');
	$('#schedule_section').css('height', '');

	var docHeight = getMaxHeight();
	var sidebarHeight = $('#create_quote').height();
	var sidebarPos = $('#create_quote').position().top;
	var sidebarBottom = sidebarHeight + sidebarPos;
	var diff = 0;
	
	if (sidebarBottom < docHeight) {
		diff = docHeight - sidebarBottom;
		
		$('#create_quote').height(sidebarHeight + diff);
	}
	
	var schedulePos = $('#schedule_section').position().top + sidebarPos;
	
	var scheduleHeight = $('#schedule_section').height();
	var scheduleBottom = schedulePos + scheduleHeight;
	
	if (scheduleBottom < docHeight) {
		diff = docHeight - scheduleBottom;
		$('#schedule_section').height(scheduleHeight + diff);
		$('#schedule_section').css('overflow', 'hidden');
	}
}

function getMaxHeight() {
	var contentHeight = getContentHeight();
	var docHeight = getDocumentHeight();

	if (contentHeight > docHeight) {
		return contentHeight;
	} else {
		return docHeight;
	}
}

function getContentHeight() {
	if ($('#content_pane').length === 0) {
		return false;
	}
	
	var c_height = $('#content_pane').height();
	var c_pos = $('#content_pane').position().top;
	var content_height = c_height + c_pos;
	
	return content_height;
}

function getDocumentHeight() {
	var docHeight = $(document).height();
	var winHeight = $(window).height();

	if (winHeight > docHeight) {
		return winHeight;
	} else {
		return docHeight;
	}
}
