$(document).ready(function() {
	
	var activeLink = $('#SectionNavigationLinks li.Active');
	var activeParents = activeLink.parents('#SectionNavigationLinks li');
	var activeChildren = activeLink.contents('#SectionNavigationLinks li');
	var activeSiblings = activeLink.siblings('#SectionNavigationLinks li');
	var activeParentUL = activeLink.parents('#SectionNavigationLinks ul');
	var activeChildUL = activeLink.children('#SectionNavigationLinks ul');

	activeParents.addClass('ActiveParent');
	activeChildren.addClass('ActiveChild');
	activeSiblings.addClass('ActiveSibling');
	activeParentUL.css('display','block');
	activeChildUL.css('display','block');
	
	var mainLinks = $('#SectionNavigationLinks li').not('li.Active').not('li.ActiveParent');
	
	mainLinks.hover(
		function(){
			$(this).addClass('MouseOver');
			var elm = $(this);
			setTimeout(function(){
				handleNavigation(elm,'expand');
			},500);
		},
		function(){
			$(this).removeClass('MouseOver');
			var elm = $(this);
			setTimeout(function(){
				handleNavigation(elm,'collapse');
			},500);
		}
	);

	var currentNavHeight = 0;

	$('#SectionNavigationLinks > li').each(function(i){
		curLink = $(this);
		currentNavHeight += curLink.height();
	});
	
	var maxExt = 0;
	
	$('#SectionNavigationLinks > li').not('li.Active').not('li.ActiveParent').each(function(i){
		curLink = $(this);
		var childUL = curLink.contents('ul');
		var childLinks = childUL.children();
		if (maxExt < childLinks.size())
		{
			maxExt = childLinks.size();
		}
	});
	
	var maxNavHeight = currentNavHeight + (maxExt*30);
	$('#SectionNavigationLinks').height(maxNavHeight);
	insertFlash();
});

$(window).load(
    function() {
		adjustLayout();
    }
);




function adjustLayout()
{
	
	//$('#Body').css('background','#FF99FF');
	//$('#LeftSideBar').css('background','#FFFF99');

	var calcHeight = ($('#ContentInternal').height() + $('#ContentFooter').height() + $('#ContactButtonsHolder').height() + 28)
	//alert('Body: ' + getBodyHeight() + '\nSidebar: ' + $('#SideBar').height() + '\nNav: ' + $('#SectionNavigationLinks').height() + '\nCalculated Body: ' + calcHeight);
	//$('#Body').height($('#SideBar').height());

	if (getBodyHeight() < $('#SideBar').height())
	{
		var lsbTop = $('#LeftSideBar').offset().top;
		var lsbBottom = $('#ContentFooter').offset().top;
		var lsbHeight = lsbBottom - lsbTop;
		var lsbDiff = $('#LeftSideBar').height() - $('#SectionNavigationLinks').height();
		var snlHeight = lsbHeight - lsbDiff;
		
		$('#SectionNavigationLinks').height(snlHeight);
		
		var diff = $('#SideBar').height() - getBodyHeight();
		var newNavHeight = $('#SectionNavigationLinks').height() + diff;
		$('#SectionNavigationLinks').height(newNavHeight);
		//$('#ContentFooter').width('725px');
	}
	else if (getBodyHeight() > $('#SideBar').height())
	{
		var diff1 = $('#SideBar').height() - $('#frmContact fieldset').height();
		var newContactHeight = getBodyHeight() - diff1;
		$('#frmContact fieldset').height(newContactHeight);
		
		var lsbTop = $('#LeftSideBar').offset().top;
		var lsbBottom = $('#ContentFooter').offset().top;
		var lsbHeight = lsbBottom - lsbTop;
		var lsbDiff = $('#LeftSideBar').height() - $('#SectionNavigationLinks').height();
		var snlHeight = lsbHeight - lsbDiff;
		
		$('#SectionNavigationLinks').height(snlHeight);
	}

	//var lsbTop = $('#LeftSideBar').offset().top;
	//var lsbBottom = lsbTop + $('#LeftSideBar').height();
	
	//var cfTop = $('#ContentFooter').offset().top;

	//var gap = cfTop - lsbBottom;

	//alert('Body: ' + getBodyHeight() + '\nSidebar: ' + $('#SideBar').height() + '\nNav: ' + $('#SectionNavigationLinks').height());

}

function getBodyHeight()
{
	var bodyTop = $('#Body').offset().top;
	var cbtnsTop = $('#ContactButtonsHolder').offset().top;
	var cbtnsBottom = cbtnsTop + $('#ContactButtonsHolder').height();
	return cbtnsBottom - bodyTop;
}

function insertFlash()
{
	$('#DvdLinkWrapper').flash({
		src: '/assets/flash/dvd.swf',
		width: 200,
		height: 180
	});
	
	$('#VideoLinkWrapper').flash({
		src: '/assets/flash/trial.swf',
		width: 200,
		height: 180
	});
	
	$('#VideoLinkWrapper a').remove();
	$('#DvdLinkWrapper a').remove();
}


function handleNavigation(elm,action)
{
	var otherLinks = elm.siblings('li');
	if (!otherLinks.contents('ul').is(':animated'))
	{
		switch (action)
		{
			case 'expand' :
				expandNavigation(elm);
				break;
			case 'collapse' :
				collapseNavigation(elm);
				break;
		}
	}
	else
	{
		setTimeout(function(){
			handleNavigation(elm,action);
		}, 50);
	}
}

function expandNavigation(elm)
{
	if (elm.hasClass('MouseOver'))
	{
		elm.children('ul').slideDown('fast');
	}
}

function collapseNavigation(elm)
{
	if (!elm.hasClass('MouseOver'))
	{
		elm.children('ul').slideUp('fast');
	}
}

