/**
 *	@author Jan De Wilde
 *	@date 30.08.2011
 *	@description PDF basket logic for 1Click CMS @ Wax! Interactive
 *	@version 1.0
 */
 
if(typeof $.BASKET == "undefined") {
	$.BASKET = Object;
};

$.BASKET = {
	cookieName : 'pdfbasket',
	cookieProperties : { path: '/' },
	itemDivider : '#',
	itemsOverviewId : 'itemsOverview',
	addClass : 'addToBasket',
	removeClass : 'removeFromBasket',
	basketClass : 'viewBasket',
	itemAttr : 'rel',
	itemAddedText : 'Item added',
	noItemsInBasket : 'There are no items in your basket.',
	
	construct : function() {
		this.setActions();
	},
	
	setActions : function() {
		var _this = this;
		$('.'+_this.addClass+', .'+_this.removeClass).bind('click', function(e) {
			var $cartRef = $(this);
			var itemValue = $cartRef.attr(_this.itemAttr);
			if(itemValue == '') {
				alert("Value is empty");
			} else {
				switch($cartRef.attr('class')) {
					case _this.addClass:
						_this.addItem(itemValue,$cartRef);
					break;
					case _this.removeClass:
						_this.removeItem(itemValue,$cartRef);
					break;
					default:
						alert("ID not found");
					break;
				}
			}
			e.preventDefault();
		});
	},
	
	getCookie : function() {
		var _this = this;
		return $.cookie(_this.cookieName);
	},
	
	setCookie : function(itemValue) {
		var _this = this;
		if(typeof(itemValue) === "undefined") {
			$.cookie(_this.cookieName, '', _this.cookieProperties);
		} else {
			$.cookie(_this.cookieName, itemValue, _this.cookieProperties);
		}
	},
	
	clearCookie : function() {
		var _this = this;
		$.cookie(_this.cookieName, null, _this.cookieProperties);
	},
	
	addItem : function(itemValue,$cartRef) {
		var _this = this;
		var cookie = _this.getCookie();
		if(cookie === null || cookie === '') {
			_this.setCookie(itemValue);
		} else {
			var arrItems = _this.getItems(cookie);
			if($.inArray(itemValue,arrItems) == -1) {
				arrItems.push(itemValue);
				_this.setCookie(arrItems.join(_this.itemDivider));
			}
		}
		_this.addItemSuccess($cartRef);
	},
	
	removeItem : function(itemValue,$cartRef) {
		var _this = this;
		var cookie = _this.getCookie();
		if(cookie !== null) {
			var arrItems = _this.getItems(_this.getCookie());
			var itemIndex = $.inArray(itemValue, arrItems);
			if(itemIndex != -1) {
				arrItems = $.grep(arrItems, function(n){
					return (n != itemValue);
				});
				if(arrItems.length > 0) {
					_this.setCookie(arrItems.join(_this.itemDivider));
				} else {
					_this.clearCookie();
				}
			}
		}
		_this.removeItemSuccess($cartRef);
	},
	
	getItems : function(cookie) {
		var _this = this;
		return cookie.split(_this.itemDivider);
	},
	
	updateBasket : function() {
		var _this = this;
		if(_this.getCookie() === null) {
			$('.basket .count').text('0');
		} else {
			$('.basket .count').text(_this.getItems(_this.getCookie()).length);
		}
		// Update cufon here
	},
	
	addItemSuccess : function($cartRef) {
		var _this = this;
		var $addOn = $('<span>')
		.addClass(_this.addClass+'Success')
		.text(_this.itemAddedText);
		$cartRef.fadeOut('fast', function() {
			$cartRef.after($addOn);
			setTimeout(function() {
				$addOn.fadeOut('fast', function() {
					$(this).remove();
					$cartRef.next('.'+_this.basketClass).fadeIn('fast');
				});
			}, 2500);
		});
		_this.updateBasket();
	},
	
	removeItemSuccess : function($cartRef) {
		var _this = this;
		$cartRef.parents('tr').hide(function() {
			$(this).remove();
		});
		if(_this.getCookie() === null) {
			var $noItemsInBasket = $('<p>').text(_this.noItemsInBasket).hide();
			$('#'+_this.itemsOverviewId).fadeOut(function() { $(this).after($noItemsInBasket).next().show().prev().remove(); });
		}
		_this.updateBasket();
	}
};
