var PopUp1 = new Class({
    Implements: Options,
    options: {
        content: null,
        container: $(document.body),
        rafting: 10,
        positions: "auto",
        width: "auto"
    },
    element: null,
    Obj: null,
    ObjText: null,
    ObjPointerBottom: null,
    ObjPointerTop: null,
    ObjPointerLeft: null,
    ObjPointerRight: null,
    time_hide: 0,
    initialize: function(element, options) {
        this.element = element;
        if (this.element == null || this.element == undefined)
            return false;
        this.setOptions(options);

        if (this.options.container == null)
            this.options.container = $(document.body);

        this.CreateUI();
        this.Refresh();

        this.Obj.addEvents({
            "mouseout": function(a) {
                return function() {
                    a.time_hide = setTimeout(function(a2) {
                        return function() {
                            a2.Hide();
                        }
                    } (a), 80);
                }
            } (this)
        });
        this.Obj.addEvents({
            "mousemove": function(a) {
                return function() {
                    clearTimeout(a.time_hide);
                }
            } (this)
        });
    },
    CreateUI: function() {
        if (this.options.width == "auto")
            this.options.width = "";
        this.Obj = new Element("div", {
            "class": "PopUp1",
            "styles": {
                "width": this.options.width
            }
        });
        this.ObjText = new Element("div", {
            "html": this.options.content,
            "class": "PopUp1-text"
        });
        this.ObjPointerBottom = new Element("div", {
            "class": "PopUp1-pointer-bottom"
        });
        this.ObjPointerTop = new Element("div", {
            "class": "PopUp1-pointer-top"
        });
        this.ObjPointerLeft = new Element("div", {
            "class": "PopUp1-pointer-left"
        });
        this.ObjPointerRight = new Element("div", {
            "class": "PopUp1-pointer-right"
        });
        this.ObjText.inject(this.Obj);
        this.ObjPointerBottom.inject(this.Obj);
        this.ObjPointerTop.inject(this.Obj);
        this.ObjPointerLeft.inject(this.Obj);
        this.ObjPointerRight.inject(this.Obj);
        this.Obj.inject($(document.body), "top");
    },
    Refresh: function() {
        var cont_size = this.options.container.getSize();
        var cont_scroll_size = this.options.container.getScroll();
        var pos_text = "center";
        var abc = null;

        if (this.options.positions == "auto") {
            var x1 = cont_scroll_size.y;
            var temp_top = this.GetElementCoordinates("top");
            var temp_bottom = this.GetElementCoordinates("bottom");
            var temp_left = this.GetElementCoordinates("left");
            var temp_right = this.GetElementCoordinates("right");
            if ((temp_top.y - cont_scroll_size.y) > 0) {
                abc = temp_top;
                pos_text = "top";
            }
            else if (((cont_scroll_size.y + cont_size.y) - (temp_bottom.y + temp_bottom.height)) > 0) {
                abc = temp_bottom;
                pos_text = "bottom";
            }
            else if ((temp_left.x - cont_scroll_size.x) > 0) {
                abc = temp_left;
                pos_text = "left";
            }
            else if (((cont_scroll_size.x + cont_size.x) - (temp_right.x + temp_right.width)) > 0) {
                abc = temp_right;
                pos_text = "right";
            }
            else {
                abc = this.GetElementCoordinates("center");
                pos_text = "center";
            }
        }
        else {
            abc = this.GetElementCoordinates(pos_text);
        }

        this.ObjPointerLeft.setStyle("display", "none");
        this.ObjPointerRight.setStyle("display", "none");
        this.ObjPointerTop.setStyle("display", "none");
        this.ObjPointerBottom.setStyle("display", "none");
        if (pos_text == "left") {
            this.ObjPointerRight.setStyle("display", "block");
        }
        else if (pos_text == "right") {
            this.ObjPointerLeft.setStyle("display", "block");
        }
        else if (pos_text == "top") {
            this.ObjPointerBottom.setStyle("display", "block");
        }
        else if (pos_text == "bottom") {
            this.ObjPointerTop.setStyle("display", "block");
        }
        else {
            this.ObjPointerBottom.setStyle("display", "block");
        }
        this.Obj.setStyles({
            "left": abc.x + "px",
            "top": abc.y + "px"
        });
    },
    GetElementCoordinates: function(p) {
        var obj_size = this.Obj.getSize();
        var size = this.element.getSize();
        var pos = this.element.getPosition();
        var ret = new Object();
        if (p == "left") {
            ret = {
                "x": pos.x + this.options.rafting - obj_size.x - this.ObjPointerLeft.getStyle("width").toInt(),
                "y": pos.y + size.y / 2 - this.ObjPointerLeft.getStyle("top").toInt(),
                "height": obj_size.y,
                "width": obj_size.x + this.ObjPointerTop.getStyle("width").toInt()
            };
        }
        else if (p == "right") {
            ret = {
                "x": pos.x + size.x - this.options.rafting + this.ObjPointerRight.getStyle("width").toInt(),
                "y": pos.y + size.y / 2 - this.ObjPointerRight.getStyle("top").toInt(),
                "height": obj_size.y,
                "width": obj_size.x + this.ObjPointerTop.getStyle("width").toInt()
            };
        }
        else if (p == "top") {
            ret = {
                "x": pos.x + size.x / 2 - this.ObjPointerTop.getStyle("left").toInt(),
                "y": pos.y + this.options.rafting - this.ObjPointerTop.getStyle("height").toInt() - obj_size.y,
                "height": obj_size.y + this.ObjPointerTop.getStyle("height").toInt(),
                "width": obj_size.x
            };
        }
        else if (p == "bottom") {
            ret = {
                "x": pos.x + size.x / 2 - this.ObjPointerBottom.getStyle("left").toInt(),
                "y": pos.y + size.y - parseInt(this.options.rafting) + this.ObjPointerBottom.getStyle("height").toInt(),
                "height": obj_size.y + this.ObjPointerTop.getStyle("height").toInt(),
                "width": obj_size.x
            };
        }
        else if (p == "center") {
            ret = {
                "x": pos.x + size.x / 2 - this.ObjPointerTop.getStyle("left").toInt(),
                "y": pos.y + size.y / 2 - this.ObjPointerTop.getStyle("height").toInt() - obj_size.y,
                "height": obj_size.y + this.ObjPointerTop.getStyle("height").toInt(),
                "width": obj_size.x
            };
        }
        else {
            ret = {
                "x": pos.x + size.x / 2 - this.ObjPointerTop.getStyle("left").toInt(),
                "y": pos.y + size.y / 2 - this.ObjPointerTop.getStyle("height").toInt() - obj_size.y,
                "height": obj_size.y + this.ObjPointerTop.getStyle("height").toInt(),
                "width": obj_size.x
            };
        }
        return ret;
    },
    Show: function() {
        this.Refresh();
        this.Obj.setStyle("visibility", "visible");
        //this.Obj.fade("in");
        //document.title = this.TEMP;
        clearTimeout(this.time_hide)
    },
    Hide: function() {
        this.time_hide = setTimeout(function(a2) {
            return function() {
                a2.Hide2();
            }
        } (this), 80);
    },
    Hide2: function() {
        this.Obj.setStyle("visibility", "hidden");
        //this.Obj.fade("out");
    },
    SetContent: function(Html) {
        this.options.content = Html;
        this.ObjText.set("html", Html);
    },
    IsShow: function() {
        if (this.Obj.setStyle("visibility") == "visible")
            return true;
        else
            return false;
    }
});