function OpenPage (url)
{
	var href = document.location.href;
	if (href.indexOf (url) == -1) {
		document.location = "http://tools.dottoro.com/" + url;
	}
}

function HoverMenu (hoverID, menuID, src, dir, openOnClick, horDir)
{
	this.m_menu = document.getElementById (menuID);
	try {
		this.m_main = document.getElementById (menuID + "_drhovermenumain");
	} catch (e) {
		this.m_main = null;
	}
	this.m_menuID = menuID;
	this.m_hover = document.getElementById (hoverID);
	this.m_src = src;
	if (this.m_src) {
		this.m_iframe = this.m_menu.getElementsByTagName ("iframe")[0];
	}
	this.m_dir = dir;
	this.m_horDir = horDir;
	this.m_openOnClick = openOnClick;
	this.m_isOver = false;
	this.m_isVisible = false;
	var events = ["mouseover", "mouseout", "mouseenter", "mouseleave"];
	var flags = [3,2,1,1];
	for (var i=0; i < events.length; i++) {
		this.AddEvent (events[i], this.OnEvents, this.m_menu, flags[i], this);
		this.AddEvent (events[i], this.OnEvents, this.m_hover, flags[i], this);
	}
	if (this.m_openOnClick) {
		this.AddEvent ("click", this.OnEvents, this.m_hover, 3, this);
	}
	if (!this.m_main) {
		this.CreateMain ();
	}
}

HoverMenu.prototype.AddEvent = function (type, func, elem, flag, myObj)
{
// IE : 1 , FF,OP,SA,CH : 2
	var retFunc = function (e) {func(e, myObj, elem)};
	if (elem.addEventListener && (flag & 2)) {
		elem.addEventListener(type, retFunc, false);
	} else if (elem.attachEvent && (flag & 1)) {
		elem.attachEvent("on" + type, retFunc);
	}
};

HoverMenu.prototype.CreateMain = function ()
{
	this.m_main = document.createElement ("div");
	this.m_main.id = this.m_menuID + "_drhovermenumain";
	this.SetStyles (this.m_main, "absolute", 0, 0, 100, "none");
	this.SetStyles (this.m_menu, "relative", 0, 0, 1, "block");
	this.m_main.appendChild (this.m_menu);
	document.body.insertBefore (this.m_main, document.body.firstChild.nextSibling);
	this.m_shadow = document.createElement ("div");
	this.SetStyles (this.m_shadow, "absolute", 5, 5, 0, "block");
	with (this.m_shadow.style) {
		bottom = "-4px";
		right = "-4px";
		background = "#000";
		opacity = 0.41;
		filter = "alpha(opacity=41)";
	}
	this.m_main.appendChild (this.m_shadow);
};

HoverMenu.prototype.SetStyles = function (elem, sPos, sTop, sLeft, sZidx, sDisp)
{
	with (elem.style) {
		position = sPos;
		top = sTop + "px";
		left = sLeft + "px";
		zIndex = sZidx;
		display = sDisp;
	}
};

HoverMenu.prototype.OnEvents = function (event, obj, elem)
{
	var type = event.type.toLowerCase ();
	if (elem == obj.m_menu) {
		if (type == "mouseover" || type == "mouseenter") {
			obj.m_isOver = true;
		} else {
			obj.m_isOver = false;
			setTimeout (function () {obj.Hide ();}, 200);
		}
	} else {
		if (type == "click") {
			if (obj.m_isVisible) {
				obj.m_isOver = false;
				setTimeout (function () {obj.Hide ();}, 200);
			} else {
				obj.m_isOver = true;
				setTimeout (function () {obj.Show ();}, 20);
			}
		} else if (type == "mouseover" || type == "mouseenter") {
			obj.m_isOver = true;
			if (!obj.m_openOnClick) {
				setTimeout (function () {obj.Show ();}, 20);
			}
		} else {
			obj.m_isOver = false;
			setTimeout (function () {obj.Hide ();}, 200);
		}
	}
};

HoverMenu.prototype.OnIframeLoad = function (event, obj, elem)
{
	var doc = obj.m_iframe.contentWindow.document;
	obj.AddEvent("scroll", obj.CancelScrollBubble, doc, 3, obj);
	obj.AddEvent("mousewheel", obj.CancelScrollBubble, doc, 3, obj);
	obj.AddEvent("DOMMouseScroll", obj.CancelScrollBubble, doc, 2, obj);
}

HoverMenu.prototype.CancelScrollBubble = function (event, obj) {
	var doc = obj.m_iframe.contentWindow.document;
	var scrollDiv = doc.getElementById ("scrollDiv");
	return obj.ScrollContent (event, scrollDiv);
};

HoverMenu.prototype.ScrollContent = function (event, elem) {
	var step = 0;
	var type = event.type.toLowerCase ();
	if (type == "mousewheel") {
		step = -120 * (event.wheelDelta == 0)? 0 : -120 * event.wheelDelta / Math.abs (event.wheelDelta);
	}
	if (type == "dommousescroll" || type == "scroll") {
		step = 120 * (event.detail == 0)? 0 : 120 * event.detail / Math.abs (event.detail);
	}
	if (step) {
		if (elem == this.m_iframe.contentWindow) {
			elem.scrollBy (0, step);
		} else {
			elem.scrollTop = elem.scrollTop + step;
		}
	}
	
	if (event && navigator.appName.toLowerCase() != "microsoft internet explorer") {
		event.preventDefault();
		event.stopPropagation();
	} else if (event) {
		event.cancelBubble = true;
		event.returnValue = false;
	}
	return false;
};

HoverMenu.prototype.Hide = function ()
{
	if (!this.m_isOver && this.m_isVisible) {
		this.m_main.style.display = "none";
		this.m_isVisible = false;
	}
};

HoverMenu.prototype.Show = function ()
{
	if (this.m_isOver && !this.m_isVisible) {
		if (this.m_iframe && !this.m_iframe.src) {
			this.AddEvent("load", this.OnIframeLoad, this.m_iframe, 3, this);
			this.m_iframe.src = this.m_src;
		}
		this.MoveToHover ();
		this.m_main.style.display = "block";
		this.m_isVisible = true;
	}
};

HoverMenu.prototype.GetShowDirection = function (x, y, hoverW, mainW)
{
	var pos = {};
	pos["x"] = x;
	if (this.m_horDir) {
		if (this.m_horDir == "alignright") {
			pos["x"] = pos["x"] + hoverW - mainW;
		}
	}
	var mainH = this.m_main.offsetHeight;
	var hoverH = this.m_hover.offsetHeight;
	if (this.m_dir) {
		if (this.m_dir == "top") {
			pos["y"] = y - mainH;
			return pos;
		} else {
			pos["y"] = y + hoverH;
			return pos;
		}
	} else {
		var scrollTop = document.documentElement.scrollTop;
		scrollTop = (scrollTop == 0 && document.body.scrollTop)? document.body.scrollTop : scrollTop;

		if (y - scrollTop > mainH) {
			pos["y"] = y - mainH;
			return pos;
		} else {
			pos["y"] = y + hoverH;
			return pos;
		}
	}
};

HoverMenu.prototype.MoveToHover = function (side)
{
	this.m_main.style.visibility = "hidden";
	this.m_main.style.display = "";
	var x = this.m_hover.offsetLeft;
	var hoverW = this.m_hover.offsetWidth;
	var y = this.m_hover.offsetTop;
	var oParent = this.m_hover.offsetParent;
	if (oParent != this.m_hover) {
		while (oParent) {
			x += oParent.offsetLeft;
			y += oParent.offsetTop;
			oParent = oParent.offsetParent;
		}
	}
	var w = this.m_hover.offsetWidth;
	var mainW = this.m_main.offsetWidth;
	var offset = (mainW < w)? -2 : (mainW - w) / 2 - 2;

	if (!this.m_horDir) {
		x -= offset + 2;
	}

	var pos = this.GetShowDirection (x, y, hoverW, mainW);

	with (this.m_main.style) {
		left = pos["x"] + "px";
		top = pos["y"] + "px";
		display = "none";
		visibility = "visible";
	}
};


