/**
 * управляет верхней панелью
 * @class управляет верхней панелью
 * генерирует события 
 * toppanel down - верхняя панель упала вниз
 * toppanel top - верхняя панель поднимается вверх
 */
;function TopPanel() {
    var that = this;
    var scaled = false;
    var buttonResize = $("#changeSize");
    var navigation =  $(".topPanelSort");
    
    
    
    var init = function () {
        that.obj.addClass("two-page-mode");
        bind();
    }
    
    /**
     * Кнопка "показать верх"
     */
    var _showTopButton = $("#showTopButton");
    
    var bind = function () {
        $("#mmlogo")
            .bind({
                'click': function () {
                    // верхняя панель поднимается вверх
                    coreNotice('toppanel top');
                }
            })
            .hover(function () {
                (that.opened) ? mmLogoColor() : mmLogoColor("red");
            }, function () {
                (that.opened) ? mmLogoColor("red") : mmLogoColor();
            });
         
       
        
        $("#changeSize").click(function () {
            that.obj.trigger("toppanelswitchsize", { scaled: !scaled });
        });
        
        // клик по кнопке "Вверх"
        $('#orange_close').click(function() {
            // верхняя панель поднимается вверх
            coreNotice('toppanel top');
        });
        
        // клик по кнопке опросы
        $('.voting_show').click(function() {
            coreNotice('toppanel down');
        });
        
        // клик по кнопке "Вниз"
        _showTopButton.click(function (event) {
            
            //$(".topPanel").css('opacity', '0');
            // верхняя панель упала вниз
            coreNotice('toppanel down');
            
            return;
            
            /*if (that.opened) {
                //that.obj.trigger("toppanelclosearchive");
                that.hideUp();
                that.obj.trigger("toppanelhome");
                
                
            } else {
                that.obj.trigger("toppanelopenarchive");
                $("#orange_bottom_menu")
                    .css({"bottom" : -53 + "px"})
                    .animate({"bottom" :  -8 + "px"})
                    .animate({"bottom" :  -13 + "px"});
                that.showDown();
            }*/
        });
        
        $('#hideCommunity, #orange_logo').click(function() {
            //$("#orange_bottom_menu").hide()
            //that.obj.trigger("toppanelclosearchive");
            // верхняя панель поднимается вверх
            
            coreNotice('toppanel top');
        });
        
        
    };
    
    var mmLogoColor = function (color) {
        if (typeof color === 'undefined') {
            color = '';
        }
        $("#mmlogo").attr("src", "/media/images/topPanelMM" + color + ".png");
    };
    
    var displayCurrentPage = function (cur_id) {
        //TODO не передавать 000 id
        if (!!cur_id.match("000")) return;
        var realPages = $("#" + cur_id).find("p").map(function () {
            return $(this).text() + '';
        });
        
        //TODO передавать настоящий номер странциы
        if (realPages.length === 2) {
            realPages = realPages[0] + " - " + realPages[1];
        } else {
            realPages = realPages[0];
        }
        
        //TODO передавать последнюю страницу
        /*var lastPage = $('.preview_element:last p:last').text();
        if(realPages[0] === "0") {
            disablePrev();
        } else if (realPages.match(lastPage)) {
            disableNext();
        } else {
            enableNextAndPrev();
        }*/
        $(".topPanelPagesSort").text(realPages);
    };
    
    /*var disableNext = function () {
        navigation.addClass("disableNext").removeClass("disablePrev");
    };*/
    
    /*var disablePrev = function () {
        navigation.addClass("disablePrev").removeClass("disableNext");
    };*/
    
    /*var enableNextAndPrev = function () {
        navigation.removeClass("disableNext disablePrev");
    }*/
    
    this.obj = $('.topPanelContainer');
    
    // покачать оранжевую панель
    this.bounceOrange = function () {
        $(".topPanel")
        .css({
            'opacity': '0',
            'height' : '0px',
            'overflow': 'hidden'})
        
        $("#orange_bottom_menu")
        .show()
        .css({"bottom" : -53 + "px"})
        .animate({"bottom" :  -8 + "px"})
        .animate({"bottom" :  -13 + "px"});
    };
    
    // покачать белую панель
    this.bounceWhite = function () {
        $("#orange_bottom_menu")
        .hide();
        
        $(".topPanel")
            .css({
                'opacity': '1',
                'height' : '40px',
                'overflow': 'visible'})
            .css({"top" : -40 + "px"})
            .animate({"top" :  5 + "px"})
            .animate({"top" :  0 + "px"});
    };
    
    this.twoPageMode = function() {
        that.obj
            .addClass("two-page-mode")
            .removeClass("one-page-mode");
        buttonResize.attr("rel", "страница");
        scaled = false;
    };
    
    this.onePageMode = function () {
       that.obj
            .addClass("one-page-mode")
            .removeClass("two-page-mode");
       buttonResize.attr("rel", "разворот");
       scaled = true;
       
       
       this.tryScroll();
    };
    
    this.currentPage = function (page) {
        displayCurrentPage(page);
    };
    
    this.tryScroll = function() {
        
        return false;
        var pagesAboveMain = 3;
        var toCenterPlus = - 11,
        st = $(document).scrollTop();
        
        if (scaled) {
            if (st < 1142 * pagesAboveMain + toCenterPlus) {
                this.scroll(1142 * pagesAboveMain + toCenterPlus - st);
                this.opened = true;
                _showTopButton.css('background-position', '-874px 0');
                $('.topPanel').css('opacity', '0');
                $('#orange_bottom_menu').show();
            }  else {
                this.scroll(0);
                $('.topPanel').css('opacity', '1');
                $('#orange_bottom_menu').hide();
                this.opened = false;
                _showTopButton.css('background-position', '-792px top');
            }
        }
        
        
    };
    
    this.scroll = function (pos) {
        var maxOffset = $(window).height() - 38 + 5; //TODO in var
        
        if ( pos > maxOffset) {
            $(".topPanel").css({ top: maxOffset });
        } else {
            $(".topPanel").css({ top: pos - 1 });
        }

        return;
    };
    
    this.opened = false;
    this.showDown = function () {
        mmLogoColor("red");
        var maxOffset = $(window).height() - 38; //TODO in var
        $(".topPanel")
            .animate({ "top" : 1059 - 5 + "px" }, {
                duration: 2000,
                step: function (now, fx) {
                    if (now > maxOffset + 100) {
                        _showTopButton.css('background-position', '-874px 0');
                        $(this).stop();
                    }
                }
            })
            .animate({"top" : maxOffset - 5 + "px"})
            .animate({"top" : maxOffset + 2 + "px"});
        
        that.opened = true;
    };
    
    this.hideUp = function () {
        mmLogoColor();

        var menuBottomOffset = $("#menu").offset().top - $(window).scrollTop() + $("#menu").height();
        
        _showTopButton.css('background-position', '-792px top');
        //.css({ top: menuBottomOffset }) //TODO инкапсуляция
        $(".topPanel")
            .css({"top" : -40 + "px"})
            .animate({"top" :  5 + "px"})
            .animate({"top" :  0 + "px"});
        
        /*var menuBottomOffset = $("#menu").offset().top - $(window).scrollTop() + $("#menu").height();
        var duration = 1300 * menuBottomOffset / $("#menu").height();
        $(".topPanel")
            .css({ top: menuBottomOffset }) //TODO инкапсуляция
            .animate({"top" : -40 + "px"}, {
                duration: duration,
                complete: function () {
                    //_showTopButton.attr("src", "/media/images/topPanelBigStrel.png");
					_showTopButton.css('background-position', '-792px top');
                }
            })
            .animate({"top" :  5 + "px"})
            .animate({"top" :  0 + "px"});*/
        
        that.opened = false;
    };
    
    this.dropDown = function () {
        //$(".topPanel").offset({ top: $("#menu").height - $("#menu").offset().top }) //TODO инкапсуляция
        //$(".topPanel").offset({ top: 1059 - $("#menu").offset().top }) //TODO инкапсуляция
        return this;
    };
    
    init();
}
/*--- Top Panel ---*/
