function photoMuseum()
{

    this.galleries = new Array();
    this.view = 'first';

    this.addGallery = function(_gallery)
    {
        this.galleries.push(_gallery);
    }

    this.getGalleryById = function(_id)
    {
        for (var i = 0; i < this.galleries.length; i++) {
            if (this.galleries[i].id == _id) {
                return this.galleries[i];
            }
        }

        return false;
    }

    this.show = function(_galleryId, _photoIndex)
    {
        this.getGalleryById(_galleryId).getPhotoByIndex(_photoIndex).show();
    }

    this.next = function(_galleryId)
    {
        this.getGalleryById(_galleryId).next();
    }

	 this.prev = function(_galleryId)
    {
        this.getGalleryById(_galleryId).prev();
    }

    this.checkScrollers = function()
    {
        for (var i = 0; i < this.galleries.length; i++) {
            this.galleries[i].checkScrollers();
        }
    }

    this.scrollLeft = function(_galleryId)
    {
        this.getGalleryById(_galleryId).scrollLeft();
    }

    this.scrollRight = function(_galleryId)
    {
        this.getGalleryById(_galleryId).scrollRight();
    }

    this.fullScreen = function(_galleryId)
    {
        this.getGalleryById(_galleryId).fullScreen();
    }

    this.resize = function(_galleryId)
    {
        for (var i = 0; i < this.galleries.length; i++) {
            this.galleries[i].resize();
        }
    }
}

function photoGallery(_id, _params)
{
    this.getWrapHtml = function() {
        var html =
            '<dl class="photos photos-init fullScreen" id="' + this.id + '_popup" style="display: none;">' +
                '<dt class="illu">' +
                    '<div class="close"></div>' +
                    '<ins class="full-screen"></ins>' +
		            '<img src="" style="max-height: 600px;">' +
                '</dt>' +
                '<dt class="title">' +
                    '<div class="operate">' +
			            '<span class="prev">←</span> Ctrl<span class="next"> →</span>' +
		            '</div>' +
                '</dt>' +
                '<dd>' +
                    '<ins class="left" style="display: none;"></ins>' +
                    '<div class="previews smallPreviews">' +
                        '<ul>' +
                        '</ul>' +
                    '</div>' +
                    '<ins class="right" style="display: none;"></ins>' +
                '</dd>' +
            '</dl>'
        ;

        return html;
    }

    this.init = function()
    {
        $('body').append(this.getWrapHtml());

        this.ele = $("#" + this.id);
        this.popupEle = $("#" + this.id + '_popup');
        this.illu = this.popupEle.find('dt.illu');

        this.photos = new Array();

        this.closeBtn = this.popupEle.find('.close');
        this.selectedPhoto;

        var galleryId = this.id;
        var self = this;
        var imagesHtml = '';

        this.ele.find('img.list').each(
            function(index) {
                if (index == 0) {
                    self.illu.find('img').attr(
                        'src',
                        $(this).parent().attr('href')
                    );
                }

                $(this).parent().find('img').click(
                    function() {
                        self.photos[index].show();
                        return false;
                    }
                );

                imagesHtml += '<li><a href="' + $(this).parent().attr('href') + '"><img src="' + $(this).attr('src') + '" width="' + $(this).attr('width') + '" height="' + $(this).attr('height') + '" alt="" /></a></li>';
            }
        );
        this.popupEle.find('dd div.previews ul').append(imagesHtml);

        this.previewsContainer = this.popupEle.find(".previews");
        var elements = this.previewsContainer.find("li");

        var amountWidth = 0;
        var item, jqEle;

        for (var i = 0; i < elements.length; i++) {
            jqEle = $(elements[i]);
            item = new photo(this, jqEle);

            if (i == 0)                         item.setFirst(true);
            else if (i == elements.length - 1)  item.setLast(true);

            this.photos.push(item);
            amountWidth += item.getWidth();
        }

        this.previews = this.previewsContainer.find("ul:first").css({
            width: amountWidth,
            height: item.ele.outerHeight()
        });


		this.closeBtn.click(function(event){
            self.hide();
			$("body").removeClass("hide-part-body")

			event = event || window.event; // кросс-браузерно

            if (event.stopPropagation) {
                event.stopPropagation();
            } else {
                event.cancelBubble = true;
            }

            if ($(window).height()<=750){
                self.popupEle.addClass("noFixedGallery")


            }
            else {
                self.popupEle.removeClass("noFixedGallery")

            }

        });

        this.btnLeft = this.popupEle.find('ins.left');
        this.btnRight = this.popupEle.find('ins.right');

        this.btnLeft.click(function() {
            if (museum.view == 'first')
			{
				museum.scrollLeft(galleryId);
			}
			if (museum.view == 'second')
			{
			    window.clearTimeout(photoClick);
			    photoClick = window.setTimeout("museum.prev('" + galleryId + "')", 500);

			}
        });

        this.btnRight.click(function() {
            if (museum.view == 'first')
			{
				museum.scrollRight(galleryId);
			}
			if (museum.view == 'second')
			{
			    window.clearTimeout(photoClick);
			    photoClick = window.setTimeout("museum.next('" + galleryId + "')", 500);
			}
        });

        this.scrollNow;

        //this.photos[50].show();
    }

    this.show = function() {
        var self = this;

        if (this.popupEle.css('display') != 'block') {
            this.popupEle.show();
            if ($(window).height()<=750){
                this.popupEle.addClass("noFixedGallery")
                window.scrollTo(0,0)
				$("body").addClass("hide-part-body")


            }

            this.resize();

          $('body').append($("<div class=\"dark mainDark\"></div>").clone());

         // $("body").addClass("dark")

            $(document).keyup(function(event){
                if (event.keyCode == 27) {
                    self.hide();
                }
            });
        }
    }

    this.showPhoto = function(_photo) {
        this.show();
        this.selectedPhoto = _photo;

        //this.title.html(_photo.title);

        this.illu.find('img').remove();
        this.illu.append("<img src='" + _photo.photo + " '/>");
        this.illu.find('img').css('max-height', this.illu.height());

        var self = this;

        this.illu.find('img').click(
            function() {
                self.next();
            }
        );

        this.scrollCenter();
    }

    this.hide = function() {
        this.popupEle.hide();
        $('.dark').remove();
    }

    this.getPhotoByIndex = function(_index)
    {
        return this.photos[_index];

    }

    this.next = function()
    {
        for (var i = 0; i < this.photos.length; i++) {
            if (this.photos[i] == this.selectedPhoto) {
				this.photos[i < this.photos.length - 1 ? i + 1 : 0].ele.click();
                break;
            }
        }
    }

	this.prev = function()
    {
        for (var i = 0; i < this.photos.length; i++) {
            if (this.photos[i] == this.selectedPhoto) {
				if (i!=0){this.photos[i-1].ele.click()};
                break;
            }
        }
    }

    this.isScrollable = function()
    {
        return this.previews.width() > this.previewsContainer.width();
    }

    this.checkScrollers = function(_futureX)
    {
        if (this.isScrollable()) {
            var max = this.getScrollMaxLeftOffset();
            var offset = this.getScrollOffset();

            if (isNaN(_futureX)) {
                if (max > offset)       this.move(max);
                else if (offset > 0)    this.move(0);
                offset = this.getScrollOffset();

            } else {
                offset = _futureX;
            }

            if (offset == 0)    this.btnLeft.hide();
            else                this.btnLeft.show();

            if (max == offset)  this.btnRight.hide();
            else                this.btnRight.show();

        } else {
            this.previews.css("margin", "0 auto");
            this.btnLeft.hide();
            this.btnRight.hide();
        }
    }

    this.getScrollMaxLeftOffset = function()
    {
        var offset = this.previews.width() - this.previewsContainer.width();
        return offset > 0 ? -offset : 0;
    }

    this.getScrollOffset = function()
    {
        return parseInt(this.previews.css("marginLeft"));
    }

    this.move = function(_x)
    {
        this.previews.css("marginLeft", _x);
    }

    this.scroll = function(_x)
    {
        this.previews.css("marginLeft", _x);
/*         this.previews.animate({ marginLeft: _x }, 600); */
        this.checkScrollers(_x);
    }

    this.getPreviewWidth = function()
    {
        return this.photos[0].getWidth();
    }

    this.getScrollStep = function()
    {
        return this.getPreviewWidth() * 3;
    }

    this.scrollLeft = function()
    {
        var position = this.getScrollOffset();

        if (this.isScrollable() && position < 0) {
            var offset = position + this.getScrollStep();

            if (offset > 0 || offset + this.getPreviewWidth() > 0) {
                offset = 0;
            }

            this.scroll(offset);
        }
    }

    this.scrollRight = function()
    {
        var position = this.getScrollOffset();
        var max = this.getScrollMaxLeftOffset();

        if (this.isScrollable() && position > max) {
            var offset = position - this.getScrollStep();

            if (offset < max || offset - this.getPreviewWidth() < max) {
                offset = max;
            }

            this.scroll(offset);
        }
    }

    this.scrollCenter = function()
    {
        if (this.isScrollable()) {
            var max = this.getScrollMaxLeftOffset();
            var step = this.getPreviewWidth();
            var center = Math.round((this.previewsContainer.width() - step) / 2);
            var position = this.selectedPhoto.ele.position().left - this.getScrollOffset();
            var offset = position - center;
            offset *= -1;

            if (offset > 0)         offset = 0;
            else if (offset < max)  offset = max;

            this.scroll(offset);
        }
    }

    this.resize = function()
    {
        if (this.popupEle.css('display') == 'block') {
            var hPadding = this.popupEle.outerWidth() - this.popupEle.width();
            var vPadding = this.popupEle.outerHeight() - this.popupEle.height();

            var width = $(window).width() - hPadding;

            this.popupEle.css("width", width);
            //this.popupEle.css("marginLeft", -1 * (width / 2));

            this.illu.find('img').css('max-height',this.illu.height())

            this.checkScrollers();
            if ($(window).height()<=750){
                this.popupEle.addClass("noFixedGallery")
				$("body").addClass("hide-part-body")


            }
            else {
               this.popupEle.removeClass("noFixedGallery")
			   $("body").removeClass("hide-part-body")


            }
        }
    }

    this.id = _id;
    this.params = _params;
    this.init();
}

function photo(_gallery, _ele)
{
    function getPhotoIndex(_ele)
    {
        var elems = _ele.parentNode.getElementsByTagName(_ele.nodeName);

        for (var i = 0; i < elems.length; i++) {
            if (elems[i] == _ele) {
                return i;
            }
        }

        return null;
    }

    this.init = function()
    {
        this.preview = this.ele.find("a img");
        this.photo = this.ele.find("a").attr("href");
        this.title = this.preview.attr("alt");
		this.nextFoto = this.ele.next().find("a img")

        this.sel = $('<ins class="sel"></ins>');

        this.sel.insertAfter(this.ele.find(":last-child:first")).
        css({
            width: this.preview.attr("width") - 6,
            height: this.preview.attr("height") - 6
        });

        var galleryId = this.gallery.id;
        var gallery = this.gallery;
		foto = this;

        this.ele.click(function(event) {
			museum.show(galleryId, getPhotoIndex(this));
            return false;
        });
    }

    this.setFirst = function(_isFirst)
    {
        var width = this.preview.attr("width");

        /*
        if (_isFirst) {
            this.sel.css({
                width: width - 4,
                left: 0
            });
        } else {
            this.sel.css({
                width: width - 2,
                left: -2
            });
        }
        */
    }

    this.setLast = function(_isLast)
    {
        var width = this.preview.attr("width");
        //this.sel.css("width", _isLast ? width - 4 : width - 2);

        if (_isLast) {
            this.ele.css("paddingRight", 0);
        }
    }

    this.getWidth = function()
    {
        var width = this.ele.find('img').attr('width')
                  ? parseInt(this.ele.find('img').attr('width'), 10) + 2
                  : 0;

        if (!width) {
            width = this.ele.outerWidth(true);
        }

        if (!width) {
            width = 62;
        }

        return width;
    }

    this.show = function()
    {
        if (this.gallery.selectedPhoto) {
            this.gallery.selectedPhoto.hide();
        }

        this.ele.addClass("sel");
        this.gallery.showPhoto(this);
    }

    this.next = function()
	{
		this.nextFoto.css('border','1px solid red')
	}

    this.hide = function()
    {
        this.ele.removeClass("sel");
    }

    this.gallery = _gallery;
    this.ele = _ele;
    this.init();
}

var museum = new photoMuseum();

$(window).resize(function() {
    museum.resize();
});


