// Code for default.rhtml layout
var DefaultLayout = function() {

    var currentSidebarAccordeon = 'now';

    var self = {

        init: function() {
            self.initSidebar();
            self.initSidebarFeedback();
            self.initLogo();
            setInterval(DefaultLayout.decCounter, 1000);
        },

        decCounter: function() {
            if (typeof counterValue != 'undefined') {
                counterValue--;

                if (counterValue <= 0) {
                    counterValue = 14 * 24 * 60 * 60 - 1;
                }

                var d = Math.floor(counterValue / (3600 * 24));
                var h = Math.floor(counterValue / 3600) - d * 24;
                var m = Math.floor((counterValue % 3600) / 60);
                var s = counterValue - d * 3600 * 24 - h * 3600 - m * 60;

                document.getElementById('s_timer_val').innerHTML =
                (h >= 10 ? h : "0" + h) + "<span>h</span>. " +
                (m >= 10 ? m : "0" + m) + "<span>m</span>. " +
                (s >= 10 ? s : "0" + s) + "<span>s</span>.";
            }
        },

        toggleSidebar: function() {
            if ($(this).is('.toggle-open')) {
                sb = $('#content #sidebar');
                $.cookie('sideBar', 1, {
                    path: '/'
                });
                sb.removeClass('sidebar-closed').addClass('sidebar-open');
                sb.children('.toggle-close').show();
                sb.children('.toggle-open').hide();
                sb.css('left', '-260px');
                $('#content #sidebar').animate({
                    left: '0px'
                }, 300);

                if ($('#main-content').find("div.box-question").length > 0) {
                    $('#main-content').animate({
                        'marginLeft': '285px'
                    }, 400);
                }
                else {
                    if ($("div.title-page").length > 0) {
                        $('#main-content').animate({
                            'marginLeft': '257px'
                        }, 400);
                        $('div.title-page').animate({
                            'marginLeft': '0px'
                        }, 400);
                    }
                    else {
                        $('#main-content').animate({
                            'marginLeft': '257px'
                        }, 400);
                    }
                }
                self.resizeSidebar();
            } else {
                $.cookie('sideBar', 0, {
                    path: '/'
                });
                if ($('#main-content').find("div.box-question").length > 0) {
                    $('#main-content').animate({
                        'marginLeft': '28px'
                    }, 400);
                }
                else {
                    if ($("div.title-page").length > 0) {
                        $('#main-content').animate({
                            'marginLeft': '28px'
                        }, 400);
                        $('div.title-page').animate({
                            'marginLeft': '8px'
                        }, 400);
                    }
                    else {
                        $('#main-content').animate({
                            'marginLeft': '0px'
                        }, 400);
                    }
                }
                $('#content #sidebar').animate({
                    left: '-260px'
                }, 300, 'linear', function() {
                    sb = $('#content #sidebar');
                    sb.css('left', '0px');
                    sb.removeClass('sidebar-open').addClass('sidebar-closed');
                    sb.children('.toggle-close').hide();
                    sb.children('.toggle-open').show();
                });
                self.resizeSidebar();
            }
            return false;
        },

        toggleSidebarAccordeon: function() {
            accItem = $(this).parent();
            itemId = accItem.attr('id').replace('sidebar-', '');
            if (itemId == currentSidebarAccordeon) return false;
            $('#content #sidebar').children('.sidebar-content').children('ul.menu-sidebar').children('li').removeClass('current').children('.menu-sidebar-content').slideUp(300);
            accItem.children('.menu-sidebar-content').slideDown(300);
            accItem.addClass('current');
            currentSidebarAccordeon = itemId;
            $.cookie('sideBarAcc', itemId, {
                path: '/'
            })
            return false;
        },

        resizeSidebar:function(){
            setTimeout(function(){
                _elWindow = $(window);
                _elContainer = $(".container");
                _elSidebar = $("#sidebar");
                _elMainContent = $("#main-content");
                if ((_elWindow.height() - 128) > $('.sidebar-content-inner').height() && _elWindow.height() > _elContainer.height()) {
                    _elSidebar.height(_elWindow.height() - 128);
                } else if ($('.sidebar-content-inner').height() > _elContainer.height()) {
                    _elSidebar.height($('.sidebar-content-inner').height());
                    _elMainContent.height($('.sidebar-content-inner').height());
                } else {
                    _elSidebar.height(_elContainer.height() - 128);
                }
            }, 400);
        },

        initSidebar: function() {
            sb = $('#content #sidebar');
            if (sb.length) {
                sb.children('a.toggle-sidebar').click(self.toggleSidebar);

                if ($.cookie('sideBar') == null)
                    $.cookie('sideBar', 1, {
                        path: '/'
                    });

                if ($.cookie('sideBar') == 1 && sb.is(':visible')) {
                    sb.removeClass('sidebar-closed').addClass('sidebar-open');
                    if ($('#main-content').find("div.box-question").length > 0) {
                        $('#main-content').css('margin-left', '285px');
                    }
                    else {
                        if ($("div.title-page").length > 0) {
                            $('#main-content').animate({
                                'marginLeft': '257px'
                            }, 400);
                            $('div.title-page').animate({
                                'marginLeft': '0px'
                            }, 400);
                        }
                        else {
                            $('#main-content').css('margin-left', '257px');
                        }
                    }
                    sb.children('.toggle-close').show();
                    sb.children('.toggle-open').hide();
                    self.resizeSidebar();
                }
                sb.children('.sidebar-content').children('ul.menu-sidebar').children('li').children('a.toggle').click(self.toggleSidebarAccordeon);
                currentSidebarAccordeon = $.cookie('sideBarAcc');
                if (!currentSidebarAccordeon) currentSidebarAccordeon = 'now';
                $('#content #sidebar').children('.sidebar-content').children('ul.menu-sidebar').children('li#sidebar-' + currentSidebarAccordeon).addClass('current').children('div.menu-sidebar-content').show();
            } else setTimeout(self.initSidebar, 1000);
        },

        initSidebarFeedback: function() {
            $('#gsfn_widget').load(function() {
                $('#gsfn_widget').contents().find("a")
                .each(function(i, elem) {
                    $(elem).attr('target', '_blank');
                })
                $('#gsfn_widget').contents().find("li.gsfn_praise").each(function() {
                    $(this).children('.time').prependTo(this);
                })
            });
        },

        initLogo: function() {
            $('#header h1 span').click(function() {
                document.location = '/';
            })
        }

    };
    return self;
} ();

// prepare layout
$(document).ready(function() {
    setTimeout(DefaultLayout.init, 0);
});

function show_remain_characters_counter(el) {
    if (document.getElementById(el.id + "_counter") != undefined) {
        l = el.getAttribute('maxlength') - el.value.length;
        document.getElementById(el.id + "_counter").innerHTML = (l <= 9 ? "0" + l : l);
    }
    return true;
}
