/// <reference path="/Telerik.UI/js/ui.js" />
/// <reference path="//Microsoft.WinJS.1.0/js/ui.js" />  
/// <reference path="//Microsoft.WinJS.1.0/js/base.js" /> 
(function () {
	"use strict";

	WinJS.UI.Pages.define("pages/gridFilter/gridFilter.html", {
		ready: function (element, options) {
		},

		unload: function () {
		},

		updateLayout: function (element, viewState, lastViewState) {
		}
	});
	WinJS.Namespace.define("GridFilterExample", {
		getFiltersForField: function (expression, field) {
			var filters = [];
			((expression || {}).filters || []).forEach(function (filter) {
				if (filter.field === field) {
					filters.push(filter);
				}
			});
			return filters;
		},
		filterByDate: function (operator, actual, specified) {
			filters = {
				"gte": function (a, b) {
					return a - b >= 0
				},
				"lte": function (a, b) {
					return a - b <= 0
				}
			}

			actual = new Date(actual);
			specified = new Date(specified);

			return filters[operator](actual, specified);
		},
		refreshDateFilter:WinJS.Utilities.markSupportedForProcessing(function(element, expression) {
			var filters = GridFilterExample.getFiltersForField(expression, "HireDate"),
			value;
			
			if (filters[0] && fromDatePicker) {
				value = new Date(+filters[0].value);
				fromDatePicker.winControl.value = value;
			}

			if (filters[1] && toDatePicker) {
				value = new Date(+filters[1].value);
				toDatePicker.winControl.value = value;
			}
		}),
		applyDateFilter: WinJS.Utilities.markSupportedForProcessing(function (element, expression) {
			var filters = GridFilterExample.getFiltersForField(expression, "HireDate"),
			from = fromDatePicker.winControl.value,
			to = toDatePicker.winControl.value;
			if (!from && !to) {
				//reset the form and remove all filters for this field
				element.reset();
				return false;
			}
			//remove all existing filters for this field
			filters.forEach(function (filter) {
				expression.filters.splice(expression.filters.indexOf(filter), 1);
			});
			if (from) {
				//create a filter expression with a custom operator function
				expression.filters.push({
					field: "HireDate",
					operator: GridFilterExample.filterByDate.bind(null, "gte"),
					value: from.valueOf() + ""
				});
			}
			if (to) {
				//create a filter expression with a custom operator function
				expression.filters.push({
					field: "HireDate",
					operator: GridFilterExample.filterByDate.bind(null, "lte"),
					value: to.valueOf() + ""
				});
			}
			return expression;
		}),
		Data:[
			{
				Name: 'Kenya Bruni',
				City: 'Anchorage',
				HireDate: new Date(1995, 12, 14), 
				JobTitle: 'Chief Accounting Officer',
				RecID: 1
			}, {
				Name: 'Karry Evertt',
				City: 'San Antonio',
				HireDate: new Date(2005, 1, 7),
				JobTitle: 'Chief Information Technology Officer',
				RecID: 2
			}, {
				Name: 'Jacklyn Emayo',
				City: 'Boston',
				HireDate: new Date(1999, 5, 22),
				JobTitle: 'Chief Development Officer',
				RecID: 3
			}, {
				Name: 'Robin Crotts',
				City: 'New York',
				HireDate: new Date(1996, 6, 19),
				JobTitle: 'Chief Financial Officer',
				RecID: 4
			}
		]
	}); 
})();