	$(document).ready(function() {

		var megazoom_img;
		var zoom_img;
		var ratio;
		var ratioY;
		var zoom_offset_top = $('.zoom_content').offset().top;
		var zoom_offset_left = $('.zoom_content').offset().left;
		var megazoom_height = 0;
		var megazoom_width = 0;
		var isZoomed = false;

		var timerCounter = 0;
		var timerResetValue = 5;

		
		
		$('img.smallImage').dbtips({
			text    : 'CLICK TO ZOOM',
			offsetX : -10,
			offsetY : 40
		});

		// Show prices on mouseover for related items
		$('.related_product').hover(function() {
		   $(this).find('span').fadeIn('normal');
		}, function() {
			 $(this).find('span').fadeOut('normal');
		});


		// tabs starts
		$(function(){
			$("ul.tabs").tabs("div.panes > div");
		});
		// tabs ends

		if ( $('#fit_guide_details').is('div') ) {
			$('#fit_guide_details').jScrollPane();
		}

		$('.size_guide_link a').click(function() {
			$('#fit_guide').css('visibility', 'visible');
			return false;
		});

		$('#close_details').click(function() {
			$('#fit_guide').css('visibility', 'hidden');
			return false;
		});

		
		/**
		 * Other views images actions
		 */
		$('#more_images_links a').click(function() {
			return false;
		});
		
		$('#more_images_links a').mouseenter(function() {
			var thisIndex = $('#more_images_links a').index(this);

			if ( !$('#small_carousel .jqzoomsContent:eq(' + thisIndex + ')').hasClass('active') ) {
				if ( isZoomed == true ) {
					$('#zoom_area').trigger('click');
				}
				
				$('#small_carousel .jqzoomsContent').removeClass('active').eq(thisIndex).addClass('active');

				$('#small_carousel .jqzoomsContent.active').fadeIn(300);

				$('#small_carousel .jqzoomsContent:not(.active)').fadeOut(300);
			}

			return false;
		});

		var image_number_p = 1;
		var image_count_p = $('.more_images img.otherview').length;
		var image_jump_p = 130;
		var images_to_display_p = 3;
		var animate_in_progress_p = false;

		refreshStepsP();

		$('#otherview_next').click(function() {
			doStepP(true);
			return false;
		});

		$('#otherview_prev').click(function() {
			doStepP(false);
			return false;
		});

		function getCurrentPositionP() {
			return Math.abs(parseInt($('#more_images_links').css('margin-top')));
		}

		function isStepAvailableP(next) {
			if ( next == true ) {
				return (image_count_p>images_to_display_p && (image_count_p-images_to_display_p)>=image_number_p) ? true : false;
			}
			else {
				return (image_number_p>1) ? true : false;
			}
		}

		function refreshStepsP() {
			if ( isStepAvailableP(true) ) {
				$('#otherview_next').fadeIn();
			}
			else {
				$('#otherview_next').fadeOut();
			}

			if ( isStepAvailableP(false) ) {
				$('#otherview_prev').fadeIn();
			}
			else {
				$('#otherview_prev').fadeOut();
			}
		}

		function getStepP(next) {
			var currentPosition = getCurrentPositionP();
			
			if ( isStepAvailableP(next) ) {
				if ( next == true ) {
					currentPosition += image_jump_p;
				}
				else {
					currentPosition -= image_jump_p;
				}
			}

			return (currentPosition>0) ? currentPosition : 0;
		}

		function doStepP(next) {
			if ( !animate_in_progress_p ) {
				animate_in_progress_p = true;
				
				$('#more_images_links').animate(
					{
						marginTop: '-' + getStepP(next) + 'px'
					},
					{
						queqe: false,
						duration: 750,
						complete: function(){
							animate_in_progress_p = false;

							if ( next ) {
								image_number_p++;
							}
							else {
								image_number_p--;
							}

							refreshStepsP();
						}
					}
				);
			}
		}



		$('#size_select').change(function() {
			var sizeText = $(this).find('option:eq('+ this.selectedIndex +')').text();
			$('#product_size').val(sizeText);
		});
		
		// reload the correct page if colour is changed
		$('#colour_select').change(function() {
			if($(this).val() != "") {
				$('#colour_change_form').submit();
			}
		});
		
		var is_adding_product = false;
		
		// AJAX handle the buy button being clicked
		$('#buy').click(function() {
			handleBuyRequest();
			return false;
		});
		
		function handleBuyRequest() {
			var error_flag = 0;
			if ( $('#size_select').val() == '' ) {
				error_flag = 1;
				showMessage('Please select a size before adding this item', 2);
			}
			if ( is_adding_product == true ) {
				error_flag = 1;
				showMessage('Adding item to your basket', 2);
			}
			
			if(error_flag == 0) {
				is_adding_product = true;
				showMessage('Adding item to your basket', 5);
				
				$.post(reiss_base_url+'shopping_bag/', {
					'prod_desc_id' : getProdDescId(),
					'ajax'         : true,
					'act'          : 'add'
				}, function(){
					if ( !msg ) {
						showMessage('This item has been added to your basket', 2);
						$('#purchase_link_container').show();
					}
					else {
						showMessage(msg, 3);
					}
					
					is_adding_product = false;

					var oldClass = 'items1';
					var newClass = 'items1';
					
					if ( newCount > 1 ) {
						oldClass = 'items' + (newCount-1);
						newClass = 'items' + newCount;
						
						if ( newCount > 25 ) {
							newClass = 'items25-2';
						}
					}
					
					$('.navBasket li a').removeClass(oldClass).addClass(newClass);
					$('.navBasketReiss1971 li a').removeClass(oldClass).addClass(newClass);
				}, "script");
			}
		}
		
		// this function returns the prod_desc_id as there may not be a size dropdownlist (one size)
		function getProdDescId() {
			if ( $('#size_select').length > 0 ) {
				return $('#size_select').val();
			}
			else {
				return $('#one_size_value').val();
			}
		}


		var timerInterval = setInterval(
			function() {
				timerCounter++;
				if ( timerCounter > timerResetValue ) {
					timerCounter = 0;
					$('#message_pane').fadeOut('normal', function() {
						$('#message_pane').empty();
					});
				}
			}, 1000
		);
		
		// show a custom error message
		function showMessage(messages, timeout) {
			timerCounter = 0;
			timerResetValue = timeout;

			$('#message_pane').html(messages);
			$('#message_pane').fadeIn('normal');
		}

		/**
		 * PRODUCT ZOOM FUNCTIONALITY
		 */
		

		/**
		 * MOUSEMOVE EVENT CAPTURE
		 */
		$('#zoom_area').live('mousemove', function(e){
			if ( isZoomed == true ) {
				megazoom_img.css({
					'top' : (0 - ((e.pageY - zoom_offset_top) * ratio)) + 'px',
					'left': (0 - ((e.pageX - zoom_offset_left) * ratioY)) + 'px'
				});
			}
		});

		$('#zoom_area').mouseleave(function(e) {
			if ( e.relatedTarget.className != undefined && e.relatedTarget.className == 'dbtips' ) {
				return true;
			}

			$('#zoom_area').trigger('click');
			
			return true;
		});

		/**
		 * ZOOM IN
		 */
		$('.jqzooms').click(function() {
			$('#zoom_area .zoom_content').empty();
			$('#zoom_area').show();

			showLoader($('#zoom_area'));

			$('#zoom_area .zoom_content').append('<img class="megazoom_image" />');
			megazoom_img = $('#zoom_area').find('.megazoom_image');
			zoom_img = $(this).find('img');
			var href = $(this).attr('href');

			$("<img />")
			.bind("load", function() {
				$(megazoom_img).attr("src", href);

				megazoom_height = megazoom_img.height();
				megazoom_width = megazoom_img.width();

				ratio = (megazoom_height - zoom_img.height()) / zoom_img.height();
				ratioY = (megazoom_width - zoom_img.width()) / zoom_img.width();

				var position_left = (megazoom_width - zoom_img.width()) / 2;
				var position_top = (megazoom_height - zoom_img.height()) / 2;

				megazoom_img.css({
					'width' : zoom_img.width(),
					'height' : zoom_img.height(),
					'top' : 0,
					'left' : 0,
					'opacity' : 0
				});

				megazoom_img.dbtips({
					text    : 'CLICK TO CLOSE',
					offsetX : -10,
					offsetY : 40
				});

				hideLoader($('#zoom_area'));

				megazoom_img.animate(
					{
						'left'		: '-' + position_left + 'px',
						'top'		: '-' + position_top + 'px',
						'width'		: megazoom_width + 'px',
						'height'	: megazoom_height + 'px',
						'opacity'	: 1
					},
					{
						duration	: 1500,
						queque		: false,
						complete	: function() {
							zoom_offset_top = $('.zoom_content').offset().top;
							zoom_offset_left = $('.zoom_content').offset().left;
							
							isZoomed = true;
						}
					}
				);
			})
			.attr("src", href);

			return false;
		});

		/**
		 * ZOOM OUT
		 */
		$('#zoom_area').click(function() {
			if ( isZoomed == true ) {
				$('#zoom_area').find('img').animate(
					{
						'left'		: '0px',
						'top'		: '0px',
						'width'		: '400px',
						'height'	: '400px'
					},
					{
						duration	: 750,
						queque		: false,
						complete	: function() {
							$('#zoom_area').hide();

							isZoomed = false;
						}
					}
				);
			}
			
			return false;
		});



	});


/**
 * Show AJAX loader
 */
function showLoader(container) {
	container.css(
		'background',
		'transparent url(/images/ajax-loader_large.gif) no-repeat center center'
	);
}

/**
 * Hide AJAX loader
 */
function hideLoader(container) {
	container.css(
		'background',
		'none'
	);
}