﻿var basket = {
    cookieName: 'basketBoardIds',
    getBoardIds: function() {
        var boardIds = [];
        if (base.cookies.getCookie(this.cookieName))
            $j(base.cookies.getCookie(this.cookieName).split(',')).each(function(index, boardId) { boardIds.push(parseInt(boardId)); });
        return boardIds;
    },
    setBoardIds: function(boardIds) { base.cookies.setCookie(this.cookieName, boardIds.join(',')); },
    getCount: function() { return this.getBoardIds().length; },
    toggleBoardId: function(boardId) {
        var boardIds = this.getBoardIds();
        var boardIdIndex = boardIds.indexOf(boardId);
        if (boardIdIndex == -1)
            boardIds.push(boardId);
        else
            boardIds.splice(boardIdIndex, 1);
        this.setBoardIds(boardIds);
    },
    clear: function() {
        $j('#BoardListContainer a.buy').each(function(index, img) { img.className = 'buy2'; });
        this.getBoardIds().each(function(boardId) { $j('#tit_' + boardId).removeClass().addClass('tit'); });

        base.cookies.removeCookie(this.cookieName);
        this.refresh();
    },
    toggleAndRefresh: function(boardId, anchor) {
        this.toggleBoardId(boardId);
        this.refresh(boardId, anchor);
    },
    refresh: function(boardId, anchor) {
        if (boardId && anchor) {
            var isAdded = this.getBoardIds().indexOf(boardId) != -1;

            $('tit_' + boardId).className = isAdded ? 'tit hbas' : 'tit';
            anchor.className = isAdded ? 'buy' : 'buy2';
            anchor.setAttribute("alt", isAdded ? "убрать из корзины" : "добавить в корзину");
            anchor.setAttribute("title", isAdded ? "убрать из корзины" : "добавить в корзину");
        }
        $j('#BasketCount').html(this.getCount());
        $j('#notepad').css('display', this.getCount() > 0 ? 'block' : 'none');

        SetBasketPosition();
    }
}