var productImages = {
	init: function() {
		if($('.product .imagery').length == 0) return;
		
		productImages.thumbs= $('.product .imagery .thumbs li a');
		productImages.active= $('.product .imagery .big li.active');
		productImages.duration= 500;
		
		productImages.thumbs.click(function() {
			productImages.activate($(this));
			
			return false;
		});
		
		$(".product .imagery .big li a").fancybox({
			'hideOnContentClick': true,
			'overlayShow': true,
			'overlayOpacity': 0.4
		}); 
	},
	activate: function(thumb) {
		li= thumb.parent();
		index= productImages.thumbs.index(thumb);
		image= $('.product .imagery .big li').eq(index);
		
		productImages.thumbs.parents().removeClass('active');
		li.addClass('active');
		
		productImages.active.fadeOut(productImages.duration);
		image.fadeIn(productImages.duration);
		productImages.active= image;
	}
}

var buySingle = {
	init: function() {
		if($('.product.single .buying').length == 0) return;
		
		buySingle.price= $('.product.single .buying .price');
		buySingle.add= $('.product.single .buying a.add');
		buySingle.another= $('.product.single .buying a.another');
		buySingle.checkout= $('.product.single .buying a.checkout');
		buySingle.slug= $('.product.single .buying .slug').text();
		
		buySingle.add.add(buySingle.another).click(function() {
			buySingle.add.hide();
			buySingle.another.css('display', 'block');
			
			buySingle.price.text('Added One!');
			
			cart.add(buySingle.slug);
						
			return false;
		});
		
		if($('#buynowfromlink').length > 0) {
			buySingle.add.click();
		}
		
		buySingle.checkout.click(function() {
			cart.checkout();
			
			return false;
		});
		
	}
}

var catalog = {
	init: function() {
		if($('.products.multiple').length == 0) return;
				
		$('.products.multiple ul#products li.product a.image').hover(function() {
			catalog.hover($(this).parent(), 'info');
		}, function() {
			catalog.unhover($(this).parent(), 'info');
		});
		
		$('.products.multiple ul#products li.product .caption .name a').hover(function() {
			catalog.hover($(this).parent().parent().parent(), 'info');
		}, function() {
			catalog.unhover($(this).parent().parent().parent(), 'info');
		});
		
		$('.products.multiple ul#products li.product .caption a.info').hover(function() {
			catalog.hover($(this).parent().parent(), 'info');
		}, function() {
			catalog.unhover($(this).parent().parent(), 'info');
		});
		
		$('.products.multiple ul#products li.product .caption a.buy').hover(function() {
			catalog.hover($(this).parent().parent(), 'buy');
		}, function() {
			catalog.unhover($(this).parent().parent(), 'buy');
		});
		
	},
	hover: function(product, type) {
		product.addClass('hover-' + type);
	},
	unhover: function(product, type) {
		product.removeClass('hover-' + type);
	}
}

$(document).ready(function() {
	productImages.init();
	buySingle.init();
	catalog.init();
});