$.ajaxSetup({
	async: false
});

var refresh_time;

(function($){

	$.fn.update_amount = function(){
	
		var $all = $(this);
		var timeout;
		
		return $(this).each(function(){
			
			var $obj = $(this);
			$obj.change(function(){
				clearTimeout(timeout);
				if($obj.val() == '')
				{
					$obj.val(0);
				}
				$("#cart_form").css("opacity",0.7);
				$("#checkout_submit .right").slideUp(150);
				$(".loader").show();
				timeout = setTimeout(function(){
					$("#cart_form").submit();
				}, 1000);
			}).keyup(function(){
				clearTimeout(timeout);
				if($obj.val() == '')
				{
					return;
				}
				$("#cart_form").css("opacity",0.7);
				$("#checkout_submit .right").slideUp(150);
				$(".loader").show();
				timeout = setTimeout(function(){
					$("#cart_form").submit();
				}, 1000);
			});
			
			$obj.focus(function(){
				clearTimeout(timeout);
			});			
						
		});

	}
	
})(jQuery);


$(function(){
	$("input.error").focus(function(){
		$(this).removeClass('error');
	});

	$('#search select').selectbox();
	$("#search input.selectbox").change(function(){
		$("input[name=search_current_page]").val(0);
		$("#wines_result").css("opacity",0.3);
		clearTimeout(refresh_time);
		refresh_time = setTimeout(function(){
			$("#search").submit();
		}, 500);
		
	});

	$("#search_country").bind('change',function(){
		$("#search_region_placeholder").load("/actions/get_regions/"+$(this).val());
	});
	
	$("#search_region_placeholder").load("/actions/get_regions/"+$('#search_country').val());
	
	$(".reset_filter").click(function(e){
		e.preventDefault();
		$("#extra_action").val('reset_order');
		$("#search_input").val('');
		$(".select_ui input.selectbox").val('-');
		$('#search .select_ui.selectbox').each(function(){
			$(this).find('.select_ui_button').html($(this).find('.select_ui_item').first().html());
		});
		$("#search").submit();
		return false;
	});
	
	$("#search_reset").click(function(e){
		e.preventDefault();
		$("#search_input").val('');
		$("#search").submit();
		return false;
	});
	
	var per_page = $('input[name=search_per_page]').val();
	$(".per_page_links a").removeClass('active').each(function(){
		if($(this).data('amount') == per_page)
		{
			$(this).addClass('active');
		}
		$(this).unbind('click').click(function(e){
			e.preventDefault();
			$(".per_page_links .active").removeClass('active');
			$(this).addClass('active');
			$('input[name=search_per_page]').val($(this).data('amount'));
			$("input[name=search_current_page]").val(0);
			$("#search").submit();
		});
	});
	
	$("#skip_to_page").unbind('change').change(function(){
		$("input[name=search_current_page]").val($(this).val());
		$("#search").submit();
	});
	
	$("#search").submit(function(){
		clearTimeout(refresh_time);
		var opt = {};
		
		var $elements = $(this).find('input, select').each(function(){
			opt[$(this).attr('name')] = $(this).val();
		});
		
		reload_result(opt);
		
		return false;
	});
	
	$("#search_input").keyup(function(){
		$("#wines_result").css("opacity",0.3);
		clearTimeout(refresh_time);
		refresh_time = setTimeout(function(){
			$("#search").submit();
		}, 500);
	});
	
	$("#cart_form input").update_amount();
	$("#search").submit();
	
	$("#email_news").focus(function() {
		$(this).val("");
	});
	
	$("#submit_email").click(function() {
		var email=$("#email_news").val();
		var lang=$("#email_lang").val();
		if(email!="" && email!="Uw emailadres" && email!="Your email address"){
			$.post("/website/add_email", {email:email,lang:lang}, function(data){
				if(data == 1){
					$("#news_error").slideUp();
					$("#news_form").slideUp();
					$("#news_thnx").slideDown();
				}
			});
		}
		else {
			$("#news_error").hide().slideDown();
		}

	});
	
});

var request;
function reload_result(options)
{
	try{
		request.abort();
	}catch(error){
	
	}
	var _opt = $.extend({
		"wine_list": wine_list
	}, options);
	$.post("/actions/reload_result", _opt, function(data){
		$("#wines_result").html(data);
		$("#wines_result").animate({
			'opacity':1
		}, 300);
	});
}

