//'use strict';

/*global window, document, console, jQuery, debug, alert */

jQuery.noConflict();

if (typeof jQuery !== 'undefined') {

	// set log to shorthand
	window.log = window.debug = function () {
		window.log.history = window.log.history || [];
		window.log.history.push(arguments);
		if (this.console) {
			console.log(Array.prototype.slice.call(arguments));
		}
	};

	(function ($) {
		// run the application
		$(document).ready(function () {
			var $form			= $('#quick-brick-selector').children('form'),
				$colour			= $('#quick-brick-selector-colour'),
				selects			= ['type', 'texture', 'size'],
				$colourInputs	= $colour.find('input');

			$colour.bind('click', function (event) {
				var $label	= $(event.target).closest('label'),
					$li		= $label.closest('li').not('.disabled').not('.selected'),
					$input	= $li.find('input').not(':disabled');

				if ($input.length === 1) {
					$colourInputs.attr('checked', false);
					$input.attr('checked', true);
					$li.addClass('selected').siblings('li').removeClass('selected');

					$form.trigger('submit');
				}
			});

			$form.find('select').bind('change', function (event) {
				$form.trigger('submit');
			});

			$form.bind('submit reset', function (event) {
				var data		= $(this).serialize(),
					formData	= $(this).serializeArray(),
					$submit		= $(this).find('div.submit input');

				if (!event.originalEvent || event.type === 'reset') {
					event.preventDefault();

					if (event.type === 'reset') {
						data		= {};
						formData	= [];
						$submit.blur();
					}

					$.ajax({
						url:		'/quick-brick.asp',
						type:		'get',
						cache:		true,
						data:		data,
						dataType:	'json',
						success:	function (data) {
							var colours;

							// update the colours
							if (typeof data.colour !== 'undefined' && data.colour.length) {
								// map the colours so they're all lowercase
								colours = $.map(data.colour, function (c, i) {
									return c.toLowerCase();
								});

								$colour.find('li').each(function (i, element) {
									var $li		= $(element),
										$input	= $li.find('input'),
										val		= $input.val();

									$input.removeAttr('disabled');
									$li.removeClass('disabled');

									if (event.type === 'reset') {
										$input.removeAttr('selected').removeAttr('checked');
										$li.removeClass('selected');
									}

									if ($.inArray(val.toLowerCase(), colours) === -1) {
										$input.attr('disabled', true);
										$li.addClass('disabled');
									}
								});
							}

							// update the select boxes
							$.each(selects, function (s, type) {
								var id = 'quick-brick-selector-' + type + '-select',
									select = [],
									ss = 0;

								// update the select boxes
								if (typeof data[type] !== 'undefined' && data[type].length) {
									$.each(data[type], function (i, t) {
										var val			= t,
											text		= t,
											selected	= '';

										if (t.toLowerCase() === 'all') {
											val		= '';
											text	= 'All';
										}
										
										$.each(formData, function (ii, d) {
											if (d.name === type && d.value.toLowerCase() === t.toLowerCase()) {
												selected = ' selected="selected"';
											}
										});

										select[ss++] = '<option value="' + val + '"' + selected + '>';
										select[ss++] = text;
										select[ss++] = '</option>';
									});
								}

								$('#' + id).empty().append(select.join(''));
							});
						},
						beforeSend:	function (XMLHttpRequest) {},
						error:		function (XMLHttpRequest, textStatus, errorThrown) {},
						complete:	function (event, XMLHttpRequest, ajaxOptions) {}
					});
				}
			});
		});
	}(jQuery));
}
