$(function() {

	$("#grab_menus").live("click", function() {
		$("#welcome_top").fadeOut('400', function() {
		$("#search_results").fadeIn();
			
			var qs = '';
			
			$.ajax({
				type: "POST",
				url: "/listingspostcode",
				data: { 
					qs: qs
				},
				dataType: 'html',
				success: function(data) {
					$('#search_results').delay(800).html('<h1>Here are the menus we found for "NP19"</h1>'+data);
				},
				error: function(error) {
					//alert('Error:'+error);
				}
			});
			
			return false;
		});
		
		return false;		
	});

	
	$("#menu_image, #iPad1024 #menu_image").live("mouseenter", function() {
		$(".frontbtn, .backbtn").fadeIn();
		
	});
	
	$("#menu_image, #iPad1024 #menu_image").live("mouseleave", function() {
		$(".frontbtn, .backbtn").fadeOut();
		
	});

	$(".destroy").click(function() {
		$("#scroll_note").remove();
		return false;
	});
	
	
	/*$(window).scroll(function(d,h) {
	
		//  a = $("#container").offset().top + $(this).height();
	    b = $(window).scrollTop() + $(window).height();
	    a = $("#withoutholder").offset().top + 450;
	    if (a < b) {
	    	$("#scroll_note").fadeIn();
	    } else {
	    	$("#scroll_note").fadeOut();
	    	
	    }
	});
	*/

	 $("#image2").dblclick(function() {
		$("#menu_content #right").toggle();
		var small_width = 818;
		var large_width = 1200;
		var current_width = $(this).width();
	
	 	if(current_width == small_width) {
	 		$(this).width(large_width);
	 		$("#menu_image").width(large_width);
	 	} else {
	 		$(this).width(small_width);
	 		$("#menu_image").width(small_width);
	 	}
	 });

	 $("#image1").dblclick(function() {
		$("#menu_content #right").toggle();
		var small_width = 650;
		var large_width = 1200;
		var current_width = $(this).width();
	
	 	if(current_width == small_width) {
	 		$(this).width(large_width);
	 		$("#menu_content #left, #iPad1024 #menu_image, #iPad1024 #menu_image img").width(large_width);
	 	} else {
	 		$(this).width(small_width);
	 		$("#menu_content #left, #iPad1024 #menu_image, #iPad1024 #menu_image img").width(small_width);
	 	}
	 });
                                 	
	 $(".backtotop").click(function() {
	 	window.scrollTo(0, 0);
	 	return false;
	 });
	 	
	 $("#iphone-map-btn").click(function() {
	 	window.scrollTo(0,400);
	 	return false;
	 });

	 $('#locationbox').change( function() {
     	
     	var location = $(this).val();
     	updateListings(location);
     	    	 
     });	
			
	$("#advancedsearch").toggle(function() {
		$(this).html('Hide Advanced Search');
		$("#filters").fadeIn();
	}, function() {
	
		$(this).html('Advanced Search');
		$("#filters").fadeOut();
	});
	
	$(".showitems").live("click", function() {
		$("#business_details_form .form_row, #business_details_form .submit").fadeIn();
		return false;
	});
	
	$("#form_Find").click(function() {
		var telephone = $("#form_telephone").val();
		$.ajax({
			type: "GET",
			url: "ajax.php",
			data: { 
				telephone: telephone,
				action: 'business_details'
			},
			dataType: 'json',
			success: function(json) {
				$("#form_business_name").val(json.business_name);
				$("#form_business_address").val(json.address);
				$("#form_business_address2").val(json.address2);
				$("#form_business_address3").val(json.address3);
				$("#form_business_city").val(json.city);
				$("#form_business_postcode").val(json.postcode);
				$("#form_business_county").val(json.county);
				$("#form_business_country").val(json.country);
				return false;
			},
			error: function(error) {
				alert('Error:'+error);
			}
		});
		
		return false;
	});

	function updateListings(location) {

		filteroption = $(".filteroption:checked").val();
		//cuisine = $("#cuisine").val();
		var cuisines = new Array();
		$("#cuisinelist li").each(function() {
			cuisines[$(this).find('input').attr('value')] = $(this).find('input').attr('checked');
		});

		$.ajax({
			type: "POST",
			url: "/listings",
			data: { 
				qs: $('#searchfield').val(),
				location: location,
				filteroption: filteroption,
				cuisines: cuisines
			},
			dataType: 'html',
			success: function(menus) {
				$('#container').html(menus);
			},
			error: function(error) {
				//alert('Error:'+error);
			}
		});
	}

	$("#searchfield").keyup(function(){
		updateListings();
	});
	

	function locationList() {
		
		var qs = $("#townlistqs").val();
		
		$.ajax({
			type: "POST",
			url: "/townlist",
			data: { 
				qs: qs
			},
			dataType: 'html',
			success: function(data) {
				$('#locationlist').html(data);
			},
			error: function(error) {
				//alert('Error:'+error);
			}
		});
	}
	
	$("#townlistqs").keyup(function(){
		locationList();
	});
	
	locationList();

	$(".filteroption").click(function() {
		updateListings();
	});

	$("#cuisinelist").change(function() {
		updateListings();
	});
	
	// inital load of data
	updateListings();
	
	
	$("#changelocationqs").live("keyup", function(){

		$.ajax({
			type: "POST",
			data: {
				qs: $(this).val()
			},
			url: "/locations",
			dataType: 'html',
			success: function(data) {
				$('#city_listings').html(data);
			},
			error: function(error) {
				alert('Error:'+error);
			}
		});
		
	});



	var address2 = $("#address-for-gmap").html();

	$("div#google-map").gMap({markers: [{ 
							  	address: address2,
                             	 html: "_address" }],
                 			  zoom: 15 });        			  

	$("div#mobile-map").gMap({markers: [{ 
							  	address: address2,
                             	 html: "_address" }],
                 			  zoom: 13 });	
  
	$("div#large-map").gMap({markers: [{ 
							  	address: address2,
                             	 html: "_address" }],
                 			  zoom: 15 });    

	$("div#landscape-map").gMap({markers: [{ 
							  	address: address2,
                             	 html: "_address" }],
                 			  zoom: 15 });   
                 			                   			                  			  
	$(".middle-block .tablink").live("click", function() {
		
		$(".middle-block .tab").hide();
		$(".middle-block .tablink").removeClass("active");
		$(this).addClass("active");
		var showthis = $(this).attr("rel");
		$("."+showthis).show();
		
	});

	
});
