$(function () { // seriesFilter
	$.fn.initProductFilter = function(settings) {
		settings = jQuery.extend({
			siteidVar: '1058161179129',
			sitelanguageVar: 'kor_be_home_nl',
			cidVar: '1206693445640'
		}, settings);
		
		return $(this).each(function () {
			// *********************** seriesfilter

			var rootelement = $(this);
			rootelement.find('.availableSeriesSelection option.hidden').removeClass('hidden');

			$.fn.updateProductSelectbox = function() {
				return $(this).each(function () {
					var that = $(this);

					switch(that.val()) {
						case '':
							rootelement.find('.seriesFilter button[type=submit]').hide();
							rootelement.find('.productFilter').hide();
							break;
						case 'ALL':
							rootelement.find('.seriesFilter button[type=submit]').show();
							rootelement.find('.productFilter').hide();
							break;
						default:
							rootelement.find('.productFilter').trigger('load.products');			
							rootelement.find('.seriesFilter button[type=submit]').hide();
							break;
					}
				});
			};
			
			rootelement.find('.availableSeriesSelection').updateProductSelectbox();
			rootelement.find('.availableSeriesSelection').change($.fn.updateProductSelectbox);

			rootelement.find('.availableSeriesSelection').parents('form').submit(function () {
				var form = $(this);
				var option = form.find('option:selected');
				if (option.val () === 'ALL') {
					form.find(':input').attr('disabled', 'disabled'); // disabling all input elements (including selects)
					form.find('.availableSeriesSelection').removeAttr('disabled'); // enabling select (option with value "ALL" should be selected)
					form.find('fieldset.ALL :input').removeAttr('disabled'); // enable all input elements from ALL fieldset
				} else {
					form.find(':input').removeAttr('disabled'); // enabling all input elements
					form.find('fieldset.ALL :input').attr('disabled', 'disabled');
					return false;
				}
			});
			
			
			// *********************** productfilter
			rootelement.find('.productFilter').removeClass('hidden').hide();
			
			rootelement.find('.availableProductsForSpecificSerieSelection').parents('form').submit( function () {
				var form = $(this);
				var option = form.find('option:selected');
				if (option.val() === 'ALL') {
					form.find(':input').attr('disabled', 'disabled');
					form.find('select').removeAttr('disabled');
					form.find('fieldset.ALL :input').removeAttr('disabled');
				}
			});

			rootelement.find('.productFilter').bind('load.products', function (event) {
				var productfilter = $(this);
				
				var cid = rootelement.find('.availableSeriesSelection').val();
				
				productfilter.find('option.postProcessed').remove();

				// updating input box in product area -> making search for specific series possible
				productfilter.find('fieldset.ALL :input[name=cid]').val(cid);

				var url = '/servlet/Satellite';
				var params = { 'pagename': 'Wienerberger/Dynamic/Dyn-ProductFilter-Products',
								'sl': settings.sitelanguageVar,
								'seriesId': cid,
								'siteId': settings.siteidVar,
								'cid': settings.cidVar,
								'c': 'Page'
								};

				var loadindicator = productfilter.prev('.loadindicator');
				if (loadindicator.length === 0) {
					loadindicator = $('<div class="loadindicator"><img src="/images/nav/load.gif"/></div>');
				}
				
				productfilter.hide().before(loadindicator);

				$.getJSON(url, params, 
					function(json) {
						$.each(json, function() {
							var seriesName = $.trim(rootelement.find('.seriesFilter option:selected').text());
							var productName = $.trim(this.assetName.replace(seriesName, ''));
							rootelement.find('.availableProductsForSpecificSerieSelection').append(
								$('<option value="' + this.assetId 
									+ '" class="postProcessed"'
									+ ' title="'+productName+'">' 
									+ productName 
									+ '</option>'));
						});
						rootelement.find('.availableProductsForSpecificSerieSelection option.loading').remove();
						loadindicator.remove();
						productfilter.show();
					});
				});			
		});
	};
});

