/**
 * Yacht Builders search 
 *
 * Author: waldemarm
 */
$(document).ready(function() {
	
	$("#search_button").click(function() {
		$("#location_form").submit();
	});
	
	$("#go_button").click(function() {
		$("#name_form").submit();
	});
						   
						   
	var location_options = {
		script: "/agent/country_suggest",
		varname: "?name",
		json: false,
		maxresults: 35,
		callback: function(d) {
			/*
			if (d.id.indexOf("b", 0) == 0) {
				//Its a branch
				var branch_id = d.id.substring(1);
				var redirect_url = "/yacht-brokers/list.htm?name=" + branch_id;
				window.location = redirect_url;
			} else {
				var country_id = d.id;
				var redirect_url = "/yacht-brokers/list.htm?cc=" + country_id;
				window.location = redirect_url;
			}
			*/
		}
	};
	
	var as = new bsn.AutoSuggest('by_location', location_options);
	
	
	var name_options = {
		script: "/builder/name_suggest",
		varname: "?type=refitter&name",
		json: false,
		maxresults: 35,
		callback: function(d) {
			//var redirect_url = "/yacht-brokers/index.htm?id=" + d.id;
			//alert(redirect_url);
			//window.location = redirect_url;
		}
	};
	
	var as = new bsn.AutoSuggest('by_name', name_options);
});


$(document).ready(function() {	
		
		window.submitForm = function() {
			window.onbeforeunload = null;
			document.detailForm.submit();
		}
	
		$("a.filter").click(function() {
			document.detailForm.filter.value = this.innerHTML;
			document.detailForm.submit();
		});	
		
		$('#by_name').focus(function(){
			if( $(this).val() == 'Builder Name' ) $(this).val('');
		})
		$('#by_name').blur(function(){
			if( $(this).val() == '' ) $(this).val('Builder Name');
		})
		$('#by_location').focus(function(){
			if( $(this).val() == 'Country' ) $(this).val('');
		})
		$('#by_location').blur(function(){
			if( $(this).val() == '' ) $(this).val('Country');
		})
		
		$("#builder_search").bind('keyup',function(){
			$.ajax({
				url: '/property/yacht/builder/search?type=builder&amp;name=' + $("#builder_search").val(),
				type: 'GET',			
				success: function(data) {
					var founded_items = $(data).find("items_found").text($(data).find("items_found").html());
					
					if(founded_items==''){
						$("#itemsFound").hide('fast');
						$('#showResults').css('display','none');
					}
					else{
						if(founded_items>0){
							$('#showResults').css('display','');
						}
						else{
							$('#showResults').css('display','none');
						}
						$("#itemsFound").show('fast');
						$("#itemsFound").html('Found: '+founded_items);
					}
				 	

					$("#result_id_list").attr('value',$(data).find("items_found").attr("ids"));
					
				}
			});				
		});
		
	});	

