﻿var basket = {
	cookieName: 'newBasketBoardIds',
	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);
			$j.get("/Basket/Notepad.aspx", { notepadAction: "push", notepadBoardId: boardId });
		}
		else {
			boardIds.splice(boardIdIndex, 1);
			$j.get("/Basket/Notepad.aspx", { notepadAction: "splice", notepadBoardId: boardId });
		}
		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;
			jQuery('tit_' + boardId).className = isAdded ? 'tit hbas' : 'tit';
			anchor.className = isAdded ? 'buy' : 'buy2';
			anchor.setAttribute("alt", isAdded ? "убрать из корзины" : "добавить в корзину");
			anchor.setAttribute("title", isAdded ? "убрать из корзины" : "добавить в корзину");
		}
		var sum = this.getCount();
		$j('#BasketCount2').html(sum);
		if (sum > 0) {
			$j('#B1').css('color', '#CC0011');
			$j('#BasketCount2').css('color', '#CC0011');
		} else {
			$j('#B1').css('color', '#005CC3');
			$j('#BasketCount2').css('color', '#005CC3');
		}
		//$j('#notepad2').css('display', this.getCount() > 0 ? 'block' : 'none');
		//$j('#BasketCount').html(this.getCount());
		//$j('#notepad').css('display', this.getCount() > 0 ? 'block' : 'none');
		//SetBasketPosition();
	}
}
