var currentOpenPadding = 0;
var currentClosePadding = 0;
var linkOpeningTimer;
var linkClosingTimer;
var animatingOpenDiv;
var animatingCloseDiv;
var previousPage;
var submenuOpacity;
var animatingOpenDivInner;
var animatingCloseDivInner;
var doAnimation = false;

// finds an argument in a URL
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function animateOpeningSubmenu()
{
	submenuOpacity += 0.1;
	document.getElementById("leftLinkAreaContent").style.opacity = submenuOpacity;
	if (submenuOpacity < 1.0)
		setTimeout("animateOpeningSubmenu()", 50);
}

function showPhotosSubmenu()
{
	var theDiv = document.getElementById("leftLinkAreaContent");
	theDiv.style.opacity = 0;
	var gallery = gup("gallery");
	var submenu = "\
		<div style=\"height: 70px;\"> </div>\
		<div class=\"leftLink\" id=\"Fashion_Advertising_id\"><a href=\"photography.php?gallery=Fashion_Advertising\">fashion/advertising</a></div>\
		<div class=\"leftLink\" id=\"Portraits_id\"><a href=\"photography.php?gallery=Portraits\">portraits</a></div>\
		<div class=\"leftLink\" id=\"Products_id\"><a href=\"photography.php?gallery=Products\">products</a></div>\
		<div class=\"leftLink\" id=\"Personal_id\"><a href=\"photography.php?gallery=Personal\">personal</a></div>\
		<div class=\"leftLink\" id=\"Family_Album_id\"><a href=\"photography.php?gallery=Family_Album\">family album</a></div>";
	
	theDiv.innerHTML = submenu;
	document.getElementById(gallery+"_id").style.color = "#123456;";
	submenuOpacity = 0;
	animateOpeningSubmenu();
}

function showReelsSubmenu()
{
	var theDiv = document.getElementById("leftLinkAreaContent");
	theDiv.style.opacity = 0;
	var submenu = "\
		<div style=\"height: 50px;\"> </div>\
		<div class=\"leftLink\"><a href=\"reels.php?reel=timelapse\">time lapse</a></div>\
		<div class=\"leftLink\"><a href=\"reels.php?reel=commercials\">commercials</a></div>\
		<div class=\"leftLink\"><a href=\"reels.php?reel=documentary\">documentary</a></div>\
		<div class=\"leftLink\"><a href=\"reels.php?reel=sports\">sports</a></div>\
	";
	
	theDiv.innerHTML = submenu;
	submenuOpacity = 0;
	animateOpeningSubmenu();
}

function animateOpening()
{	
	currentOpenPadding += 5;
	animatingOpenDiv.style.margin = "0px "+ currentOpenPadding +"px 0px 0px";
				
	if (animatingOpenDivInner.offsetWidth + currentOpenPadding < 145)
	{
			linkOpeningTimer = setTimeout("animateOpening()", 5);
	}
	else
	{
		// done animating. Some areas have submenus
		if (currentPage == "photography")
			showPhotosSubmenu();
		if (currentPage == "reels")
			showReelsSubmenu();
	}
}

function animateClosing()
{
	if (currentClosePadding <= 5)
		return;
		
	currentClosePadding -= 5;
	animatingCloseDiv.style.margin = "0px "+ currentClosePadding +"px 0px 0px";
				
	if (currentClosePadding > 5)
		linkClosingTimer = setTimeout("animateClosing()", 5);
	else
		animatingCloseDiv.style.margin = "0px 0px 0px 0px";
}

function startAnimateClosing()
{
	var offset = animatingCloseDivInner.offsetWidth;
	if (offset > 0 && offset < 150)
	{
		currentClosePadding = 150 - offset; 
		animatingCloseDiv.style.margin = "0px "+ currentClosePadding + "px 0px 0px";
		setTimeout("animateClosing()",200);
	}
	else
	{
		setTimeout("startAnimateClosing()",10);
	}
}

function _openTopLevelLink() 
{	
	// move previous, if we have one
	previousPage = gup("previousPage");
	if (previousPage != "" && previousPage != currentPage && doAnimation)
	{
		animatingCloseDivInner = document.getElementById(previousPage+"Div");
		animatingCloseDiv = document.getElementById(previousPage+"DivOuter");
		startAnimateClosing();
		
		animatingOpenDiv = document.getElementById(currentPage+"DivOuter");
		animatingOpenDivInner = document.getElementById(currentPage + "Div");
		document.getElementById(currentPage+"Link").style.color = "#d09525";
		currentOpenPadding = 0;
		linkOpeningTimer = setTimeout("animateOpening()", 200);
	}
	else
	{
		// open without animating
		animatingOpenDiv = document.getElementById(currentPage+"DivOuter");
		animatingOpenDivInner = document.getElementById(currentPage + "Div");
		var offset = 150 - animatingOpenDivInner.offsetWidth;
		animatingOpenDiv.style.margin = "0px "+ offset + "px 0px 0px";
		document.getElementById(currentPage+"Link").style.color = "#d09525";
		
			if (currentPage == "photography")
				showPhotosSubmenu();
			if (currentPage == "reels")
				showReelsSubmenu();
	}
}

function openTopLevelLink() 
{
	if (doAnimation)
		setTimeout("_openTopLevelLink()",10);
	else
		_openTopLevelLink();
	
}

