$( function() {
	tagsObj.init();
})
var tagsObj = {
    list: null,
    openHeight: null,
    closeHeight: '120px',
    init: function () {
        tagsObj.list = $('.tag-list');
        tagsObj.closeHeight = tagsObj.list.height();
        tagsObj.list.css({
            height: 'auto'
        });
        tagsObj.openHeight = tagsObj.list.height();
        tagsObj.list.css({
            height: tagsObj.closeHeight
        });
        //open/close
        $("#tags .more").bind("click", function () {
            var btn = $(this);
            if (btn.hasClass('open')) {
                btn.removeClass('open');
                tagsObj.close();
            } else {
                btn.addClass('open');
                tagsObj.open();
            }
            return false;
        })
        //filter
        $('.tag-type a').bind('click', function () {
            $('.tag-type li').removeClass('selected');
            $(this).parent().addClass('selected');
            $('.tag-type a').removeClass("selected");
            var type = $(this).text();
            tagsObj.list.find('li').each(function () {
                if ($(this).hasClass(type) || type == 'All') {
                    $(this).show();
                } else {
                    $(this).hide();
                }
            });
            tagsObj.resetOpenClose();
            return false;
        })

        //click on a tag
        $(".tag-list a").bind("click", function () {
            $(".tags-info").show();
            $(".tags-info .tag-title").text($(this).text());
            $(".product-list>li").hide();
            var tagClass = $(this).text();
            $(".product-list>li." + tagClass).show();
            if (!$(this).attr('href')) {
                return false;
            }
        })
        $(".tags-info.product-filter .close-button").bind('click', function () {
            $(".product-list>li").show();
            $(".tags-info").hide();
            return false;
        })
        $(".tags-info.product-filter").hide();
        tagsObj.resetOpenClose();
    },
    resetOpenClose: function () {
        tagsObj.list.css({
            height: 'auto'
        });
        tagsObj.openHeight = tagsObj.list.height();
        tagsObj.list.css({
            height: tagsObj.closeHeight
        });
        if (tagsObj.closeHeight >= tagsObj.openHeight) {
            $("#tags .more").hide();
        } else {
            $("#tags .more").show();
        }
        $("#tags .more .label").text('SHOW MORE');
        $("#tags .more").removeClass('open');
    },
    open: function () {
        $("#tags .more .label").text('HIDE');
        tagsObj.list.animate({
            height: tagsObj.openHeight
        }, 500);
    },
    close: function () {
        $("#tags .more .label").text('SHOW MORE');
        tagsObj.list.animate({
            height: tagsObj.closeHeight
        }, 500);
    }
}
