/**
 * @property jQuery[] nodes
 * @property CLASS_IMAGE[] images
 * @property CLASS_TOOLTIP tooltip
 */
gc.appendHelper("rooms_lightbox", {

    'NODES': "li.room div.roomdesc a",
    'NODE_TOOLTIP': "#rooms_lb",    

    'run': (function() {
        this.nodes   = $(this.NODES);
        this.tooltip = new this.CLASS_TOOLTIP(this.NODE_TOOLTIP);
        this.createImages();
    }),
    
    'createImages': (function() {
        this.images = [];
        var len = this.nodes.length;
        for (var i = 0; i < len; i++) {
            var node  = this.nodes.eq(i);
            var image = new this.CLASS_IMAGE(node, this.tooltip);
            this.images.push(image);
        }
    }),
    
    '__destruct': (function() {
        var len = this.images.length;
        for (var i = 0; i < len; i++) {
            this.images.destroy();
        }
    }),

    /**
     * @property jQuery node
     * @property CLASS_TOOLTIP tooltip
     * @property jQuery img
     * @property string href
     * @property string status ("wait", "show", "hide")
     * @property int x
     * @property int y
     * 
     */
    'CLASS_IMAGE': go.Class(null, {
    
        'DELAY': 200,

        '__construct': (function(node, tooltip) {
            this.node    = $(node);
            this.tooltip = tooltip;
            this.img     = this.node.find("img");
            this.href    = this.node.attr("href");
            this.status  = "hide";
            this.initEvents();
            this.timeout = null;
            this.x       = 0;
            this.y       = 0;
        }),
        
        'initEvents': (function() {
            this.node.mouseover(this.onMouseOver);
            this.node.mouseout(this.onMouseOut);
            this.node.mousemove(this.onMouseMove);
            this.node.click(this.onClick);
        }),

        'onMouseOver_bind': (function(e) {
            this.status  = "wait";
            this.x = e.pageX;
            this.y = e.pageY;                  
            this.timeout = setTimeout(this.show, this.DELAY);
        }),
        
        'onMouseOut_bind': (function(e) {
            if (this.status == "wait") {
                clearTimeout(this.timeout);
                this.timeout = null;
            } else if (this.status == "show") {
                this.tooltip.hide();            
            }
            this.status = "hide";            
        }),
        
        'onMouseMove_bind': (function(e) {        
            this.x = e.pageX;
            this.y = e.pageY;
            if (this.status == "show") {
                this.tooltip.setCoord(this.x, this.y);
            }
        }),
        
        'onClick_bind': (function(e) {
            e.preventDefault();
        }),
        
        'show_bind': (function() {
            if (this.status == "wait") {
                this.status  = "show";
                this.timeout = null;
                this.tooltip.setImage(this.href, this.x, this.y);
            }
        }),

        'eoc': null
    }),

    /**
     * @property jQuery node
     * @property jQuery image
     * @property bool visible
     * @property string src
     * @property int x
     * @property int y
     * @property int width
     * @property int dx
     * @property int dy
     */
    'CLASS_TOOLTIP': go.Class(null, {  
  
        'DELTA_X' : 10,
        'DELTA_Y' : 20,
    
        '__construct': (function(node) {
            this.node    = $(node);
            this.indiv   = this.node.find(".in_room_lb");
            this.indiv.html("");
            this.image   = null;
            this.visible = false;
            this.src     = "";
            this.x       = 0;
            this.y       = 0;
            this.width   = 0;
            this.dx      = 0;
            this.dy      = 0;
            this.height  = 0;
            $("body").append(this.node);
        }),
        
        'show': (function() {
            if (!this.visible) {
                this.node.show();
                this.visible = true;
            }
        }),
        
        'hide': (function() {
            if (this.visible) {
                this.node.hide();
                this.indiv.html("");
                this.image = null;                
                this.visible = false;
            }
        }),
        
        'setImage': (function(src, x, y) {
            this.show();
            var html = '<img src="' + src + '" alt="">';
            this.image = $(html);
            this.image.appendTo(this.indiv);
            this.src = src;
            this.height = this.image.height();		
            if (this.height > 50) {
                this.setCoord(x, y);
            } else {
                var _this = this;
                this.image.bind("load", (function() {_this.onLoad(x, y);}));
            }
        }),
        
        'onLoad': (function(x, y) {        
            this.height = this.image.height();
            this.setCoord(x, y);
        }),
        
        
        'setCoord': (function(x, y) {
            this.x = x;
            this.y = y;
            var ix = x + this.DELTA_X;
            
            var etop    = this.y - $(document).scrollTop();
            var dheight = $.browser.opera? window.innerHeight : $(window).height();
            var ebottom = dheight - etop;
            
            if (ebottom > etop) {
                if (ebottom >= this.height) {
                    var iy = y + this.DELTA_Y;
                } else {
                    var iy = y - etop;
                }
            } else {
                if (etop >= this.height) {
                    var iy = y - this.height - this.DELTA_Y;
                } else {
                    var iy = y - etop;
                }
            }
            
            this.node.css("left", ix);
            this.node.css("top", iy);
        }),
    
        'eoc': null
    }),
    
    'eoc': null
});

