
var Merkzettel, merkzettel;

Merkzettel = function ($) {
	
	var that = this;
	
	this.init = function () {
		this.show_products_count();
		
		this.get_products();
		
		this.set_checkboxes();
		
		this.set_remove_links();
	};
	
	this.show_products_count = function () {
		var t_cookie = getCookie('table_id_list'),
			c_cookie = getCookie('chair_id_list'),
			count;
		
		if (t_cookie == false || t_cookie == undefined) t_cookie = [];
		else t_cookie = t_cookie.split(',');
		
		if (c_cookie == false || c_cookie == undefined) c_cookie = [];
		else c_cookie = c_cookie.split(',');
		
		count = t_cookie.length + c_cookie.length;
		$('#bmm_mrk_field').val((count > 0 ? count : 'Keine') + ' Artikel');
		$('.merzettel_count').html((count > 0 ? count : 'Keine') + ' Artikel');
	};
	
	this.set_checkboxes = function () {
		$('input[type="checkbox"].merkzettel').each(function () {
			$(this).unbind().click(function (event) {
				event.stopImmediatePropagation();
				that.update_products();
			});
		});
	};
	
	this.get_products = function () {
		if (typeof(merkzettel_realm) == 'undefined') return;
		
		var cookie = getCookie(merkzettel_realm + '_id_list'),
			i;
		
		if (cookie == false || cookie == undefined) return;
		
		cookie = cookie.split(',');
				
		$('input[type="checkbox"].merkzettel').attr('checked', false);
		
		for (i = 0; i < cookie.length; i++) {
			$('input[type="checkbox"].merkzettel.item-' + cookie[i]).attr('checked', true);
		}
	};
	
	this.update_products = function () {
		if (typeof(merkzettel_realm) == 'undefined') return;
		
		var cookie = getCookie(merkzettel_realm + '_id_list');
		
		if (cookie == false || cookie == undefined) cookie = [];
		else cookie = cookie.split(',');
				
		$('input[type="checkbox"].merkzettel').each(function () {
			if($(this).attr('checked') == true && cookie.indexOf($(this).attr('item_id')) < 0) {
				$('input[type="checkbox"].merkzettel.item-' + $(this).attr('item_id')).attr('checked', true);
				cookie.push($(this).attr('item_id'));
			} else if ($(this).attr('checked') == false && cookie.indexOf($(this).attr('item_id')) >= 0) {
				$('input[type="checkbox"].merkzettel.item-' + $(this).attr('item_id')).attr('checked', false);
				cookie = cookie.without($(this).attr('item_id'));
			}
		});
		
		setCookie(merkzettel_realm + '_id_list', cookie.join(','));
		
		this.get_products();
		this.show_products_count();
	};
	
	this.remove_product = function (id, type) {
		if (type == "") return;
		
		var cookie = getCookie(type + '_id_list');
		
		if (cookie == false || cookie == undefined) return;
		
		cookie = cookie.split(',');
		
		cookie = cookie.without(id);
		
		setCookie(type + '_id_list', cookie.join(','));
		
		$('.merkzettel.item-' + id).remove();
		
		this.get_products();
		this.show_products_count();
	};
	
	this.set_remove_links = function () {
		$('a.merkzettel_remove').click(function (event) {
			event.preventDefault();
			event.stopImmediatePropagation();
			that.remove_product($(this).attr('item_id'), $(this).attr('item_type'));
		});
	};
	
	
	$(document).ready(function () { that.init(); });
	
};

merkzettel = new Merkzettel(jQuery);

;if (typeof(window['wlog']) != "function") {
	window.wlog = function (m) {
		try {
			console.log(m);
		} catch (e) {}
	};
};
