$jq(document).ready(function() {
  // ajax tabs
  var tabToLoadIndex = 0;
	if (document.activate_rel_after_load) {
	  var tabToLoad = $jq('.gallery_menu a[rel='+document.activate_rel_after_load+']')[0];
		tabToLoadIndex = $jq('.gallery_menu a').index(tabToLoad);
		document.activate_rel_after_load = null;
	}
	
	$jq('.pro_details_gallery .gallery_menu').ajaxtabs($jq('.pro_details_gallery > .gallery_content_container'), {
		onLoadActiveTab: tabToLoadIndex
	});
	
	//41699: Product Model Page - SEO: Content in 1st tab(Overview) is not preloaded. Search engine unable to index keywords.
	//41699: Start
	$jq('.pro_det_gal_seo .gal_seomenu').ajaxtabs($jq('.pro_det_gal_seo > .gal_content_cont_seo'), {
		onLoadActiveTab: tabToLoadIndex
	});
	//41699: End

	// gallery thumbnails interaction
	var $scroller = $jq('.pro_details_gallery .gallery_menu ul');
	var animating = false;
	// disable next and back arrow if there are no images available for navigation
	if ($jq('.pro_details_gallery .gallery_menu ul li a').length < 8){
        $jq('.pro_details_gallery .next a').addClass("inactive");
        $jq('.pro_details_gallery .back a').addClass("inactive");
	}

	$jq('.pro_details_gallery .next a').click(function(e) { 
		e.preventDefault();
		move('next');
	});

	$jq('.pro_details_gallery .back a').click(function(e) { 
		e.preventDefault();
		move('back');
	});

	$jq('.pro_details_gallery .back a').addClass("inactive");
	function move(where) {
		if (!animating) {
			var $thumbnails = $jq('.pro_details_gallery .gallery_menu ul li');
			var activeIndex = $thumbnails.index($scroller.find('li.active')[0]);
			var currentLeft = parseInt($scroller.css('left'));
			var totalLeft = ($thumbnails.length-7)*-75;
		
			if (where == 'next') {
				
				if (currentLeft > ($thumbnails.length - 7) * -75) {
					$jq('.pro_details_gallery .back a').removeClass("inactive");
					animating = true;
					$scroller.animate({left: '-=75px'}, 800, function() { 
						if (currentLeft == activeIndex * -75) {
							$thumbnails.eq(activeIndex).next().children().click();
							
						}
						animating = false;
						
						// RYAN QUIOGUE - disables the next arrow if the last image is displayed
						var currLeft = parseFloat($jq('.gallery_menu ul').css('left'));
						if(totalLeft == currLeft){
							$jq('.pro_details_gallery .next a').addClass("inactive");
						}
					});
				}else{
					$jq('.pro_details_gallery .next a').addClass("inactive");
				}
			}
			else if (where == 'back') {
				if (currentLeft < 0) {
					
					$jq('.pro_details_gallery .next a').removeClass("inactive");
					animating = true;
					$scroller.animate({left: '+=75px'}, 800, function() {
						if (currentLeft == (activeIndex - 6) * -75) {
							$thumbnails.eq(activeIndex).prev().children().click();
						}
						animating = false;
						
						// RYAN QUIOGUE - disables the back arrow if the first image is displayed
						var currLeft = parseFloat($jq('.gallery_menu ul').css('left'));
						if(currLeft == 0){
							$jq('.pro_details_gallery .back a').addClass("inactive");
						}
					});
				}
				else{
					$jq('.pro_details_gallery .back a').addClass("inactive");
				}
			}
		}
	}
});