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')
            .bind('mouseover', Search.highlightMember)
            .bind('mouseout', Search.lowlightMember)
            .bind('click', Search.goToMember);

		$("select[name='category_id']")
            .bind('change', Search.populateMemberSubType);
		$("select[name='city_id']")
            .bind('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();

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

        if (city == '') {
            var usedValues = Array();
            jQuery.each(CityCats, function(id, obj) {
                if (CityCats[id]) {
                    jQuery.each(CityCats[id], function(i, j) {
                        if (!usedValues[i]) {
                            usedValues[i] = true;
                            var cat = mainCats[i];
                            $("select[name='category_id']")
                                .append('<option value="'+i+'">'+cat+'</option>');
                        }
                    });
                }
            });
        } else {
            jQuery.each(CityCats[city], function(id, obj) {
                if (CityCats[city]) {
                    var cat = mainCats[id];
                    $("select[name='category_id']")
                        .append('<option value="'+id+'">'+cat+'</option>');
                }
            });
        }

        //  If only one option is available, default to have it selected.
        if ($("select[name='category_id'] option").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();

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

        var usedValues = Array();
        if (city == '') {
            jQuery.each(CityCats, function(id, obj) {
                if (CityCats[id][cat]) {
                    jQuery.each(CityCats[id][cat], function(i, j) {
                        if (!usedValues[i]) {
                            usedValues[i] = true;
                            $("select[name='sub_category_id']")
                                .append('<option value="'+i+'">'+j+'</option>');
                        }
                    });
                }
            });
        } else {
            jQuery.each(CityCats[city], function(key, obj) {
                jQuery.each(CityCats[city][cat], function(i, j) {
                    if (!usedValues[i]) {
                        usedValues[i] = true;
                        $("select[name='sub_category_id']")
                            .append('<option value="'+i+'">'+j+'</option>');
                    }
                });
            });
        }

        //  If only one option is available, default to have it selected.
        if ($("select[name='sub_category_id'] option").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);
