// <script>

// Copyright (C) 2005 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Distribute this code, any part or any modified version of it.
//     Instead, you can link to the homepage of this code:
//     http://www.php-development.ru/javascripts/menu.php.
//
// YOU MAY
// (1) Use this code on your website.
// (2) Use this code as a part of another product provided that
//     its main use is not creating javascript menus.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind, either
// expressed or implied, including, but not limited to, the implied warranties
// of merchantability and fitness for a particular purpose. You expressly
// acknowledge and agree that use of this code is at your own risk.

var g_PopupIFrame;

function at_display(x)
{
  var win = window.open();
  for (var i in x) win.document.write(i+' = '+x[i]+'<br>');
}

// ----- DropDown Control ------------------------------------------------------

var at_timeout = 50;

// ----- Show Aux -----

function at_show_aux(parent, child)
{
  var p = document.getElementById(parent);
  var c = document.getElementById(child);

  p.className        = "active";

  if (c.offsetWidth <= 0)
  {
    c.style.position   = "absolute";
    c.style.visibility = "visible";
    c.style.display    = "block";
  }

  var direction = undefined;
  if (p.parentNode && p.parentNode["at_position"] == "x")
    direction = p.parentNode["at_direction"];

  var top   = (c["at_position"] == "y") ?  p.offsetHeight+2 : 0;
  var left1 = (c["at_position"] == "x") ?  p.offsetWidth +2 : 0;
  var left2 = (c["at_position"] == "x") ? -c.offsetWidth -2 : 0;
  var left3 = (c["at_position"] == "x") ?  p.offsetWidth +2 : 0;

  for (; p; p = p.offsetParent)
  {
    if (p.style.position != 'absolute')
    {
      left1 += p.offsetLeft;
      left2 += p.offsetLeft;
      top   += p.offsetTop;
    }
    left3 += p.offsetLeft;
  }

  if (direction)
  {
    left = (direction == 'right') ? left1 : left2;
    c['at_direction'] = direction;
  }
  else
  {
    left = (left3+c.offsetWidth < document.body.offsetWidth) ? left1 : left2;
    c['at_direction'] = (left3+c.offsetWidth < document.body.offsetWidth) ? 'right' : 'left';
  }

  c.style.position   = "absolute";
  c.style.visibility = "visible";
  c.style.display    = "block";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
}

// ----- Hide Aux -----

function at_hide_aux(parent, child)
{
  document.getElementById(parent).className        = "parent";
  document.getElementById(child ).style.visibility = "hidden";
  document.getElementById(child ).style.display    = "block";
}

// ----- Show -----

function at_show(evt)
{
var e = evt? evt : window.event;
if(!e) return;
var key = 0;
if (e.keyCode) { key = e.keyCode; } // for moz/fb, if keyCode==0 use 'which'
else if (typeof(e.which)!= 'undefined') { key = e.which; } 
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);

  
	  at_show_aux(p.id, c.id);
	 if(key=='40'){
	   var links = c.getElementsByTagName("a");
	   if(links[0] != null)
			links[0].focus();	
			
	   //c.getElementsByTagName("A").focus();
	 }
	 
	  
	  clearTimeout(c["at_timeout"]);
	  
	  // added by P for showing <div> on top of <select>
	  if (IsIE())
	  {
		  if(g_PopupIFrame==null)
			ShowPopupDiv(c.id);
	  }
    
}

function at_show2(evt)
{
var e = evt? evt : window.event;
if(!e) return;
var key = 0;
if (e.keyCode) { key = e.keyCode; } // for moz/fb, if keyCode==0 use 'which'
else if (typeof(e.which)!= 'undefined') { key = e.which; } 
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child"]);
		if(key=='39'){
		   var links = p.parentNode.parentNode.parentNode.getElementsByTagName("a");			
		   var i=0;
		   for (i = 0; i <links.length; i++) {
				if(links[i].id == this["at_parent"]){					
					if (i >=links.length-1){
						links[0].focus();	
					}
					else{					
						links[i+1].focus();										
					}
						
				}			
			}   
		 }
		  else if(key=='37'){
		   var links = p.parentNode.parentNode.parentNode.getElementsByTagName("a");

		   for (i = links.length-1; i >=0; i--) {
				if(links[i].id == this["at_parent"]){
					if (i>0)			
						links[i-1].focus();	
					else
						links[links.length-1].focus();							 
					
				}
				
			}   
		 }else  {
			  at_show_aux(p.id, c.id);
			 if(key=='40'){
			   var links = c.getElementsByTagName("a");
			   if(links[0] != null)
					links[0].focus();	
					
			   //c.getElementsByTagName("A").focus();
			 }
			 
			  
			  clearTimeout(c["at_timeout"]);
			  
			  // added by P for showing <div> on top of <select>
			  if (IsIE())
			  {
				  if(g_PopupIFrame==null)
					ShowPopupDiv(c.id);
			  }
			  
		}
}

// ----- Hide -----

function at_hide()
{
	
		  var c = document.getElementById(this["at_child"]);

		  c["at_timeout"] = setTimeout("at_hide_aux('"+this["at_parent"]+"', '"+this["at_child" ]+"')", at_timeout);
		  
		  // added by P for showing <div> on top of <select>
		  if (IsIE())
		  {
			if(g_PopupIFrame != null){
				document.body.removeChild(g_PopupIFrame);
				g_PopupIFrame=null;
			}
		  }
	 
}
function at_hide2(evt)
{
var e = evt? evt : window.event;
if(!e) return;
var key = 0;
if (e.keyCode) { key = e.keyCode; } // for moz/fb, if keyCode==0 use 'which'
else if (typeof(e.which)!= 'undefined') { key = e.which; } 
	if(key!='40' && key!='38'  ){
		  var c = document.getElementById(this["at_child"]);
		  var p = document.getElementById(this["at_parent"]);
		  at_hide_aux(this["at_parent"], this["at_child" ]);
		  
		
		
		  // added by P for showing <div> on top of <select>
		  if (IsIE())
		  {
			if(g_PopupIFrame != null){
				document.body.removeChild(g_PopupIFrame);
				g_PopupIFrame=null;
			}
		  }
		  
		/*  if(key=='39'){
		   var links = p.parentNode.getElementsByTagName("a");

		   for (i = 0; i <links.length; i++) {
				if(links[i].id == this["at_parent"]){
					if (i+1<links.length)							
						links[i+1].focus();	
					else
						links[0].focus();	
					
				}			
			}   
		 }
		  else if(key=='37'){
		   var links = p.parentNode.getElementsByTagName("a");

		   for (i = links.length-1; i >=0; i--) {
				if(links[i].id == this["at_parent"]){
					if (i-1>=0)			
						links[i-1].focus();	
					else
						links[i].focus();	
						 
					
				}
				
			}   
		 }*/
	 }else {return false;}
}
// ----- Attach -----

function at_attach(parent, child, position)
{
  p = document.getElementById(parent);
  c = document.getElementById(child );

  p["at_child"]    = c.id;
  c["at_child"]    = c.id;
  p["at_parent"]   = p.id;
  c["at_parent"]   = p.id;
  c["at_position"] = position;
  p.onkeydown= at_hide2;
  p.onmouseover = at_show;
  p.onmouseout  = at_hide;
   p.onkeyup= at_show2;
  //p.Onblur= at_hide;
 c.onkeydown= at_hide2;
  c.onmouseover = at_show;
  c.onmouseout  = at_hide;
   //c.onkeyup= at_show2;
  // c.onkeyup= at_show;
  //c.onblur=  at_hide;
}

// ----- DropDown Menu ---------------------------------------------------------

// ----- Build Aux -----

function dhtmlmenu_build_aux(parent, child, position)
{
  document.getElementById(parent).className = "parent";

  document.write('<div class="vert_menu" id="'+parent+'_child">');

  var n = 0;
  for (var i in child)
  {
    if (i == '-')
    {
      document.getElementById(parent).href = child[i];
      continue;
    }

    if (typeof child[i] == "object")
    {
      document.write('<a class="parent" id="'+parent+'_'+n+'" onkeydown="keyarrow(this,event)" title="'+i+'">'+i+'</a>');
      dhtmlmenu_build_aux(parent+'_'+n, child[i], "x");
    }
    else document.write('<a id="'+parent+'_'+n+'" href="'+child[i]+'" onkeydown="keyarrow(this,event)" title="'+i+'" >'+i+'</a>');
    n++;
  }

  document.write('</div>');

  at_attach(parent, parent+"_child", position);
}
function keyarrow(hyper,evt){
var e = evt? evt : window.event;
if(!e) return;
var key = 0;
if (e.keyCode) { key = e.keyCode; } // for moz/fb, if keyCode==0 use 'which'
else if (typeof(e.which)!= 'undefined') { key = e.which; } 
if(key=='13'){
window.location=hyper.href;
}
 if(key=='40'){
   var links = hyper.parentNode.getElementsByTagName("a");
   for (i = 0; i <links.length; i++) {
		if(links[i].id == hyper.id){
			if (i+1<links.length)			
				links[i+1].focus();	
			else
				links[0].focus();	
		}
		
	}
   	return false;
 }
 if(key=='38'){
   var links = hyper.parentNode.getElementsByTagName("a");
    for (i = links.length-1; i >=0; i--) {
		if(links[i].id == hyper.id){
			if (i-1>=0)			
				links[i-1].focus();	
			else
				links[links.length-1].focus();	
		}
		
	}
   	return false;
 }
  if(key=='9'){
   var links = hyper.parentNode.getElementsByTagName("a");

   for (i = 0; i <links.length; i++) {
		if(links[i].id == hyper.parentNode["at_parent"]){
			if (i+1<links.length)	{		
				links[i+1].focus();				
			}				
			else{
				inks = hyper.parentNode.parentNode.parentNode.getElementsByTagName("a");
				i=0;
			   for (i = 0; i <links.length; i++) {
					if(links[i].id == hyper.parentNode["at_parent"]){
						if (i+1<links.length)			
							links[i+1].focus();	
						else
							links[0].focus();	
						
					}
					
				}   
			}
		}
		
	}   
	return false;
 }
 if(key=='39'){
   var links = hyper.parentNode.parentNode.parentNode.getElementsByTagName("a");
  
   for (i = 0; i <links.length; i++) {
		if(links[i].id == hyper.parentNode["at_parent"]){
		/*	if (i+1<links.length)			
				links[i+1].focus();	
			else
				links[0].focus();	*/
				links[i].focus();	
			
		}
		
	}   
 }
  if(key=='37'){
   var links = hyper.parentNode.parentNode.parentNode.getElementsByTagName("a");
  
   for (i = links.length-1; i >=0; i--) {
		if(links[i].id == hyper.parentNode["at_parent"]){
		/*	if (i-1>=0)			
				links[i-1].focus();	
			else*/
				links[i].focus();				
		}
		
	}   
 }
 
}
// ----- Build -----

function dhtmlmenu_build(menu)
{
  for (var i in menu) dhtmlmenu_build_aux(i, menu[i], "y");
}

/*
 * Written by  : P
 * Date        : Jan 14, 2008
 *
 * Description : The codes below are used to make the pull-down <div> on top
 *               of the <select>. Actually, it is a bug found in IE 6 or
 *               below (Other browsers function properly.)
 */
function IsIE()
{
	return ( navigator.appName=="Microsoft Internet Explorer" );
}


function ShowPopupDiv(divID)
{
	var divPopup=document.getElementById(divID);

	//Increase default zIndex of div by 1, so that DIV appears before IFrame
	divPopup.style.zIndex=divPopup.style.zIndex+1;

	var iFrame = document.createElement("IFRAME");
	iFrame.setAttribute("src", "");

	//Match IFrame position with divPopup
	iFrame.style.position="absolute";
	iFrame.style.left =divPopup.offsetLeft + 'px';
	iFrame.style.top =divPopup.offsetTop + 'px';
	iFrame.style.width =divPopup.offsetWidth + 'px';
	iFrame.style.height =divPopup.offsetHeight + 'px';

	document.body.appendChild(iFrame);

	//Store iFrame in global variable, so it can get removed when divPopup is hidden
	g_PopupIFrame=iFrame;
}
