var PhotoliteParts = new Class({
    Implements: Options,
    options: {
        scrollingView: true
    },
    initialize: function(options) {
        this.setOptions(options);
        this.Parts = $$(".photolite_part");
        if (this.Parts.length > 0) {
            for (var i = 0; i < this.Parts.length; i++) {
                this.Parts[i].plp = new Photolite({
                    scrollingView: this.options.scrollingView,
                    container: this.Parts[i]
                });
            }
        }
        this.ImageOne = $$(".photolite_one");
        if (this.ImageOne.length > 0) {
            for (var i = 0; i < this.ImageOne.length; i++) {
                this.ImageOne[i].plp = new Photolite({
                    scrollingView: this.options.scrollingView,
                    container: this.ImageOne[i]
                });
            }
        }
    }
});
var Photolite = new Class({
    Implements: Options,
    options: {
        scrollingView: true,
        container: null,
        heightContainer: null,
        widthVideo: '460px',
        heightVideo: '360px'
    },
    SelectIndex: null,
    IsFirstShow: true,
    initialize: function(options) {
        this.setOptions(options);
        if (!this.options.container) {
            return false;
        }
        if (this.options.container.hasClass("photolite_one")) {
            this.Images = Array();
            this.Images[0] = this.options.container;
        }
        else {
            this.Images = this.options.container.getElements(".photolite");
        }
        if (this.Images.length == 0) {
            return false;
        }
        this.loadImages();
        this.initUI();
    },
    initUI: function() {
        this.ObjCenter = new Element("center", { "class": "photolite_Center" });
        this.ObjShowContainer = new Element("div", { "class": "photolite_ShowContainer" });

        this.ObjTools = new Element("center", { "class": "photolite_Tools" });
        this.ObjImageContainer = new Element("div", { "class": "photolite_ImageContainer" });
        this.ObjFlashContainer = new Element("div", { "class": "photolite_FlashContainer" });
        this.ObjImageShow = new Element("img", { "class": "photolite_ImageShow" });
        this.ObjImageShow.inject(this.ObjImageContainer);
        this.ObjImageShow.addEvents({
            "click": function(a) {
                return function() {
                    if (a.Images.length == 1)
                        a.Close();
                    else
                        a.nextImage();
                }
            } (this),
            "mouseenter": function(a) {
                return function() {
                    a.overControls();
                }
            } (this),
            "mouseout": function(a) {
                return function() {
                    a.outControls();
                }
            } (this)
        });

        this.ObjImageLast = new Element("a", {
            "class": "photolite_ImageLast",
            "text": "предыдущая",
            "href": "javascript:void(0)",
            "events": {
                "click": function(a) {
                    return function() {
                        a.lastImage();
                    }
                } (this),
                "mouseenter": function(a) {
                    return function() {
                        a.overControls();
                    }
                } (this),
                "mouseout": function(a) {
                    return function() {
                        a.outControls();
                    }
                } (this)
            }
        });
        this.ObjImageNext = new Element("a", {
            "class": "photolite_ImageNext",
            "text": "следующая",
            "href": "javascript:void(0)",
            "events": {
                "click": function(a) {
                    return function() {
                        a.nextImage();
                    }
                } (this),
                "mouseenter": function(a) {
                    return function() {
                        a.overControls();
                    }
                } (this),
                "mouseout": function(a) {
                    return function() {
                        a.outControls();
                    }
                } (this)
            }
        });
        this.ObjImageClose = new Element("img", {
            "class": "photolite_ImageClose",
            "src": "/themes/default/images/photolite/close.gif",
            "events": {
                "click": function(a) {
                    return function() {
                        a.Close();
                    }
                } (this),
                "mouseenter": function(a) {
                    return function() {
                        a.overControls();
                    }
                } (this),
                "mouseout": function(a) {
                    return function() {
                        a.outControls();
                    }
                } (this)
            }
        });
        if (this.Images.length == 1) {
            this.ObjImageClose.setStyle("visibility", "hidden");
        }
        this.ObjTools.grab(this.ObjImageLast, "top");
        this.ObjTools.grab(this.ObjImageNext, "bottom");
        this.ObjTools.grab(this.ObjImageClose, "top");

        this.ObjTools.inject(this.ObjShowContainer, "top");
        this.ObjImageContainer.inject(this.ObjShowContainer, "bottom");
        this.ObjFlashContainer.inject(this.ObjShowContainer, "bottom");

        this.ObjShowContainer.inject(this.ObjCenter);
        this.ObjCenter.inject($(document.body), "top");

        this.ObjCenter.hide();

        $(document.body).addEvent("keydown", function(a) {
            return function() {
                if (a.isShow()) {
                    var e = new Event();
                    if (e.code == 27) /*esc*/{
                        a.Close();
                        e.stop();
                    }
                    else if (e.code == 37) /*left*/{
                        a.lastImage();
                        e.stop();
                    }
                    else if (e.code == 39) /*right*/{
                        a.nextImage();
                        e.stop();
                    }
                }
            }
        } (this));
    },
    loadImages: function() {
        this.imgLoadedTemp = new Element("div");
        this.imgLoadedTemp.setStyle("display", "none");

        this.Bg = new BackgrounScreen($(document.body), {
            bgcolor: "#000000",
            clickHide: true,
            events: {
                hide: function(a) {
                    return function() {
                        a.Hide();
                    }
                } (this)
            }
        });
        var i = 0;
        for (i = 0; i < this.Images.length; i++) {
            var ext = String(this.Images[i].href).substr(String(this.Images[i].href).lastIndexOf(".") + 1).toLowerCase();
            if (ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "png" || ext == "bmp") {
                var myImage = new Asset.image(this.Images[i].href, {
                    onload: function(a, b, c) {
                        return function() {
                            a.Images[b].pl_prop.is_load = true;
                            var imgTemp = new Element("img", { "src": c });
                            imgTemp.inject(a.imgLoadedTemp);
                            a.refreshUI(false);
                        }
                    } (this, i, this.Images[i].href)
                });
                var temp_img = new Element("img", { "src": this.Images[i].href });
                var type = "image";
            }
            else if (ext == "flv") {
                var myImage = null;
                var temp_img = null;
                var type = "video";
            }
            this.Images[i].pl_prop = {
                "src_big": this.Images[i].href,
                "myImage": myImage,
                "temp_img": temp_img,
                "is_load": false,
                "width": 0,
                "height": 0,
                "ext": ext,
                "type": type
            };

            this.Images[i].addEvent("click", function() {
                return false;
            });

            this.Images[i].addEvent("click", function(a, b) {
                return function() {
                    a.showImageByIndex(b);
                }
            } (this, i));
        }
    },
    showImageByIndex: function(Index) {
        if (Browser.Engine.trident && this.Images[Index].pl_prop.type == "image") {
            var imgTemp = new Element("img", { "src": this.Images[Index].pl_prop.src_big });
            imgTemp.inject(this.imgLoadedTemp);
        }

        this.SelectIndex = Index;
        if (this.Images[this.SelectIndex].pl_prop.type == "image") {
            this.ObjImageShow.src = this.Images[Index].pl_prop.src_big;
            this.ObjImageShow.setStyle("background", "url(" + this.Images[Index].pl_prop.src_big + ")");
            this.ObjImageShow.show();
        }
        else if (this.Images[this.SelectIndex].pl_prop.type == "video") {
            new Swiff("/themes/default/flash/mediaplayer.swf", {
                width: this.options.widthVideo,
                height: this.options.heightVideo,
                container: this.ObjFlashContainer,
                params: {
                    wmode: "transparent",
                    bgcolor: "#000000",
                    allowfullscreen: true,
                    allowscriptaccess: true
                },
                vars: {
                    file: this.Images[Index].pl_prop.src_big,
                    autostart: true,
                    provider: "video"
                },
                properties: {
                    "class": "photolite_FlashShow"
                }
            });
        }
        this.Show();
        this.IsFirstShow = false;

        if (Browser.Engine.trident && this.Images[Index].pl_prop.type == "image") {
            setTimeout(function(a) {
                return function() {
                    a.ObjImageShow.setStyle("width", a.ObjImageShow.getStyle("width"));
                    a.refreshUI();
                }
            } (this), 15);
        }
    },
    isShow: function() {
        if (this.ObjCenter.getStyle("display") == "block")
            return true;
        else
            return false;
    },
    Show: function() {
        if (this.SelectIndex != null) {
            this.Bg.Show();
            this.ObjCenter.setStyle("display", "block");
            this.ObjShowContainer.setStyle("display", "block");

            if (this.Images[this.SelectIndex].pl_prop.type == "image") {
                this.ObjFlashContainer.setStyle("display", "none");
                this.ObjImageContainer.setStyle("display", "block");
            }
            else if (this.Images[this.SelectIndex].pl_prop.type == "video") {
                this.ObjImageContainer.setStyle("display", "none");
                this.ObjFlashContainer.setStyle("display", "block");
            }
            this.refreshUI();
        }
    },
    Close: function() {
        this.Bg.Hide();
        this.Hide();
    },
    Hide: function() {
        this.ObjCenter.setStyle("display", "none");
        this.IsFirstShow = true;
    },
    lastImage: function() {
        if (this.SelectIndex > 0)
            this.showImageByIndex(this.SelectIndex - 1);
    },
    nextImage: function() {
        if (this.SelectIndex < (this.Images.length - 1))
            this.showImageByIndex(this.SelectIndex + 1);
    },
    overControls: function() {
        this.ObjImageShow.addClass("photolite_ImageShow_Hover");
    },
    outControls: function() {
        this.ObjImageShow.removeClass("photolite_ImageShow_Hover");
    },
    refreshUI: function(ff) {
        if (this.SelectIndex != null) {
            if (this.Images[this.SelectIndex].pl_prop.type == "image") {
                if (!this.options.heightContainer)
                    var heightContainer = this.ObjImageContainer.getCoordinates().height;
                else
                    var heightContainer = this.options.heightContainer;

                this.ObjShowContainer.setStyle("width", (this.ObjImageShow.getCoordinates().width + 20) + "px");
                this.ObjShowContainer.setStyle("left", (($(document.body).getSize().x - this.ObjImageShow.getCoordinates().width) / 2) + "px");
                this.ObjTools.setStyle("left", ((this.ObjImageShow.getCoordinates().width - this.ObjTools.getSize().x) / 2) + "px");
            }
            else if (this.Images[this.SelectIndex].pl_prop.type == "video") {
                if (!this.options.heightContainer)
                    var heightContainer = this.ObjFlashContainer.getCoordinates().height;
                else
                    var heightContainer = parseInt(this.options.heightVideo);

                this.ObjShowContainer.setStyle("width", (parseInt(this.options.widthVideo) + 20) + "px");
                this.ObjShowContainer.setStyle("left", (($(document.body).getSize().x - parseInt(this.options.widthVideo)) / 2) + "px");
                this.ObjTools.setStyle("left", ((parseInt(this.options.widthVideo) - this.ObjTools.getSize().x) / 2) + "px");
            }
            if (Browser.Engine.trident) {
                this.ObjImageShow.setStyle("zoom", "1");
                this.ObjImageShow.setStyle("overflow", "hidden");
            }

            if (this.IsFirstShow == true && ff == undefined) {
                if (this.options.scrollingView) {
                    var temp = (heightContainer - this.options.container.getSize().y) / 2;
                    var temp2 = this.options.container.getCoordinates().top - temp;
                    if (temp2 < 0)
                        temp2 = temp - this.options.container.getCoordinates().top / 2;
                    this.ObjCenter.setStyle("top", temp2);

                    var temp3 = $(document.body).getScroll();
                    new Fx.Scroll($(document.body), {
                        duration: 700
                    }).start(temp3.x, Math.ceil(this.options.container.getCoordinates().top - temp - 50));
                }
                else {
                    var temp = ($(document.body).getSize().y - heightContainer) / 2;
                    this.ObjCenter.setStyle("top", $(document.body).getScroll().y + temp);
                }
            }

            if (this.SelectIndex < 1) {
                this.ObjImageLast.setStyle("visibility", "hidden");
                this.ObjImageNext.setStyle("visibility", "visible");
            }
            else if (this.SelectIndex == (this.Images.length - 1)) {
                this.ObjImageLast.setStyle("visibility", "visible");
                this.ObjImageNext.setStyle("visibility", "hidden");
            }
            else {
                this.ObjImageLast.setStyle("visibility", "visible");
                this.ObjImageNext.setStyle("visibility", "visible");
            }
            if (this.Images.length <= 1) {
                this.ObjImageLast.setStyle("visibility", "hidden");
                this.ObjImageNext.setStyle("visibility", "hidden");
            }
        }
    }
});
window.addEvent("domready", function() {
    var temp = new PhotoliteParts();
});
