﻿var showingFilters = false;
var extendedItems = [];
function ExtendItem(item) {
	var tempItem = extendedItems[item.ProductNumber];
	if((tempItem == undefined) || (tempItem == null)) {
		item.Rendered = null;
		item.Render = function() {
			if(this.Rendered != null)
				return;
			var container = $('<li/>');
			if(this.ImageUrl != null) {
				var imageAnchor = $('<a/>').attr('href', this.ProductUrl);
				$('<img/>').attr('src', this.ImageUrl).css('border', '0').attr('alt', this.ProductName + ' ' + this.ProductNumber).appendTo(imageAnchor);
				imageAnchor.appendTo(container);
				$('<br/>').appendTo(container);
			}
			$('<a/>').attr('href', this.ProductUrl).text(this.ProductName + ' ' + this.ProductNumber).appendTo(container);
			$('<br/>').appendTo(container);
			switch(this.ProductType) {
				case('Ceiling Fan'):
					if(this.Finish != null)
						$('<div/>').text('Finish: ' + this.Finish).appendTo(container);
					if(this.Location != null)
						$('<div/>').text('Location: ' + this.Location).appendTo(container);
					if(this.MotorSize != null)
						$('<div/>').text('Motor Size: ' + this.MotorSize).appendTo(container);
					if(this.Span != null)
						$('<div/>').text('Span: ' + this.Span.toString()).appendTo(container);
					if((this.Housing != null) && (this.Housing.length > 0))
						$('<div/>').text('Housing: ' + this.Housing.toString()).appendTo(container);
					$('<br/>').appendTo(container);
					break;
				case('Downrods'):
					if(this.Length != null)
						$('<div/>').text('Length: ' + this.Length).appendTo(container);
					if(this.Finish != null)
						$('<div/>').text('Finish: ' + this.Finish).appendTo(container);
					break;
				case('Light Fixtures'):
					if(this.Glass != null)
						$('<div/>').text('Type: ' + this.Glass).appendTo(container);
					$('<br/>').appendTo(container);
					break;
				case('Blades'):
					if(this.Finish != null)
						$('<div/>').text('Finish: ' + this.Finish).appendTo(container);
					if(this.Span != null)
						$('<div/>').text('Span: ' + this.Span.toString()).appendTo(container);
					$('<br/>').appendTo(container);
					break;
				case('Controls'):
					if(this.ControlType != null)
						$('<div/>').text('Type: ' + this.ControlType).appendTo(container);
					$('<br/>').appendTo(container);
					break;
				case('Other'):
					break;

			}
			this.Rendered = container;
		}
		extendedItems[item.ProductNumber] = item;
		return (item);
	}
	else {
		return (tempItem);
	}
}
function getListGuid(productType) {
	switch(productType) {
		case('Ceiling Fan'):
			return('{3cfb2ae9-8bae-47d8-9dbf-f61c7928102e}');
		case('Downrods'):
			return('{c0fd4e73-9677-4e66-9092-d1af3c9d845c}');
		case('Light Fixtures'):
			return('{85ae173e-0411-482d-a3eb-9b220aceb3b6}');
		case('Blades'):
			return('{21608260-df2b-4098-969e-1129af36b852}');
		case('Controls'):
			return('{99f97ec2-3de0-4883-aaa3-d99c1773f469}');
		case('Other'):
			return('{47d0b7a5-fe13-4e86-adc1-1b79fdef9fe3}');
		default:
			return('');
	}
	return('');
}
function JsonSearch() {
	$('#ListSearchTextBox').JsonServerSideSearch({
		filterOnProperties: [
			{ Property: 'ProductType', DisplayText: 'Product Type', Type: 'Text', DefaultValue: 'Ceiling Fan', Key: true,
				Values: function() {
					var values = [];
					$.ajax({ url: '/_layouts/Json.aspx?ListGuid={8797548e-c910-49e8-8ccb-1d9a5df1ed95}', async: false, dataType: 'json', success: function(data) { $.each(data, function(i, item) { values.push(item.Title); }); } });
					return (values);
				}
			},
			{ Property: 'Finish', DisplayText: 'Finish', Type: 'Text', DefaultValue: null, Key: false, Values: function() { return ([]); } },
			{ Property: 'Location', DisplayText: 'Location', Type: 'Text', DefaultValue: null, Key: false, Values: function() { return ([]); } },
			{ Property: 'Housing', DisplayText: 'Housing', Type: 'Text', DefaultValue: null, Key: false, Values: function() { return ([]); } },
			{ Property: 'MotorSize', DisplayText: 'Motor Size', Type: 'Text', DefaultValue: null, Key: false, Values: function() { return ([]); } },
			{ Property: 'Span', DisplayText: 'Span', Type: 'Text', DefaultValue: null, Key: false, Values: function() { return ([]); } },
			{ Property: 'Length', DisplayText: 'Length', Type: 'Text', DefaultValue: null, Key: false, Values: function() { return ([]); } },
			{ Property: 'Glass', DisplayText: 'Glass', Type: 'Text', DefaultValue: null, Key: false, Values: function() { return ([]); } },
			{ Property: 'ControlType', DisplayText: 'Control Type', Type: 'Text', DefaultValue: null, Key: false, Values: function() { return ([]); } },
			{ Property: 'AccessoryType', DisplayText: 'Accessory Type', Type: 'Text', DefaultValue: null, Key: false, Values: function() { return ([]); } },
			{ Property: 'EnergyStar', DisplayText: 'Energy Star', Type: 'Boolean', DefaultValue: null, Key: false, Values: [true, false] },
			{ Property: 'NewProduct', DisplayText: 'New Product', Type: 'Boolean', DefaultValue: null, Key: false, Values: [true, false] },
			{ Property: 'IncludesReceiver', DisplayText: 'Includes Receiver', Type: 'Boolean', DefaultValue: null, Key: false, Values: [true, false] }
		],
		keyProperties: [
			{ Property: 'ProductType', Value: 'Ceiling Fan', FilterOnProperties: ['Finish', 'Location', /*'MotorSize',*/ 'Housing', 'Span', 'EnergyStar', 'NewProduct'] },
			{ Property: 'ProductType', Value: 'Downrods', FilterOnProperties: ['Length', 'Finish'] },
			{ Property: 'ProductType', Value: 'Blades', FilterOnProperties: ['Span', 'Finish'] },
			{ Property: 'ProductType', Value: 'Light Fixtures', FilterOnProperties: ['Glass'] },
			{ Property: 'ProductType', Value: 'Controls', FilterOnProperties: ['ControlType', 'IncludesReceiver'] },
			{ Property: 'ProductType', Value: 'Other', FilterOnProperties: ['AccessoryType'] }
		],
		keyFiltersContainer: '#JsonSearchKeyFilters',
		filtersContainer: '#JsonSearchFilters',
		resultsContainer: '#JsonSearchResults',
		initialClass: 'watermarkOn',
		defaultText: 'Product Search',
		searchKeyPressTimeout: 500,
		columns: 3,
		clearResultsButton: '#LiveSearchClearResults',
		loadPreviousResultsButton: '#LiveSearchLoadPreviousResults',
		callbackOnFocus: function() {
			if(!showingFilters) {
				$('#ProductType').change();
				$('#JsonSearchKeyFilters').show('slow');
				$('#JsonSearchFilters').show('slow');
				$('#JsonSearchResults').show('slow');
				$('#LiveSearchClearResults').show('slow');
			}
			showingFilters = true;
		},
		callbackOnQueryChange: function(filters, hint) {
			var searchData = [];
			var productType = getProductType(filters);
			var ajaxUrl = '/_layouts/Json.aspx?ListGuid=' + getListGuid(productType);
			for(var i = 0; i < filters.length; i++) {
				if(typeof(filters[i].Value) == 'boolean') {
					ajaxUrl += '&' + filters[i].Property + '=' + ((filters[i].Value == true) ? '1' : '0');
				}
				else {
					if(filters[i].Value == 'true')
						ajaxUrl += '&' + filters[i].Property + '=' + '1';
					else if(filters[i].Value == 'false')
						ajaxUrl += '&' + filters[i].Property + '=' + '0';
					else
						ajaxUrl += '&' + filters[i].Property + '=' + filters[i].Value.toString();
				}
			}
			if(hint != '')
				ajaxUrl += '&HintScore=0.60&Hint=' + hint;
			$.ajax({ url: ajaxUrl, async: false, dataType: 'json', success: function(data) { $.each(data, function(i, item) { searchData.push(ExtendItem(item)); }); } });
			return (searchData);
		}
	});
}
function handleItems() {
	$.ajax({ url: '/_layouts/Json.aspx?ListGuid=' + getListGuid('Ceiling Fan'), async: true, dataType: 'json', success: function(data) { $.each(data, function(i, item) { ExtendItem(item); }); } });
	$.ajax({ url: '/_layouts/Json.aspx?ListGuid=' + getListGuid('Downrods'), async: true, dataType: 'json', success: function(data) { $.each(data, function(i, item) { ExtendItem(item); }); } });
	$.ajax({ url: '/_layouts/Json.aspx?ListGuid=' + getListGuid('Light Fixtures'), async: true, dataType: 'json', success: function(data) { $.each(data, function(i, item) { ExtendItem(item); }); } });
	$.ajax({ url: '/_layouts/Json.aspx?ListGuid=' + getListGuid('Blades'), async: true, dataType: 'json', success: function(data) { $.each(data, function(i, item) { ExtendItem(item); }); } });
	$.ajax({ url: '/_layouts/Json.aspx?ListGuid=' + getListGuid('Controls'), async: true, dataType: 'json', success: function(data) { $.each(data, function(i, item) { ExtendItem(item); }); } });
	$.ajax({ url: '/_layouts/Json.aspx?ListGuid=' + getListGuid('Other'), async: true, dataType: 'json', success: function(data) { $.each(data, function(i, item) { ExtendItem(item); }); } });
}
function getProductType(filters) {
	var productType = '';
	for(var i = 0; i < filters.length; i++) {
		if(filters[i].Property != 'ProductType')
			continue;
		productType = filters[i].Value;
	}
	return(productType);
}
