﻿/**
 * 123mua eBizTooltips Class.
 * @version 0.1a.
 * Using mootools version 1.2.
 */
 
var eBizTooltips = new Class({
	
	Implements: [Events, Options],
    
    options: {
    	selector: 'a.profile',
    	popupId: 'popup',
    	popupContentId: 'popup-content',
    	popupLoading: 'popup-loading',
    	widthPopup: 300
    },
    
    initialize: function(options){
        this.setOptions(options);
        
        this.flag = false;
        this.timer = null;
        this.currenId = null;

        $$(this.options.selector).each(function(item, index){            
            item.addEvents({  
                'mouseover': function(evt){                    
                    this.display(item);
                }.bind(this),        
                'mouseleave': function(evt){
                    this.hide();
                }.bind(this)
            });
        }.bind(this));
        
        $(this.options.popupId).set({
            'events': {
                'mouseover': function(evt){
                    clearTimeout(this.timer);
                }.bind(this),
                'mouseleave': function(evt){
                    $(this.options.popupId).setStyles({
                        'display': 'none'
                    });
                }.bind(this)
            }   
        });
    },
    
    waiting: function()
    {     
        if(!this.flag)
        {   
            $(this.options.popupId).setStyles({
                'display': 'none'
            });
        }
    },
    
    getData: function()
    {
        return "Must override this function to get data";
    },
    
    display: function(item)
    {   
        if($defined(this.timer))
        {
            clearTimeout(this.timer);
        }
        
        this.flag = true;
        
        var de = document.documentElement;
        var w = self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
        var parentArea = w - item.getSize().x - item.getPosition().x;
        var positionY = item.getPosition().y;
        
        if(parentArea > (this.options.widthPopup + 30))
        {
            var positionX = item.getPosition().x + item.getSize().x + 10;
        }
        else
        {
            var positionX = item.getPosition().x - (this.options.widthPopup) - 15;
        }
        
        $(this.options.popupId).setStyles({
            'left': positionX + 'px',
            'top': positionY + 'px',
            'display': 'block'
        });
        
        var newId = item.getProperty('cid').toInt();
        
        if(this.currenId != newId)
        {
            this.currenId = newId;
            $(this.options.popupContentId).empty();
            
            var values = $H({
                uid: item.getProperty('cid')
            }).toQueryString();
            
            var request = new Request.HTML({
    		    url: "/API/PopupProfiles.ashx",    		
    		    evalScripts: true,
    		    async: true,
    		    onRequest: function(){ 
                    $(this.options.popupLoading).setStyle('display', 'block');
    		    }.bind(this),  	
    		    update: $(this.options.popupContentId), 
    		    onComplete: function(html){
    			    $(this.options.popupLoading).setStyle('display', 'none');
    		    }.bind(this)
    	    }).send(values);
        }     
    },
    
    hide: function()
    {        
        this.flag = false;
        this.timer = setTimeout(this.waiting.bind(this), 500);    
    }       
});

/**
 * Quá trình cập nhật
 *
 * @Linhlc - 29/11/2008
 * @Anpvt - 01/12/2008
 * 
 */