/**
* Copyright www.fijiwebdesign.com
* Tooltip Overlay Library
* packaged for Ajax Chat
* For the open source version visit http://www.fijiwebdesign.com
*/

/*
* Example: Show tip on mouseover, hide tip on mouseout. Sticky tip on onclick. Close on clicking close button.
* <a href="http://www.example.com/"
* onmouseover="ajcTip.showTip(this, 'tip_id', 20, 20, 200, false, true)"
* onclick="ajcTip.tipStatus(false);return false;"
* onmouseout="ajcTip.hideTip('tip_id');"
* Link Text
* </a>
*/

var is_ajcTooltipLib = true; // Ajax Chat ToolTip Library flag
var ajcTip = new ajcTooltip(); // new instance of the ajcTooltip

/**
* Ajax Chat Tooltip Obj
*/
function ajcTooltip() {
    this.tip_status = true; // tooltip on/off flag
    this._debug = false; // debugging on/off flag
    this._safemode = false; // safely catch any exceptions
}

/**
* Sticky or unstick the tooltip
* @param bool status
*/
ajcTooltip.prototype.tipStatus = function(status) {
    this.tip_status = status;
}

/**
* Creates a custom hovering tooltip
* @param object htmlDivElement srcElement of Event
* @param string id of custom tooltip div
* @param string urlencoded html
* @param int top offset of tooltip from srcElement
* @param int left offset of tooltip from srcElement
* @param int width of tooltip div
* @param bool static tooltip or follow cursor   (todo)
* @param bool show/hide close button
*/
ajcTooltip.prototype.showTip = function(el, id, html, top_offset, left_offset, tip_width, cursor_follow, is_close, fixedpos) {

    this.debug();

    if (!this.tip_status) return false;

     // create a tooltip if not in document
     var x = false;
     if (!(x = document.getElementById(id))) {
        x = document.createElement('div');
        x.style.position = 'absolute';
        x.setAttribute('id', id);
     }
	 
	 x.innerHTML = ''; // remove previous html
     x.style.position = 'absolute';
     x.style.width = (tip_width) ? parseInt(tip_width) + 'px' : '240px';

     if (is_close) {
         var img = document.createElement('img');
         var _this = this;
         img.onclick = function() { _this.tipStatus(true); _this.hideTip(''+ id +'', 1); }
         img.setAttribute('align', 'right');
         img.src = 'components/com_ajaxchat/images/icons/cancel.gif';
         img.style.cursor = 'pointer';
         x.appendChild(img);
     }

     if (typeof(html) == 'object') {
        x.innerHTML += html.innerHTML;
     } else {
        var span = document.createElement('div');
        span.innerHTML = this.unEscape(html);
        x.appendChild(span);
     }

     x.style.display = (x.style._display) ? x.style._display : 'block';
     x.style.zIndex = '1000';

	if (el.style && fixedpos == false) {
     var el_h = ( el_h = parseInt(el.style.height)) ? el_h : 0;
     var el_w = ( el_w = parseInt(el.style.width)) ? el_w : 0;
	} else {
	 var el_h = 0;
	 var el_w = 0;
	}

     // position
     x.style.top = (this.Top(el) + el_h + top_offset)  + 'px';
     x.style.left = (this.Left(el) + el_w + left_offset)  + 'px';

     // add our tooltip to the doc
     document.body.appendChild(x);

     return true;
}

/**
* Show or Hide the tooltip
* @param string tooltip element id
* @param bool force hide of of tooltip
*/
ajcTooltip.prototype.hideTip = function(id, force) {
    if (!this.tip_status && force != 1) return false;
    try {
        var x = document.getElementById(id);
        x.style._display = x.style.display;
        x.style.display = 'none';
        return true;
    } catch(e) {
        //this.debug(e);
    }
}

/**
* Decodes escaped html text
* @param string html
*/
ajcTooltip.prototype.unEscape = function(txt) {
    return decodeURIComponent(txt);
}

/**
* Escapes txt for javascript input
* @param string html
*/
ajcTooltip.prototype.Escape = function(txt) {
    return encodeURIComponent(txt);
}

/**
* Calculates the left offset from body of the element
* @param object htmlElement
*/
ajcTooltip.prototype.Left = function(obj){
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

/**
* Calculates the top offset from body of the element
* @param object htmlElement
*/
ajcTooltip.prototype.Top = function(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/**
* Event handler that toggles the visibility of an element while keeping initial visibility attr value
* @param string id of element to be toggled
* @param object reference to the image which is the source element of event
* @param string img src 1 of source element
* @param string img src 2 to be toggled with 1
*/
ajcTooltip.prototype.tipToggle = function(id, el, src1, src2) {
    var x = document.getElementById(id);
    if (x.style.display == 'none') {
        x.style.display = (x.style._display) ? x.style._display : 'block';
    } else {
        x.style._display = x.style.display;
        x.style.display = 'none';
    }
    if (!el) return;
    if (el.tagName.toLowerCase() == 'img') {
        el.src = (el.src == src1) ? src2 : src1;
    }
}

/**
* Debugging
*/
ajcTooltip.prototype.debug = function(str) {
    if (!this._debug) return false;
    if (!str) {
        for (var i = 0; i < arguments.length; i++) {
            alert('Argument:'+i+' Value:'+arguments[i]);
        }
    } else {
        alert(str);
    }
}

