var x, y;
var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );

i = 0;
var Dom = {
        get: function(el) {
          if (typeof el === 'string') {
            return document.getElementById(el);
          } else {
            return el;
          }
        },
        add: function(el, dest) {
          var el = this.get(el);
          var dest = this.get(dest);
          dest.appendChild(el);
        },
        remove: function(el) {
		  if (i > 10) {
		  	var el = this.get(el);
          	el.parentNode.removeChild(el);
		  } else {
		  	i++;
		  }
        }
      };
var Event = {
        add: function() {
          if (window.addEventListener) {
            return function(el, type, fn) {
              Dom.get(el).addEventListener(type, fn, false);
            };
          } else if (window.attachEvent) {
            return function(el, type, fn) {
              var f = function() {
                fn.call(Dom.get(el), window.event);
              };
              Dom.get(el).attachEvent('on' + type, f);
            };
          }
        }()
      };

function getXHTTP() {
  var xhttp;
   try {
      xhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
	    try {
	      xhttp = new XMLHttpRequest();
	    } catch (e3) {
	      xhttp = false;
	    }
      }
    }
  return xhttp;
}

var http = getXHTTP();

function showmenu(a, obj, name, numpages) {
   	var curleft = curtop = 0;
	var h = a.offsetHeight - 1;
	if (a.offsetParent) {
	do {
			curleft += a.offsetLeft;
			curtop += a.offsetTop;

	} while (a = a.offsetParent);
	}
	
	if (document.getElementById("menu"+obj) && document.getElementById("menu"+obj).innerHTML != '')
	{
		var newdiv = document.getElementById("menu"+obj);
		document.getElementById("submenu").style.display = 'none';
		newdiv.style.left = curleft + "px";
		x = curleft;
		newdiv.style.display = "";
	} else {
	var newdiv = document.createElement('div');
   	newdiv.setAttribute('id', "menu"+obj);
   	newdiv.style.width = "auto";
	newdiv.style.zIndex = "200";
   	newdiv.style.position = "absolute";
	newdiv.style.display = "none";
	x = curleft;
	y = curtop + h;
	newdiv.style.left = x + "px";
   	newdiv.style.top = y + "px";
	
	newdiv.style.textAlign = "left";
	newdiv.style.fontSize = "11px";
   
  	newdiv.style.background = "#B0C6CF";
 	newdiv.style.border = "1px solid #666";
   
  	document.body.appendChild(newdiv);	
	fillmenu(obj, name, curleft);
	}
	
	var n = 0;
	do {
		rdiv = document.getElementById("menu"+n);
		if (obj != n)
		{
			if (rdiv) {
				rdiv.style.display = 'none';
			}
		}
		n++;
	} while (n < numpages)
}

function drawmenuf(a, id, size) {
   	var curl = curt = 0;
	var he = a.offsetHeight - 1;
	if (a.offsetParent) {
	do {
			curl += a.offsetLeft;
			curt += a.offsetTop;

	} while (a = a.offsetParent);
	}
	
	po = curt + he;
	
	document.getElementById("drawmenu").style.top = po + "px";
	document.getElementById("drawmenu").style.left = curl + "px";
   	document.getElementById("drawmenu").style.background = "url(images/newbck.jpg)";
	
	checkdraw(id, size);
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 4;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	document.getElementById("rvalue").value = randomstring;
}

function keepmenu(p, t, d) {
p.style.display = "";
if (d == "in") {
	t.style.backgroundColor = "#FFFF71";
	t.style.border = "1px solid #999";
} else {
	t.style.backgroundColor = "";
	t.style.border = "1px solid #B0C6CF";
}
}

function keepsub() {
var o = document.getElementById("submenu");
if (o.style.display == "" || o.style.display == "none")
	document.getElementById("submenu").style.display = "block";	
}

function fillmenu(obj, name, curleft) {
http.open("GET", "getmenu.php?code="+document.getElementById('rvalue').value+"&menu="+name, true);
http.onreadystatechange = function() { getHttpRes(obj, curleft); }
http.send(null);
}

function checkdraw(id, size) {
http.open("GET", "getmenu.php?code="+document.getElementById('rvalue')+"&drawid="+id+"&size="+size, true);
http.onreadystatechange = function() { drawres(); }
http.send(null);
}

function mouseLeaves(element, evt) {
	if (typeof evt.toElement != 'undefined' && typeof element.contains != 'undefined')
	{
		return !element.contains(evt.toElement);
	}
	else if (typeof evt.relatedTarget != 'undefined' && evt.relatedTarget)
	{
		return !contains(element, evt.relatedTarget);
	}
}

function contains (container, containee) {
	while (containee) {
		if (container == containee) {
			return true;
		}
		containee = containee.parentNode;
	}
	return false;
}

function drawres() {
if (http.readyState == 4) {
		var res = http.responseText;  // These following lines get the response and update the page
		if (res != '') {
			document.getElementById("drawmenu").innerHTML = res;
			$('drawmenu').appear({ duration: .2 });
			return false;
			}
 	 	}
	randomString();
}

function drawsave(obj) {
	Event.add(obj, 'mouseout', function(e) {
		if (mouseLeaves(obj, event)) {
			$(obj.id).fade({ duration: .5 });
			return false;
			}		
		});
}

function showsub(obj, category, b, dir) {
keepmenu(b, obj, dir);
if (is_chrome == false) {
http.open("GET", "getmenu.php?code="+document.getElementById('rvalue').value+"&menu=submenu&category="+category, true);
http.onreadystatechange = function() { getHttpRessub(obj, b, dir); }
http.send(null);
}
}

function hidem(a) {
	if (a)
		a.style.display = "none";
	else
		document.getElementById("submenu").style.display = "none";
		
}

function getHttpRessub(a, b, dir) {
if (http.readyState == 4) {
		var res = http.responseText;  // These following lines get the response and update the page
		if (res != '') {
			a.parentNode.parentNode.parentNode.parentNode.parentNode.appendChild(document.getElementById("submenu"));
			keepmenu(b, a, dir);
			document.getElementById("submenu").style.left = a.offsetLeft + a.parentNode.offsetLeft + a.offsetWidth - 10 + "px";
			document.getElementById("submenu").style.top = a.offsetTop + "px";
			document.getElementById("submenu").innerHTML = res;
			document.getElementById("submenu").style.display = "block";
			}
 	 	}
	randomString();
}

function getHttpRes(obj, curleft) {
document.getElementById('menu'+obj).innerHTML = '';
var i = 0;

if (http.readyState == 4) {
	var res = http.responseText;  // These following lines get the response and update the page
    if (document.getElementById('menu'+obj).style.left != curleft + "px")
	{
		document.getElementById('menu'+obj).style.left = curleft + "px";
	}
	if (res == '') {
		document.body.removeChild(document.getElementById('menu'+obj));
	} else {
		document.getElementById('menu'+obj).innerHTML = res;
		document.getElementById('menu'+obj).style.display = "";
		var el = document.getElementById('menu'+obj);
		Event.add(el, 'mouseover', function(e) {
      		el.style.display = "";
			});
		Event.add(el, 'mouseout', function(e) {
			el.style.display = "none";
			});
	}
	randomString();
  }
}