/*

	ivTT - a jQuery Tooltip
	author: Ingo Volkmann www.godiv.de
    
    use ivTTappendToSelector to define the element where the tooltip should be appended
    default should be body
    while using isotope and layer, you can choose the item list container
    
    use ivTTtype to define the behavior of the layer
    possible values are:
        layer = the layer will be shown by hovering the element and gets delayed by closing. it will also not disappear when the mouse is over it. 
                the position left and top depending on the isotope class. if isotope is used, the layer will be shown at the top of the item
        table = the layer has a fix position of the left border so it will always shown under the mouse
        tooltip = the layer appears where it has enough space, left/top, right/top, right/bottom, left/bottom
    
	
*/

var ivTTtimeout;
var ivTTobjElement;
var ivTTobjCurrentElement;
var ivTTlayerOpen = false;
var ivTTisOver = false;

function ivTT (imgPath, ivTTtype, ivTTappendToSelector) {

	this.ivTTimagePath = imgPath;
    this.ivTTtype = ivTTtype;
    this.objSelfIvTT = this;
    this.ivTTappendToSelector = ivTTappendToSelector;
		
	this.ivTT_init = function () {
	    var type = this.ivTTtype;
        var objSelf = this.objSelfIvTT;
		$(this.ivTTappendToSelector).append('<div id="ivTT"></div>');
		$(this.ivTTappendToSelector).append('<iframe src="' + this.ivTTimagePath + 'blank.gif" frameborder="0" id="ivTTiframe"></iframe>');
		$(".ivTT").hover(function(event){
            ivTTobjCurrentElement = $(this);
            ivTTisOver = true;
            if ($(ivTTobjCurrentElement).attr('id') != $(ivTTobjElement).attr('id') || !ivTTlayerOpen) {
                ivTTobjElement = $(this);
                if (type == 'layer') {
                    var boolIsHover = false;
                    $('#ivTT').hover(function(){
                        window.clearTimeout(ivTTtimeout);
                        boolIsHover = true;
                        ivTTisOver = false;
                    }, function(){
                        window.setTimeout(function(){
                            if (!objSelf.ivTT_same()) {
                                objSelf.ivTT_destroy();
                                ivTTlayerOpen = false;
                            } else {
                                window.setTimeout(function(){
                                    if (!ivTTisOver) {
                                        objSelf.ivTT_destroy();
                                        ivTTlayerOpen = false;
                                    }
                                }, 20);
                            }
                        }, 20);
                    });
                    if (!boolIsHover) {
                        window.clearTimeout(ivTTtimeout);
                        objSelf.ivTT_destroy();
                        ivTTlayerOpen = false;
                    }
                }
        		objSelf.ivTT_show(event, this.id, this);
                ivTTlayerOpen = true;
            }
        }, 
        function(){
            if (type == 'layer') {
                ivTTtimeout = window.setTimeout(function(){
                    objSelf.ivTT_destroy();
                    ivTTlayerOpen = false;
                    ivTTisOver = false;
                }, 300);
            } else {
                objSelf.ivTT_destroy();
            }
		}).mousemove(function(event){
            if (type != 'layer') {
    			objSelf.ivTT_move(event, this.id, this);
            }
		}).click(function(){
            return false;
        });	
	}
    
    this.ivTT_same = function () {
        
        if ($(ivTTobjCurrentElement).attr('id') == $(ivTTobjElement).attr('id')) {
            return true;
        }
        return false;
        
    }
	
	this.ivTT_move = function (e, linkId, objElement) {
	    var objSelf = this.objSelfIvTT;
		if(!e) e = window.event;
		var pos = { left: e.clientX, top:e.clientY };
	
		var b = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ?
		window.document.documentElement : window.document.body || null;
	
		if (b)
		{
			pos.left += b.scrollLeft;
			pos.top +=  b.scrollTop;
		}
		this.ivTT_calc (linkId, pos.left, pos.top, objElement);
	}

	this.ivTT_show = function (e, linkId, objElement){
	    var objSelf = this.objSelfIvTT;
		if(!e) e = window.event;
		var pos = { left: e.clientX, top:e.clientY };
	
		var b = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ?
		window.document.documentElement : window.document.body || null;
	
		if (b)
		{
			pos.left += b.scrollLeft;
			pos.top +=  b.scrollTop;
		}
		
		var html = document.getElementById(linkId+"div").innerHTML;
		$("#ivTT").append("<div id='ivTT_copy'>"+html+"</div>");
		$("#ivTT").append("<div id='ivTT_arrow'></div>");
		this.ivTT_calc (linkId, pos.left, pos.top, objElement);
	}

	this.ivTT_calc = function (linkId, left, top, objElement) {
	    var objSelf = this.objSelfIvTT;
	    if (this.ivTTtype == 'table') {
            left = 10;
            top = top + 40;
        } else if (this.ivTTtype == 'layer') {
            left = parseInt(left) - 15;
		    if ($(objElement).hasClass('isotope-item')) {
                var objOffset = $(objElement).data('isotope-item-position');
                top = objOffset.y + 5;
                left = objOffset.x + (($(objElement).outerWidth(true) - $("#ivTT_copy").outerWidth(true)) / 2) - 2;
            }
        }
		var arrowX = 'l';
		var arrowY = 't';
		var alignX = -6;
		var alignY = 0;
		var _cEx = (parseInt(left) + 25);
		var _jtW = $("#ivTT").width();
		var _wW = $(window).width();
		var _cEy = (parseInt(top) - 5);
		var _jtH = $("#ivTT").height();
		if (window.innerHeight) var _wH = window.innerHeight;
		else var _wH = $(window).height();
		var _wSt = $(window).scrollTop();
		var _fT = (parseInt(_wH) - (top - parseInt(_wSt)));
		
		if (_fT < parseInt(_wH) - _fT) {
			_cEy = (parseInt(_cEy) - parseInt(_jtH) + 20);
			arrowY = 'b';
			alignY = _jtH - 17;
		} 
		if ((parseInt(_wW) - left) < left) {
			_cEx = (parseInt(_cEx) - parseInt(_jtW) - 45);
			arrowX = 'r';
			alignX = _jtW;
		} 
		if ($.browser.safari)
		{
			_cEy = (parseInt(_cEy) + parseInt(_wSt));
		}
        $('#ivTT_copy').addClass(this.ivTTtype);
        if (this.ivTTtype == 'tooltip') {
    		$('#ivTT_arrow').css({top: (alignY)+"px", left: (alignX)+'px', backgroundImage: "url("+this.ivTTimagePath+arrowY+arrowX+".gif)"});
        }
        // iframe has to much width and height for the layer and must be recalculated before using
		//$('#ivTTiframe').css({top: (_cEy - 5)+'px', left: (_cEx - 5)+'px', width: (_jtW + 10)+'px', height: (_jtH + 10)+'px', display: 'block'});
		$('#ivTT').css({top: (_cEy)+"px", left: (_cEx)+'px'});
		//$('#ivTT').show(150);
        $('#ivTT').fadeIn(250);
	}
    
    this.ivTT_destroy = function () {
        var objSelf = this.objSelfIvTT;
        $('#ivTT').hide();
		$('#ivTT').empty();
		//$('#ivTTiframe').hide();
    }

}



