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);

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

        Search.setGET();
        if (window.location.GET["_qf__SearchForm"] == '') {
            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']").length) {
			$(this).addClass('search-result-item-on');
		}
	},

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

    //  }}}
    //  {{{ goToMember()
    
	goToMember: function(event)
	{
		if ($(this).find("a[title='More Info']").length) {
			var link = $(this).find("a[title='More Info']");
			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 == '') {
            $.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 {
            $.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-child")
                .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 (parseInt(category_id) != 'NaN') {
            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 == '') {
            $.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 {
            $.each(CityCats[city], function(key, obj) {
                $.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);
