/*
 My tooltip lib
*/
var config = {
	width:		0,		// Tooltip width, 0 for auto
	OffsetX:	10,		// Horizontal offset of left-top corner from mousepointer
	OffsetY:	10,		// Vertical offset
	Sticky:		true,	// Move or not while shown
	Border:		false,	// Show border
	step:		100,	// Opacity step time
	timeUp:		10,		// Show opacity time
	timeDown:	70		// Hide opacity time
}

// Broser depend data
var db = document.body || document.documentElement;
tt_aV = new Array();        // Caches and enumerates config data for currently active tooltip
// Mouse data
var tt_musX = 0, tt_musY = 0/*,w.scr_x = 0, tt_scrlY = 0*/;
var tt_flagOpa = 0;
var tt_u = "undefined";

var	w = {
		scr_x: $j(window).scrollLeft(),
		scr_y: $j(window).scrollTop(),
		x: $j(window).width(),
		y: $j(window).height()
};
// tip data
var tt_opaTimer = new Number(0),
tt_mainDiv = 0,     // Main div
tt_subDiv = 0,      // Main sub div - for opacity
tt_status  = 0,     // Status & 1 - tip shown/hide
tt_element = 0,     // onmouseover element for hide tooltip
tt_opacity = 0,     // Current sub div opacity
tt_Cache = new Array(),
tt_ajaxObjects = new Array(),
tt_currentTip,
tt_loading_text = '<div class=loading></div>';

function ajaxTip(){
    tt_currentTip = arguments[0];
    if(tt_Cache[tt_currentTip]){
        arguments[0] = tt_Cache[tt_currentTip];
        tt_Tip(arguments);
    } else {
        arguments[0] = tt_loading_text;
        tt_Tip(arguments);
        var ajaxIndex = tt_ajaxObjects.length;
        var i = tt_currentTip;
		tt_ajaxObjects[ajaxIndex] = new Ajax.Request('/ajax.php',{
			method: 'POST',
			encoding: 'UTF-8',
			parameters: {
				action: 'tooltip',
				tip: tt_currentTip
			},
			onSuccess: function(result) {
				tt_Cache[i] =  result.responseText;
				tt_UpdateTip(tt_Cache[i]);
			}
		});
    }
}
function viewport(){
	w = {
		scr_x: document.viewport.getScrollOffsets().left,
		scr_y: document.viewport.getScrollOffsets().top,
		x: document.viewport.getWidth(),
		y: document.viewport.getHeight()
	};
}
function updatePosition(){
    var div_width  = tt_mainDiv.offsetWidth  || tt_mainDiv.style.pixelWidth || 0;
    var div_height = tt_mainDiv.offsetHeight || tt_mainDiv.style.pixelHeight || 0;
	viewport();
    var x = tt_musX - w.scr_x + tt_aV[OFFSETX];
    var y = tt_musY - w.scr_y + tt_aV[OFFSETY];
    var css = tt_mainDiv.style;
    css.left = x + w.scr_x;
    css.top  = y + w.scr_y;
}
function tt_Tip(arg){
    tt_ReadCmds(arg);
    tt_UpdateTip(arg[0]);
    updatePosition();
    tt_startShowTip();
}
function tt_UpdateTip(text){
    var width = tt_aV[WIDTH]==0 ? '' : (' style="width:' + tt_aV[WIDTH] + 'px;"');
    if (tt_aV[BORDER]){
        var tt_tipBody = document.getElementById('tt_tip_body');
        if (tt_tipBody){
            tt_tipBody.innerHTML = text;
            if (tt_aV[WIDTH]) tt_tipBody.style.width = tt_aV[WIDTH];
            return;
        }
        tt_mainDiv.innerHTML = '<div id=tt_tooltip '+width+'>'+text+'</div>';
    }else{
        tt_mainDiv.innerHTML = '<div id=tt_tooltip '+width+'>'+text+'</div>';
	}
    tt_subDiv = document.getElementById('tt_tooltip');
}
function tt_ReadCmds(a){
	var i = 0;
	for(var j in config)
		tt_aV[i++] = config[j];

	if(a.length & 1){
		for(i = a.length - 1; i > 0; i -= 2)
			tt_aV[a[i - 1]] = a[i];
		return true;
	}
	return false;
}
function tt_startShowTip(){
    tt_opaTimer.EndTimer();
    if (tt_element){
        tt_RemEvtFnc(tt_element, "mouseout", tt_Hide);
        tt_element = 0;
    }
    tt_status|=1;
    tt_mainDiv.style.visibility = "visible";
    if (tt_flagOpa && tt_aV[TIMEUP]){
        tt_opacity = 0;
        tt_opaStepUp(tt_aV[STEP]);
    } else {
        tt_opacity = 100;
        tt_SetOpa(tt_subDiv.style, tt_opacity);
    }
}
Number.prototype.Timer = function(s, iT, bUrge){
	if(!this.value || bUrge)
		this.value = window.setTimeout(s, iT);
};
Number.prototype.EndTimer = function(){
	if(this.value){
		window.clearTimeout(this.value);
		this.value = 0;
	}
}
function tt_RemEvtFnc(el, sEvt, PFnc){
	if(el){
		if(el.removeEventListener)
			el.removeEventListener(sEvt, PFnc, false);
		else
			el.detachEvent("on" + sEvt, PFnc);
	}
}
function tt_opaStepUp(step){
    tt_opacity+=(100*step/tt_aV[TIMEUP]);
    var time = Math.floor(tt_aV[TIMEUP]/step);
    if (tt_opacity < 100){
        tt_opaTimer.Timer("tt_opaStepUp(" + step + ")", time, true);
    } else {
		tt_opaTimer.EndTimer();
		tt_opacity = 100;
	}
    tt_SetOpa(tt_subDiv.style, tt_opacity);
}
function tt_SetOpa(css, opa){
	if(tt_flagOpa == 5){
		if(opa < 100){
			var bVis = css.visibility != "hidden";
			css.zoom = "100%";
			if(!bVis) css.visibility = "visible";
			css.filter = "alpha(opacity=" + opa + ")";
			if(!bVis) css.visibility = "hidden";
		}else css.filter = "";
	} else {
		opa /= 100.0;
		switch(tt_flagOpa){
			case 1: css.opacity = opa; break;
			case 2: css.KhtmlOpacity = opa; break;
			case 3: css.KHTMLOpacity = opa; break;
			case 4: css.MozOpacity = opa; break;
		}
	}
}
function tt_Hide(e){
	e = window.event || e;
    if (e){
        var target = e.target || e.srcElement;
        if (tt_element == target){
            tt_RemEvtFnc(tt_element, "mouseout", tt_Hide);
            tt_element = 0;
            tt_startHideTip();
        }
    }
}
function tt_startHideTip(){
    tt_opaTimer.EndTimer();
    tt_status&=~1;
    if (tt_flagOpa && tt_aV[TIMEDOWN])
        tt_opaStepDown(tt_aV[STEP]);
    else
        tt_finishHideTip();
}
function tt_opaStepDown(step){
    tt_opacity-=(100*step/tt_aV[TIMEDOWN]);
    var time = Math.floor(tt_aV[TIMEDOWN]/step);
    if (tt_opacity > 0)
        tt_opaTimer.Timer("tt_opaStepDown(" + step + ")", time, true);
    else
        {tt_opaTimer.EndTimer();tt_finishHideTip();}
    tt_SetOpa(tt_subDiv.style, tt_opacity);
}
function tt_finishHideTip(){
    tt_mainDiv.style.visibility = "hidden";
    tt_opacity = 0;
}
function Init(){
    // Create the tooltip DIV
    if(db.insertAdjacentHTML)
        db.insertAdjacentHTML("afterBegin", tt_MkMainDivHtm());
    else if(typeof db.innerHTML != tt_u && document.createElement && db.appendChild)
        db.appendChild(tt_MkMainDivDom());
    tt_mainDiv = document.getElementById('tt_mytooltip');
    tt_mainDiv.style.position = "absolute";
    tt_mainDiv.style.zIndex   = 3000;

    tt_MkCmdEnum();
    tt_OpaSupport();
    Event.observe(window, "scroll", viewport);
    Event.observe(document, "mousemove", tt_Move);
    Event.observe(window, "unload", tt_finishHideTip);
    tt_finishHideTip();
}
function tt_MkCmdEnum(){
	var n = 0;
	for(var i in config)
		eval("window." + i.toString().toUpperCase() + " = " + n++);
}
function tt_OpaSupport(){
	var css = db.style;
	tt_flagOpa = (typeof(css.opacity) != tt_u) ? 1
				: (typeof(css.KhtmlOpacity) != tt_u) ? 2
				: (typeof(css.KHTMLOpacity) != tt_u) ? 3
				: (typeof(css.MozOpacity) != tt_u) ? 4
				: (typeof(css.filter) != tt_u) ? 5
				: 0;
}
// function tt_AddEvtFnc(el, sEvt, PFnc){
	// if(el){
		// if(el.addEventListener)
			// el.addEventListener(sEvt, PFnc, false);
		// else
			// el.attachEvent("on" + sEvt, PFnc);
	// }
// }
function tt_MkMainDivDom(){
	var el = document.createElement("div");
	if(el) el.id = "tt_mytooltip";
	return el;
}
function tt_MkMainDivHtm(){
	return('<div id=tt_mytooltip></div>');
}
function tt_Move(e){
	e = window.event || e;
	if(e){
        tt_musX = (typeof(e.pageX) != tt_u) ? e.pageX : (e.clientX + w.scr_x);
        tt_musY = (typeof(e.pageY) != tt_u) ? e.pageY : (e.clientY + w.scr_y);
        if (tt_element == 0 && tt_status & 1){
            tt_element = e.target || e.srcElement;
            Event.observe(tt_element, "mouseout", tt_Hide);
        }
        if (!tt_aV[STICKY] && tt_status&1)
			updatePosition();
	}
}
/*************************************/
Init();
