var resizeTimer = null;

var ADDRESS_COOKIE_NAME = 'address';
var PRODUCT_COOKIE_NAME = 'products';
var options = { path: '/'};

$(document).ready(function(){
	$('form.addressList').each(function() {
		$.cookie(ADDRESS_COOKIE_NAME, null, options);
		$('form.addressList input[type="checkbox"]').attr('checked', false);
	});
	
	$('form.productList').each(function() {
		$.cookie(PRODUCT_COOKIE_NAME, null, options);
		$('form.productList input[type="checkbox"]').attr('checked', false);
	});	

	$('form.addressList input[type="checkbox"]').click(function() {
		var selected = new Array();

		if (this.value == 0 && $(this).is(':checked')) {
			$(':input', $(this).parents("form")).attr('checked', true);
			// $(':input', $(this).parents("form")).attr('disabled', true);

			$(this).attr('disabled', false);
			$.cookie(ADDRESS_COOKIE_NAME, 0, options);
			return;
		}
		else if (this.value == 0) {
			$(':input', $(this).parents("form")).attr('checked', false);
			$.cookie(ADDRESS_COOKIE_NAME, 0, options);
		}

		$('form.addressList :input').each(function(){
			if ($(this).is(':checked')) {
				selected.push(this.value);
			}
		});

		$.cookie(ADDRESS_COOKIE_NAME, selected.join(), options);
	});
	
	$('form.addressList span.next a').click(function() {
		if ($.cookie(ADDRESS_COOKIE_NAME) == null || $.cookie(ADDRESS_COOKIE_NAME) == '') {
			alert(unescape("Es ist kein Produzent ausgew%E4hlt"));
			return false;
		}
		return true;
	});

	$('form.addressList a.print').click(function() {
		if($(this).attr("href").indexOf("=all") > 0) {
			return true;
		}		
		if ($.cookie(ADDRESS_COOKIE_NAME) == null || $.cookie(ADDRESS_COOKIE_NAME) == '') {
			alert(unescape("Es ist kein Produzent ausgew%E4hlt"));
			return false;
		}
		return true;
	});

	$('form.productList input[type="checkbox"]').click(function() {
		var selected = new Array();

		if (this.value == 0 && $(this).is(':checked')) {
			$(':input', $(this).parents("form")).attr('checked', true);

			$(this).attr('disabled', false);
			$.cookie(ADDRESS_COOKIE_NAME, 0, options);
		}
		else if (this.value == 0) {
			$(':input', $(this).parents("form")).attr('checked', false);
			$.cookie(PRODUCT_COOKIE_NAME, 0, options);
		}

		$('form.productList :input').each(function(){
			if ($(this).is(':checked') && this.value != 0) {
				selected.push(this.value);
			}
		});

		$.cookie(PRODUCT_COOKIE_NAME, selected.join(), options);
	});	
	
	$('form.productList span.next a').click(function() {
		if ($.cookie(PRODUCT_COOKIE_NAME) == null || $.cookie(PRODUCT_COOKIE_NAME) == '') {
			alert(unescape("Es ist kein Produkt ausgew%E4hlt"));
			return false;
		}
		return true;
	});

	$('form.productList a.print').click(function() {
		if($(this).attr("href").indexOf("=all") > 0) {
			return true;
		}		
		
		if ($.cookie(PRODUCT_COOKIE_NAME) == null || $.cookie(PRODUCT_COOKIE_NAME) == '') {
			alert(unescape("Es ist kein Produkt ausgew%E4hlt"));
			return false;
		}
		return true;
	});	
});
