﻿function drag(objname)
{
	var obj = document.getElementById(objname);
	var s = obj.style;
	var b = document.body;
	var x = event.clientX + b.scrollLeft - s.pixelLeft;
	var y = event.clientY + b.scrollTop - s.pixelTop;
	var m = function()
	{
		if (event.button == 1)
		{
			s.pixelLeft = event.clientX + b.scrollLeft - x;
			s.pixelTop = event.clientY + b.scrollTop - y;
		}
		else document.detachEvent("onmousemove", m);
	}
	document.attachEvent("onmousemove", m);

	if (!this.z) this.z = 999;
	s.zIndex = ++this.z;
	event.cancelBubble = true;
}

function getScrollTop(){
    if(document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    else if(document.body)
        return document.body.scrollTop;
        else return 0;
};

function getScrollLeft(){
    if(document.documentElement && document.documentElement.scrollLeft)
        return document.documentElement.scrollLeft;
    else if(document.body)
            return document.body.scrollLeft;
        else return 0;
};

function getClientWidth(){
    if(document.documentElement && document.documentElement.clientWidth)
        return document.documentElement.clientWidth;
    else if(document.body)
            return document.body.clientWidth;
        else return 0;
};

function getClientHeight(){
    if(document.documentElement && document.documentElement.clientHeight)
        return document.documentElement.clientHeight;
    else if(document.body)
            return document.body.clientHeight;
        else return 0;
};

var ie = /msie/ig.test(navigator.userAgent);
var popwinConfig = {
	showDelay:	0,
	hideDelay:	500,
	keepTime:	10000
};
document.write('<div id="popwin" style="position:absolute; padding:0; margin:0;" onmouseover="clearTimeout(window.hideTimeout);clearTimeout(window.keepTimeout)" onmouseout="popwin.hide()"></div>');
var popwin = { 
	shown:	false,
	show:	function (e, sHtml, setPos, left, top, iKeepTime) {
		window.clearTimeout(window.keepTimeout);
		window.clearTimeout(window.hideTimeout);
		window.showTimeout = window.setTimeout(this.display, popwinConfig.showDelay);
		if(iKeepTime > 0)
			popwinConfig.keepTime = iKeepTime;
		window.keepTimeout = window.setTimeout(this.vanish, popwinConfig.keepTime);
		e = e || window.event;
		
		this.setView(sHtml);
		
	    var el = document.getElementById("popwin");
	    if(setPos)
		    this.setPosition(e, el);
        else
            this.setFixPosition(el, left, top);
	},
	
	setView:    function (sHtml) {
		this.shown = true;
	    var el = document.getElementById("popwin");
		el.innerHTML = sHtml;
	},

	hide:	function () {
		window.clearTimeout(window.showTimeout);
		window.clearTimeout(window.keepTimeout);
		window.hideTimeout = window.setTimeout(popwin.vanish, popwinConfig.hideDelay);
		popwin.shown = false;	
	},

	display:	function () {
		var el = document.getElementById("popwin");
		el.style.visibility = "visible";		
	},

	vanish:		function () {
		var el = document.getElementById("popwin");
		el.style.visibility = "hidden";	
	},

	mousemove:	function (e) {
		if (popwin.shown) {
			var el = document.getElementById("popwin");
			//popwin.setPosition(e, el);
		}
	},

	setPosition:	function (e, el) {
		e = e|| window.event;
		var x = e.clientX + 0;
		var y = e.clientY + 0;
		var iWidth = el.firstChild.offsetWidth;
		var iHeight = el.firstChild.offsetHeight;
		var InnerWidth = ie ? getClientWidth() : window.innerWidth;
		var InnerHeight = ie ? getClientHeight() : window.innerHeight;
		var xOffset = ie ? getScrollLeft() : window.pageXOffset;
		var yOffset = ie ? getScrollTop() : window.pageYOffset;
		if (x + iWidth - InnerWidth > 0) {
			x = x - iWidth + xOffset - 30;
			if (x < 0) x = xOffset;
		}
		else 
			x = x + xOffset;
		if (y + iHeight - InnerHeight > -30) {
			y = y - iHeight + yOffset - 30;
			if (y < 0) y = yOffset;
		}
		else
			y = y + yOffset;
		el.style.left = x + "px";
		el.style.top = y + "px";
	},
	
	setFixPosition:	function (el, x, y) {
	if(x==NaN)x=0;
	if(y==NaN)y=0;
		el.style.left = x + "px";
		el.style.top = y + "px";
	}
};
