	$(document).ready(function() {

		var megazoom_img;
		var ratio;
		var ratioY;
		var zoom_offset_top = $('#product_zoom_content .zoom_content').offset().top;
		var zoom_offset_left = $('#product_zoom_content .zoom_content').offset().left;
		var megazoom_height = 0;
		var megazoom_width = 0;
		var zoom_height = 0;
		var zoom_width = 0;
		var isZoomed = false;
		var zoom_clicked_image_no = 0;
		var zoom_lighbox_height = $('#wrapper').height()>800 ? $('#wrapper').height() : 800;
		var zoom_lighbox_width = 950;
		var zoom_animation_in_progress = false;
		var zoom_mouse_over_thumb_image = -1;
		var image_count_p = $('.more_images .otherview').length;

		var timerCounter = 0;
		var timerResetValue = 5;


		/**---------------------------------------------------------------------
		 * Apply jCarouselLite plugin to alternative view images
		 */
		if ( $('#product_content #more_images_links .more_images_a .otherview').length > 2 ) {
			$('#product_content #more_images_links').jCarouselLite({
				btnNext: '#otherview_next',
				btnPrev: '#otherview_prev',
				vertical: false
			});
		}
		else {
			$('#otherview_next, #otherview_prev').click(function(){
				return false;
			});
			
			if ( $('#product_content #more_images_links .more_images_a .otherview').length == 2 ) {
				$('#product_content #more_images_links').jCarouselLite({
					// btnNext: '#otherview_next',
					// btnPrev: '#otherview_prev',
					vertical: false
				});
			}
		}


		/**---------------------------------------------------------------------
		 * Apply DBTIPS pligin for product images on main product page
		 */
		/*$('img.smallImage').dbtips({
			text    : 'CLICK TO ZOOM',
			offsetX : -10,
			offsetY : 40
		});*/

		/**---------------------------------------------------------------------
		 * Create select size boxes
		 */
		$('select#size_select').each(function(){
			var size_select_box = $('<ul/>');
			var temp_size_box = '';
			var size_select = $(this);
			var counter = 0;
			
			$(this).hide();
			$(this).children().each(function(){
                if ( $(this).val() ) {
                    counter++;
					temp_size_box = $('<li/>').addClass('size_button').append($(this).text());
                    if ( $(this).attr('selected') != '' ) {
                        temp_size_box.addClass('size_selected');
                    }
                    if ( $(this).hasClass('size_not_available') ) {
                        temp_size_box.addClass('size_not_available');
                    }
					if ( /oos\_status\_/.test($(this).attr('class')) ) {
						temp_size_box.addClass($(this).attr('class').replace(/.*(oos\_status\_.*)/, '$1'));
					}
                    if ( counter%4 == 0 ) {
                        temp_size_box.css('margin-right', '0px');
                    }
                    size_select_box.append(temp_size_box.append($('<span class="under_line"></span>')));
                }
			});
			
			size_select_box.find('li.size_button').each(function(){
				$(this).click(function(){
					$(this).trigger('mouseenter');
					
					var temp_this_index = size_select_box.find('li.size_button').index(this);
					var temp_this_index_select = temp_this_index;
					if ( size_select.children().eq(0).val() == '' ) {
						temp_this_index_select += 1;
					}
					
					size_select_box.find('li.size_button.size_selected').removeClass('size_selected');
					size_select_box.find('li.size_button').eq(temp_this_index).addClass('size_selected');
					size_select.children().attr('selected', false).eq(temp_this_index_select).attr('selected', 'selected');

					return false;
				});
				
				$(this).mouseenter(function(){
					if ( $(this).hasClass('size_not_available') ) {
						$('#buy').hide();
						$('.oos_text').hide();
						$('.oos_button').show();
						
						if ( $(this).hasClass('oos_status_notify') ) {
							// TODO later:
						}
						else if ( $(this).hasClass('oos_status_preorder') ) {
							// TODO later:
						}
					}
					else {
						$('.oos_button').hide();
						$('#buy').show();
						$('.oos_text').show();
					}
				});
				
				$(this).mouseleave(function(){
					if ( $(this).hasClass('size_selected') && $(this).hasClass('size_not_available') ) {
						// no action required here
					} else {
						$('#buy').show();
						$('.oos_text').show();
						$('.oos_button').hide();
					}
				});
			});
			

			size_select.after(size_select_box);
		});

		/**---------------------------------------------------------------------
		 * Hide colour select and use nice colour swatches
		 */
		$('#colour_select').hide();
		$('#colour_select_box').show();
		$('#colour_select_box li.colour_button').click(function(){
			var temp_this_index = $('#colour_select_box').find('li.colour_button').index(this);
			$('').find('li.colour_button.colour_selected').removeClass('colour_selected');
			$('#colour_select_box').find('li.colour_button').eq(temp_this_index).addClass('colour_selected');
			$('#colour_select').children().attr('selected', false).eq(temp_this_index).attr('selected', 'selected');
			$('#colour_select').trigger('change');
			return false;
		});
		$('#colour_select_box li.colour_button').mouseover(function(){
			$(this).addClass('hover');
		});
		$('#colour_select_box li.colour_button').mouseout(function(){
			$(this).removeClass('hover');
		});
		
		

/*
		// Show prices on mouseover for related items
		$('.related_product').hover(function() {
		   $(this).find('span').fadeIn('normal');
		}, function() {
			$(this).find('span').fadeOut('normal');
		});
*/
		$('.related_items a:not(#related_toggle)').click(function(){
			if (typeof(event_tracking_enabled) != 'undefined') {
				track.productRelatedClick($('#event_tracking_category').text(), $(this).attr('href'));
			}
		});

		/**
		 * Size Guide / Fit Guide
		 */
		if ( $('#fit_guide_details').is('div') ) {
			$('#fit_guide_details').jScrollPane();
		}

		$('.size_guide_link a').click(function() {
			if (typeof(event_tracking_enabled) != 'undefined') {
				track.sizeChartClick($('#event_tracking_category').text(), 'size chart');
			}
			$('#fit_guide').css('visibility', 'visible');
			return false;
		});

		$('#fit_guide_close').click(function() {
			$('#fit_guide').css('visibility', 'hidden');
			return false;
		});
		

		/**
		 * Fit Guide Tabs
		 */
		 $('#tab_chart').click(function() {
			closeAll();
			$(this).addClass('selected');
			$('#det_chart').show();	
			return false;
		});
		$('#tab_conversion').click(function() {
			closeAll();
			$(this).addClass('selected');
			$('#det_conversion').show();
			return false;
		});
		$('#tab_guide').click(function() {
			closeAll();
			$(this).addClass('selected');
			$('#det_guide').show();
			return false;
		});
		 
		 function closeAll() {
			 $('#fit_guide_tabs ul a').removeClass('selected');
			 $('#fit_guide_tabs .fit_guide_det').hide();
		 }
		 
		 
		
		
		
		/**
		 * Related product toggle
		 */
		$('#related_toggle').click(function() {
			if( $('#related_product_container').css("display") == 'none' ) {
				$('#related_product_container').slideDown();
				$('#related_toggle').css('background-image', 'url("/images/pages/product/arrow_up.gif")');
			} else {
				$('#related_product_container').slideUp();
				$('#related_toggle').css('background-image', 'url("/images/pages/product/arrow_down.gif")');
			}
		});
		
		/**
		 * Accordion Menu
		 */
		//accordion menu initialiser for product details
		$("#product_details_accordion").accordionMenu({
			beforeStart : function() {
				//
			},
			afterEnd: function() {
	
				$('.open').find('ul').slideDown();
				
				var $firstLi = $('#product_details_accordion li:first-child');
				var $li = $(this).parent();
	
				if(!$li.parent().children().hasClass('open')) {
					$li.next().find('> ul').slideDown();
					$li.next().addClass('open');
	
					if($li.nextAll().length == 0) {
						$firstLi.find('> ul').slideDown();
						$firstLi.addClass('open');
					}
				}
			}
		});
		$('.size_button').click(function(){
			if ( !$('#size_and_fit_tab').parent().parent().hasClass('open') ) {		 
				$('#size_and_fit_tab').trigger('click');
			}
			return false;
		});
		
		/**
		 * Other views images actions
		 */
		$('#product_content #more_images_links li img').each(function(){
			if ( $(this).height() > 100 ) {
				$(this).css('height', '100px');
			}
		});

		$('#product_content #more_images_links li a').click(function() {
			return false;
		});
		
		$('#product_content #more_images_links li a').mouseenter(function() {
			if ($(this).parent().hasClass('play_video_button')) {
				return false;
			}
			
			var thisIndex = parseInt($(this).attr('class').replace(/image_number_/, ''));
			
			if ( !$('#product_content #small_carousel .jqzoomsContent:eq(' + thisIndex + ')').hasClass('active') ) {
				$('#product_content #small_carousel .jqzoomsContent').removeClass('active').eq(thisIndex).addClass('active').fadeIn(300);
				$('#product_content #small_carousel .jqzoomsContent:not(.active)').fadeOut(300);
			}

			return false;
		});

		/**---------------------------------------------------------------------
		 * Show video player on product page
		 */
		$('#product_content .play_video_link').click(function(){
			if ( $('#product_content .jqzoomsContentVideo').length != 0 ) {
				var thisIndex = $('#product_content .jqzoomsContent').index($('#product_content .jqzoomsContentVideo'));
				
				if ( !$('#product_content #small_carousel .jqzoomsContent:eq(' + thisIndex + ')').hasClass('active') ) {
					$('#product_content #small_carousel .jqzoomsContent').removeClass('active').eq(thisIndex).addClass('active').fadeIn(300);
					$('#product_content #small_carousel .jqzoomsContent:not(.active)').fadeOut(300);
				}
			}
			return false;
		});

		
		$('#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();
			$('#buy').addClass('selected');
			return false;
		});
		
		function handleBuyRequest() {
			var error_flag = 0;
			if ( $('#size_select').val() == '' ) {
				error_flag = 1;
				showMessage('Please select your size from the available options', 2);
			}
			if ( is_adding_product == true ) {
				error_flag = 1;
				showMessage('Adding item to your basket', 2, is_adding_product);
			}
			
			if(error_flag == 0) {
				is_adding_product = true;
				var product_added = $('#event_tracking_product').text();
				showMessage(product_added+'<br />has been added to your bag', 5, is_adding_product);
				if (typeof(event_tracking_enabled) != 'undefined') {
				    track.basketAddProduct($('#event_tracking_category').text(), $('#event_tracking_price').text());
				}
				
				$.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);
					}
					
					is_adding_product = false;

					var oldClass = 'items0';
					var newClass = 'items1';
					var items_text = 'item';

					if ( newCount > 1 ) {
						items_text = 'items';
						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);
					$('.navBasket li a').text('BASKET (' + newCount + ')');
					var currency_symbol = $('#event_tracking_currency_symbol').text();
					$('#message_pane #bag_contents').html( '<a href="' + reiss_base_url + 'shopping_bag/" id="bag_contents_link" >' + 'Your Bag (' + newCount + ') ' + items_text + ' ' + currency_symbol + newPrice + '</a>' );
					$('.toplinks .shopping_bag a').text('Bag {' + newCount + '}');
				}, "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();
					//});
					$('#buy').removeClass('selected');
				}
			}, 1000
		);
		
		// show a custom error message
		function showMessage(messages, timeout, is_adding_product) {
			timerCounter = 0;
			timerResetValue = timeout;

            category_url = $('#category_url').val();
            
			var content = '<a id="message_pane_close">CLOSE</a>';
			content = '<p class="message">'+messages+'</p>'+content;
			if (is_adding_product === true) {
				content = content+'<p id="bag_contents"></p>';
				content = content+'<p>Would you like to</p>';
				content = content+'<a id="proceed_to_checkout" href="' + reiss_base_url + 'checkout1/">PROCEED TO CHECKOUT</a>';
				content = content+'<a id="continue_shopping" href="' + category_url + '">CONTINUE SHOPPING</a>';
			}
			$('#message_pane').html(content);
			$('#message_pane').fadeIn('normal');
			
			$('#message_pane_close').click(function() {
				$('#message_pane').fadeOut('normal', function() {
				$('#message_pane').empty();
			});
		});
		}

		/**
		 * PRODUCT ZOOM FUNCTIONALITY
		 */
		

		/**
		 * MOUSEMOVE EVENT CAPTURE
		 */
		/*$('#product_zoom_content #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'
				});
			}
		});*/

		/**---------------------------------------------------------------------
		 * Load and display megazoom image - zoom in
		 */
		/*$('#product_zoom_content .jqzoomsContent').live('click', function() {
			if ( $(this).hasClass('jqzoomsContentVideo') ) {
				zoom_animation_in_progress = true;

				closeZoom();

				$('#product_zoom_content #zoom_area .zoom_content').empty();
				$('#product_zoom_content #zoom_area').show();

				showLoader($('#product_zoom_content #zoom_area'), false);

				hideLoader($('#product_zoom_content #zoom_area'));

				zoom_animation_in_progress = false;

				return false;
			}

			zoom_animation_in_progress = true;

			closeZoom();
			
			$('#product_zoom_content #zoom_area .zoom_content').empty();
			$('#product_zoom_content #zoom_area').show();

			showLoader($('#product_zoom_content #zoom_area'), false);
			
			$('#product_zoom_content #zoom_area .zoom_content').append('<img class="megazoom_image" />');
			megazoom_img = $('#product_zoom_content #zoom_area').find('.megazoom_image');
			var zoom_img = $(this).find('.jqzooms img');
			var href = $(this).find('.jqzooms').attr('href');
			
			$("<img />")
			.bind("load", function() {
				$(megazoom_img).attr("src", href);

				megazoom_height = megazoom_img.height();
				megazoom_width = megazoom_img.width();
				
				zoom_height = zoom_img.height() + (zoom_img.position().top * 2);
				zoom_width = zoom_img.width() + (zoom_img.position().left * 2);

				ratio = (megazoom_height - zoom_height) / zoom_height;
				ratioY = (megazoom_width - zoom_width) / zoom_width;

				var position_left = (megazoom_width - zoom_width) / 2;
				var position_top = (megazoom_height - zoom_height) / 2;

				megazoom_img.css({
					'width' : zoom_width,
					'height' : zoom_height,
					'top' : 0,
					'left' : 0,
					'opacity' : 0
				});

				megazoom_img.dbtips({
					text    : 'CLICK TO CLOSE',
					offsetX : -10,
					offsetY : 40
				});
				
				megazoom_img.animate(
					{
						'left'		: '-' + position_left + 'px',
						'top'		: '-' + position_top + 'px',
						'width'		: megazoom_width + 'px',
						'height'	: megazoom_height + 'px',
						'opacity'	: 1
					},
					{
						duration	: 750,
						queque		: false,
						complete	: function() {
							zoom_offset_top = $('#product_zoom_content .zoom_content').offset().top;
							zoom_offset_left = $('#product_zoom_content .zoom_content').offset().left;

							isZoomed = true;
							
							hideLoader($('#product_zoom_content #zoom_area'));

							zoom_animation_in_progress = false;
						}
					}
				);
			})
			.attr("src", href);

			return false;
		});*/
		
		/**---------------------------------------------------------------------
		 * Close zoom image
		 */
		function closeZoom() {
			isZoomed = false;
		}

		/**---------------------------------------------------------------------
		 * Close zoom lighbox
		 */
		function closeZoomLightbox() {
			closeZoom();
			
			$('#wrapper #product_zoom_lightbox').fadeOut(500, function(){
				$('#wrapper #product_zoom_lightbox').css(
					{
						width: 0,
						height: 0,
						top: '400px',
						left: '350px'
					}
				);
			});

			if ( $('#product_content .jqzoomsContentVideo').hasClass('active') ) {
				$('#product_content .jqzoomsContentVideo').show();
			}
		}

		/**---------------------------------------------------------------------
		 * Product zoom area click event
		 */
		/*$('#product_zoom_lightbox #zoom_area .megazoom_image').live('click' ,function() {
			if ( isZoomed == true ) {
				closeZoomLightbox();
			}
			return false;
		});*/

		/**---------------------------------------------------------------------
		 * Product page big image click event - display zoom in lighbox
		 */
		$('#product_content .jqzooms').click(function(){
			$('#product_content .view_larger_image_link').trigger('click');
			return false;
		});

		/**---------------------------------------------------------------------
		 * Product page big image click event - display zoom in lighbox
		 */
		$('#product_content .view_larger_image_link').click(function(){
			if ( $('#product_content .jqzoomsContentVideo').hasClass('active') ) {
				$('#product_content .jqzoomsContentVideo').hide();
			}

			zoom_clicked_image_no = $('#product_content #small_carousel .jqzoomsContent').index($('#product_content #small_carousel .jqzoomsContent.active'));
			
			// Scroll page to top
			$('html,body').animate({scrollTop : 0}, 400, 'swing');

			// If zoom is not already loaded
			if ( $('#wrapper #product_zoom_lightbox').length == 0 ) {
				// Create zoom lightbox container
				$('#wrapper').append('<div id="product_zoom_lightbox"></div>');

				// Display close button
				$('#wrapper #product_zoom_lightbox').append('<div id="product_zoom_content"><div class="product_zoom_content_right"><a href="#" class="close">CLOSE X</a></div></div>');

				// Create lightbox close button
				var tempElem = $('<div/>')
									.attr('id', 'ajax_loader')
									.css({
											width: zoom_lighbox_width + 'px',
											height: zoom_lighbox_height + 'px'
										})
									.append('<div class="product_zoom_content_right"><a href="#" class="close">CLOSE X</a></div>');

				$('#wrapper #product_zoom_lightbox').append(tempElem);
			}

			// Show page ajax loader
			showLoader($('#wrapper #product_zoom_lightbox #ajax_loader'), true);

			// Animate stretching zoom lightbox
			$('#wrapper #product_zoom_lightbox').animate(
				{
					width: zoom_lighbox_width + 'px',
					height: zoom_lighbox_height + 'px',
					top: 0,
					left: 0
				},
				{
					duration: 1000,
					complete: function() {
						// If zoom is not already loaded
						if ( $('#wrapper #product_zoom_lightbox .product_zoom_content_center').length == 0 ) {
							$.ajax({
								url: reiss_base_url + 'product_lightbox/',
								data: {
									prod_desc_id : $('#lightbox_prod_desc_id').val(),
									ajax : 1
								},
								success: function(data) {
									// Create lightbox close button
									var tempElem = $('<div/>')
														.attr('id', 'ajax_loader')
														.css({
																width: zoom_lighbox_width + 'px',
																height: zoom_lighbox_height + 'px',
																display: 'none'
															})
														.append('<div class="product_zoom_content_right"><a href="#" class="close">CLOSE X</a></div>');

									// Display lighbox content
									$('#wrapper #product_zoom_lightbox').html(data).append(tempElem);

									$('#product_zoom_content .jqzoomsContentLookbook img').each(function(){
										if ( $(this).height() > 724 ) {
											$(this).css('height', '724px');
										}
									});
									
									// Load Superzoom on mouseenter over thumb images on left hand side
									$('#product_zoom_content .more_images_links a').click(function() {
										zoom_mouse_over_thumb_image = $('#product_zoom_content .more_images_links a').index(this);
										showZoom();
									});

									// Smal hack for old lookbok images to display them scalled in the window
									if ( $('#product_zoom_content .jqzoomsContentLookbook img').height() > 724 ) {
										$('#product_zoom_content .jqzoomsContentLookbook img').css('height', '724px');
									}

									/**---------------------------------------------------------------------
									 * Close zoom lightbox after click on big image
									 */
									$('#product_zoom_content .jqzoomsContent').click(function(){
										closeZoomLightbox();
										return false;
									});

									/**---------------------------------------------------------------------
									 * Product zoom lightbox close button event
									 */
									$('#product_zoom_lightbox .close').bind('click', function() {
										closeZoomLightbox();
										return false;
									});

									/**---------------------------------------------------------------------
									 * Product zoom lightbox previous arrow event - zoom previous product
									 */
									$('#wrapper #product_zoom_lightbox .prev').bind('click', function(){
										if ( zoom_mouse_over_thumb_image < 0 ) {
											zoom_mouse_over_thumb_image = zoom_clicked_image_no;
										}

										zoom_mouse_over_thumb_image--;

										if ( zoom_mouse_over_thumb_image < 0 ) {
											zoom_mouse_over_thumb_image = image_count_p + $('#product_zoom_content .otherviewVideo').length -1;
										}

										showZoom();

										return false;
									});

									/**---------------------------------------------------------------------
									 * Product zoom lightbox next arrow event - zoom next product
									 */
									$('#wrapper #product_zoom_lightbox .next').bind('click', function(){
										if ( zoom_mouse_over_thumb_image < 0 ) {
											zoom_mouse_over_thumb_image = zoom_clicked_image_no;
										}

										zoom_mouse_over_thumb_image++;

										if ( zoom_mouse_over_thumb_image >= image_count_p + $('#product_zoom_content .otherviewVideo').length ) {
											zoom_mouse_over_thumb_image = 0;
										}

										showZoom();

										return false;
									});

									$('#product_zoom_content img.smallImage').dbtips({
										text    : 'CLICK TO CLOSE',
										offsetX : -10,
										offsetY : 40
									});

									$('#product_zoom_content .more_images_links a').eq(zoom_clicked_image_no).trigger('click');

									// Remove ajax loader
									hideLoader($('#wrapper #product_zoom_lightbox #ajax_loader'), true);
								}
							});
						}
						else {
							$('#product_zoom_content .more_images_links a').eq(zoom_clicked_image_no).trigger('click');

							// Remove page ajax loader
							hideLoader($('#wrapper #product_zoom_lightbox #ajax_loader'), true);
						}

					}
				}
			);
		});

		function showZoom() {
			zoom_clicked_image_no = zoom_mouse_over_thumb_image;

			$('#product_zoom_content .more_images .more_images_a').removeClass('active').eq(zoom_clicked_image_no).addClass('active');

			$('#product_zoom_content .small_carousel .jqzoomsContent').removeClass('active').eq(zoom_clicked_image_no).addClass('active').fadeIn(200);
			$('#product_zoom_content .small_carousel .jqzoomsContent:not(.active)').fadeOut(200);

			return false;
		}

		
	});


/**
 * Show AJAX loader
 */
function showLoader(container, is_page_loader) {
	if ( is_page_loader === true ) {
		container.css({
			background : '#ffffff url(' + reiss_base_url + 'images/ajax-loader_large_solid.gif) no-repeat center center',
			display : 'block'
		});
	}
	else {
		container.css(
			'background',
			'transparent url(' + reiss_base_url + 'images/ajax-loader_large.gif) no-repeat center center'
		);
	}
}

/**
 * Hide AJAX loader
 */
function hideLoader(container, is_page_loader) {
	if ( is_page_loader === true ) {
		container.css({
			display : 'none'
		});
	}
	else {
		container.css(
			'background',
			'none'
		);
	}
}


//Set Variable for Event Tracking
product_page = true;


