$(function() {
	 if(!document.createElement)return;
	 
	galleryThumbs();
	
	$("input.quant").click(function(){
		// Select field contents
		this.select();
	});
	
	$("input.quant").click(function(){
		// Select field contents
		this.select();
	});
	
	$('a.addToCart').click(function(e) {
		// Pop items into cart using json and show result
		e.preventDefault();
		var pID = $(this).attr('rel');
		var quant = $(this).parent().find('input').val();

		$('#addToCartPopup').find('a.close').click(function() {
			closeCartPopup();
		});
		$('#addToCartPopup').find('span.button.continue').click(function() {
			closeCartPopup();
		});
		$('#addToCartPopup').css({
			top: $(this).offset().top + $(this).height() + 10 + 'px',
			left: $(this).offset().left - ($('#addToCartPopup').width() / 2) - 35 + 'px'
		});
		
		if(quant == 0) {
			$('#addToCartPopup').show().find('h4').text('No items added to cart.');
			$('#addToCartPopup').find('p').html('<span class="instruct">Please enter a quantity and press \'Add\'.</span><span class="button continue"><a name="continue">Continue shopping</a></span><span class="button checkout"><a href="/cart">Cart/Checkout</a></span>');
			$('body').bind('click', closeCartPopup);
		} else {
			$('#addToCartPopup').show().find('h4').text('Please wait...');
			$('#addToCartPopup').find('p').html('');

			$.ajax({
				dataType: 'json',
				url : "/cart.json?cis=" + pID + "&itemQuantity=" + quant,
				type: 'GET',
				beforeSend: function() {
					$('#addToCartPopup').show().find('h4').text('Please wait...');
					$('#addToCartPopup').find('p').html('');
				},
				success: function(data) {
					var pluralCart = ((data.Boss.Cart.NumItems > 1 || data.Boss.Cart.NumItems == 0) ? 's' : '');
					var pluralQuant = ((quant > 1 || quant == 0) ? 's' : '');
					
					$('body').bind('click', closeCartPopup);
					if (!data.error) {
						$('#addToCartPopup').show().find('h4').text(quant + ' item' + pluralQuant + ' added to cart!');
						$('#addToCartPopup').find('p').html('<span class="numItems">You have ' + data.Boss.Cart.NumItems + ' item' + pluralCart + ' in your cart.</span><span class="totalCost"><strong>Total cost: ' + data.Boss.Cart.TotalPrice + '</span></strong></span><span class="button continue"><a name="continue">Continue shopping</a></span><span class="button checkout"><a href="/cart">Cart/Checkout</a></span>');
						$('#addToCartPopup').fadeIn(250);
					} else {
						$('#addToCartPopup').show().find('h4').text('Insufficient Stock');
						$('#addToCartPopup').find('p').html('<span class="numItems">You have ' + data.Boss.Cart.NumItems + ' item' + pluralCart + ' in your cart.</span><span class="totalCost"><strong>Total cost: ' + data.Boss.Cart.TotalPrice + '</span></strong></span><span class="button continue"><a name="continue">Continue shopping</a></span><span class="button checkout"><a href="/cart">Cart/Checkout</a></span>');
						$('#addToCartPopup').fadeIn(250);
					}
				}
			});
		}
		return false;
	});
});

function closeCartPopup() {
	$('#addToCartPopup').fadeOut(200);
	$('body').unbind('click', closeCartPopup);
}


function galleryThumbs() {
	 // Gallery Page thumbnails
	 var $imgThumb = null;
	 var $imgLarge = null;

	$('#pic-gallery .large img').load(function(){
		$('#pic-gallery').fadeIn(600);
	});

	 $('#pic-gallery .thumbnails a').click(
		function(event){
			event.preventDefault();

			$imgThumb = $(this).find('img');
			$imgLarge = $('#pic-gallery .large img');

			$('#pic-gallery .large').fadeOut(400, function() {
				var oldUrl = $imgLarge.attr('src');
				var imageUrl = $imgThumb.attr('src').replace('-th','');

				$imgLarge.attr('src',imageUrl);
				$imgLarge.removeClass();
		 
				if($imgThumb.width() > $imgThumb.height()) {
					 $imgLarge.addClass('landscape');
				} else {
					 $imgLarge.addClass('portrait');
				}

				// check if same image clicked
				if(imageUrl = oldUrl) {
					$('#pic-gallery .large').fadeIn(400);
				}

				$imgLarge.load(function() {
						var caption = $imgThumb.attr('alt');
						$('#pic-gallery .large .caption p').html(caption);
						$('#pic-gallery .large').fadeIn(400);
				});
			});
		}
	);
}

