﻿var searchWidget = function () {
    var self = this;
    this.state =
	{
	    element: null,
	    persist: null,
	    onchange: function () {
	        this.persist.value = this.element.value;
	        self.city.filterByState();
	    }
	};
    this.city =
	{
	    element: null,
	    persist: null,
	    data: [],
	    filterByState: function () {
	        this.element.innerHTML = "";
	        this.element.options.add(new Option("All Cities", ""));
	        var val = self.state.persist.value;
	        if (val == "")
	            return;
	        for (var i = 0; i < this.data.length; i++) {
	            var obj = this.data[i];
	            if (val == obj.s) {
	                this.element.options.add(new Option(obj.c, obj.c));
	            }
	        }
	    },
	    onchange: function () {
	        this.persist.value = this.element.value;
	    }
	};
    this.init = function () {
        $(this.state.element).val(this.state.persist.value);
        $(this.state.element).change(function () { self.state.onchange(); });
        $(this.city.element).val(this.city.persist.value);
    };
};
