/**
 * BasicConfigurator.js - Euro Events basic configurator widget
 * 
 * @param {Mixed} form Selector or reference of the basic configurator form
 */
function BasicConfigurator(form) {
	this.form = $(form);
	this.defaults = null;
	this.init();
};

BasicConfigurator.prototype = {
	/**
	 * Initialize
	 */
	init: function() {
		if(this.form) {
			this.defaults = {
				propertiesUrl: '/catalog/product/properties.json',
				sizesUrl: '/catalog/product/colors.json'
			};
			
			this.form.find('.spinner-input').each(function() {
				new Spinner(this, {
					min: 1
				});
			});
			
			this.addEventListeners();
		}
	},
	
	/**
	 * Add event listeners
	 */
	addEventListeners: function() {
		var self = this;
		
		this.form.find('#basic-config-product').change(function() {
			self.getProductProperties(this.value);
		});

        this.form.find('#basic-config-size').change(function() {
            self.getProductSizes(this.value);
        });
	},
	
	/**
	 * Get product properties
	 * 
	 * @param {String} product A product identifier
	 */
	getProductProperties: function(product) {
		var self = this;
		
		$.ajax({
			url: self.defaults.propertiesUrl,
			cache: false,
			dataType: 'json',
			type: 'POST',
			data: 'data[Product][id]=' + product,
			success: function(data) {
				self.updateForm(data);
                self.getProductSizes($('#basic-config-size').val())
			}
		});
	},
	
	/**
	 * Get product sizes
	 * 
	 * @param {String} product A product identifier
	 */
	getProductSizes: function(size) {
		var self = this;
		
		$.ajax({
			url: self.defaults.sizesUrl,
			cache: false,
			dataType: 'json',
			type: 'POST',
			data: 'data[ProductOption][id]=' + size,
			success: function(data) {
				self.updateSizesForm(data);
			}
		});
	},

	/**
	 * Update the form
	 * 
	 * @param {Object} data The updated form data in JSON format
	 */
	updateForm: function(data) {
		/**
		 * JSON:
		 * 
			{
				"sizes": [
					{
						"text": "Kies uw formaat",
						"value": "",
						"defaultSelected": true,
						"selected": true
					},
					{
						"text": "15 mm",
						"value": "15",
						"defaultSelected": false,
						"selected": false
					},
					{
						"text": "25 mm",
						"value": "25",
						"defaultSelected": false,
						"selected": false
					}
				],
				"printMethods": {
					"print": true,
					"engrave": false,
					"numbering": false
				}
			}
		 */
		
		// Sizes
		var sizeSelect = this.form.find('#basic-config-size')[0];
		WS.Util.clearSelect(sizeSelect);
		WS.Util.addOptionsJSON(sizeSelect, data.sizes);
		
		// Print methods
		this.form.find('input[name*="printMethod"]').each(function() {
			if(data.printMethods[this.value] == false) {
				$(this).attr('disabled', true).parent().hide();
			}
			else {
				$(this).attr('disabled', false).parent().show();
			}
		});
	},

	/**
	 * Update sizes form
	 * 
	 * @param {Object} data The updated form data in JSON format
	 */
	updateSizesForm: function(data) {
		/**
		 * JSON:
		 * 
			{
				"printMethods": {
					"print": true,
					"engrave": false,
					"numbering": false
				}
			}
		 */

		// Print methods
		this.form.find('input[name*="printMethod"]').each(function() {
            if (data.printMethods) {
                if(data.printMethods[this.value] == false) {
                    $(this).attr('disabled', true).parent().hide();
                }
                else {
                    $(this).attr('disabled', false).parent().show();
                }
            }
		});
	}
};
