/**
 * checkout provides the checkout module with additional opdated order-information from the server
 * @author Tom Cool @ FrenzyMedia B.V. - http://frenzymedia.eu
 * @class checkout object
 * @constructor
 * @param {jQuery} $ The jQuery object - to prevent conflicts with similar libraries
 */
var shoppingcart = (function ($) {

	/**
	 * Additional css-selectors are stored seperately
     * @namespace Private properties
     */
	var selectors = {
		cart :		'.module.shoppingcart',
		list :		'.module.shoppingcart .shoppingcart_list',
		total :		'.module.shoppingcart .totalprice',
		add :		'#inbasket',
		remove :	'.shoppingcart .remove a',
		articleid : '.articleid'
	},

	/**
     * @namespace Private properties and methods
     */
	privs = {

		/**
         * Removes an item from the shoppingcart
         * @type jQuery ajax
         * @private
         */
		remove : function (id) {

			$.ajax({
				type: 'post',
				url: '/winkelwagen/verwijder/' + id,
				data: 'checkout=true',
				success: function (data) {
					data = eval(data);
					
					/**
					 * update shoppingcart within the header
					 * @todo header wil be a promobar in de future
					 */
					UpdateWinkelwagen(data['totaalprijs'], data['aantal']);

					shoppingcart.update();
				}
			});
		},

		/**
		 * Observer
         * When an item is added to the shoppingcart, the list in the shoppingcart-module is updated after 2 secs
		 * @todo fix shoppingcart parent functionality
         * @private
         */
		setUpdateListner : function () {
			$(selectors.add).live('click', function () {
				//timeout is used, because functionality that updates the parent shoppingcart is initiated several times....
				setTimeout( function () { shoppingcart.update() }, 2000);
			});
		},

		/**
         * Observer for removing an item from the shoppingcart module
         * @private
         */
		setRemoveListner : function () {
			$(selectors.remove).live('click', function (){
				var id = this.id;
				privs.remove(id.substring(3, id.length));
				return false;
			});
		}
	};

	/**
	 * @namespace Public properties and methods
     * @scope log
     */
	return {

		/**
         * Updates the shoppingcart, also within the module
		 * @type jQuery ajax
         * @public
         */
		update : function () {

			$.ajax({
				type : 'get',
				url : '/modules/shoppingcart/shoppingcart.php',
				data : 'list=true',
				success : function (data) {
					data = eval(data);
					$(selectors.list).html(data[0]);
					$(selectors.total).html(data[1]);
				}
			});
		},

		/**
		 * Function to be launched at body.onload
		 * @public
		 */
		initiate : function () {
			privs.setUpdateListner();
			privs.setRemoveListner();
		}
	};

})(jQuery);

/**
 * The initiate method is launched on page load
 */
jQuery( function () {shoppingcart.initiate();});
