/* 
Author: your name here
*/

(function($) {
	
	$('html').removeClass('no-js');
	
	/**
	 * Reverse stack the navigation
	 */
	
	$('.navInner ul').each(function() {
		var __childLength = $(this).children().length;
		$(this).children().each(function(i) {	
			$(this).css('zIndex', __childLength);
			$(this).addClass('nav'+i);
			__childLength--;
		});
	});
	
	/** 
	 * Homepage spotlight links
	 */
	 
	$('.bubbleMe').each(function() {
		$(this).closest('li').find('.imgWrap img').click(function() {
			window.location = $(this).closest('li').find('.bubbleMe').attr('href');
		});
	});
	


	$('.navInner ul li').hover(function() {
		$(this).find('ul').show();
	}, function() {
		$(this).find('ul').hide();
	});
	
	$('.navInner ul li ul li').hover(function() {
		$(this).find('ul li').show();
	}, function() {
		$(this).find('ul li').hide();
	});
	

	
	
	/**
	 * Cycle.
	 */
	
	if(typeof $.cycle !== 'undefined' && $('.cycle').length) {
		$('.cycle').cycle();
	}
	
	/**
	 * Forms
	 */
	 
	var formValidation = $('.validation-form');

	if(formValidation.length) {
	
		jQuery.validator.addMethod('required_group', function(val, el) {
		        var $module = $(el).parents('.panel');
		        return $module.find('.required_group:filled').length;
		});
		
		jQuery.validator.addClassRules('required_group', {
		        'required_group' : true
		});
		
		jQuery.validator.messages.required_group = 'Please fill out at least one of these fields.';
	
		formValidation.validate();
		
	}


	/**
	 * Fix IE6 PNGs
	 */
	 
	if(typeof DD_belatedPNG != 'undefined') {
		DD_belatedPNG.fix('.png_bg');
		DD_belatedPNG.fix('.trees');
		DD_belatedPNG.fix('.clouds');
		DD_belatedPNG.fix('.featuredNavinner ul li a span');
		DD_belatedPNG.fix('.navInner ul li.home strong');
		DD_belatedPNG.fix('.welcomeimage');
		DD_belatedPNG.fix('.navInner ul li.nav1 a');
		DD_belatedPNG.fix('.navInner ul li.nav2 a span');
		DD_belatedPNG.fix('.navInner ul li.first a span');
		DD_belatedPNG.fix('.navInner ul li a:hover, .navInner ul li.selected a');
	}
	
	
	/**
	 * Accordian form
	 */
	
	var items = $('.formWrap');
	if(items.length) {
		items.children('strong').click(function(e) {
			e.preventDefault();
			var detail = $(this).next('.accWrap');
			items.removeClass('open');	
			if(detail.is(':hidden')) {
				$(this).parent().addClass('open');
				items.find('.accWrap:visible').slideUp();
				detail.slideDown();
			}
			else {
				detail.slideUp();
			}
		});
	}
	
	
	
	

	/**
	 * Sliding navigation hero thing
	 */
	
	var navEasing;
	if(typeof jQuery.easing.easeInOutExpo != 'undefined') {
		navEasing = 'easeInOutExpo';
	}
	else {
		navEasing = 'swing';
	}
	
	var featuredItems = $('.featuredNavinner ul li:not(.static)'),
		largeWidth = featuredItems.filter('.large').eq(0).width(),
		normalWidth = featuredItems.not('.large').eq(0).width(),
		tryAgain = false,
		timeout;
	
	featuredItems.hover(function() {
		
		if(tryAgain) {
			tryAgain = false;
		}
		else {
			clearTimeout(timeout);
		}
		
		if(featuredItems.filter(':animated').length) {
			tryAgain = true;
			//timeout = setTimeout(function() {
			//	$(this).hover();
			//}, 1000);
			return false;
		}
		
		featuredItems.filter('.large').stop().animate({'width': normalWidth}, 400, navEasing);
		$(this).stop().animate({'width': largeWidth}, 400, navEasing, function() {
			$(this).addClass('large');
		});
		
	}, function() {
		
	});

	/** 
	 * Product Carousel
	 */
	
	$('.firstField').hide();
	
	var carousel = $('#homeCarouselWrap');
	if(carousel.length) {
		carousel.carousel({
			size: 4,
			prev: 'prev',
			next: 'next',
			extraSpace: 0,
			innerCarousel: '.scrollable'
		});
	}
	
	

})(jQuery);

$(window).load(function() {
	

	
	/** 
	 * Product Gallery
	 */
	 
	var productGallery = $('.productGallery');
	if(productGallery.length) {
		(function() {
			
			// Define some dynamics and cache selectors
			
			var largeContainer = productGallery.find('.caraLarge').eq(0),
				thumbContainer = productGallery.find('.caraThumbs').eq(0),
				currentIndex = 0,
				prev = productGallery.find('.prev').eq(0),
				next = productGallery.find('.next').eq(0),
				disabledClass = 'disabled',
				currentClass = 'current',
				thumbs = thumbContainer.find('a'),
				largeImages = largeContainer.find('li'),
				largeWidth = largeImages.eq(0).width();
			
			// Click the thumbnails to change the main image...
			
			thumbs.click(function(e) {
				
				// Event selectors
				var thumb = $(this);
				var index = thumb.parent('li').index();
				e.preventDefault();
				
				// Can it be clicked?
				if(largeContainer.is(':animated')) {
					return;
				}

				// Current class
				thumbs.parent('li').removeClass(currentClass);
				thumb.parent('li').addClass(currentClass);
				
				// Reset the disabled classes.
				prev.add(next).removeClass(disabledClass);
				
				// Check if its the end or the start
				if(index == 0) {
					prev.addClass(disabledClass);
				}
				if((index+1) == largeImages.length) {
					next.addClass(disabledClass);
				}
				
				largeContainer.css({
					left: ((index * largeWidth) * -1)
				});
				
				currentIndex = index;

			}).eq(0).click();
			
			// Previous and next wrappers.
			
			var adjacentClick = function(e, difference, self) {
				if(!self.hasClass(disabledClass)) {
					thumbs.eq(difference).trigger('click');
				}
			};
			
			prev.click(function(e) { e.preventDefault(); if(prev.hasClass('disabled')) return; adjacentClick(e, (currentIndex - 1), $(this)); });
			next.click(function(e) { e.preventDefault(); if(next.hasClass('disabled')) return; adjacentClick(e, (currentIndex + 1), $(this)); });
			
		})();
	}

});




