$(document).ready(function() {
    var id,
        hash = window.location.hash.substr(1),
        $content = $('#content'),
        $pSearch = $('#product-search');

    if (/^pl\d+$/.test(hash)) {
        id = hash.substr(2);
        $('[name=productlines][value='+id+']').attr('checked', 'checked');
    } else if (/^mk\d+$/.test(hash)) {
        id = hash.substr(2);
        $('[name=markets][value='+id+']').attr('checked', 'checked');
    } else if (/^mf\d+$/.test(hash)) {
        id = hash.substr(2);
        $('[name=manufacturers][value='+id+']').attr('checked', 'checked');
    }
        
    $('input[type=checkbox]').change(function() {
        $pSearch.submit();
    });

    var timer = null, cache = '';
    $('input[type=text]').keyup(function() {
        clearTimeout(timer);
        if (this.value !== cache) {
            cache = this.value;
            timer = setTimeout(function() {
                $pSearch.submit();
            }, 200);
        };
    });

    $pSearch.submit(function(evt) {
        evt.preventDefault();
        var action = this.action;
        var data = $pSearch.serialize();
        $.ajax({
            url: action,
            data: data,
            success: function(resp) {
                $content.html(resp);
                $('.gallery').each(galleryBind);
            }
        });
    }).submit();

	$('#content .nw').live('click', function() {
		window.open(this.href);
		return false;
	});
});
