$(document).ready(function()
{
	var product_id = $('#var-product-id').text();
	var photo_id = $('#var-photo-id').text();
	var base_url = $('#var-base-url').text();

	//front-back switching
	$('.quickflip-wrapper').quickFlip();
	$('.front-back-flipper').click(function(event)
	{
		event.preventDefault();
		$('.quickflip-wrapper').quickFlipper();
		return false;
	});

	//mouseover/out controls show/hide (prev-next)
	var config = 
	{
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 400, // number = milliseconds for onMouseOver polling interval    
		over: hide_controls, // function = onMouseOver callback (REQUIRED)    
		timeout: 1500, // number = milliseconds delay before onMouseOut    
		out: show_controls // function = onMouseOut callback (REQUIRED)    
	};
	$('.product-container').hoverIntent(config)
	
	//add the current product to the shopping cart
	$('.action-add-to-cart').click(function(event)
	{	
		event.preventDefault();
		$('#add-to-cart-failed').hide();
		$('#add-to-cart-adding').show('normal');
		$('#header-shop-resume').fadeOut();
		$('#header-shop-loading').show();
		
		//do ajax
		var uri_add_product = base_url + 'products/ajax_add/' + product_id + '/1/1337' + Math.floor(Math.random()*9999999);
		$.get(uri_add_product, function(data)
		{
			$('#add-to-cart-adding').slideUp(100);
			if(data == 'SUCCESS') 
			{
				var product_count = parseInt($('#cart-product-amount').text());
				product_count += 1; 
//				$('#cart-product-amount').html(product_count);
				$('#product-in-basket').slideDown();
				
				//update the cart summary in the header
				var shop_url = base_url + 'products/ajax_cart/1337' + Math.floor(Math.random()*9999999);
				$.getJSON( shop_url, function(cart_info)
				{
					$('#header-shop-nr-of-products span').text(cart_info.nr_of_products);
					$('#header-shop-final-price span').text(cart_info.display_final_price);
					$('#header-shop-loading').hide();
					$('#header-shop-resume').slideDown('normal');

					var new_product_amount = parseInt(cart_info.products[product_id].amount);
					if(new_product_amount > 0)
					{
						$('#adding-success').show();
						$('.amount_container').slideDown();
						$('#add-to-cart-link').hide();
						$('#cart-product-amount').val(new_product_amount);
					}
				});
			}
			else
			{
				$('#add-to-cart-failed').show();
				
				$('#header-shop-loading').hide();
				$('#header-shop-resume').slideDown('normal');
			}
		});
		Cufon.now();
		return false;
	});


//	//add canvas to the shopping cart
//	$('#action-add-canvas-to-cart').click(function(event)
//	{	
//		event.preventDefault();
//		$('#add-to-cart-success').hide();
//		$('#add-to-cart-failed').hide();
//		$('#add-to-cart-adding').show('normal');
//		$('#header-shop-resume').fadeOut();
//		$('#header-shop-loading').show();
//		

		//do ajax// /1337' + Math.floor(Math.random()*9999999);
//		var uri_add_product = base_url + 'products/ajax_add_canvas/' + photo_id;
//		$.get(uri_add_product, function(data)
//		{
//			alert(data);
//			$('#add-to-cart-adding').hide();
//			if(data == 'SUCCESS') 
//			{
//				$('#add-to-cart-success').show();
//				var product_count = parseInt($('#product-amount').text());
//				product_count += 1; 
//				$('#product-amount').html(product_count);
//				$('#product-in-basket').slideDown();
//				
//				//update the cart summary in the header
//				var shop_url = base_url + "products/ajax_cart";
//				///1337' + Math.floor(Math.random()*9999999);
//				$.getJSON( shop_url, function(cart_info)
//				{
//					$('#header-shop-nr-of-products span').text(cart_info.nr_of_products);
//					$('#header-shop-final-price span').text(cart_info.display_final_price);
//					$('#header-shop-loading').hide();
//					$('#header-shop-resume').slideDown('normal');
//				});
//			}
//			else
//			{
//				$('#add-to-cart-failed').show();
//			}
//		});
//		return false;
//	});

	//vote for this product
	$('#vote-container a').click(function(event)
	{
		event.preventDefault();
		$('#product-vote-status-success').hide();
		$('#product-vote-status-failed').hide();
		$('#product-vote-status-invalid').hide();
		
		$('.product-vote-status').slideDown('fast', function()
		{
		
			var uri_vote = base_url + 'votes/product/' + product_id + '/ajax/1337' + Math.floor(Math.random()*9999999);
			$.get(uri_vote, function(data)
			{
				$('.product-vote-status').slideUp('normal', function()
				{
					if(data == 'SUCCESS') 
					{
						$('#product-vote-status-success').show('normal', function()
						{
							//update the current amount of votes
							var counter = parseInt($('#product-vote-count').html()); 
							counter++;
							$('#product-vote-count').html(counter);
						});
					}
					else if(data == 'FAILED') 
					{
						$('#product-vote-status-failed').show(); 
					}
					else
					{
						//(data == 'NOT_FOUND')
						$('#product-vote-status-invalid').show(); 
					}
				});
			});
			$('#vote-container a').unbind('click').click(function()
			{ 
				$('#product-vote-status-success').slideUp('fast');
				$('#product-vote-status-invalid').slideUp('fast');
				$('#product-vote-status-failed').slideUp('fast');
				
				$('#vote-container a').unbind('click').click(function()
				{
					$('#product-vote-status-success').hide();
					$('#product-vote-status-invalid').hide();
					$('#product-vote-status-failed').slideToggle('normal');
					return false; 
				})
				return false;
			});
		});
	    return false;
	});
});

function hide_controls()
{  
	$('#action-browse-prev').fadeOut();
	$('#action-browse-next').fadeOut();
}
function show_controls()
{
	$('#action-browse-prev').fadeIn();
	$('#action-browse-next').fadeIn();
}
