function mouseX(evt) {
    if (!evt) {
        evt = window.event;
    }
    if (evt.pageX) {
        return evt.pageX;
    } else if (evt.clientX) {
        return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
    } else {
        return 0;
    }
}
function mouseY(evt) {
    if (!evt) {
        evt = window.event;
    }
    if (evt.pageY) {
        return evt.pageY;
    } else if (evt.clientY) {
        return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    } else {
        return 0;
    }
}

function follow(evt) {
    if (document.getElementById('infos')) {
        var offsetX = 15;
        var offsetY = 20;
        
        var obj = document.getElementById('infos');
        var wsize = window.size();
        
        var width = obj.offsetWidth;
        var left = parseInt(mouseX(evt))+offsetX;
        if (left+width > wsize.width-20) {
            left = wsize.width - width - 20;
        }
        var height = obj.offsetHeight;
        var top = parseInt(mouseY(evt))+offsetY;
        if (top+height > wsize.height-20) {
            top = wsize.height - height - 20;
        }
        obj.style.left = left + 'px';
        obj.style.top = top + 'px';
    }
}

function fadeIn(obj, to) {
    this.obj = obj;
    this.to = to;
    this.actual = 0;
    this.grow = function() {
        this.actual += 7;
        if (this.actual <= this.to) {
            setOpacity(this.obj, this.actual);
            setTimeout(this.grow, 1);
        }
    }
    this.obj.style.display = 'block';
    this.grow();
}

function fadeOff(obj, from) {
    this.obj = obj;
    this.to = 0;
    this.actual = from;
    this.grow = function() {
        this.actual -= 7;
        if (this.actual >= this.to) {
            setOpacity(this.obj, this.actual);
            setTimeout(this.grow, 1);
        } else {
            this.obj.style.display = 'none';
        }
    }
    this.grow();
}

function showAjaxMask() {
    var obj = document.getElementById('ajaxMask');
    if (obj.style.display != 'block') {
        fadeIn(obj, 60);
    }
}
function hideAjaxMask() {
    var obj = document.getElementById('ajaxMask');
    if (obj.style.display != 'none') {
        fadeOff(obj, 60);
    }
}

function ajaxWindow() {
    this.obj = document.getElementById('ajaxContent');
    this.w = 0;
}
ajaxWindow.prototype.setWidth = function(w) {
    this.obj.style.width = w+'px';
    this.w = w;
}
ajaxWindow.prototype.show = function() {
    this.obj.style.display = 'none';
    this.center();
    showAjaxMask()
    this.obj.style.display = 'block';
}
ajaxWindow.prototype.hide = function() {
    this.obj.style.display = 'none';
    hideAjaxMask();
}
ajaxWindow.prototype.close = function() {
    this.hide();
    this.obj.innerHTML = '';
    this.setWidth(0);
}
ajaxWindow.prototype.center = function() {
    this.obj.style.position = 'absolute';
    var size = window.size();
    var left = Math.max(0, ((size.width/2)-(this.w/2)));
    var top = 25;
    this.obj.style.left = left+'px';
    this.obj.left = left;
    this.obj.style.top = top+'px';
    this.obj.top = top;
}

function setOpacity(obj, xOpacity) {
    obj.style.opacity = xOpacity/100;
    obj.style.MozOpacity = xOpacity/100;
    obj.style.KHTMLOpacity = xOpacity/100;
    obj.style.filter = 'alpha(opacity=' + (xOpacity) + ')';
}

function getOpacity(obj) {
    if (obj.style.opacity) {
        return obj.style.opacity*100;
    } else if(obj.style.MozOpacity) {
        return obj.style.MozOpacity*100;
    } else if(obj.style.KHTMLOpacity) {
        return obj.style.KHTMLOpacity*100;
    } else if(obj.style.filter) {
        var s = obj.style.filter.indexOf('opacity=')+8;
        var e = obj.style.filter.indexOf(')', s);
        return obj.style.filter.substr(s, e);
    }
}

window.size = function() {
	var w = 0;
	var h = 0;

	if(!window.innerWidth) {
		if(!(document.documentElement.clientWidth == 0)) {
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		} else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	} else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}


function initDhtml() {
    var obj = document.getElementById('ajaxMask');
    if (obj) {
        var x,y;
        if (document.body.scrollHeight > document.body.offsetHeight) {
        	x = document.body.scrollWidth;
        	y = document.body.scrollHeight;
        } else {
        	x = document.body.offsetWidth;
        	y = document.body.offsetHeight;
        }
        obj.style.width = x+'px';
        obj.style.height = y+'px';
    }
    
    var obj = document.getElementById('infos');
    if (obj) {
        obj.onmouseover = function() {
            this.style.display = 'block';
        }
        obj.onmouseout = function() {
            this.style.display = 'none';
        }
    }
}

dhtml_onload = window.onload;
window.onload = function() {
    if (dhtml_onload) {
        dhtml_onload();
    }
    initDhtml();
}
document.onmousemove = follow;

