(function($){
	function init_saved_payment_method() {
		$(".checkout_saved_payment_method").each(function(){
			
			var elem  = this;
			var table = $("table", elem);
			var door = $(".saved_payment_method_door", elem);
			var door_close = $(".saved_payment_method_door .close", elem);
			var door_open = $(".saved_payment_method_door .open", elem);
			
			if (table.length < 1 || door.length < 1) {
				return;
			}
			
			var table_height = 0, i = 0, closed_height=0;
			$("tr", table).each(function(){
				table_height += $(this).outerHeight();
				
				$(this).data("pricelist_h", table_height);
				i++;
				if (i > 2 && closed_height==0) {
					closed_height = table_height;
				}
			});
			
			if ($.browser.msie && $.browser.version > 7 && $.browser.version < 9) {
				table_height += 4;
				closed_height += 2;
			}
			
			$(table).wrap( $("<div />").addClass("pricelist_room") );
			
			var room = $(".pricelist_room", elem);
			room.css({
				padding:0,
				margin:0,
				overflow:"hidden",
				height:closed_height+"px"
			});
			
			if ($.browser.msie && $.browser.version < 7) {
				room.css({
					display:"inline-block"
				});
			}
			
			door_close.hide();
			door_open.show();
			
			door_close.bind("click", function(){
				this.blur();
				room.stop();
				room.animate({height: closed_height+"px"}, {duration:300, complete:function(){
					door_close.hide();
					door_open.show();
					$(this).css({
						overflow:"hidden",
						height:closed_height+"px"
					});
				}});
				return false;
			});
			
			door_open.bind("click", function(){
				this.blur();
				room.stop();
				room.animate({height: table_height+"px"}, {duration:600, complete:function(){
					door_open.hide();
					door_close.show();
				}});
				return false;
			});
		});
	};
	
	function init_saved_shipping_address(visibleItems) {
		visibleItems = visibleItems || 6;
		$(".checkout_saved_shipping_address").each(function(){
			var elem  = this;
			var table = $("table", elem);
			var door = $(".saved_shipping_address_door", elem);
			var doorDisabled = false;
			var door_close = $(".saved_shipping_address_door .close", elem);
			var door_open = $(".saved_shipping_address_door .open", elem);
			
			if (table.length < 1 || door.length < 1) {
				return;
			}
			
			var table_height = 0, i = 0, closed_height=0, detail;
			
			var recalcHeight = function() {
				table_height = 0; 
				closed_height = 0;
				i =0;
				$("tr", table).each(function(){
					var tr = this;
					if ( $(this).is(".row") ) {
						table_height += $(tr).outerHeight();
						if ($(tr).data("detail") && $(tr).data("open")) {
							table_height += $(tr).data("detail_height");
						}
						i++;
						if (i >= visibleItems && closed_height==0) {
							closed_height = table_height;
						}
						
					} else if ( !$(tr).is(".row_detail") ) {
						table_height += $(tr).outerHeight();
					}
					
					
				});
				
				if (closed_height == 0) {
					closed_height = table_height;
				}
				
				if ($.browser.msie && $.browser.version > 7 && $.browser.version < 9) {
					table_height += 4;
					closed_height += 2;
				}
			};
			
			var showDetail = function(tr) {
				
				if ( $(tr).data("detail") ) {
					if ($(tr).data("open")) {
						$(tr).data("detail").hide();
						r = room.innerHeight();
						r -= $(tr).data("detail_height");
						room.css("height", r+"px");
						$(tr).data("open",0);
					} else {
						$(tr).data("detail").show();
						r = room.innerHeight();
						
						r += $(tr).data("detail_height");
						
						room.css("height", r+"px");
						$(tr).data("open",1);
						
					}
				}
			};
			
			$("tr", table).each(function(){
				var tr = this;
				if ( $(this).is(".row") ) {
					table_height += $(tr).outerHeight();
					var p = $(tr).next();
					if ( $(p).is(".row_detail")) {
						$(tr).data("detail", p);
						$(tr).data("open", 0);
						$(tr).data("detail_height", $(p).outerHeight());
						
						$(".menu1 a", tr).bind("click", function(){
							showDetail(tr);
							this.blur();
							return false;
						});
					}
					
					i++;
					if (i >= visibleItems && closed_height==0) {
						closed_height = table_height;
					}
					
				} else if ( $(tr).is(".row_detail") ) {
					$(tr).hide();
				} else {
					table_height += $(tr).outerHeight();
				}
				
				$(this).data("cheight", table_height);
				
				/*
				detail = $(this).find(".add1_detail");
				if (detail.length) {
					var p = $(this).before();
					console.debug(p);
					$(this).data("detail", detail);
					$(this).data("open", 0);
					$(this).data("detail_height", $(detail).innerHeight());
					
					
					
				} else {
					$(this).data("detail", null);
					$(this).data("open", 0);
					$(this).data("detail_height", 0);
				}
				
				table_height += $(this).outerHeight() - ($(this).data("detail_height") - 1);
				
				//if (detail.length) $(detail).hide();
				
				$(this).data("pricelist_h", table_height);
				i++;
				if (i > visibleItems && closed_height==0) {
					closed_height = table_height;
				}
				*/
			});
			
			if (closed_height == 0) {
				closed_height = table_height;
			}
			if (closed_height == table_height) {
				doorDisabled = true;
			}
			
			if ($.browser.msie && $.browser.version > 7 && $.browser.version < 9) {
				table_height += 4;
				closed_height += 2;
			}
			
			$(table).wrap( $("<div />").addClass("pricelist_room") );
			
			var room = $(".pricelist_room", elem);
			room.css({
				padding:0,
				margin:0,
				overflow:"hidden",
				height:closed_height+"px"
			});
			
			if ($.browser.msie && $.browser.version < 7) {
				room.css({
					display:"inline-block"
				});
			}
			
			if (!doorDisabled) {
				door_close.hide();
				door_open.show();
				
				door_close.bind("click", function(){
					this.blur();
					room.stop();
					recalcHeight();
					room.animate({height: closed_height+"px"}, {duration:300, complete:function(){
						door_close.hide();
						door_open.show();
						$(this).css({
							overflow:"hidden",
							height:closed_height+"px"
						});
					}});
					return false;
				});
				
				door_open.bind("click", function(){
					this.blur();
					room.stop();
					recalcHeight();
					room.animate({height: table_height+"px"}, {duration:600, complete:function(){
						door_open.hide();
						door_close.show();
					}});
					return false;
				});
			} else {
				door.hide();
			}
			
			
		});
	};
	
	$().ready(function(){
		init_saved_payment_method();
		init_saved_shipping_address(3);
	});
})(jQuery);

