var _isPositionFixedSupported = null;

function isPositionFixedSupported() {
	if (_isPositionFixedSupported === null) {
		var isSupported = null;
		if (document.createElement) {
			var el = document.createElement('div');
			if (el && el.style) {
				el.style.width = '1px';
				el.style.height = '1px';
				el.style.position = 'fixed';
				el.style.top = '10px';
				var root = document.body;
				if (root && 
						root.appendChild && 
						root.removeChild) {
					root.appendChild(el);
					isSupported = (el.offsetTop === 10);
					root.removeChild(el);
				}
				el = null;
			}
		}
		_isPositionFixedSupported = isSupported;
	}
	return _isPositionFixedSupported;
}



function transitionProductSheet(oldSheet, newSheet) {	
	var direction = (newSheet > oldSheet) ? 1 : -1;
	$('div.products div.sheet-'+oldSheet).animate({left: (direction < 0 ? '' : '-') +"700px"});
	$('div.products div.sheet-'+newSheet).css({display: "block", left: (direction < 0 ? '-' : '') +"700px"});
	$('div.products div.sheet-'+newSheet).animate({left: "0px"});
}

var currentProduct = null;
var popupBackground = null;

function nextProductSibling(link, x) {
	var $product = $(link).parents('div.product').next();
	if (!$product.length)
		$product = $(link).parents('div.product').siblings(':first');
	_showProductPopup($product);
}

function prevProductSibling(link, x) {
	var $product = $(link).parents('div.product').prev();
	if (!$product.length)
		$product = $(link).parents('div.product').siblings(':last');
	_showProductPopup($product);
}

function _showProductPopup($product) {
	if (currentProduct)
		currentProduct.fadeOut();

	if (!popupBackground) {
		$('body').append('<div id="popup-background"></div>');
		$('#popup-background').css('opacity', '0.7');
		if (!isPositionFixedSupported()) {
			// IE 6 bug workaround
			$('#popup-background').css({	'position': 'absolute',
											'height': $(document).height()+'px'
			});
		}
	};

	currentProduct = $product;
	$('#popup-background').fadeIn();
	currentProduct.fadeIn();
	
	// Vertically center it
	popupSetTop();
	
}

function showProductPopup(product) {
	_showProductPopup( $('#product-'+product) );
	return false;
}

function popupSetTop() {
	if (currentProduct) {
		var height = (currentProduct.outerHeight());
		var windowHeight = $(window).height();
		if (height > windowHeight) {
			currentProduct.css({'margin-top': '0px', top: '10px', position: 'absolute'});
		} else {
			currentProduct.css({'margin-top': 0-(height/2), top: '50%', position: isPositionFixedSupported() ? 'fixed' : 'absolute'});
		}
	}
}

$(window).resize(popupSetTop);
	

function closeProductPopup() {
	currentProduct.fadeOut();
	$('#popup-background').fadeOut();
	return false;
}

function popupSelectImageThumbnail(linkNode, imagePosition) {
	var productDiv = $(linkNode).parents("div.product");
	productDiv.find("img.image-main").not(".image-"+imagePosition).fadeOut();
	productDiv.find("img.image-main.image-"+imagePosition).fadeIn();
	
	return false;
}



$(document).ready(function() {
	// Move popups to root (body) node, they'll work better there.
	$('.product-popups').appendTo($('body'));
	if (isPositionFixedSupported()) {
		$('.product-popups').children().css({	'position': 'fixed',
												'left':	'50%',
												'top': '50%',
												'margin-left': '-320px',
												'margin-top': '-160px'
		});
	} else {
		var contentPosition = $('#content').offset();
		$('.product-popups').children().css({	'position': 'absolute',
												'left':	contentPosition.left + 'px',
												'top': contentPosition.top + 20 + 'px'
		});
	}

	// IE 6 hacks
	if ($.browser.msie && $.browser.version < 7) {
		$('ul.products a').css('width', '100px');
		$('span.featured-star, p.featured-item').css('background-image', "url('../../images/featured-star-30x30.png8.png')");
	}
	


});

