jQuery(document).ready(function() {
	/* Pitch */
	jQuery('ul#pitch li:first-child').addClass('current');
	jQuery('ul#pitch li').each(function(index) {
		jQuery(this).addClass('slide' + index);
	});
	
	jQuery('ul#pitchControls li:first-child').addClass('current');
	jQuery('ul#pitchControls li').each(function(index) {
		jQuery(this).addClass('slide' + index);
		jQuery(this).click(function() {
			gotoPitch(index);
			return false;
		});
	});
	
	var pitchReady = 1;
	var pitchCurrentSlide = 0;
	var pitchLastSlide = jQuery('ul#pitch li').length - 1;
	
	function autoPitch() {
		if (pitchCurrentSlide < pitchLastSlide) {
			pitchCurrentSlide++;
		} else {
			pitchCurrentSlide = 0;
		}
		
		jQuery('ul#pitch li.slide' + pitchCurrentSlide).css('display', 'inline');
		jQuery('ul#pitch li.current').fadeOut(3000, function() {
			jQuery(this).removeClass('current');
			jQuery('ul#pitch li.slide' + pitchCurrentSlide).addClass('current');			
		});
		jQuery('ul#pitchControls li.current a img').fadeOut(3000, function() {
			jQuery('ul#pitchControls li.current a img').css('display', 'none');
			jQuery('ul#pitchControls li.current').removeClass('current');
		});
		jQuery('ul#pitchControls li.slide' + pitchCurrentSlide + ' a img').fadeIn(3000, function() {
			jQuery('ul#pitchControls li.current').removeClass('current');
			jQuery('ul#pitchControls li.slide' + pitchCurrentSlide).addClass('current');
		});
	}
	
	function gotoPitch(index) {
		if (pitchReady == 1) {
			pitchReady = 0;
			clearInterval(autoModePitch);
			jQuery('ul#pitchControls li.slide a img').css('display', 'none');
			pitchCurrentSlide = index;
			
			jQuery('ul#pitch li.slide' + pitchCurrentSlide).css('display', 'inline');
			jQuery('ul#pitch li.current').fadeOut(3000, function() {
				jQuery(this).removeClass('current');
				jQuery('ul#pitch li.slide' + pitchCurrentSlide).addClass('current');			
			});
			jQuery('ul#pitchControls li.current a img').fadeOut(3000, function() {
				jQuery('ul#pitchControls li.current a img').css('display', 'none');
				jQuery('ul#pitchControls li.current').removeClass('current');
			});
			jQuery('ul#pitchControls li.slide' + pitchCurrentSlide + ' a img').fadeIn(3000, function() {
				jQuery('ul#pitchControls li.current').removeClass('current');
				jQuery('ul#pitchControls li.slide' + pitchCurrentSlide).addClass('current');
				
				autoModePitch = setInterval(autoPitch, 6000);
				pitchReady = 1;
			});
		}
	}
	
	var autoModePitch = setInterval(autoPitch, 6000);
	
	/* Gallery */	
	jQuery('#gallery ul.gallery li:first-child').addClass('current');
	jQuery('#gallery ul.gallery li').each(function(index) {
		jQuery(this).addClass('slide' + index);
	});
	
	jQuery('ul#galleryControls li:first-child').addClass('current');
	jQuery('ul#galleryControls li').each(function(index) {
		jQuery(this).addClass('slide' + index);
		jQuery(this).click(function() {
			gotoGallery(index);
			return false;
		});
	});
	
	var galleryReady = 1;
	var galleryCurrentSlide = 0;
	var galleryLastSlide = jQuery('#gallery ul.gallery li').length - 1;
	
	function autoGallery() {
		galleryReady = 0;
		if (galleryCurrentSlide < galleryLastSlide) {
			galleryCurrentSlide++;
		} else {
			galleryCurrentSlide = 0;
		}
		
		jQuery('#gallery ul.gallery li.slide' + galleryCurrentSlide).css('display', 'inline');
		jQuery('#gallery ul.gallery li.current').fadeOut(3000, function() {
			jQuery(this).removeClass('current');
			jQuery('#gallery ul.gallery li.slide' + galleryCurrentSlide).addClass('current');			
		});
		jQuery('ul#galleryControls li.current a img').fadeOut(3000, function() {
			jQuery('ul#galleryControls li.current a img').css('display', 'none');
			jQuery('ul#galleryControls li.current').removeClass('current');
		});
		jQuery('ul#galleryControls li.slide' + galleryCurrentSlide + ' a img').fadeIn(3000, function() {
			jQuery('ul#galleryControls li.current').removeClass('current');
			jQuery('ul#galleryControls li.slide' + galleryCurrentSlide).addClass('current');
			galleryReady = 1;
		});
	}
	
	function gotoGallery(index) {
		if (galleryReady == 1) {
			galleryReady = 0;
			clearInterval(autoModeGallery);
			jQuery('ul#galleryControls li.slide a img').css('display', 'none');
			galleryCurrentSlide = index;
			
			jQuery('#gallery ul.gallery li.slide' + galleryCurrentSlide).css('display', 'inline');
			jQuery('#gallery ul.gallery li.current').fadeOut(900, function() {
				jQuery(this).removeClass('current');
				jQuery('#gallery ul.gallery li.slide' + galleryCurrentSlide).addClass('current');			
			});
			jQuery('ul#galleryControls li.current a img').fadeOut(900, function() {
				jQuery('ul#galleryControls li.current a img').css('display', 'none');
				jQuery('ul#galleryControls li.current').removeClass('current');
			});
			jQuery('ul#galleryControls li.slide' + galleryCurrentSlide + ' a img').fadeIn(900, function() {
				jQuery('ul#galleryControls li.current').removeClass('current');
				jQuery('ul#galleryControls li.slide' + galleryCurrentSlide).addClass('current');
				
				autoModeGallery = setInterval(autoGallery, 7000);
				galleryReady = 1;
			});
		}
	}
	
	jQuery('ul#galleryNextPrevious li.next a').click(function() {
		if (galleryReady == 1) {
			if (galleryCurrentSlide < galleryLastSlide) {
				galleryCurrentSlide++;
			} else {
				galleryCurrentSlide = 0;
			}
			gotoGallery(galleryCurrentSlide);
		}
		return false;
	});
	
	jQuery('ul#galleryNextPrevious li.previous a').click(function() {
		if (galleryReady == 1) {
			if (galleryCurrentSlide > 0) {
				galleryCurrentSlide--;
			} else {
				galleryCurrentSlide = galleryLastSlide;
			}
			gotoGallery(galleryCurrentSlide);
		}
		return false;
	});
	
	var autoModeGallery = setInterval(autoGallery, 6000);
	
	/* Products */
	jQuery('ul.productGallery li:first-child').addClass('current');
	jQuery('ul.productGallery li').each(function(index) {
		jQuery(this).addClass('slide' + index);
	});
	
	var productReady = 1;
	jQuery('ul.displayOptions li:first-child a').addClass('current');
	jQuery('ul.displayOptions a').each(function(index) {
		jQuery(this).click(function() {
			if (productReady == 1) {
				productReady = 0;
				jQuery('ul.displayOptions a.current').removeClass('current');
				jQuery(this).addClass('current');
				
				jQuery('ul.productGallery li.slide' + index).css('display', 'block');
				jQuery('ul.productGallery li.current').fadeOut(3000, function() {
					jQuery('ul.productGallery li.current').removeClass('current');
					jQuery('ul.productGallery li.slide' + index).addClass('current');
					productReady = 1;
				});
			}			
			return false;
		});
	});
	
	/* Link highlighting for when AJAX is enabled
	jQuery('ul.prices a').click(function() {
		jQuery('ul.prices a.current').removeClass('current');
		jQuery(this).addClass('current');
		return false;
	});
	*/
});
