$('document').ready(function () {
    SimplyButtons.init();
});

// page init
$(function () {
    initOpenClose();
    initTabs();
    initFilter();
    initSlide();
    ieHover('.post-holder, .filter dd', 'hover');
});
// filter function
function initFilter() {
    $('.filter dd, .filter2 dd').each(function () {
        var _drop = $(this).find('div.drop');
        var _box = $(this).find('span.s-option');
        var _items = _drop.find('li a');
        _items.click(function () {
            var _val = $(this).html();
            _box.html(_val);
            _box.parent('dd').removeClass('hover');
            return true;
        });
    });
}
function ieHover(h_list, h_class) {
    if (!h_class) var h_class = 'hover';
    $(h_list).mouseenter(function () {
        $(this).addClass(h_class);
    }).mouseleave(function () {
        $(this).removeClass(h_class);
    });
}
// open-close init
function initOpenClose() {
    $('ul.add-nav > li').OpenClose({
        activeClass: 'expanded',
        opener: '>a.sub',
        slider: '>ul.subnav',
        slideSpeed: 400
    });
}

// open-close plugin
jQuery.fn.OpenClose = function (_options) {
    var _options = jQuery.extend({
        activeClass: 'active',
        opener: '.opener',
        slider: '.slide',
        slideSpeed: 400,
        event: 'click'
    }, _options);

    return this.each(function () {
        // options
        var _holder = jQuery(this);
        var _slideSpeed = _options.slideSpeed;
        var _activeClass = _options.activeClass;
        var _opener = jQuery(_options.opener, _holder);
        var _slider = jQuery(_options.slider, _holder);
        var _event = _options.event;
        if (_slider.length) {
            _opener.bind(_event, function () {
                if (_holder.hasClass(_activeClass)) {
                    _slider.slideUp(_slideSpeed);
                    _holder.removeClass(_activeClass);
                } else {
                    _holder.addClass(_activeClass);
                    _slider.slideDown(_slideSpeed);
                }
                return false;
            });
            if (_holder.hasClass(_activeClass)) _slider.show();
            else _slider.hide();
        }
    });
}
// initTabs
function initTabs() {
    $('ul[class^=tabset]').each(function () {
        var _list = $(this);
        var _links = _list.find('a.tab');

        _links.each(function () {
            var _link = $(this);
            var _href = _link.attr('href');
            var _tab = $(_href);

            if (_link.hasClass('active')) _tab.show();
            else _tab.hide();

            _link.click(function () {
                _links.filter('.active').each(function () {
                    $($(this).removeClass('active').attr('href')).hide();
                });
                _link.addClass('active');
                _tab.show();
                return false;
            });
        });
    });
}

function initSlide() {
    var _duration = 300; //in ms
    $('div.panel').each(function () {
        var _hold = $(this);
        var _box = _hold.find('div.login');
        var _btn = _hold.find('a.lock');
        var _h = _box.height();
        var _username = _hold.find('input.username');
        if (_hold.hasClass('opened')) _box.show();
        else _box.hide();

        _btn.click(function () {
            if (_hold.hasClass('opened')) {
                _hold.removeClass('opened');
                _box.stop().animate({ height: 0 }, _duration, function () {
                    $(this).css({ display: 'none', height: 'auto' });
                });
            }
            else {
                _hold.addClass('opened');
                if (_box.is(':hidden')) {
                    _box.show();
                    _h = _box.height();
                    _box.height(0);
                }
                _box.stop().animate({ height: _h }, _duration, function () {
                    $(this).height('auto');
                })
            }
            return false;
        });

        _hold.find('.submit').click(function () {
            _btn.click();
            return true;
        });

    });
}
