var Search =
{
    //  {{{ properties

    catSelected: false,
    subCatSelected: false,

    //  }}}
    //  {{{ init()

	init: function()
	{
        $(".t-list-link").bind("click", function(){
            var href = $(this).attr('href');
            $(this).attr('href', href + '&pageOffset=' + window.pageYOffset);
        });

		$('.search-result-item')
            .mouseover(Search.highlightMember)
            .mouseout(Search.lowlightMember)
            .click(Search.goToMember);

		//	Stop the propagation so the goToMember function won't
		//	be called when these links are clicked.
		$('.search-result-email, .search-result-website, .search-result-directions')
			.click(function(event) {
				event.stopPropagation();
			});

		$("select[name='category_id']").change(Search.populateMemberSubType);
		$("select[name='city_id']").change(Search.repopulateCategories);

        Search.setGET();
		Search.repopulateCategories();
		Search.populateMemberSubType();
	},

    //  }}}
    //  {{{ setGET()

    setGET: function()
    {
        var get=(""+location.search).substring(1).split("&");
        window.location.GET = new Array();
        for (var i in get) {
            var temp = get[i].split("=");
            window.location.GET[temp[0]] = temp.splice(1, temp.length-1).join("=");
        }
    },

    //  }}}
    //  {{{ highlightMember()

	highlightMember: function(event)
	{
		if ($(this).find("a[title='More Info']").is('a')) {
			$(this).addClass('search-result-item-on');
		}
	},

    //  }}}
    //  {{{ lowlightMember()

	lowlightMember: function(event)
	{
		if ($(this).find("a[title='More Info']").is('a')) {
			$(this).removeClass('search-result-item-on');
		}
	},

    //  }}}
    //  {{{ goToMember()

	goToMember: function(event)
	{
		var link = $(this).find("a.moreInfo");
		if (link.is('a')) {
			document.location.href = link.attr('href');
		}
	},

    //  }}}
    //  {{{ repopulateCategories()

    /**
     * repopulate the sub category box based on the the value
     * selected in the city box
     *
     * if no city was selected, then get all main categories available
     *
     * if a city was selected, then get all main categories available in that
     * city.
     */
    repopulateCategories: function(event)
    {
        var city = $("select[name='city_id'] option:selected").val();

		var catList = $("select[name='category_id']");

        $("select[name='category_id'], select[name='sub_category_id']")
            .empty()
            .append('<option value="">-- Select --</option>');

		var usedValues = [];
		var options = [];
		var cats = [];
        if (city == '') {
			if (window.CityCats != undefined) {
				$.each(CityCats, function(id, obj) {
					if (CityCats[id]) {
						$.each(CityCats[id], function(i, j) {
							if (!usedValues[i]) {
								usedValues[i] = true;
								var cat = mainCats[i];
								cats[mainCats[i]] = i;
								options.push(mainCats[i]);
							}
						});
					}
				});
			}
        } else {
			if (window.CityCats != undefined) {
				$.each(CityCats[city], function(id, obj) {
					if (CityCats[city]) {
						var cat = mainCats[id];
						cats[mainCats[id]] = id;
						options.push(mainCats[id]);
					}
				});
			}
        }

		options.sort();
		$.each(options, function(idx, itm) {
			catList.append('<option value="'+cats[itm]+'">'+itm+'</option>');
		});

        //  If only one option is available, default to have it selected.
		var catListOptions = catList.children('option');
        if (catListOptions.length == 2) {
            $("select[name='category_id'] option:last")
				.attr('selected', 'selected');
            //  need to repopulate the sub cats, b/c we already know what
            //  the selected cat is.
            Search.populateMemberSubType(false);
        }

        //  Figure out wich category we need to set as the selected value
        var category_id = window.location.GET['category_id'];
        if (!isNaN(parseInt(category_id))) {
            if (!Search.catSelected) {
                Search.catSelected = true;
                $("select[name='category_id'] option[value='"+category_id+"']").attr('selected', 'selected');
            }
        }
    },

    //  }}}
    //  {{{ populateMemberSubType()

    /**
     * repopulate the sub category box based on the the value
     * selected in the main category box
     *
     * if no city was selected, then get all sub categories available
     * under the main category selected
     *
     * if a city was selected, then get all sub categories available in that
     * city under the main category selected.
     */
    populateMemberSubType: function(event)
    {
        var city = $("select[name='city_id'] option:selected").val();
        var cat  = $("select[name='category_id'] option:selected").val();
		var subList = $("select[name='sub_category_id']");

        subList.empty()
               .append('<option value="">-- Select --</option>');

        var usedValues = [];
		var options = [];
		var subCats = [];
        if (city == '') {
			if (window.CityCats != undefined) {
				$.each(CityCats, function(id, obj) {
					if (CityCats[id][cat]) {
						$.each(CityCats[id][cat], function(i, j) {
							if (!usedValues[i]) {
								usedValues[i] = true;
								subCats[j] = i;
								options.push(j);
							}
						});
					}
				});
			}
        } else {
			if (window.CityCats != undefined) {
				$.each(CityCats[city], function(key, obj) {
					if (cat != '') {
						$.each(CityCats[city][cat], function(i, j) {
							if (!usedValues[i]) {
								usedValues[i] = true;
								subCats[j] = i;
								options.push(j);
							}
						});
					}
				});
			}
        }

		options.sort();
		$.each(options, function(idx, itm) {
			subList.append('<option value="'+subCats[itm]+'">'+itm+'</option>');
		});

        //  If only one option is available, default to have it selected.
		var subListOptions = subList.children('option');
        if (subListOptions.length == 2) {
            $("select[name='sub_category_id'] option:last-child")
                .attr('selected', 'selected');
        }

        //  Figure out wich sub category we need to set as the selected value
        var sub_category_id = window.location.GET['sub_category_id'];
        if (parseInt(sub_category_id) != 'NaN') {
            if (!Search.subCatSelected && event != false) {
                Search.subCatSelected = true;
                $("select[name='sub_category_id'] option[value='"+sub_category_id+"']").attr('selected', 'selected');
            }
        }
    }

    //  }}}
};

$(document).ready(Search.init);

