var WebPartPlanningV1House = new Class({
    Implements: Options,
    options: {
        Id: null,
        StartFloor: 0,
        PageOrder: 0
    },
    Floors: Array(0),
    SelectFloorIndex: null,
    initialize: function(options) {
        this.setOptions(options);

        this.FloorContainer = $(this.options.Id).getElement(".floor");
        this.FloorItemsContainer = $(this.options.Id).getElement(".floors");

        this.SelectFloorIndex = this.options.StartFloor;
    },
    AddFloor: function(Name, FloorImg, FloorImgWidth, FloorImgHeight, Offices) {
        var l = this.Floors.length;

        this.Floors[l] = new WebPartPlanningV1Floor({
            Id: this.options.Id,
            Name: Name,
            FloorImg: FloorImg,
            FloorImgWidth: FloorImgWidth,
            FloorImgHeight: FloorImgHeight,
            Offices: Offices,
            Index: l,
            PageOrder: this.options.PageOrder
        });
        this.Floors[l].options.Parent = this;
        this.Floors[l].CreateContext();
        if (l == 0 && this.SelectFloorIndex === null) {
            this.SelectFloorIndex = 0;
            this.Floors[l].Show();
        }
        else if (this.SelectFloorIndex === l) {
            this.Floors[l].Show();
        }
    },
    SelectFloor: function(Index) {
        if (this.SelectFloorIndex !== null) {
            this.Floors[this.SelectFloorIndex].Hide();
        }
        this.SelectFloorIndex = Index;
        this.Floors[this.SelectFloorIndex].Show();
    }
});

var WebPartPlanningV1Floor = new Class({
    Implements: Options,
    options: {
        Id: null,
        Name: null,
        FloorImg: null,
        FloorImgWidth: null,
        FloorImgHeight: null,
        Offices: null,
        Parent: null,
        Index: null,
        PageOrder: 0
    },
    OfficeObjs: Array(),
    OfficeTextObjs: Array(),
    OfficeSquaresObjs: Array(),
    initialize: function(options) {
        this.setOptions(options);
        return true;
    },
    CreateContext: function() {
        this.MapName = this.options.Id + "_map_" + this.options.Index;
        this.ObjBg = new Element("img", {
            "class": "floor_bg",
            "src": this.options.FloorImg
        });
        this.ObjImgLinks = new Element("img", {
            "usemap": "#" + this.MapName,
            "class": "floor_img_links",
            "src": "/themes/default/images/pix.gif"
        });
        this.ObjMap = new Element("map", { "name": this.MapName });
        this.ObjBg.inject(this.options.Parent.FloorContainer);
        this.ObjImgLinks.inject(this.options.Parent.FloorContainer);
        this.ObjMap.inject(this.options.Parent.FloorContainer);

        this.ObjImgLinks.setStyles({
            "width": this.options.FloorImgWidth,
            "height": this.options.FloorImgHeight
        });

        this.options.Parent.FloorContainer.setStyles({
            "width": this.options.FloorImgWidth,
            "height": this.options.FloorImgHeight
        });

        this.ObjItem = new Element("div", {
            "class": "item",
            "text": this.options.Name,
            "events": {
                "click": function(a) {
                    return function() {
                        a.options.Parent.SelectFloor(a.options.Index);
                    }
                } (this)
            }
        });
        this.ObjItem.inject(this.options.Parent.FloorItemsContainer);

        var i = 0;
        for (i = 0; i < this.options.Offices.length; i++) {
            var l = this.OfficeObjs.length;
            var l2 = this.OfficeTextObjs.length;

            this.OfficeObjs[l] = new Element("img", {
                "src": "/themes/default/images/pix.gif",
                "class": "office_img_over"
            });
            if (parseInt(this.options.Offices[i].is_take) == 0) {
                //alert(parseInt(this.options.Offices[i].is_take));
                this.OfficeObjs[l].src = this.options.Offices[i].image_take;
            }
            this.OfficeObjs[l].inject(this.options.Parent.FloorContainer);
            this.OfficeObjs[l].setStyles({
                "left": this.options.Offices[i].left_office + "px",
                "top": this.options.Offices[i].top_office + "px",
                "width": this.options.Offices[i].w + "px",
                "height": this.options.Offices[i].h + "px"
            });
            if (parseInt(this.options.Offices[i].is_take) == 1) {
                this.OfficeObjs[l].setStyles({
                    "display": "none"
                });
            }
            else {
                this.OfficeObjs[l].popup = new PopUp1(this.OfficeObjs[l], {
                    "content": this.options.Offices[i].description,
                    "positions": "auto",
                    "rafting": 20,
                    "width": 250
                });
            }
            new Asset.image(this.options.Offices[i].image_over);
            //alert(this.options.Offices[i].left_office + " " + this.options.Offices[i].top_office);
            var area = new Element("area", {
                "shape": "poly",
                "coords": this.options.Offices[i].office_area_link,
                "alt": "",
                "href": this.options.PageOrder + '?id=' + this.options.Offices[i].id
            });
            area.inject(this.ObjMap);
            area.addEvents({
                "mouseenter": function(a, b, c) {
                    return function() {
                        if (c.is_take == 0) {
                            b.src = c.image_over;
                            b.popup.Show();
                        }
                    }
                } (this, this.OfficeObjs[l], this.options.Offices[i]),
                "mouseleave": function(a, b, c) {
                    return function() {
                        if (c.is_take == 0) {
                            b.src = c.image_take;
                            b.popup.Hide();
                        }
                        else
                            b.src = "/themes/default/images/pix.gif";
                    }
                } (this, this.OfficeObjs[l], this.options.Offices[i])
            });

            this.OfficeTextObjs[l2] = new Element("div", {
                "class": "office_text",
                "text": this.options.Offices[i].name,
                "styles": {
                    "left": this.options.Offices[i].left_office_text + "px",
                    "top": this.options.Offices[i].top_office_text + "px"
                }
            });
            this.OfficeTextObjs[l2].inject(this.options.Parent.FloorContainer);

            if (this.options.Offices[i].text_size == 1)
                this.OfficeTextObjs[l2].addClass("lite");

            if (this.options.Offices[i].squares.length > 0) {
                var i2 = 0;
                for (i2 = 0; i2 < this.options.Offices[i].squares.length; i2++) {
                    var l3 = this.OfficeSquaresObjs.length;
                    this.OfficeSquaresObjs[l3] = new Element("div", {
                        "class": "square",
                        "html": this.options.Offices[i].squares[i2].name + " м<sup>2</sup>",
                        "styles": {
                            "left": this.options.Offices[i].squares[i2].x + "px",
                            "top": this.options.Offices[i].squares[i2].y + "px"
                        }
                    });
                    this.OfficeSquaresObjs[l3].inject(this.options.Parent.FloorContainer);
                }
            }
        }
    },
    Show: function() {
        this.ObjBg.setStyle("display", "block");
        this.ObjImgLinks.setStyle("display", "block");
        this.ObjItem.addClass("sel");

        var i = 0;
        for (i = 0; i < this.OfficeObjs.length; i++) {
            this.OfficeObjs[i].setStyle("display", "block");
        }
        i = 0;
        for (i = 0; i < this.OfficeTextObjs.length; i++) {
            this.OfficeTextObjs[i].setStyle("display", "block");
        }
        i = 0;
        for (i = 0; i < this.OfficeSquaresObjs.length; i++) {
            this.OfficeSquaresObjs[i].setStyle("display", "block");
        }
    },
    Hide: function() {
        this.ObjBg.setStyle("display", "none");
        this.ObjImgLinks.setStyle("display", "none");
        this.ObjItem.removeClass("sel");

        var i = 0;
        for (i = 0; i < this.OfficeObjs.length; i++) {
            this.OfficeObjs[i].setStyle("display", "none");
        }
        i = 0;
        for (i = 0; i < this.OfficeTextObjs.length; i++) {
            this.OfficeTextObjs[i].setStyle("display", "none");
        }
        i = 0;
        for (i = 0; i < this.OfficeSquaresObjs.length; i++) {
            this.OfficeSquaresObjs[i].setStyle("display", "none");
        }
    }
});

