﻿function webClient() {

  var i;

  this.IE    = false;
  this.NS    = false;
  this.OP    = false; 
  this.release = null;
  
  if ((i = navigator.userAgent.indexOf("Opera")) >= 0) {
    this.OP = true;
    this.version = parseFloat(navigator.userAgent.substr(i + 5));
    return;
  }

  
  if ((i = navigator.userAgent.indexOf("MSIE")) >= 0) {
    this.IE = true;
    this.release = parseFloat(navigator.userAgent.substr(i + 4));
    return;
  }

  if ((i = navigator.userAgent.indexOf("Netscape6/")) >= 0) {
    this.NS = true;
    this.release = parseFloat(navigator.userAgent.substr(i + 10));
    return;
  }

  if ((i = navigator.userAgent.indexOf("Netscape7/")) >= 0) {
    this.NS = true;
    this.release = parseFloat(navigator.userAgent.substr(i + 10));
    return;
  }

  if ((i = navigator.userAgent.indexOf("Gecko")) >= 0) {
    this.NS = true;
    this.release = 6.1;
    return;
  }
}


var webClient = new webClient();

var activeButton = null;
var activeSubButton = null;

 if (webClient.IE)
	 document.onmousedown = pageMouseDown;
  else
     document.addEventListener("mousedown", pageMouseDown, true);



function pageMouseDown(event) {

  var el;
  
  //if (activeButton == null)
  //  return;

  if (webClient.IE)
    el = window.event.srcElement;
  else
    el = (event.target.tagName ? event.target : event.target.parentNode);

  if (el == activeButton || el == activeSubButton)
    return;
    
  if (el.className != "menuButton"  && el.className != "menuItem" &&
      el.className != "menuItemSep" && el.className != "menu") {
	    if (activeButton) closeMenu(activeButton);
    	if (activeSubButton) closeSubMenu(activeSubButton);
	}
	
	return;

}

function setclass(event, menuName, close){
	if (webClient.IE)
   		button = window.event.srcElement;
  	else
    	button = (event.target.tagName ? event.target : event.target.parentNode);
    
    if (!button.menu && menuName)
   		 button.menu = document.getElementById(menuName);	
   	
   	button.className = "menuItem";  
	if(close)
		closeSubMenu(activeSubButton);
}

function menuTouch(event, menuName) {
  var button;
  if (webClient.IE)
    button = window.event.srcElement;
  else
    button = (event.target.tagName ? event.target : event.target.parentNode);

  if (!button.menu && menuName)
    button.menu = document.getElementById(menuName);

  if (activeButton && activeButton != button){
    closeMenu(activeButton);
    }
  if (activeSubButton && activeSubButton != button)
    closeSubMenu(activeSubButton);
	
  if (!button.isactive)
  	if (menuName && button.menu) {
	    openMenu(button);
	//} else {
	//	activeButton=button;
	//	button.isactive=true;
	//	button.className = "menuButtonActive";
	}
  return false;
}

function subMenuTouch(event, menuName) {
  var button;

  if (webClient.IE)
    button = window.event.srcElement;
  else
    button = (event.target.tagName ? event.target : event.target.parentNode);

  if (!button.menu && menuName)
    button.menu = document.getElementById(menuName);

  if (activeSubButton && activeSubButton != button)
    closeSubMenu(activeSubButton);

  if (!button.isactive && menuName &&  button.menu)
    openSubMenu(button);
  return false;
}

function openSubMenu(button) {
  button.className = "menuItemActive";  
  button.menu.style.visibility = "visible";
  button.isactive = true;
  activeSubButton = button;
}

function openMenu(button) {
  button.className = "menuButtonActive";
  button.menu.style.visibility = "visible";
  button.isactive = true;
  activeButton = button;
}

function closeMenu(button) {
  button.className = "menuButton";
  if (button.menu) 
  	button.menu.style.visibility = "hidden";
  button.isactive = false;
  activeButton = null;
}

function closeSubMenu(button) {
  if (button) {
	  button.className = "menuItem";
	  button.menu.style.visibility = "hidden";
	  button.isactive = false;
  }
  activeSubButton = null;
}


