﻿var timerlen = 5;
var slideAniLen = 250;
var moving = new Array();
var dir = new Array();
var timerID = new Array();
var startTime = new Array();
var obj = new Array();

// Used to expand and contract the height of a div
var endHeight = new Array();

// Used to expand and contract the widths of 2 divs
var endWidth = new Array();


function toggleSlide(objname) {
	if (document.getElementById(objname).style.z == "none") {
		// div is hidden, so let's slide down
		slidedown(objname);
	} else {
		// div is not hidden, so slide up
		slideup(objname);
	}
}

function slidedown(objname) {
	if (moving[objname])
		return;

	if (document.getElementById(objname).style.display != "none")
		return; // cannot slide down something that is already visible

	moving[objname] = true;
	dir[objname] = "down";
	startslide(objname);
}

function slideup(objname) {
	if (moving[objname])
		return;

	if (document.getElementById(objname).style.display == "none")
		return; // cannot slide up something that is already hidden

	moving[objname] = true;
	dir[objname] = "up";
	startslide(objname);
}

function startslide(objname) {
	obj[objname] = document.getElementById(objname);

	endHeight[objname] = parseInt(obj[objname].style.height);
	startTime[objname] = (new Date()).getTime();

	if (dir[objname] == "down") {
		obj[objname].style.height = "1px";
	}

	obj[objname].style.display = "block";

	timerID[objname] = setInterval('slidetick(\'' + objname + '\');', timerlen);
}

function slidetick(objname) {
	var elapsed = (new Date()).getTime() - startTime[objname];

	if (elapsed > slideAniLen)
		endSlide(objname)
	else {
		var d = Math.round(elapsed / slideAniLen * endHeight[objname]);
		if (dir[objname] == "up")
			d = endHeight[objname] - d;

		obj[objname].style.height = d + "px";
	}

	return;
}

function endSlide(objname) {
	clearInterval(timerID[objname]);

	if (dir[objname] == "up")
		obj[objname].style.display = "none";

	obj[objname].style.height = endHeight[objname] + "px";

	delete (moving[objname]);
	delete (timerID[objname]);
	delete (startTime[objname]);
	delete (endHeight[objname]);
	delete (obj[objname]);
	delete (dir[objname]);

	return;
}

function SlideRight(objNameLeft, objNameRight, objMapContainerName) {
	if (moving[objNameLeft])
		return;

	if (document.getElementById(objNameLeft).style.display != "none")
		return; // cannot slide down something that is already visible

	moving[objNameLeft] = true;
	dir[objNameLeft] = "right";
	StartSlideWidth(objNameLeft, objNameRight, objMapContainerName);
}

function SlideLeft(objNameLeft, objNameRight, objMapContainerName) {
	if (moving[objNameLeft])
		return;

	if (document.getElementById(objNameLeft).style.display == "none")
		return; // cannot slide up something that is already hidden

	moving[objNameLeft] = true;
	dir[objNameLeft] = "left";
	StartSlideWidth(objNameLeft, objNameRight, objMapContainerName);
}

function StartSlideWidth(objNameLeft, objNameRight, objMapContainerName) {
	obj[objNameLeft] = document.getElementById(objNameLeft);
	obj[objNameRight] = document.getElementById(objNameRight);

	endWidth[objNameLeft] = parseInt(obj[objNameLeft].style.width);
	endWidth[objNameRight] = parseInt(obj[objNameRight].style.width);

	startTime[objNameLeft] = (new Date()).getTime();

	if (dir[objNameLeft] == "right") {
		obj[objNameRight].style.width = "1px";
	}
	else {
		obj[objNameLeft].style.width = "1px";
	}

	obj[objNameLeft].style.display = "block";
	obj[objNameRight].style.display = "block";
	obj[objNameLeft].style.zIndex = 1;
	obj[objNameRight].style.zIndex = 1;
	timerID[objNameLeft] = setInterval("SlideTickWidth('" + objNameLeft + "','" + objNameRight + "','" + objMapContainerName + "');", timerlen);
}


function SlideTickWidth(objNameLeft, objNameRight, objMapContainerName) {
	var elapsed = (new Date()).getTime() - startTime[objNameLeft];

	if (elapsed > slideAniLen)
		EndSlideWidth(objNameLeft, objNameRight, objMapContainerName)
	else {
		var widthLeft = Math.round(elapsed / slideAniLen * endWidth[objNameLeft]);
		var widthRight = Math.round(elapsed / slideAniLen * endWidth[objNameRight]);

		if (dir[objNameLeft] == "left") {
			widthLeft = endWidth[objNameLeft] - widthLeft;
		}
		else {
			widthRight = endWidth[objNameRight] - widthRight;
		}

		obj[objNameLeft].style.width = widthLeft + "px";
		obj[objNameRight].style.width = widthRight + "px";
	}

	return;
}

function EndSlideWidth(objNameLeft, objNameRight, objMapContainerName) {
	clearInterval(timerID[objNameLeft]);

	if (dir[objNameLeft] == "left") {
		obj[objNameLeft].style.display = "none";
		obj[objNameLeft].style.zIndex = 100;
		obj[objNameRight].style.zIndex = 1;
		obj[objNameRight].style.borderLeft = '';
		obj[objNameLeft].style.borderRight = 'solid 1px Black';

		// Clear the div where the map is

		var mapContainer = document.getElementById(objMapContainerName), child;
		while (child = mapContainer.firstChild) {
			mapContainer.removeChild(child);
		}
		// Reinitialize the map

		googleMapObject = new GoogleMap.Map(mapContainer, 0, 0, 1, mapLoaded, true);
		googleMapObject._initialize();
		obj[objNameLeft].scrollTop = obj[objNameLeft].offsetTop + 100;
	}
	else {
		obj[objNameRight].style.display = "none";
		obj[objNameLeft].style.zIndex = 1;
		obj[objNameRight].style.zIndex = 100;
		obj[objNameRight].style.borderLeft = 'solid 1px Black';
		obj[objNameLeft].style.borderRight = '';
	}

	obj[objNameLeft].style.width = endWidth[objNameLeft] + "px";
	obj[objNameRight].style.width = endWidth[objNameRight] + "px";

	delete (moving[objNameLeft]);
	delete (timerID[objNameLeft]);
	delete (startTime[objNameLeft]);
	delete (endWidth[objNameLeft]);
	delete (endWidth[objNameRight]);
	delete (obj[objNameLeft]);
	delete (obj[objNameRight]);
	delete (dir[objNameLeft]);

	return;
}

function sleep(msecs) {
	var start = new Date().getTime();
	var cur = start
	while (cur - start < msecs) {
		cur = new Date().getTime();
	}
}

