

$(function() {

	var bgImageId = '#bgimg';

	var viewport = 	{
			width: function() {
					return $(window).width();
					},
			height: function() {
					return $(window).height();
					}
			}

	var bgImage = 	{
			width: function() {
						return $(bgImageId).attr('width');
					},
			height: function() {
						return $(bgImageId).attr('height');
					},
			ratio: function() {
						return bgImage.width() / bgImage.height();
					},
			setWidth: function(newWidth) {
						newHeight = Math.round(newWidth / bgImage.ratio());
						$(bgImageId).attr({width: newWidth, height: newHeight});
						return true;
					},
			setHeight: function(newHeight) {
						newWidth = Math.round(newHeight * bgImage.ratio());
						$(bgImageId).attr({width: newWidth, height: newHeight});
						return true;
					},
			setLeft: function() {
						leftPos = '-' + Math.round( (bgImage.width() - viewport.width()) / 2 ) + 'px';
						$(bgImageId).css('left', leftPos);
					}

			}

	/**
	 * Hack to vertical align a links in categories
	 */
	$("#content .product_group_row .item a span").each(function(e) {
		if($(this).height() <= 12) {
			$(this).parent().css('line-height', '24px');
		}
		$(this).replaceWith($(this).text());
	});

	/**
	 * Hack to allow multiple lines in the header
	 */
	$("#content h2").each(function(e) {
		if($(this).text().length > 40) {
			$(this).css({
									'line-height': '20px',
									'width': '380px',
									'height': '50px',
									'padding': '5px 180px 0 20px'
								});
		}
	});

	/**
	 * Background scaling function
	 */
	$(window).bind('load resize', function() {

		$(bgImageId).css('width', 'auto');
		bgImage.setHeight(viewport.height());

		if(bgImage.height() <  viewport.height()) {
			bgImage.setHeight(viewport.height());
		}

		if(bgImage.width() <  viewport.width()) {
			bgImage.setWidth(viewport.width());
		}

		bgImage.setLeft();

	});


});

function popitup(url, width, height) {
	newwindow = window.open(url,'name','height=600px' + height + ',width=800px' + width + '');
	if (window.focus) {newwindow.focus()}
	return false;
}

