﻿/* MOverlib: The Minimal Overlib Alternative */
function $(id){
    return document.getElementById(id);
}

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 addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

function removeEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
        do {
		    curleft += obj.offsetLeft;
		    curtop += obj.offsetTop;
	    } while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}


var MOverlib = {
	_monitor_movement:false,
	monitorMousePosition:function(e){
		this._mouse_x = mouseX(e);
		this._mouse_y = mouseY(e);
		this.positionBox(this._mouse_x, this._mouse_y);
	},
    ensureMOverBoxExists:function(){
		if(this._box)
			return;
		
		var el = $('moverbox_container');
		if(!el)
		{
			el = document.createElement('div');
		    el.setAttribute('id', 'moverbox_container');
		    document.body.appendChild(el);
		}
		
		el.innerHTML = '<div id="moverbox" style="display: none;"><h1 id="moverbox_title">t</h1><div id="moverbox_content">p</div></div>';

		this._box = $('moverbox');
		this._title = $('moverbox_title');
		this._content = $('moverbox_content');
    },

    positionBox:function(x,y){
		this._box.style.left= (x + 10) + "px";
		this._box.style.top = (y + 10) + "px";
		
		var topleft = findPos(this._box);
		var left = topleft[0];
		var top = topleft[1];
		
		var w = this._box.offsetWidth;
		var h = this._box.offsetHeight;
		
		var too_far_right_by = (left + w) - document.body.clientWidth;
		if(too_far_right_by > 0)
		    this._box.style.left= (x + 10 - too_far_right_by) + "px";

		var too_far_down_by = (top + h) - document.body.clientHeight;
		if(too_far_down_by > 0)
		    this._box.style.top= (y + 10 - too_far_down_by) + "px";
    },

    setText:function(title, content){
		this._content.innerHTML = content;
		this._title.innerHTML = title;
	},

    setClass:function(klass){
		this._box.setAttribute("class", klass);
		this._box.className = klass;
	},
	
	showBox:function(){
		this._box.style.display = "block";
		if(this._hideing_tiemout)
			window.clearTimeout(this._hideing_tiemout);
	},
	
	hideBox:function(){
		this._box.style.display = "none";
	},

	show:function(title, text, klass, ops){
	    if (null == ops) 
	        ops = {};
	    x = ops.x;
	    y = ops.y;
		this.ensureMOverBoxExists();

		if(!klass)
			this.setClass("none");
		else
			this.setClass(klass);
		
		this.setText(title, text);
		
		this.showBox();	

		if(x||y){
			this.positionBox(x,y);
			if(this._bound_monitorMousePosition)
			    removeEventSimple(document, 'mouseover', this._bound_monitorMousePosition);
		} else {
		    if(this._bound_monitorMousePosition)
		        removeEventSimple(document, 'mouseover', this._bound_monitorMousePosition);
		    else
		        this._bound_monitorMousePosition = function(e){ return MOverlib.monitorMousePosition(e); };
		        
		    addEventSimple(document, 'mousemove', this._bound_monitorMousePosition);
			
			this.positionBox(0,0);
            if(ops.ev) //If it's been passed through
				MOverlib.monitorMousePosition(ops.ev);			
			else if(window.event) //IE6.
				MOverlib.monitorMousePosition(window.event);
		}
		
		return false;							
	},
	hide:function(){
	    if(this._box) //If we have shown the box
	    {
	        window.clearTimeout(this._hideing_tiemout)
		    this._hideing_tiemout = window.setTimeout(function(){ 
       			if(this._bound_monitorMousePosition)
			    {
			        removeEventSimple(document, 'mouseover', this._bound_monitorMousePosition);
			        this._bound_monitorMousePosition = null;
			    }
                MOverlib.hideBox();
            }, 100);
        }
	}
};
