// Timing variables

var effectDuration = 0.4; 						// How long does the Parallel effect take to complete; unit = seconds
var fadeStart = (effectDuration * 1000) - 100;	// When do we start fading in/out the expand collapse buttons; unit = milliseconds
var buttonFadeDuration = 0.6; 					// How long does the button fade last; unit = seconds
var buttonMoveDelay = fadeStart + 2;			// When do we start moving the buttons; unit = milliseconds (THIS MUST WAIT at least 2 milliseconds after the fade begins,
												//															 otherwise coordinates are assumed to be 0,0)

// Height & width variables
var content_width = 476;						// How wide is the content window; unit = pixels
var move_top_x = 36; 							// How far do we move the buttons horizontally; unit = pixels
var move_top_y = 29; 							// How far do we move the buttons vertically; unit = pixels
var move_left_x = 25; 							// How far do we move the buttons horizontally; unit = pixels
var move_left_y = 0; 							// How far do we move the buttons vertically; unit = pixels
var shift_content_up = 0; 						// How far do we move the content box UP when we collapse the LEFT menu; unit = pixels
var move_logo_x = 130; 							// How far do we move the logo off- or on-screen; unit = pixels
var left_menu_width = 175;						// How wide is the main menu; unit = pixels
var footer_margin_expanded = 0;					// What is the margin of the div containing the footer when the left menu is expanded
var footer_margin_collapsed = 95;				// What is the margin of the div containing the footer when the left menu is collapsed


// Do we need to squeeze every pixel of screen space?
var showSideboxes = false;


// Semaphore
var proceed = true;


// Opera being quite the dick about moves/slides
var needs_opera_fix = false;
needs_opera_fix = Prototype.Browser.Opera; // Eventually need to check versioning
if(needs_opera_fix) {
	left_menu_width -= 20;
	shift_content_up -= 20;
}




//-----------------------------------------------------------------------------//




function collapseAllMenus(fadeinPause) { 

	//Only called in Client Project page, which means these
	// values need to be readjusted
	left_menu_width = 258;						// How wide is the main menu; unit = pixels
	footer_margin_collapsed = 150;				// What is the margin of the div containing the footer when the left menu is collapsed
	showSideboxes = true;

	setTimeout("collapseLeftMenu()", (3.25 * fadeinPause));
	setTimeout("tryToCollapseTopMenu()", (4 * fadeinPause));


} // EOF




function tryToCollapseTopMenu() { 

	if(proceed) {
		collapseTopMenu();
	} else {
		setTimeout("tryToCollapseTopMenu()", 100);
	}


} // EOF




function collapseLeftMenu() { 


	if(proceed) {

		toggleProceedVar(); // Prevent interruptions
		new Effect.Shrink('left_menu', { direction: 'top-left', duration: effectDuration }); // Shrink the left menu


		swapCollapseExpandBttns(true, true); // collapsing, on the left

		setTimeout("new Effect.Move('content', { x: -" + left_menu_width + ", y: -" + shift_content_up + ", mode: 'relative' })", (.75 * buttonMoveDelay)); // Move the content box up and left
		setTimeout("$('content').morph('width: "+ (content_width + move_left_x + left_menu_width) +"px;')", buttonMoveDelay); // Stretch the content box to the left
		setTimeout("new Effect.Move('corner_logo', { x: -" + move_logo_x + ", y: 0, mode: 'relative' })", buttonMoveDelay); // Move the logo left
		setTimeout("$('footer_pusher').morph('margin-left: " + footer_margin_collapsed + "px;')", buttonMoveDelay); // Center the footer WRT the content box

		if(showSideboxes) {
			setTimeout("new Effect.Appear('client_links', { x: -" + move_logo_x + ", y: 0, mode: 'relative' })", buttonMoveDelay); // Fade in the "side" boxes
			setTimeout("new Effect.Appear('project_status', { x: -" + move_logo_x + ", y: 0, mode: 'relative' })", buttonMoveDelay);
		}


		// Make some of the content stretch too
		switch (loading_file) {
			case 'contact.php':
				setTimeout("$('map_canvas').morph('width: 650px;')", buttonMoveDelay);
			break;
			case 'directions.php':
				setTimeout("$('map_canvas').morph('width: 650px;')", buttonMoveDelay);
				setTimeout("$('route').morph('width: 650px;')", buttonMoveDelay);
			break;
		}

		setTimeout("toggleProceedVar()", (3.5 * buttonMoveDelay)); // We wait until after all is completed


	} // End if we could run this



} // EOF







function expandLeftMenu() { 

	if(proceed) {

		toggleProceedVar(); // Prevent interruptions
		var localButtonMoveDelay = 2 * buttonMoveDelay; // Going in the other direction is more delicate... we need more time


		var content_right = left_menu_width;
		var content_down = shift_content_up;
		if(needs_opera_fix) { // Again... being picky
			content_right += 40;
			content_down += 40;
		}


		new Effect.Move('content', { x: content_right, y: content_down, mode: 'relative' }); // Push content box back down
		setTimeout("$('content').morph('width: " + content_width + "px;')", 50); // Restrict content box width to original size

		if(showSideboxes) { // We're really hiding them now
			setTimeout("new Effect.Fade('client_links', { duration: .4 })", 50); // Drop in the menu
			setTimeout("new Effect.Fade('project_status',  { duration: .4 })", 50); // Push logo back to original place
		}


		// Put the stretched content back too
		switch (loading_file) {
			case 'contact.php':
				setTimeout("$('map_canvas').morph('width: 475px;')", buttonMoveDelay);
			break;
			case 'directions.php':
				setTimeout("$('map_canvas').morph('width: 475px;')", buttonMoveDelay);
				setTimeout("$('route').morph('width: 475px;')", buttonMoveDelay);
			break;
		}


		swapCollapseExpandBttns(false, true); // expanding, on the left

		setTimeout("new Effect.Grow('left_menu', { direction: 'top-left', duration: effectDuration })", localButtonMoveDelay); // Drop in the menu
		setTimeout("new Effect.Move('corner_logo', { x: " + move_logo_x + ", y: 0, mode: 'relative' })", localButtonMoveDelay); // Push logo back to original place
		setTimeout("$('footer_pusher').morph('margin-left: " + footer_margin_expanded + "px;')", localButtonMoveDelay); // Center the footer WRT the content box
		setTimeout("toggleProceedVar()", (2 * localButtonMoveDelay)); // We wait until after all is completed


	} // End if we could run this



} // EOF







function collapseTopMenu() {

	if(proceed) {

		toggleProceedVar(); // Prevent interruptions
		new Effect.Parallel([
			new Effect.BlindUp('top_menu', { sync: true }), 
			new Effect.Opacity('top_menu', { sync: true, from: 1, to: 0 })
		], { duration: effectDuration });


		var content_right = 0;
		var content_down = -(move_top_y + 21); // 21 accounts for space between menu and header text
		if(needs_opera_fix) { // Again... being picky
			content_right += 20;
			content_down = -move_top_y;
		}


		swapCollapseExpandBttns(true, false); // collapsing, on the right
		setTimeout("new Effect.Move('content', { x: " + content_right + ", y: " + content_down + ", mode: 'relative' })", buttonMoveDelay);
		setTimeout("toggleProceedVar()", (2 * buttonMoveDelay)); // We wait until after all is completed

	} // End if we could run this



} // EOF







function expandTopMenu() {

	if(proceed) {

		toggleProceedVar(); // Prevent interruptions
		new Effect.Parallel([
			new Effect.Opacity('top_menu', { sync: true, from: 0, to: 1 }),
			new Effect.BlindDown('top_menu', { sync: true }) 
		], { duration: effectDuration });


		var content_right = 0;
		var content_down = move_top_y + 21; // 21 accounts for space between menu and header text
		if(needs_opera_fix) { // Again... being picky
			content_right += 20;
			content_down += 20;
		}


		swapCollapseExpandBttns(false, false); // expanding, on the right
		setTimeout("new Effect.Move('content', { x: " + content_right + ", y: " + content_down + ", mode: 'relative' })", 10);
		setTimeout("toggleProceedVar()", (3 * buttonMoveDelay)); // We wait until after all is completed


	} // End if we could run this


} // EOF







function swapCollapseExpandBttns(collapsing, left) {

	var fade_bttn = 'collapse_left_button'; // Default is: Collapsing Left Menu
	var appear_bttn = 'expand_left_button';
	var move_x = -move_left_x;
	var move_y = move_left_y;


	// If we are collapsing

	if(left) {

		if(!collapsing) { // Expanding the left menu
			fade_bttn = 'expand_left_button'; // Fade this out
			appear_bttn = 'collapse_left_button'; // Fade this in
			move_x = move_left_x; // Pushing back down
		} // End if we are expanding


	} else { // TOP

		move_x = move_top_x;
		move_y = move_top_y;

		if(collapsing) { // Collapsing the top menu
			fade_bttn = 'collapse_top_button'; // Fade this out
			appear_bttn = 'expand_top_button'; // Fade this in
			move_y = -move_top_y; // Move the top buttons up

		} else {  // Expanding the top menu
			fade_bttn = 'expand_top_button'; // Fade this out
			appear_bttn = 'collapse_top_button'; // Fade this in
			move_x = -move_top_x; // Move the top buttons to the left
		} // End if we are collpasing or not


	} // End if left or top menu



	// Fade the appropriate button in or out
	setTimeout("new Effect.Fade('" + fade_bttn + "', { duration: " + buttonFadeDuration + " })", fadeStart);
	setTimeout("new Effect.Appear('" + appear_bttn + "', { duration: " + buttonFadeDuration + " })", fadeStart);

	// Move them
	setTimeout("new Effect.Move('" + fade_bttn + "', { x: " + move_x + ", y: " + move_y + ", mode: 'relative' })", buttonMoveDelay);
	setTimeout("new Effect.Move('" + appear_bttn + "', { x: " + move_x + ", y: " + move_y + ", mode: 'relative' })", buttonMoveDelay);



} // EOF







function toggleProceedVar() {

	proceed = !proceed;


} // EOF