var Layout = function() {
    var _elWindow;
    var _elBody;
    var _elContainer;
    var _elSidebar;
    var _elMainContent;
    var _elContentInner;

    var _documentHeight;

    var isIE = false; // true if its below ie7
    var isIE6 = false; // true if ie6

    var self = {
        init: function() {
            isIE = ($.browser.msie && $.browser.version < 8) ? true : false;
            isIE6 = (isIE && $.browser.version == 6) ? true : false;

            _elWindow = $(window);
            _elBody = $("body");
            _elContainer = $(".container");
            _elSidebar = $("#sidebar");
            _elMainContent = $("#main-content");
            _elContentInner = $("#content-inner");

            _documentHeight = $.getDocumentHeight();

            // fix layout immediately
            self.fixLayout();

            // bind resize
            _elWindow.bind('resize', self.fixLayout);
            if (_elSidebar.length > 0) {
                if (_elMainContent.find("div.box-question").length > 0) {
                    if (_elSidebar.hasClass("sidebar-open"))
                        _elMainContent.css({ 'marginLeft': '285px' });
                    else if (_elSidebar.hasClass("sidebar-closed"))
                        _elMainContent.css({ 'marginLeft': '28px' });
                }
                else {
                    _elMainContent.css({ 'z-index': '250' });
                    if (_elMainContent.find("div.title-page").length > 0) {
                        if (_elSidebar.hasClass("sidebar-closed")) {
                            _elMainContent.css({ 'marginLeft': '28px' });
                            _elMainContent.find("div.title-page").css({ 'marginLeft': '8px' });
                        }
                        else {
                            _elMainContent.css({ 'marginLeft': _elSidebar.css('width') });
                            _elMainContent.find("div.title-page").css({ 'marginLeft': '0px' });
                        }
                    }
                    else {
                        if (_elSidebar.hasClass("sidebar-closed"))
                            _elMainContent.css({ 'marginLeft': '0px' });
                        else
                            _elMainContent.css({ 'marginLeft': '257px' });
                    }
                }
            }
            else {
                _elMainContent.css({ 'z-index': '250' });
                _elMainContent.css({ 'marginLeft': '0px' });
            }
        },
        fixLayout: function() {

            // fix width
            var wWindow = _elWindow.width();
            //var wSidebar = _elSidebar.width();

            if (isIE6 && wWindow < 981) {
                _elContainer.css({ 'width': '980px' });
            } else {
                _elContainer.css({ 'width': '100%' });
            }

            if (_elSidebar.length > 0) {
                if (_elMainContent.find("div.box-question").length > 0) {
                    if (_elSidebar.hasClass("sidebar-open"))
                        _elMainContent.css({ 'marginLeft': '285px' });
                    else if (_elSidebar.hasClass("sidebar-closed"))
                        _elMainContent.css({ 'marginLeft': '28px' });
                }
                else {
                    _elMainContent.css({ 'z-index': '250' });
                    if (_elMainContent.find("div.title-page").length > 0) {
                        if (_elSidebar.hasClass("sidebar-closed")) {
                            _elMainContent.css({ 'marginLeft': '28px' });
                            _elMainContent.find("div.title-page").css({ 'marginLeft': '8px' });
                        }
                        else {
                            _elMainContent.css({ 'marginLeft': _elSidebar.css('width') });
                            _elMainContent.find("div.title-page").css({ 'marginLeft': '0px' });
                        }
                    }
                    else {
                        if (_elSidebar.hasClass("sidebar-closed"))
                            _elMainContent.css({ 'marginLeft': '0px' });
                        else
                            _elMainContent.css({ 'marginLeft': '257px' });
                    }
                }
            }
            else {
                _elMainContent.css({ 'z-index': '250' });
                _elMainContent.css({ 'marginLeft': '0px' });
            }

            // fix sidebar height
            _elMainContent.height('auto');
            DefaultLayout.resizeSidebar();

            // fix profile page accordion
            if ($('.list-my-profile').length > 0) self.fixProfileList();
        },
        fixProfileList: function() {
            var totalHeaderHeight = ($('ul.list-my-profile > li').length + 1) * 34;
            var insideMainHeight = 0;
            $('ul.list-my-profile > li.open').children().each(function() {
                insideMainHeight += $(this).outerHeight(true);
            });
            var theHeight;
            if ((_documentHeight - 128) > $('.sidebar-content-inner').height() && (_documentHeight - 128) > insideMainHeight + totalHeaderHeight) {
                theHeight = _documentHeight - 128;
            } else if (insideMainHeight + totalHeaderHeight < $('.sidebar-content-inner').height()) {
                theHeight = $('.sidebar-content-inner').height();
            } else {
                theHeight = insideMainHeight + totalHeaderHeight;
            }

            _elMainContent.height(theHeight);
            //_elSidebar.height(theHeight);
            var profileListHeight = theHeight;

            $('ul.list-my-profile > li').height('auto');
            if (insideMainHeight < profileListHeight - totalHeaderHeight) {
                $('ul.list-my-profile > li.open').eq(0).height(profileListHeight - totalHeaderHeight);
            } else {
                $('ul.list-my-profile > li.open').eq(0).height(insideMainHeight);
            }
            DefaultLayout.resizeSidebar();
        }
    };
    return self;

} ();


$(document).ready(function() {
    setTimeout(Layout.init, 0);
    $("#navigation ul li a").hover(
        function() {
            $(this).parent().addClass("hover");
        },
        function() {
            $(this).parent().removeClass("hover");
        }
    );
    $("div#user-info a.coins").hover(
        function() {
            $(this).parents("div#user-info").eq(0).addClass("hover-coins");
            $(this).parents("div#user-info").eq(0).find(".tooltip-coins").eq(0).css({ "marginLeft": ($(this).position().left - 278 + $(this).width() / 2) + "px" });
        },
        function() {
            $(this).parents("div#user-info").eq(0).removeClass("hover-coins");
        }
    );
    $("div#user-info a.ts").hover(
        function() {
            $(this).parents("div#user-info").eq(0).addClass("hover-ts");
            $(this).parents("div#user-info").eq(0).find(".tooltip-ts").eq(0).css({ "marginLeft": ($(this).position().left - 278 + $(this).width() / 2) + "px" });
        },
        function() {
            $(this).parents("div#user-info").eq(0).removeClass("hover-ts");
        }
    );
    $("div#heatmeter-bg").hover(
        function() {
            $(this).parent().eq(0).addClass("hover");
            $(this).parent().eq(0).find(".tooltip").eq(0).css({ "marginLeft": (parseFloat($(this).children("a").css('margin-left')) - 165 + $(this).children("a").width() / 2) + "px" });
        },
        function() {
            $(this).parent().eq(0).removeClass("hover");
        }
    );
    $(".box-question ol li h3").hover(
        function() {
            $(this).addClass("hover");
        },
        function() {
            $(this).removeClass("hover")
        }
    );
    if ($("a[href$='/profile#bonus-store']").length > 0) {
        $("a[href$='/profile#bonus-store']").eq(0).click(function() {
            if ($('#jQuery_history').length > 0) {
                var iframe = $('#jQuery_history')[0].contentDocument || $('#jQuery_history')[0].contentWindow.document;
                iframe.location.hash = '#bonus-store';
            }
        });
    }
});

$.getDocumentHeight = function() {
    return Math.max(
        $(document).height(),
        $(window).height(),
    /* For opera: */
        document.documentElement.clientHeight
    );
};

function get_authenticity_token(){
  return AUTH_TOKEN || encodeURIComponent( $("input[name='authenticity_token']").val() )
}
