$$defaults = {'location': '','price': 'price:20000-5000000','bedrooms':'bedrooms:1-6','bathrooms':'bathrooms:1-6', 'type' : 'type:residential', 'page': 'page:1','order':'order:1', 'co' : 'co:vintage-estates'};

$(document).ready( function() {

    $('#search-form,#widget_basicSearch').submit(function(e){
        e.preventDefault();

        $(this).trigger('beforeSubmit');
        a = $('.searchable').serializeArray();

        $('#search-results').addClass('loading').text('Loading results...');

        var str = $$defaults;
        var cook = {};
        for(var i in a) {
            str[a[i].name] = a[i].value ? (a[i].name+':'+a[i].value.replace(/\//g,'-')) : 0;
            cook[a[i].name] = a[i].value;
        }

        var loc = '';
        for(i in str) {
            if(str[i]) {
                loc += str[i] + '/';
            }
        }
        loc = loc.replace(/ /g,'+');
        if(!window.location.href.match(/\.[a-z]{1,4}\/search\//)) {
            window.location = '/search/#!/'+loc;
        } else {
            runAjaxSearch(loc);
            window.location.hash='#!/'+loc;

        }

        return false;
    });


    function runAjaxSearch(loc) {
        d = new Date();
        
        $('#search-results').load('/search/ajax/?q='+loc.replace(/ /g,'+'),
            function() {
                $('#search-results').removeClass('loading');
                $$selects();
               // _typeface_js.renderDocument();
            }
            );
    }


    if(window.location.href.match(/\.[a-z]{1,4}\/search\/?/)) {
        loc = window.location.hash.substr(2);
        
        opts = loc.match(/\/([^\:][^\/]+)/g);

        if(opts) {
            var str = $$defaults;
            len = opts.length;
            for(i=0;i<len;i++) {

                p = opts[i].substr(1);

                if(p) {
                    p=p.split(':');
                    if(p[1]) {
                        str[p[0]]=opts[i];
                    }

                    if (p[0]) {
                        q = $('.input-'+p[0]);
                        if(q) {
                            if (q.is('input:radio')) {
                                $('.input-'+p[0]+"[value="+p[1]+"]").attr('checked',true);
                            } else {
                                q.val(p[1].replace(/\+/g, ' '));
                            }
                        }
                    }
                }

            }
            $$defaults = str;

            runAjaxSearch(loc);
        } else {
            $('#search-form').submit();
        }
    }

/*
 * Load in the required/optional parameters
 */

$req = $('.input-type');

if($req && (r=$req.val())){
    rp = r.split('+');
    $('#type-list a[rev='+rp.join('],#type-list a[rev=')+']').addClass('in');
    //$('input[name=required[]][type=checkbox][value='+rp.join('],input[name=required[]][type=checkbox][value=')+']').attr('checked','checked');
}

$$selects = function() {

$('.select')

/**
 * We have to add the click and focus events for Webkit/Chrome,
 * since there is still an open bug with :focus.
 */
.each( function() {
    t=$(this);
    i = $('input[name='+t.attr('rev')+']');
    if ( i &&  (i=i.val()) ) {
        l = t.find('.lv_'+i);
        if( l &&  (l=l.text()) ) {
            t.find('b').text(l);
        }
    }
})
.click(function(){return false;})
.mousemove(function(){return false;})
.mousedown(
    function(){
        focused=$(this);
        focused.addClass('focus');
        $(document).one('click', function(){
            focused.trigger('blur');
        })
        return false;
    })
.blur(
    function(){
        $(this).removeClass('focus');
    })
/**
 * When we select an 'option' go ahead and send it back to the
 * display area and set the form element to that value.
 */
.find('li')
    .mouseup(
        function(){
            $match=$(this).attr('class').match(/lv_[^ ]+/);
            t = $(this).text();
            n= $match ? $match[0].substr(3) : t;
            
            $('input[name='+
                $(this).parents('a').blur()
                    .find('b').text(t).end()
                .attr('rev')
              +']').val(n).trigger('change');
          return false;
        })

    .hover(
        function(){
            $(this).addClass('hover');
        },
        function(){
            $(this).removeClass('hover');
        }
    );
};
$$selects();

$('a.submit, a.form-submit').live('click',function(){

    $(this).parents('form').submit();
});

$('a.previous').live('click', function(){
    $('#page').val( parseInt($('#page').val())-1)
    $('#search-form').submit();
    return false;
});
$('a.exactly').live('click', function(){
    $('#page').val( parseInt($(this).text()) )
    $('#search-form').submit();
    return false;
});
$('a.next').live('click', function(){
    $('#page').val( parseInt($('#page').val())+1)
    $('#search-form').submit();
    return false;
});

return true;
// end onready
});



