$(document).ready(function() {
						   
	//////////////////////////////////////////////////////////////////////	
	// Video Preview /////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	
	$('.video_text').live('click', function() {
			
		// Convert Youtube
		var href = this.href.replace(/watch\?v=/, "v/");

		$.fancybox({
			centerOnScroll 		: true,
			hideOnOverlayClick 	: false,
			width 				: 560,
			height 				: 340,
			type 				: 'swf',
			overlayOpacity 		: 0.7,
			overlayColor 		: '#000000',
			href				: href
		});

		return false;
	});
	
	//////////////////////////////////////////////////////////////////////
	// Adding Products to Basket /////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	
	$('#add_product').submit(function(){
		
		var $form = $(this);
		
		// Disable the submit button to stop multiple additions
		$form.find('[name=add_to_basket]').attr('disabled',true);
		
		$.getJSON("/shopping_basket/json/add-item.json.php",$(this).serialize(),function(response){
			
			// Enable the button again	
			$form.find('[name=add_to_basket]').attr('disabled',false);
			
			if(response.type == "success")
			{	
				// Clear any none-hidden fields
				$form.find(':input:not(:hidden,button)').val('');
				
				// Run the updateBasket function
				$.basketOverview.updateBasket(function(){
					
					// Hide all the items
					$('#basket_items .item').hide();
					
					// Add success message
					$form.append('<p class="response success">' + response.message + '</p>');
					$('#basket_totals_wrap').hide();
					
					// Find the newest item
					var $newest_item = $('#basket_items .item[rel=basket_item_' + response.id + ']');
					
					// Do some things with the newest item
					$newest_item.show().addClass('success_item')
						.before('<p class="response success">' + response.message + '</p>');
					
					// Show the basket after an item was added
					$.basketOverview.show(function(){
						// Wait 3 seconds
						setTimeout(function(){
							// And then hide it...
							$.basketOverview.hide(function(){
								$newest_item.removeClass('success_item');
								$('.response').remove();
								$('#basket_items .item').show();
								$('#basket_totals_wrap').show();
							});					
						},3000);
					});
					
					// Load any new personalisation lines
					$.get('/wedding-shop/ajax/get-personalisation-lines.php',
						{
							method 		: 'ajax', 
							product_id 	: $form.find(':input[name=product_id]').val()
						},
						function(response)
						{
							$('#existing_messages').html(response);
						},"html"
					);
					
				});				
			}
			else
			{
				// Add error message if there was a problem
				$form.append('<p class="response error">' + response.message + '</p>');
			}
			
			// Remove any responses after 3 seconds
			$form.find('.response').delay(3000).fadeOut();
			
		});
		return false;
		
	});
	
	//////////////////////////////////////////////////////////////////////
	// Product Details/Delivery Cycle ////////////////////////////////////
	//////////////////////////////////////////////////////////////////////	
	
	$('#product_detail_wrap > div').cycle({
		sync	:	0,
		fit		:	0,
		before	:	function() {
			$(this).parent().animate({height : $(this).height()});									
		}
	}).cycle('pause');
	
	$("#product_tabs").find("li").click(function() {
		$(this).addClass('active').siblings().removeClass('active');
		$('#product_detail_wrap > div').cycle($(this).index());
		return false;
	});	

});

//////////////////////////////////////////////////////////////////////
// Cloud Zoom Related Activities /////////////////////////////////////
//////////////////////////////////////////////////////////////////////	

$(window).load(function(){

	// Restrict height to 600, or take the outerheight of the product wrapping div
	var height = ($('#product_detail_info').outerHeight() > 600)
		? 600
		: $('#product_detail_info').outerHeight();
	
	// Set CloudZoom options
	var cloudZoomOpts = "adjustX: 40, adjustY: 0, zoomWidth: 437, zoomHeight: " + height + ", smoothMove: 7";
	
	// Insert options, add the class and init CloudZoom
	$('#rotating_images li:first a.has_hi_res').attr('rel', cloudZoomOpts).addClass('cloud-zoom').CloudZoom();
	
	// Start the cycle plugin
	$('#rotating_images').cycle({
		sync	:	0,
		fit		:	0,								
		timeout : 7500,
		pause 	: true,
		after 	: function(){
			// After each cycle, try apply the CloudZoom plugin to the new image
			$(this).find('a.has_hi_res:not(.cloud-zoom)').attr('rel',cloudZoomOpts).addClass('cloud-zoom').CloudZoom();
		},
		before	:	function() {
			$(this).parent().animate({height : $(this).height()});									
		}		
	});
	
	// Attach the click function to the more images thumbnails
	$('.additional_image').click(function(){
		var index = $('.additional_image').index(this);
		$('#rotating_images').cycle(index); 
		return false;
	});
	
	$('#rotating_images').mouseout(function(){
		$(this).cycle('resume');								
	});	

});
