var coupons = function(){
	var $head = $('head');
	var h = $('#coupons').outerHeight() + 200;
	var disclaimer = $('.disclaimer').html();
	var coupons_html = $('<div/>').append( $('#coupons').clone() ).html();
	var button_html = '<div style="text-align:center;"><button type="button" style="position:relative; padding:5px 20px;" onclick="window.print()"> <img style="position:relative;" src="images/print-icon.png" alt="Print Coupon" /> </button></div>';
	var html = '<html class="printable"><head>{2}</head><body class="printable"><div style="width:600px; margin:0px auto;">{0}</div><div class="clear"></div>{1}<br />{3}</body></html>'
		.replace('{0}', coupons_html)
		.replace('{1}', button_html)
		.replace('{2}', $head.html().replace('MediagisticFunctions.js', ''))
		.replace('{3}', disclaimer)
	;
	var win = window.open('', 'coupons', 'width=1000,height={0}'.replace('{0}', String(h)));
	var doc = win.document || win.contentDocument;
	doc.write(html);
	doc.close();
};

$(function(){
	$('#coupons').dblclick(coupons);
	$('#coupons-print').click(coupons);
	
	var maxheight = 0;
	$('div.border', '#coupons')
		.each(function(){
			var height = $(this).innerHeight();
			if (height > maxheight) maxheight = height;
		})
		.each(function(){
			var height = $(this).innerHeight();
			if (height == maxheight) return;
			var delta = (maxheight - height) / 2;
			$(this).css('padding-top', parseInt($(this).css('padding-top')) + delta + 'px' );
			$(this).css('padding-bottom', parseInt($(this).css('padding-bottom')) + delta + 'px' );
		});
});

