/*
 * Tooltip Class from http://firejune.com
 * modified by missu@inlinechan.com at http://ubuntu.cafe24.com/tt/
 */

var Tooltip = Class.create();
Tooltip.prototype = {
    initialize: function(elements, options){
        this.options = Object.extend({
            offsetX: 15,
            offsetY: 10,
            divX: 10,
            divY: 8,
            maxSize: 200,
            opacity: 0.8
        }, options ||
        {});
//        new Include('tooltip.css');
        this.pointerE = document.createElement('IMG');
        this.pointerE.src = '/images/arrow2.gif';
        this.pointerE.className = 'pointer';
        this.tipText = document.createElement('DIV');
        this.tipText.className = 'tooltip-html';
        this.tipE = document.createElement('DIV');
        this.tipE.id = 'item_0';
        this.tipE.className = 'tooltip';
        this.tipE.appendChild(this.pointerE);
        this.tipE.appendChild(this.tipText);
        document.body.appendChild(this.tipE);
        this.enableTip = false;
        this.eventMouseOut = this.hideTip.bindAsEventListener(this);
        this.eventMouseOver = this.showTip.bindAsEventListener(this);
        this.eventMouseMove = this.setPosition.bindAsEventListener(this);
        this.eventWindowResize = this.getPositions.bindAsEventListener(this);
        this.eventMouseClick = this.hideTipTemp.bindAsEventListener(this);
        this.getPositions();

        this.registerEvents(elements);
        Element.setOpacity(this.tipE, this.options.opacity);
    },
    registerEvents: function(elements){
        if (typeof elements == 'object') {
            for (var i = 0; i < elements.length; i++) {
                Event.observe(elements[i], "mouseout", this.eventMouseOut);
                Event.observe(elements[i], "mouseover", this.eventMouseOver);
                Event.observe(elements[i], "click", this.eventMouseClick);
            }
        }
        else {
            Event.observe(elements, "mouseout", this.eventMouseOut);
            Event.observe(elements, "mouseover", this.eventMouseOver);
            Event.observe(elements, "click", this.eventMouseClick);
        }
        Event.observe(window, 'resize', this.eventWindowResize);
        Event.observe(window, 'scroll', this.eventWindowResize);
        Event.observe(document, "mousemove", this.eventMouseMove);
        Event.observe(window, 'unload', this.unregisterEvents.bindAsEventListener(this, elements));
    },
    unregisterEvents: function(event, elements){
        if (typeof elements == 'object') {
            for (var i = 0; i < elements.length; i++) {
                Event.stopObserving(elements[i], "mouseout", this.eventMouseOut);
                Event.stopObserving(elements[i], "mouseover", this.eventMouseOver);
            }
        }
        else {
            Event.stopObserving(elements, "mouseout", this.eventMouseOut);
            Event.stopObserving(elements, "mouseover", this.eventMouseOver);
        }
        Event.stopObserving(window, 'resize', this.eventWindowResize);
        Event.stopObserving(window, 'scroll', this.eventWindowResize);
        Event.stopObserving(document, "mousemove", this.eventMouseMove);
        this.pointerE = null;
        this.tipText = null;
        this.tipE = null;
    },
    showTip: function(event){
        var element = Event.element(event);
        if (element.tagName && 
          ( element.tagName == 'HTML' || 
            element.tagName == 'DIV' ||
            element.tagName == 'BODY')) {
            return;
        }
        this.innerTitle = this.getTitle(element);
        if (this.innerTitle.text) {
            var text = this.innerTitle.text, width;
            
            var dimensions = {};
            dimensions.width = ' width="160"';
            dimensions.height = ' height="120"';
            
            this.tipText.innerHTML = text;
            var child = this.tipText.firstChild;
            if (child && child.tagName == 'IMG') {
                child.onCompleteLoadImage = function(){
                    if (!child.loaded) {
                        child.loaded = true;
                        child.src = child.loader.origSrc;
                        child.style.background = '';
                        if (child.loader.origSrc.indexOf('girafa.com') != -1) {
                            Element.addClassName(child, 'linkthumb');
                        }
                        this.tipText.style.width = child.offsetWidth + "px";
                    }
                }.bind(this);
                
                if (!child.loaded) {
                    child.loader = {
                        origSrc: child.src,
                        replaceImage: "blank.gif",
                        loadingImage: "indicator-refresh.gif",
                        newImg: new Image()
                    }
                    child.loader.newImg.src = child.loader.origSrc;
                    child.src = child.loader.replaceImage;
                    child.style.background = 'url(' + child.loader.loadingImage + ') 50% 50% no-repeat';
                    try {
                        if (child.loader.newImg.complete) {
                            child.onCompleteLoadImage();
                        }
                        else {
                            child.loader.newImg.onload = child.onCompleteLoadImage;
                        }
                    } 
                    catch (e) {
                        child.onCompleteLoadImage();
                    }
                }
            }
            else {
//                this.tipText.style.width = width + "px";
            }
            
/*            
            if (element.className.indexOf('Linkthumb') != -1 || text.indexOf('msnsearch.srv.girafa.com') != -1 || text.indexOf('add/graph.php') != -1) {
                Sound.play('naturea');
                var dimensions = {};
                if (text.indexOf('girafa.com') != -1) {
                    dimensions.width = ' width="160"';
                    dimensions.height = ' height="120"';
                }
                else 
                    if (text.indexOf('add/graph.php') != -1) {
                        dimensions.width = ' width="200"';
                        dimensions.height = ' height="125"';
                    }
                    else 
                        if (text.indexOf(('w3c.org/feed' || 'w3c.org/css' || 'w3c.org/Icons')) != -1) {
                            dimensions.width = ' width="88"';
                            dimensions.height = ' height="31"';
                        }
                        else {
                            dimensions.width = '';
                            dimensions.height = '';
                        }
                text = '<img src="' + text + '"' + dimensions.width + dimensions.height + ' class="tooltipimg" alt="" />';
            }
            else 
                if (element.tagName == 'IMG' && element.src.indexOf('recent.thumb.jpg') != -1) {
                    Sound.play('clic_' + Math.floor(Math.random() * 3));
                    text = text.split(' :: ');
                    text = text[0] + ' :: <strong>' + text[1] + '</strong> ' + text[2] + '</div>';
                    width = this.options.maxSize;
                }
                else 
                    if (text.indexOf('techorati-widget') != -1) {
                        text = $('techorati-widget').innerHTML;
                        width = 74;
                        Sound.play('pop');
                    }
                    else 
                        if (text.indexOf('ratings') != -1) {
                            element = $(element).up(2).down('div.js-kit-rating').down();
                            if (!element || !element.innerHTML) {
                                return;
                            }
                            element.down().style.marginTop = '5px';
                            text = element.innerHTML;
                            Sound.play('pop');
                            width = 45;
                        }
                        else {
                            Sound.play('pop');
                            var length = text.length, towByte = 0;
                            for (var i = 0; i < length; i++) {
                                if (text.charCodeAt(i) > 127) {
                                    towByte++;
                                }
                            }
                            width = (length * 7.5) + (towByte * 5);
                            width = (width > this.options.maxSize) ? this.options.maxSize : width;
                        }
            this.tipText.innerHTML = text;
            var child = this.tipText.firstChild;
            if (child && child.tagName == 'IMG') {
                child.onCompleteLoadImage = function(){
                    if (!child.loaded) {
                        child.loaded = true;
                        child.src = child.loader.origSrc;
                        child.style.background = '';
                        if (child.loader.origSrc.indexOf('girafa.com') != -1) {
                            Element.addClassName(child, 'linkthumb');
                        }
                        this.tipText.style.width = child.offsetWidth + "px";
                    }
                }
.bind(this);
                if (!child.loaded) {
                    child.loader = {
                        origSrc: child.src,
                        replaceImage: "/images/blank.gif",
                        loadingImage: "/images/indicator-refresh.gif",
                        newImg: new Image()
                    }
                    child.loader.newImg.src = child.loader.origSrc;
                    child.src = child.loader.replaceImage;
                    child.style.background = 'url(' + child.loader.loadingImage + ') 50% 50% no-repeat';
                    try {
                        if (child.loader.newImg.complete) {
                            child.onCompleteLoadImage();
                        }
                        else {
                            child.loader.newImg.onload = child.onCompleteLoadImage;
                        }
                    } 
                    catch (e) {
                        child.onCompleteLoadImage();
                    }
                }
            }
            else {
                this.tipText.style.width = width + "px";
            }
*/
            if ( this.fartTime(event) )
              this.play('fart');
            else
              this.play('click');
            this.enableTip = true;
        }
        
        Event.stop(event);
    },
    fartTime: function(event) {
      var x, y;
      x = Event.pointerX(event);
      y = Event.pointerY(event);
      
      if ( ((x+y) % 13) == 1 )
        return true;
      else
        return false;
    },
    getTitle: function(element){
        var get = {
            text: element.title,
            node: element
        };
        if (!get.text) {
            get = {
                text: element.parentNode.title,
                node: element.parentNode
            };
        }
        if (!get.text && element.tagName == 'IMG') {
            get = {
                text: element.alt,
                node: element
            };
        }

/*
        if (get.text && get.node.tagName != 'IMG') {
            get.node.title = '';
        }
*/
        if ( get.text )
            get.node.title = '';
        return get;
    },
    getPositions: function(){
        this.winWidth = window.document.body.offsetWidth - this.options.offsetX;
//        this.winHeight = Position.getPageSize().window.height + Position.scrollY() - this.options.offsetY;
        this.winHeight = window.document.body.offsetHeight - this.options.offsetY;
    },
    setPosition: function(event){
        if (this.enableTip) {
            var pointer = {
                x: Event.pointerX(event),
                y: Event.pointerY(event)
            };
            var rightEdge = this.winWidth - pointer.x;
            var bottomEdge = this.winHeight - pointer.y;
            var styles = {
                top: this.tipText.offsetHeight,
                left: this.tipE.offsetWidth,
                visibility: 'visible'
            };
            var visibility = "visible";
            if (rightEdge < styles.left) {
                styles.left = pointer.x - styles.left + "px";
                visibility = "hidden";
            }
            else {
                styles.left = pointer.x + this.options.offsetX - this.options.divX + "px";
            }
            if (bottomEdge < styles.top) {
                styles.top = pointer.y - styles.top - this.options.offsetY + "px";
                visibility = "hidden";
            }
            else {
                styles.top = pointer.y + this.options.offsetY + this.options.divY + "px";
            }
            this.pointerE.style.visibility = visibility;
            Element.setStyle(this.tipE, styles);
        }
    },
    hideTipTemp: function(event){
      var elm = Event.element(event);
      
      if( elm.id == 'sidebar_handle_img')
        this.hideTip(event);
    },
    hideTip: function(event){
        if (this.enableTip) {
            this.innerTitle.node.title = this.innerTitle.text;
            this.enableTip = false;
            this.tipE.style.visibility = "hidden";
            this.pointerE.style.visibility = "hidden";
        }
        Event.stop(event);
    },
    play: function(id) {
        soundManager.play(id);
    }
};

Event.observe(window, 'load', function(){
  new Tooltip(['header', 'nav', 'content']);
});

