(function($) { // Wrapper for jQuery object
    $(function() {
        $('#buttons form input:first').focus(function() {
            $(this).addClass('focused');
        }).blur(function() {
            $(this).removeClass('focused');
        });
 

        $('table.restaurant-list tr').each(function() {
	    var content = $('td:last', this).html();
	    if (!content)
		return;
	    $('td:last', this).remove();

	    var tr = $('<tr class="restaurant-details"></tr>');
	    tr.append( $('<td colspan="5"></td>').html(content) );
	    tr.insertAfter( $(this) ).hide();

	    var link = $('<a href="#">+</a>');
	    link.click(function() {
		if (tr.is(':visible')) tr.hide();
		else tr.show();
		return false;
	    });

	    $(this).closest('tr').append( $('<td></td>').append(link) );
        });

        if ($('table.restaurant-list').length > 0) {
            var input = $('<input type="text" />').keyup(function() {
	        var val = $(this).val();
	        $('table.restaurant-list tr:not(.restaurant-details):not(:first)').show().each(function() {
	            if (!$(this).text().match(val))
	                $(this).hide();
	        });
	    }).insertBefore('table.restaurant-list');

            var cities = {};
            $('table.restaurant-list .restaurant-address').each(function() {
                var city = $(this).html().replace(/^.*<br[^>]*>/, '');
                city = city.replace(/,? *[0-9]+ *$/, '');
                city = city.replace(/, */, ', ');
                if (city.replace(/ /g, '') != '' && /, *[A-Z][A-Z]/.test(city))
                    cities[city] = city;
            });

            var list = [];
            for(var p in cities) {
                if (cities.hasOwnProperty(p))
                    list.push(cities[p]);
            }


            list = list.sort();

            var select = $('<select></select>').insertAfter(input).change(function() {
                var val = $(this).val();
	        $('table.restaurant-list tr:not(.restaurant-details):not(:first)').show().each(function() {
                    if (val == '')
                        return;

		    if (!$(this).text().match(val))
		        $(this).hide();
	        });
            });

            select.append( $('<option value="">Any City</option>') );
            for(var i = 0; i < list.length; i++) {
                select.append( $('<option></option>').attr('value', list[i]).text(list[i]) );
            }
        }


        $('.homepage-slider').css('background-image', 'url(' + $('.homepage-slider img:first').attr('src') + ')');
        $('.homepage-slider li').each(function() {
            var img = $(this).find('img');
            if (img) {
                if (img.parent().length > 0 && img.parent().get(0).tagName == 'P')
                    img.parent().remove();
                else
                    img.remove();
            }

            var div = $('<div class="border-box border-box-1"><div class="border-box-2"><div class="border-box-3"><div class="border-box-4"><div class="border-box-5"></div></div></div></div></div>');
            var contents = div.find('div:not(:has(div))');
            contents.append( $(this).contents() );

            $(this).empty();
            div.appendTo(this);

            $(this).mouseover(function() {
                $('.homepage-slider .active').removeClass('active');
                $(this).addClass('active');
                $('.homepage-slider').css('background-image', 'url(' + img.attr('src') + ')');
            });
        });
    });
})(jQuery);

