// Custom JavaScript by Mitchell Evan, www.evanmedia.com

/****************************
 * Generic window opener.
 ****************************/
function popGeneric(url,windowName,options) {
	var win = window.open(url, windowName, options);
	win.focus();
}

/****************************
 * Open a calculator window.
 ****************************/
function popCalc(url) {
	var options='width=490,height=500,scrollbars=yes,resizable=yes';
	popGeneric(url,'ashleyMortgageCalculator',options);
}

/* Browser detect
 */
var isIE = navigator.userAgent.indexOf("MSIE") != -1;

/* Bug workaround for Navigator 4
 */
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

/* Rollovers and submenus (menu groups)
 */
 
// Initially
var fNavOver = false;  // even if the mouse is still there (nice side effect)
var navPending = '';
var whichMenuGroupShown = '';
var navTimerId = '';
var offTimerId = '';

// Image swapping
var imagePath = 'images/';
var rememberImageSrc = new Array();
rememberImageSrc['Home'] = 'nav-off-home.gif';
rememberImageSrc['About'] = 'nav-off-about.gif';
rememberImageSrc['Buying'] = 'nav-off-buying.gif';
rememberImageSrc['Selling'] = 'nav-off-selling.gif';
rememberImageSrc['Financing'] = 'nav-off-financing.gif';
rememberImageSrc['HomeSearch'] = 'nav-off-home-search.gif';
rememberImageSrc['EastBay'] = 'nav-off-east-bay.gif';
rememberImageSrc['VitalResources'] = 'nav-off-vital-resources.gif';
rememberImageSrc['Contact'] = 'nav-off-contact.gif';

// State transition: to 4A, to 4B
function rollNav(id) {
	ms('rollNav('+id+')');
	fNavOver = true;
	if(whichMenuGroupShown == '') {
		//There is no submenu visible, so show the On state of this nav.
		//4A
		showNav(id);
	} else if(navTimerId == '') {
		//A submenu is currently visible, and the nav "twitch" allowance has expired.
		//So clear the subnav, and show the On state of this nav.
		//1C to 4A
		hideMenuGroup(whichMenuGroupShown);
		showNav(id);
	} else {
		//4B
		navPending = id;
	}
}

// Physically show the nav (the On state)
function showNav(id) {
	ms('showNav('+id+')');
	if(document.images) {
		document.images['nav' + id].src = imagePath + 'spacer.gif';
	}
}

// State transition: 4A to 1, 4B to 1B
function leaveNav(id) {
	ms('leaveNav('+id+')');
	fNavOver = false;
	navPending = '';
	hideNav(id);
}

// Physically hide the nav (return to Off or At state)
function hideNav(id) {
	ms('hideNav('+id+')');
	if(document.images) {
		document.images['nav' + id].src = imagePath + rememberImageSrc[id];
	}
}

// State transition: to 2
function rollMenuGroup(name) {
	ms('rollMenuGroup('+name+')');
	clearSubmenuTimers();
	// If another menu group was already on, clear it.
	ms('in rollMenuGroup: whichMenuGroupShown: "'+whichMenuGroupShown+'"');
	if((whichMenuGroupShown != '') && (whichMenuGroupShown != name)) {
		hideMenuGroup(whichMenuGroupShown);
	}
	// Set state flags and make visible
	fNavOver = true;
	whichMenuGroupShown = name;
	showNav(name);
	showSubmenu(name);
}

function showSubmenu(id) {
	ms('showSubmenu('+id+')');

	thisId = 'subnav' + id;
	if(document.all && document.all[thisId]) {
		//Internet Explorer
		document.all[thisId].style.visibility = 'visible';
	} else if(document.getElementById && document.getElementById(thisId)) {
		document.getElementById(thisId).style.visibility = 'visible';
	}
}

// State transition: 2 to 1B
function leaveMenuGroup(name) {
	ms('leaveMenuGroup('+name+')');
	// Set two timers.
	// First, a short timer if the user is continuing to browse the main menu.
	navTimerId = window.setTimeout("navTimeoutForMenuGroup('" + name + "')", 150); //like 150
	// Second, a longer timer if the user has rolled off.
	offTimerId = window.setTimeout("hideMenuGroup('" + name + "')", 700); //like 700
	// Set state flags
	fNavOver = false;
}

// Check if conditions are met for 4B to 4A
function navTimeoutForMenuGroup(name) {
	ms('navTimeoutForMenuGroup('+name+')');
	navTimerId = '';
	if(fNavOver) {
		showNav(navPending);
		hideMenuGroup(name);
	}
}

// State transition: 1B to 1, 4B to 4A. Possible side effect of: to 2, 1C to 4A.
function hideMenuGroup(name) {
	ms('hideMenuGroup('+name+')');
	clearSubmenuTimers()
	hideNav(name);
	hideSubmenu(name);
	navPending = '';
}

// Physically hide the subnav (return to Off or At state)
function hideSubmenu(id) {
	ms('hideSubmenu('+id+')');
	thisId = 'subnav' + id;
	if(document.all && document.all[thisId]) {
		//Internet Explorer
		document.all[thisId].style.visibility = 'hidden';
	} else if(document.getElementById && document.getElementById(thisId)) {
		document.getElementById(thisId).style.visibility = 'hidden';
	}
}

// Clear timers which would vanish the menu group
function clearSubmenuTimers() {
	ms('clearSubmenuTimers()');
	if(navTimerId != '') {
		window.clearTimeout(navTimerId);
		navTimerId = '';
	}
	if(offTimerId != '') {
		window.clearTimeout(offTimerId);
		offTimerId = '';
	}
}

//Debug writer
function ms(msg) {
	if(frames['debug']) {
		d = frames['debug'].document;
		d.write('<pre>');
		d.write(msg + '\n');
	}
}
function ms2(msg) {
	if(frames['debug']) {
		d = frames['debug'].document;
		d.write('<pre>  ');
		d.write(msg + '\n');
	}
}


