/* Minification failed. Returning unminified contents.
(1,1): run-time error CSS1019: Unexpected token, found '$'
(1,2): run-time error CSS1019: Unexpected token, found '('
(1,11): run-time error CSS1031: Expected selector, found ')'
(1,11): run-time error CSS1025: Expected comma or open brace, found ')'
(67,2): run-time error CSS1019: Unexpected token, found ')'
(69,22): run-time error CSS1031: Expected selector, found '='
(69,22): run-time error CSS1025: Expected comma or open brace, found '='
(70,18): run-time error CSS1031: Expected selector, found '='
(70,18): run-time error CSS1025: Expected comma or open brace, found '='
 */
$(document).ready(function () {
    var types = $('#mag-type > label input'),
        magContent = $("#mag-content"),
        articlesContainer = $("#mag-articles"),
        moreButton = $('#mag-more-articles'),
        data = articlesContainer.data('articles'),
        thematics = $('#mag-thematic-menu li.level > a');

    var query = $.pageQuery({
        'default': {
            types: []
        },
        onChange: function (query) { }
    });

    magContent.loader();
    types.change(function () {
        var type = $(this);
        var value = type.val();
        var index = -1;
        for (var i in query.types) {
            var current = query.types[i];
            if (current === value) {
                index = i;
            }
        }
        if (index >= 0) {
            query.types.splice(index, 1);
        } else {
            query.types.push(value);
        }
        $.pushPageQuery(query);
        magContent.loader('open');
        loadArticles(articlesContainer, moreButton, data, query, 0, function (html) {
            articlesContainer.html(html);
            magContent.loader('close');
        });
    });

    var moreLoader = $('<div class="more-loader"></div>');
    moreButton.click(function () {
        var offset = articlesContainer.find('.mag-article-preview').length;
        articlesContainer.append(moreLoader);
        moreLoader.loader();
        moreLoader.loader('open');
        loadArticles(articlesContainer, moreButton, data, query, offset, function (html) {
            articlesContainer.append(html);
            moreLoader.loader('close');
            moreLoader.remove();
        });
    });

    thematics.click(function (e) {
        e.preventDefault();
        var href = $(this).attr('href');
        var urlQuery = $.param(query, true);
        var target = href;
        if (urlQuery) {
            target += '?' + urlQuery;
        }
        document.location = target;
    });

    $('.product-zone-container').productZone();
    $("#mag-thematic .banner-zone-container").bannerZone();
    $("#mag-home .banner-zone-container").bannerZone();
});

var loadArticleQuery = undefined;
var loadArticles = function (articlesContainer, moreButton, data, query, offset, onLoad) {
    if (loadArticleQuery) {
        loadArticleQuery.abort();
    }
    loadArticleQuery = $.ajax(data.action, {
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({
            thematicId: data.thematicId,
            types: query.types,
            offset: offset
        })
    }).done(function (html) {
        onLoad(html);
        if (articlesContainer.find('.last-article').length) {
            moreButton.prop('disabled', true);
        } else {
            moreButton.prop('disabled', false);
        }
    }).fail(function (jqXHR, textStatus, errorThrown) {
        if (textStatus !== "abort") {
            if (window.console) {
                window.console.error(jqXHR);
            }
            onLoad(undefined);
        }
    });
};
