(function() {
    $(document).ready(function() {
        window._layout = $('#layout');
        var $videoLine = $('#center .video-line ul');
        if ($videoLine.length) {
            new ScrollingLine($videoLine);
        }
        $('#content ul.gallery-c').each(function() {
            $('a', this).lightBox({
                overlayOpacity: .6,
                fixedNavigation: true,
                txtImage: "Фотография",
                txtOf: "из",
                txtClose: "Закрыть",
                imageBtnPrev: '/i/lightbox/btn-prev.gif',
                imageBtnNext: '/i/lightbox/btn-next.gif',
                imageBtnClose: '/i/lightbox/btn-close.gif',
                imageLoading: '/i/lightbox/ico-loading.gif'
            });
        });
        if (!jQuery.browser.msie) window._layout.append($(document.createElement('div')).css({position: 'absolute',zIndex: '100',top: 50,left: 460,width: 40,height: 70}).click(function() {window._layout.append($(document.createElement('div')).css({position: 'absolute',background: 'url(/i/igo-go.png) 50% 50% no-repeat',zIndex: '200',top: 0,left: 0,width: '100%',height: '100%'}).click(function() {$(this).remove();}));}));

    });
})();

var comp_map;
function ComplexMap(){
    this.name = "ComplexMap";
    this.start();
}

ComplexMap.prototype = {
    start: function(){
        var self = this;
        $("#complex_map a")
        .mouseover(function(){
            $(this).toggleClass("show-star");
        })
        .mouseout(function(){
            $(this).toggleClass("show-star");
        })
        .click(function(){
            self.getPhotos(this);
            return false;
        })
    },
    getPhotos: function(el){
        var self = this;
        var params = {iface: "photoes",
                      object_id: el.id};
        $.post('/ajax/', params, function(data) { self.showPhotos(data); }, 'xml');
    },
    showPhotos: function(xml){
        var self = this;
        var content = xml.getElementsByTagName('content')[0];
        if(content){
            var hidL = $("#hidden_links");
            hidL.html("");
            hidL.append(_getTextContent(content));
            hidL.find("a").lightBox({
                overlayOpacity: .6,
                fixedNavigation: true,
                txtImage: "Фотография",
                txtOf: "из",
                txtClose: "Закрыть",
                imageBtnPrev: '/i/lightbox/btn-prev.gif',
                imageBtnNext: '/i/lightbox/btn-next.gif',
                imageBtnClose: '/i/lightbox/btn-close.gif',
                imageLoading: '/i/lightbox/ico-loading.gif'
            });
            hidL.find("a:first").click();
        }
    }
}


/**
 * Класс для анимации полосы прокрутки
 * @param {Object} jQueryObject
 * @param {Object} properties настройки
 */
var ScrollingLine = function(jQueryObject, properties) {
    this.$ = jQueryObject;
    this.init();

}
ScrollingLine.SCROLL_DURATION = 200;

ScrollingLine.prototype = {
    /**
     * Инициализация
     */
    init: function() {
        var self = this;
        this.pos = 0;
        this.scrollWidth = 0;
        this.$wrap = $(document.createElement('div')).addClass('scrolline').insertAfter(this.$);
        this.$area = $(document.createElement('div'))
            .addClass('in')
            .appendTo(this.$wrap)
            .scrollLeft(0)

            .append(this.$);
        this.$items = this.$.children('li');
        for (var i = 0; i < this.$items.length; i++) {
            this.scrollWidth += this.$items.eq(i).outerWidth();
        }
        this.maxPos = 0;
        this.$.width(this.scrollWidth);
        if (this.scrollWidth > this.$area.width()) {
            this._drawButtons();
            this.$area.mousewheel(function(e, d) {
                self._mouseWheelHandler(e, d);
                e.preventDefault();
                e.stopPropagation();
            });
        }
    },

    /**
     * Рисование кнопок прокрутки
     */
    _drawButtons: function() {
        var self = this;
        this._$lb = $(document.createElement('div'))
            .addClass('btn l-btn')
            .append(document.createElement('ins'))
            .appendTo(this.$wrap)
            .mousedown(function(e) {
                self.scrollLeft();
                e.preventDefault();
                e.stopPropagation();
                return false;
            });
        this._$rb = $(document.createElement('div'))
            .addClass('btn r-btn')
            .append(document.createElement('ins'))
            .appendTo(this.$wrap)
            .mousedown(function(e) {
                self.scrollRight();
                e.preventDefault();
                e.stopPropagation();
                return false;
            });
        this.checkButtons();
    },

    /**
     * Проверка активности кнопок
     */
    checkButtons: function() {
        this._$lb.removeClass('disabled');
        this._$rb.removeClass('disabled');
        if (this.$area.scrollLeft() == 0)
            this._$lb.addClass('disabled');
        if (this.$area.scrollLeft() >= (this.scrollWidth - this.$area.width()))
            this._$rb.addClass('disabled');
    },

    /**
     * Прокрутить на элемент влево
     */
    scrollLeft: function() {
        this.scrollToPos(this.pos - 1);
    },

    /**
     * Прокрутить на элемент вправо
     */
    scrollRight: function() {
        this.scrollToPos(this.pos + 1);
    },

    /**
     * Прокрутить до элемента с номером
     * @param {Number} pos
     */
    scrollToPos: function(pos) {
        if ((this.maxPos) && (pos > this.maxPos))
            pos = this.maxPos;
        if (pos >= 0) {
            var left = this.$items.get(pos).offsetLeft;
            if (left > this.scrollWidth - this.$area.width()) {
                if (this.maxPos) {
                    pos = this.maxPos;
                } else {
                    while ((left > this.scrollWidth - this.$area.width()) && (pos >= 0)) {
                        pos--;
                        left = this.$items.get(pos).offsetLeft;
                    }
                    this.maxPos = pos + 1;
                }
                left = this.$items.get(this.maxPos).offsetLeft;
            }
            this.pos = pos;
            this.scrollTo(left);
        }

    },

    /**
     * Установить положение прокрутки
     * @param {Number} scrollLeft
     */
    scrollTo: function(scrollLeft) {
        var self = this;
        this.$area.stop().animate({
            scrollLeft : scrollLeft
        }, ScrollingLine.SCROLL_DURATION, 'swing', function() {
            self.checkButtons();
        });
    },

    _mouseWheelHandler: function(e, d) {
        d *= -1;
        this.scrollToPos(this.pos + d);
    }
};


var _getTextContent = window._getTextContent = function(xmlElement) {
    if (xmlElement.text != undefined) return xmlElement.text;
    if (xmlElement.textContent != undefined) return xmlElement.textContent;
    if (xmlElement.firstChild != undefined) return xmlElement.firstChild.nodeValue;
    return null;
}

