// JavaScript Document
<!--
//can only do 1? opening at a time?????

var store;
var call;

//we need to factor in the current visable portion
var top_part = 50;
//and also the bottom when calculating offsets
var bott_part = 46;

//how many updates too do opening and closing
var number_of_updates = 15;
//how long in millisec this will take
var time_of_update = 400;
var updates_millsec = time_of_update / number_of_updates;
//this is the 'gravity' - could be power of 2
var g_start = Math.pow(number_of_updates,3);

//set up arrays to store the updates in pixels
// will be updates[0][0 - 16].....updates[1][0 - 16]..... etc
var updates = new Object();

//set up array which will store the current position in the updates array
var counter = new Object();

//stores if menu is opening
var opening = new Object();

//stores if menu is closing
var closing = new Object();


var menu_ids = Array(
	'surreyBUSINESS',
	'surreyLIVING',
	'recentPOSTS',
	'surreyCOMMUNITY',
	'surreyKIDS',
	'map_key_holder'
);

open_on_load[open_on_load.length] = 5;

//how many dropdown menus their are
var total_menus = menu_ids.length;

var loaded = false;

function is_firefox() {
	if(navigator.userAgent && navigator.userAgent.match(/Firefox/) && navigator.platform && navigator.platform.match(/Win/)) {
		return true;
	}
	return false;
}

function loadup() {

	// check if not already called, will be called via other pages
	if(!loaded) {
	
		for (i=0;i<total_menus;i++){
			updates[i] = new Object();	
			counter[i] = 0;	
			opening[i] = 0;
			closing[i] = 1;
			//get the height of the content	too show
			theid = menu_ids[i] + '_container';
			
			pxheight = 0;
			if(document.getElementById(theid)) {
				theElement = document.getElementById(theid);
				pxheight = theElement.offsetHeight;
				pxheight += bott_part;
			} else {
				pxheight = (location.href.match(/iframe\.php/))? 378 :  329;
			}
			
			
			
		
			var g = (pxheight / g_start);
			
			for (k=0;k<number_of_updates;k++){
				//load up our updates array
				updates[i][k] = (Math.pow(k,3)) * g;
			}
			
			//make sure start and end are correct
			updates[i][number_of_updates-1] = pxheight;
			updates[i][0] = 0;
		}
	   //fake a roll-over to open first menu
		loaded = true;
		if(!arguments[0]) {
			for(o=0; o<open_on_load.length; o++) {
			//	t = ((time_of_update + 10) * o);
			//	setTimeout("set_open("+ open_on_load[o] +");", t);
				closing[open_on_load[o]] = 0;
			}
		}
	}
}


function set_open(i) {
	// i contains 0,1 etc
	if(!loaded) {
		loadup(true);
		closing[i] = 0;
	}
	if(loaded) {
		if("undefined" != typeof call && call != -1) {
			// Wait for previous to finish
			setTimeout("set_open("+ i +");", 100);
			return;
		}
		//if opening is all ready running
		if ( !opening[i] ){
			// Set Cookies
			document.cookie = 'menu['+i+']=open;path=/'; 

			opening[i] = 1;
			//reset this counter
			counter[i] = 0;
			//store outside this function
			store = i
			// turn off the open arrow
			theid = "ma_o"+i;
			if(document.getElementById(theid)) {
				theElement = document.getElementById(theid);
				theElement.style.display = "none";
			}
			// fill in the background of xtra top bit
			theid = "mx_"+i;
			if(document.getElementById(theid)) {
				theElement = document.getElementById(theid);
				theElement.style.display = "none";
			} else {
				if(is_firefox()) {
					$('map_key_icons').style.overflow = 'visible';
					$('map_key_icons').style.paddingRight = '17px';
				}
			}
			//set off our repeat function to open menu
				setTimeout("control_open(store, true);",2);
		}
	}
}



function set_close(i) {
	// i contains 0,1 etc
	if(!loaded) {
		loadup(true);
		closing[i] = 0;
	}
	if(loaded) {
		//if opening is all ready running
		if ( !closing[i]) {
			// Set Cookie
				document.cookie = 'menu['+i+']=closed;path=/'; 
		//	closing[i] = 1;
			//reset this counter
			counter[i] = number_of_updates-1;
			//store outside this function
			store = i
			
			//turn off the close arrow
			theid = "ma_c"+i;
			if(document.getElementById(theid)) {
				theElement = document.getElementById(theid);
				theElement.style.display = "none";
			} else {
				if(is_firefox()) {
					$('map_key_icons').style.overflow = 'visible';
					$('map_key_icons').style.paddingRight = '17px';
				}
			}

			//set off our repeat function to open menu
			setTimeout("control_close(store, true);",2);
		}
	}
}   

function control_open(i) {
	// i contains 0,1 etc	
	
	//set new height of wrapper div
	theid =  menu_ids[i];
	
  theElement = document.getElementById(theid);
	var count = counter[i]++;
	
	var update = updates[i][count]
	
	// set height
	orig_height = theElement.style.height.replace(/px$/i, '');
	theElement.style.height = ((theid == "map_key_holder")? 34 : top_part)+update+"px";

	if(theid == "map_key_holder") {
		var increase = (34 + update) - orig_height;
		orig_margin = (theElement.style.marginTop.replace(/px$/i, '') * 1);
//		theElement.style.marginTop = (orig_margin - increase) + "px";
	}

	//see if updates have finished
	if (counter[i] < number_of_updates) {
		//store outside this function  
		call = i
		setTimeout("control_open(call);",updates_millsec);
	} else {
		call = -1;
		//opening has ended
		closing[i] = 0;
		
		//turn on the close arrow
		theid = "ma_c"+i;
		if(document.getElementById(theid)) {
			theElement = document.getElementById(theid);
			theElement.style.display = "block";
		} else {
			if(is_firefox()) {
				$('map_key_icons').style.overflow = 'auto';
				$('map_key_icons').style.paddingRight = '0px';
			}
		}
	}
}

function control_close(i){
	// i contains 0,1 etc	

if(arguments[1]) {
//	alert('This does the whole closing thing...');
}

	// Set new height of wrapper div
	theid = menu_ids[i];
	
	theElement = document.getElementById(theid);
	var count = counter[i]--;
	
	var update = updates[i][count]

	// Set height
	orig_height = theElement.style.height.replace(/px$/i, '');
	theElement.style.height = ((theid == "map_key_holder")? 34 : top_part) + update + "px";
	
	if(theid == "map_key_holder") {
		var decrease = orig_height - (34 + update);
		orig_margin = (theElement.style.marginTop.replace(/px$/i, '') * 1);
//		theElement.style.marginTop = (orig_margin + decrease) + "px";
	}
	
	// See if updates have finished
	if (counter[i] > 0) {
		//store outside this function  
		call = i
		setTimeout("control_close(call);",updates_millsec);
	} else {
		//closing has ended
		call = -1;
		opening[i] = 0;
		
		//turn on the open arrow
		theid = "ma_o"+i;
		if(document.getElementById(theid)) {
			theElement = document.getElementById(theid);
			theElement.style.display = "block";
		} else {
			if(is_firefox()) {
				$('map_key_icons').style.overflow = 'auto';
				$('map_key_icons').style.paddingRight = '0px';
			}
		}
		
		//fill in the background of xtra top bit
		theid = "mx_"+i;
		if(document.getElementById(theid)) {
			theElement = document.getElementById(theid);
			theElement.style.display = "block";
		}
	}
}
//-->