// Javascript for the Parking and Transportation Services web page
// Written by: Daniel Boyd

// Change these to control colors in the menus:
mb = "#336699"; // menu background
mt = "#FFFFFF"; // menu text
ab = "#FFFFFF"; // active menu background
at = "#000000"; // active menu text 


depth = getDirDepth(window.location);
var root = '';
for (i = 0; i < depth; i++)
{
	root += "../";
}

document.writeln('<script type="text/javascript" src="' + root + 'js/ckapi.js"></script>');

// Figure out how many ../'s are needed to get to the site root for relative includes
function getDirDepth(url)
{
	var base;
	var strUrl = url + "";
	if (strUrl.indexOf("file://") != -1)
	{
		base = "file:///D:/website/current/";
	}
	else
	{
		var tempAnchor = "/parking/";
		base = strUrl.substring(0, strUrl.indexOf(tempAnchor) + tempAnchor.length);
	}
	strUrl = strUrl.replace(base, '');
//	strUrl = strUrl.replace(/\..*?$/, '');
	var lastIndex = strUrl.lastIndexOf('?');
	if (lastIndex != -1)
	{
		strUrl = strUrl.substring(0, lastIndex);
	}
	var depth = 0;
	var start = 0;
	depth = strUrl.split('/').length - 1;
	return depth;
}

//***BROWSER SNIFFING***

  var agt=navigator.userAgent.toLowerCase();
  var is_major = parseInt(navigator.appVersion); 
  var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) 
            && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) 
            && (agt.indexOf('webtv')==-1)); 
  var is_nav5up = (is_nav && (is_major >= 5)); 
  var is_opera = (agt.indexOf('opera')!=-1);
  var is_ie = ((agt.indexOf('msie')!=-1) && (!is_opera));
  var is_ie5up = ((is_ie));
  
  var is_nav6old = (is_nav && ((agt.indexOf('/6.1')!=-1) || (agt.indexOf('/6.0')!=-1)));
  var is_gecko = (agt.indexOf('gecko')!=-1);
  var is_nav6up = is_gecko && (agt.indexOf('netscape')!=-1);
  var is_nav7 = is_nav6up && (agt.indexOf('/7')!=-1);
  var is_nav62 = is_nav6up && (agt.indexOf('/6.2')!=-1);
  var is_mozilla = is_gecko && !is_nav6up;
  var is_win = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
  var is_mac = (agt.indexOf('mac')!=-1);
  var is_linux = (agt.indexOf('linux')!=-1);
  var is_macie = (is_mac && is_ie);
  var is_winie = ((is_ie) && (is_win));
  var is_safari = (agt.indexOf('safari')!=-1);
  var is_konqueror = (agt.indexOf('konqueror')!=-1);
  var is_camino = (agt.indexOf('camino')!=-1);  // chimera project changed name to camino on 3-3-03
  var is_chimera = (agt.indexOf('chimera')!=-1) || is_camino;
  
  //browsers in which menus are disabled:

  //versions of opera prior to 7 do not display menus correctly
  var opera_version =
  parseInt(agt.substring(agt.indexOf('opera')+6,agt.indexOf('opera')+7));
  var is_old_opera = ((agt.indexOf('opera')!=-1) && (opera_version < 7));
 


  //Internet Explorer <= 4.x don't support menus
  var is_old_ie = (is_ie && !is_ie5up);
  
  //Netscape 4.x and lower should stay far away from the menus
  var is_old_netscape = (is_nav && !is_nav5up);

  //OmniWeb has trouble with layers, it seems
  var is_omniweb = (agt.indexOf('omniweb')!= -1);

  //iCab just ignores the layers...
  var is_icab = (agt.indexOf('icab')!= -1);

//disable menus for unsupported browsers
function supported_browser() {
  return !(is_old_opera || is_old_netscape || is_icab || is_omniweb || is_old_ie);
}
 
 /* function or opening a popup window */
function createWindow(cUrl,cName,cFeatures) {
        var xWin = window.open(cUrl,cName,cFeatures);
        xWin.focus();
}

//***POSITIONING OF SUBMENUS***
//submenus are positioned using the arrow in parent menu as a reference

// used to center image in center_img function
function imgOffset(image_elm) {
  var cell_elm = image_elm.offsetParent;
  if (is_nav62) {
    return 3;
  }
  var result = half(cell_elm.offsetHeight - image_elm.height)-1;
  if (result > 30)
  {
  	result = 3;
  }
  return result;
}

//positions submenus in relation to the arrow in the parent menu
function positionSubMenu(menu,arrow) {
  if (supported_browser()) {
	var obj = document.getElementById(menu);
    var xy = getWindowXY(arrow.id);
    var offsetX = 0;
    var offsetY = 0;

	if (is_safari || is_konqueror) { offsetX -= 18; offsetY += -2; }		//due to variations in the values browsers return for getWindowXY...
    if (is_winie) { offsetX -= 16; offsetY; }
    if (is_opera) { offsetX += -1; offsetY += -2; }
    if (is_chimera || is_nav7) { offsetX++;	offsetY++; }
    if (is_macie) { offsetX += -2; }

    offsetY -= imgOffset(arrow);

    obj.style.left = xy[0] + 10 + offsetX + "px";
    obj.style.top  = xy[1] + offsetY + "px";
  }
}

// centers image vertically inside its offsetParent;
// used here to center the arrows inside of the menu cell
function center_img(image_elm) {
  if (supported_browser()) {
    var temp = imgOffset(image_elm);
	if (temp < 0 || temp > 20) {temp = 3;}
	image_elm.style.marginTop = temp + "px";
  }
}

//***SHOWING AND HIDING OF MENUS***

var done="";

function position_menu(layer) {			//menus are positioned to work on Internet Explorer for Macintosh because
  obj=document.getElementById(layer);		//it seems to be unaffected by this function.  Their position is modified
  if (is_opera) {						    //here to work with the other browsers.
    obj.style.left=(obj.offsetLeft+8);
	obj.style.top=(obj.offsetTop+8);
  }
}

function toggleMenu(layer,mode) {
  var subm=(layer.indexOf('sub')!=-1);		//true if layer is a submenu (note that this requires 'sub' to be somewhere in the menu's id)
  if (supported_browser()) {				//if the browser is not supported, the menus are never opened			

  if (done.indexOf(layer)==-1) {
    done += layer;
	position_menu(layer);
  }
  if (mode == 'show') {
    stopall();								//stopall and keepsubopen clear the timeouts for normal menus and sub menus
	keepsubopen();
  }
      var v,obj;
	    if (mode == 'hide') {
		  v='hidden';
		  if (subm) {
		    asl='NONE';						//asl = "NONE" when no sub menus are open
		  }
		  }
		else {
		  v='visible';
		  if (subm) {
		    asl=layer;
		  } else {
		    al=layer;
		  }
		}
      obj=document.getElementById(layer);
      if (typeof(obj) != "undefined" && obj.style
		&& (!obj.style.visibility || obj.style.visibility != v))
	  {
		
		if (typeof(theFader) == "undefined" && typeof(ckapi) != "undefined")
		{
			theFader = new ckapi('null', 25, 90);
		}
		if (!(is_macie || is_safari || is_konqueror) && window.theFader && /*!document.all &&*/ obj.style && obj.style.visibility != v)
		{
			if (theFader.fading && theFader.layerObj && theFader.getTrans() != theFader.start && theFader.layerObj != obj)
			{
				theFader.stop();
				theFader.setTrans(theFader.start);
			}
			theFader.layerObj = obj;
			if (mode == "show")
			{
				var trans = theFader.getTrans();
				if (trans == -1)
				{
					theFader.setTrans(0);

				}
/*				alert(theFader.getTrans());*/
				if (trans == -1 || trans == 0)
				{
					theFader.fadeIn();
				}
			}
			else if (mode == "hide")
			{
				theFader.stop();
				theFader.setTrans(0);
			}
		}
		obj.style.visibility = v;
      }
		
  }
}



function hideLayer(id, displayToo)
{
	if (document.getElementById)
	{
		var obj = document.getElementById(id);
		if (obj.style)
		{
			obj.style.visibility = 'hidden';
			if (displayToo)
			{
				obj.style.display = 'none';
			}
			return true;
		}
	}
	return false;
}

function showLayer(id, displayToo)
{
	if (document.getElementById)
	{
		var obj = document.getElementById(id);
		if (obj.style)
		{
			obj.style.visibility = 'visible';
			if (displayToo)
			{
				obj.style.display = 'block';
			}
		}
	}
}

//dummy function
function submenus() {}
function upgrade_browser() {}
  
//used to open submenus
function opensubmenu(layer) {
	if (asl != layer)
	{
		if (asl != 'NONE')
		{
			toggleMenu(asl,'hide');
		}
		toggleMenu(layer,'show');
	}
	else
	{
		stopall();
	}
}

//***FUNCTIONS TO CONTROL THE MENU TIMEOUT***

// the menu close timeout variables
var menu_close_timeout = 0;
var submenu_close_timeout = 0;
var close_sub = true;

// delay in miliseconds until the open menus are closed
var menuDelay = 100;

// function calls the closeallmenus() function after a delay
function closeall() {
  close_sub=true;
  menu_close_timeout = setTimeout('closeMenus()', menuDelay);
  submenu_close_timeout = setTimeout('closeSubs()',menuDelay);
}
call = closeall; //alias

//clears the submenu timeout (keeps the submenu open)
function keepsubopen() {
  close_sub=false;
}
kso = keepsubopen; // alias

//close the active submenu (asl)
function closeSubs()
{	
	if ((asl != 'NONE') && (close_sub))
	{
		toggleMenu(asl,'hide');
	}
}

// stop all timeout functions (stops menus from closing)
function stopall() {
  clearTimeout(menu_close_timeout);
  if (!close_sub) {
    clearTimeout(submenu_close_timeout);
  }
}

//function returnasl() {
//  return asl;
//}

//closes the active menu (al)

function closeMenus()
{
	if (al != 'NONE')
	{			// menus have never been opened
		swapImgRestore(); 
		ClearStyles();
		toggleMenu(al,'hide');
		if (asl != 'NONE')
		{
			toggleMenu(asl,'hide');
		}
	}
}
clom = closeMenus; // alias

// clear timeout on page load
function initialize() {
  stopall();
}

//***FUNCTIONS TO "HIGHLIGHT" THE CELL IN THE MENU THAT THE MOUSE IS HOVERING OVER***

// global layer variables
var al = "NONE"; //active layer
var asl= "NONE"; //active sublayer (submenu)
var pl = "NONE"; //previous link
var pc = "NONE"; //previous cell

var ppc = "NONE"; //previous parent cell
var ppl = "NONE"; //previous parent link

// function to change colors of menu items
function cstyles(nc)  {
	var nl = nc;
    stopall();
	if (nc!=pc) {
    var newsub = ((asl.indexOf(pc)!=-1) && (nc.indexOf('Sub')!=-1));
	var activesub = ((asl!='NONE') && (nc.indexOf('Sub')!=-1));
	//window.status = "newsub=" + newsub + " && activesub=" + activesub;
    if (pc == "NONE" && pl == "NONE")  {
        var currentLink = document.getElementById(nl);
		var currentCell = document.getElementById(nc);
		currentLink.style.color = at;
		currentCell.style.backgroundColor = ab;
        pl = nl;
        pc = nc;        
      }
    else  {
        var previousLink = document.getElementById(pl);		// change previous cell back to the way it was...
        var previousCell = document.getElementById(pc);
		if(!newsub) {
          previousLink.style.color = mt;
          previousCell.style.backgroundColor = mb;
		}
		if(!activesub) {
		  swapImgRestore();		
		}
		if ((!activesub) && (ppl!='NONE')) {
          var previousParentLink = document.getElementById(ppl);
          var previousParentCell = document.getElementById(ppc);
          previousParentLink.style.color = mt;
		  previousParentCell.style.backgroundColor = mb;

		}
        var currentLink = document.getElementById(nl);		// change new cell to active colors
		var currentCell = document.getElementById(nc);
        currentLink.style.color = at;
        currentCell.style.backgroundColor = ab;
		if (newsub) {		// set global variables to be active cell, link
		  ppl = pl;
		  ppc = pc;
		}
        pl = nl;
        pc = nc;        
      }
  }
  return true;
}
//cstyles = ChangeStyles;  // alias

// resets menu colors to default values
function ClearStyles() {
  if (pl != 'NONE') {
        var previousLink = document.getElementById(pl);
        var previousCell = document.getElementById(pc);
        previousLink.style.color = mt;
        previousCell.style.backgroundColor = mb;
		pl = 'NONE';
		pc = 'NONE';
  }
  if (ppl != 'NONE') {
        var previousLink = document.getElementById(ppl);
        var previousCell = document.getElementById(ppc);
        previousLink.style.color = mt;
        previousCell.style.backgroundColor = mb;
		ppl = 'NONE';
		ppc = 'NONE';  
  }
}

//functions to swap arrows for 3rd-tier menus

// i1, i2 are set as the ids of the normal arrow and the black arrow.
// the can be global variables because only one image will ever be changed at any one time.

var i1="NONE";
var i2="NONE";

function swapImage(img1,img2) {
  i1 = img1;
  i2 = img2;
  img1_elm = document.getElementById(img1);
  i2url = document.getElementById(img2).getAttribute('src');
  img1_elm.src = i2url;
}

function swapImgRestore() {
  if (i1!="NONE") {
  img1_elm = document.getElementById(i1);
  img2_elm = document.getElementById(i2);
  if (img1_elm.src == img2_elm.src) {
    norm = document.getElementById('normalarrow');
	img1_elm.src = norm.src;
  }
  }
}

//***MISCELLANEOUS FUNCTIONS***

// div from C++
function half(num) {
  var i = num / 2;
  var m = num % 2;
  if (m==1) {i += .5}
  return i;
}

// function returns [x,y] coordinates with respect to browser window for any element
// with an offset.  Adds 16 to offsetx because PTS web page has a 16px margin.
function getWindowXY(el) {
  var offsetx=0;
  var offsety=0;
  var elm = document.getElementById(el);
  while(elm.offsetParent) {
    offsetx += elm.offsetLeft;
	offsety += elm.offsetTop;
	elm = elm.offsetParent;
  }
  if ((is_safari) | (is_ie)) { offsetx += 16; }
  return [offsetx, offsety];

}

function doSubMenus() {
	if (!document.getElementById) return;
	var arrows = [['vendingarrow', 'vendingCellsub'], ['parkingGaragesarrow', 'pgCellsub']
		, ['fsParkarrow', 'fsParkCellsub'], ['specialeventsarrow', 'specialeventsCellsub']
		, ['rulesarrow', 'rulesCellsub']];
	var obj;
	for (arrow in arrows)
	{
		obj = document.getElementById(arrows[arrow][0]);
		center_img(obj);
		positionSubMenu(arrows[arrow][1], obj);
	}
}