Arboreus.Tooltip = function (container, selector) {
    this.container = container;
    this.selector = selector;

    this.tooltip = jQuery(this.container);

    this.textContainer = jQuery(this.container).find('.container');

    this.bindControlls();
}

Arboreus.Tooltip.forceHide = function() {
    jQuery('#tooltip-template').addClass('hidden');
}

jQuery.extend(Arboreus.Tooltip.prototype, {
    tooltip: null,
    container: null,
    selector: null,

    bindControlls: function () {
        jQuery(this.selector).each(function (index, el) {
            var element = jQuery(el);

            el.tooltip = this;

            var className = element.attr('class');

            if (element.attr('unbind') === 'unbind') {
                element.unbind('mouseover');
                element.unbind('mouseout');

                if (className.indexOf('customSelect') > -1) {
                    element.parent().find('.select-area').unbind('mouseover');
                    element.parent().find('.select-area').unbind('mouseout');
                }

                return;
            }

            if (className.indexOf('customSelect') > -1) {
                var selectArea = element.parent().find('.select-area');

                if (selectArea.length > 0) {
                    selectArea.get(0).tooltip = this;
                }

                selectArea.attr('rel', element.attr('rel'));
                selectArea.attr('rev', element.attr('rev'));
                selectArea.attr('dx', element.attr('dx'));
                selectArea.attr('dy', element.attr('dy'));
                selectArea.attr('tooltipWidth', element.attr('widthTooltip'));
                selectArea.attr('fixedWidth', element.attr('fixedWidth'));
                selectArea.attr('alterPosition', element.attr('alterPosition'));
                selectArea.attr('mouseOverDisabled', element.attr('mouseOverDisabled'));

                selectArea.mouseover(this.show.scope(selectArea.get(0)));
                selectArea.mouseout(this.hide.scope(selectArea.get(0)));
            }

            if (element.attr('mouseOverDisabled') == '1') {
                el.showTooltip = this.show.scope(el);
                el.hideTooltip = this.hide.scope(el);
            } else {
                element.mouseover(this.show.scope(el));
                element.mouseout(this.hide.scope(el));
            }
        }.scope(this))
    },

    hide: function (e) {
        var el = this;
        var element = jQuery(el);

        if (!el.tooltip) {
            return;
        }

        var self = el.tooltip;

        self.tooltip.addClass('hidden');

        element.parent().removeClass('hover');
    },

    showBottom: function(el) {
        var element = jQuery(el);

        var dx = 0;
        var dy = 0;

        if (parseInt(element.attr('dx'))) {
            dx = parseInt(element.attr('dx'));
        }

        if (parseInt(element.attr('dy'))) {
            dy = parseInt(element.attr('dy'));
        }

        if (element.attr('fixedWidth')) {
            this.tooltip.width(200);
        } else {
            this.tooltip.width('auto');
        }

        if (parseInt(element.attr('tooltipWidth'))) {
            this.tooltip.width(parseInt(element.attr('tooltipWidth')));
        }

        var textContainerHeight = this.textContainer.attr('offsetHeight');

        if (textContainerHeight) {
            this.tooltip.css({'height': textContainerHeight + 'px'});
        }

        var fault = 0;
        if (this.tooltip.width() > element.width() && element.attr('dxcenter')) {
            fault = (this.tooltip.width() - element.width()) / 2 + 5;
        }

        this.tooltip.css({
            'left': Utils.getLeftPos(el) + dx - fault + 'px',
            'top': (Utils.getTopPos(el) + dy + el.offsetHeight + 5) + 'px'
        });

        jQuery('#tooltip-template .corner.png').css('top', -5 + 'px');
        jQuery('#tooltip-template .corner.png').addClass('up');
    },

    showTop: function(el) {
        var element = jQuery(el);

        var dx = 0;
        var dy = 0;

        if (parseInt(element.attr('dx'))) {
            dx = parseInt(element.attr('dx'));
        }

        if (parseInt(element.attr('dy'))) {
            dy = parseInt(element.attr('dy'));
        }

        if (element.attr('fixedWidth')) {
            this.tooltip.width(200);
        } else {
            this.tooltip.width('auto');
        }

        if (parseInt(element.attr('tooltipWidth'))) {
            this.tooltip.width(parseInt(element.attr('tooltipWidth')));
        }

        var textContainerHeight = this.textContainer.attr('offsetHeight');

        if (textContainerHeight) {
            this.tooltip.css({'height': textContainerHeight + 'px'});
        }

        var fault = 0;
        if (this.tooltip.width() > element.width() && element.attr('dxcenter')) {
            fault = (this.tooltip.width() - element.width()) / 2 + 5;
        }

        var elPosition = jQuery(el).offset();

        this.tooltip.css({
            'position': 'absolute',
            'left': (elPosition.left + dx - fault) + 'px',
            'top': (elPosition.top + dy - this.tooltip.height() - 13) + 'px'
        });

        jQuery('#tooltip-template .corner.png').css('top', textContainerHeight + 'px');
        jQuery('#tooltip-template .corner.png').removeClass('up');
    },

    show: function() {
        var el = this;
        var element = jQuery(el);

        if (!el.tooltip) {
            return;
        }

        if (typeof element.attr('rev') === 'undefined' || element.attr('rev').length === 0) {
            return;
        }

        var self = el.tooltip;

        element.parent().addClass('hover');

        self.tooltip.removeClass('hidden');

        self.textContainer.html(element.attr('rev'));

        if (element.attr('alterPosition')) {
            switch (element.attr('alterPosition')) {
                case 'top':
                    self.showTop(el);
                    break;

                case 'bottom':
                    self.showBottom(el);
                    break;
            }
        } else {
            element.css({
                'cursor': 'pointer'
            })

            var textContainerHeight = self.textContainer.attr('offsetHeight');
            
            if ((Utils.getTopPos(el) - textContainerHeight - 13) < 0) {
                self.showBottom(el);
            } else {
                self.showTop(el);
            }
        }
    }
});
