$(function() {
	
    $('.account-list .expander').click(function() {
        $(this).parents('.box:first').find('.content').slideToggle();
        var img = $('img', this)[0];
        if (img.src.indexOf('down') > 0) {
            img.src = img.src.replace('down', 'up');
        } else {
            img.src = img.src.replace('up', 'down');
        }
        return false;
    });
    
    
    $('[hint]').inputHint();
    
    //
    // Map Links
    
    $('.map-link').click(function() {
        $.ajax({
            url: this.href,
            type: 'GET',
            dataType: 'json',
            success: function(input) {
                var p = $('<p style="margin:0 0 10px 0"><a href="#">Show directions</a></p>');
                var div = $('<div/>').css({width:500,height:300});
                var wrapper = $('<div/>').append(p).append(div);
                new Boxy(wrapper, {modal: true,title: input.name + " - Map View"});
                var map = new GMap2(div[0]);
                var pnt = new GLatLng(input.latitude, input.longitude);
                var marker = new GMarker(pnt);
                map.setCenter(pnt, 13);
                map.addControl( new GSmallMapControl());
	            map.addControl( new GMapTypeControl());
	            map.addControl( new GOverviewMapControl(new GSize(100,100)));
	            map.addOverlay(marker);
                
                p.find('a').click(function() {
                    var sourceAddress = prompt("Show directions from which address:");
                    if (sourceAddress) {
                        map.clearOverlays();
                        map.addOverlay(marker);
                        var directions = new GDirections(map);
                        directions.load("from: " + sourceAddress + " to: " + input.latitude + "," + input.longitude);
                    }
                    return false;
                });
                
            }
        });
        return false;
    });
    
    //
    // Advanced Search
    
    $('.advanced-search-actuator').click(function() {
        Boxy.load("/search/dialog.php", {
            title: "Restaurant Search",
            modal: true,
            unloadOnHide: true,
            behaviours: function(c) {
                
                c.find('#name-actuator').click(function() {
                    $(this).hide();
                    $('#name-selector').show().focus();
                });

                var nameHider = function() {
                    $(this).hide();
                    var val = $(this).val().replace(/(^\s+|\s+$)/g, '');
                    $(this).val(val);
                    if (val.length > 0) {
                        $('#name-actuator').html("'" + val + "'");
                    } else {
                        $('#name-actuator').html('anything');
                    }
                    $('#name-actuator').show();
                }
                
                c.find('#name-selector').blur(nameHider);
                
                //
                // Cuisine
                
                c.find('#cuisine-actuator').click(function() {
                    $(this).hide();
                    $('#cuisine-selector').show().focus();
                });
                var cuisineHider = function() {
                    $(this).hide();
                    $('#cuisine-actuator').html(this.options[this.selectedIndex].text).show();
                };
                $('#cuisine-selector').change(cuisineHider).blur(cuisineHider);
                
                //
                // Area
                
                c.find('#area-actuator').click(function() {
                    $(this).hide();
                    $('#area-selector').show();
                });

                c.find('#area-type-actuator').change(function() {
                    switch ($(this).val()) {
                        case 'postcode':
                            $('#city-selector').hide();
                            $('#city-selector select').val('');
                            $('#postcode-selector').show();
                            break;
                        case 'area':
                            $('#city-selector').show();
                            $('#postcode-selector input[name=postcode]').val('');
                            $('#postcode-selector').val('').hide();
                            break;
                        case 'anywhere':
                            $('#postcode-selector input[name=postcode]').val('');
                            $('#postcode-selector').val('').hide();
                            $('#city-selector').hide();
                            $('#city-selector select').val('');
                            $('#area-selector').hide();
                            $('#area-actuator').html('located anywhere').show();
                            break;
                    }
                });
                
            }
        });
        return false;
    });

	  //
		// Rounded Corners
		$('.alt-box').addClass('alt-box-rounded');
		$('.alt-box-rounded').wrapInner("<div class='nw'><div class='ne'><div class='se'><div class='sw'><div class='inner'></div></div></div></div></div>");
		$('ul.nav + .alt-box-rounded .nw').addClass('square');
    
});

$.fn.businessAutocomplete = function(options) {
	
	var defaultOptions = {
		selectFirst:true,
		delay:10,
		minChars:2,
		matchSubset:1,
		matchContains:0,
		cacheLength:10,
		maxItemsToShow:10,
		autofill:true,
		width:500,
		formatItem: businessAutocompleter.formatItem
	};
	
	return $(this).autocomplete('/community/business/find', $.fn.extend(defaultOptions, options || {}));
};

var businessAutocompleter = {
  formatItem: function(row) {  
    var o = "<b style='margin-left: 5px; margin-right: 10px'>" + row[0] + "</b>";
    var address = [row[2],row[5]];
    o += "<span>" + address.join(', ') + "</span>";
    return o;
  }
}

