/**
*   Update: 10/29/2008 -- add vertical/center positioning, replace the old tip in menu.js
*   Example: Fansub|home/ Music|playlist - hEdge & vCenter tip
*/
LBP.Tip = new Hash({
    presets: {
        tipCss: 'ltip-content',
        idx: 900,
        opacity: 0.9,
        padding: 8,
        dumper: 'media-tip-dump',
        position: 'hEdge', //hEdge || vCenter || vEdge
        margin: 0
    },
    
    assign: function(objs, options){
        this.options = $merge(this.presets, options || {});
        if(!this.dump) {
                this.dump = new Element('div', { id: this.options.dumper});
                this.dump.set('styles', { position: 'absolute', zIndex: -2, opacity: 0});
                $('dummydt').adopt(this.dump);
        }
        objs.each(function(obj){
            if(!obj.retrieve('tipwidth')) {
                var tip = this.setUp(obj);
                obj.addEvents({
                    'mouseenter' : function(event){
                        this.show(obj, tip);
                    }.bind(this),
                    'mouseleave' : function(event){
                        this.hide(obj, tip);
                    }.bind(this)
                });
                obj.store('alignment', this.options.position);
                obj.store('margin', this.options.margin);
            }
        }.bind(this));
    },
    setUp: function(obj) {
        var tip = obj.getNext('div.'+this.options.tipCss);
        if(!tip) {
            tip = new Element("div", { 'class': this.options.tipCss});
            var tipinner = new Element("div", { 'class': this.options.tipCss+'-inner'});
            tip.adopt(tipinner);
            var tipCnt = obj.get('title');            
            if(tipCnt !='') {
                tipinner.set('html', tipCnt);
                obj.removeProperty('title');
            } else { tip.set({   'html': 'Chưa có nội dung'}); }
            tip.inject(obj, 'after');
        }
        var width = this.options.width;
        var height = this.options.height;
        
        if(!width || !height) {
            tip.set('styles', { display: 'block', zIndex: this.options.idx, opacity: 0, position: 'absolute'});
            var size = tip.getSize();
            width = width || size.x;
            height = height || size.y;
        }
        tip.set('styles', { display: 'none', zIndex: this.options.idx, opacity: this.options.opacity, position: 'absolute'});
        obj.store('tipwidth', width);
        obj.store('tipheight', height);
        return tip;
    },
    show: function(obj, tip) {
        var pos = obj.getPosition();
        var size = obj.getSize();
        var winScroll = window.getScroll();
        var winSize = window.getSize();
        var tipSize = { x: obj.retrieve('tipwidth').toInt(), y: obj.retrieve('tipheight').toInt() };
        var fn = obj.retrieve('alignment')+'Align';        
        pos = this[fn].run([obj, tip, winSize, winScroll, size, tipSize, pos],this);        
        tip.set('styles', { top: pos.y+'px', left: pos.x+'px', display: 'block'});
    },
    vCenterAlign: function(obj, tip, winSize, winScroll, size, tipSize, pos) {
        var margin = obj.retrieve('margin');
        if((pos.y-winScroll.y) < (tipSize.y+margin)){
            pos.y = pos.y + size.y + margin;
        } else {
            pos.y = pos.y - tipSize.y - margin;
        }
        var com = (tipSize.x - size.x)/2;
        if((winSize.x-pos.x+winScroll.x) < com) {
            pos.x = pos.x + size.x;
        } else if( (pos.x-winScroll.x) >= com ) {
            pos.x = pos.x - com;
        }
        return pos;
    },
    hEdgeAlign: function(obj, tip, winSize, winScroll, size, tipSize, pos) {
        if((winSize.x-(pos.x+size.x-winScroll.x)) < tipSize.x){
            pos.x = pos.x - tipSize.x;
        } else {
            pos.x += size.x;
        }
        if((winSize.y-(pos.y+size.y/2-winScroll.y)) < tipSize.y){
            pos.y = pos.y - tipSize.y+size.y;
        }
        return pos;
    },
    hide: function(obj, tip) {
        tip.set('styles', {display: 'none'} );
    }
});