(function($){
	window.ND=window.ND||{}
	
	ND.clickable = {
		init: function () {
			var clickables = $(".clickable");
			if (clickables.size() > 0) {
				clickables.each(function () {
					var clickable = $(this),
						href;
					
					clickable.find("A").each(function (i) {
						if (i === 0) {
							href = $(this).attr("href");
						}
					});
					if (href !== "" || href !== undefined) {
						clickable.bind("click", function () {
							window.location = href;
						});
					}
				});
			}
		}
	};
	
	//ND is in the window scope.
	ND.Slideshow = {
		init: function() { 
			$(function () {
				$(".contact-slideshow").jshow();
				
				$(".slideshow").jshow({
					controls: ['items'],
					transitionInterval: 10000
				});
			});
		}
	};
	
	ND.ProductGallery = {
		init: function () {
			var visibleThumbnails = 5;
			
			var carousel_initCallback = function (carousel) {
				$('UL.controls LI.prev A').live('click', function() {
					carousel.prev();
					return false;
				});
			
				$('UL.controls LI.next A').live('click', function() {
					carousel.next();
					return false;
				});
			};
			
			var hideImagesSlides = function () {
				$(".view-port .image-slide:not(:eq(0))").hide();
			};
			
			var initialiseCarousel = function () {
				var length = $(".thumbnails LI").size();
				if (length >= visibleThumbnails) {
					$('.thumbnails').jcarousel({
						vertical: true,
						visible: visibleThumbnails,
						scroll: 2,
						initCallback: carousel_initCallback
					});
				}
			};
			
			var formatTitle = function(title, currentArray, currentIndex, currentOpts) {
				return '<div class="lightbox-title">' + (title && title.length ? '<p class="caption">' + title + '</p>' : '' ) + '<p class="pagination">Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</p></div>';
			}
			
			var initialiseLightbox = function () {
				$(".view-larger A").colorbox();
			};
			
			var imageChange = function (item) {
				var id = $(item).attr("class");
				$(".view-port .image-slide").hide();
				$("#" + id).show();
			};
			
			var wireEvents = function () {
				$(".thumbnails A").bind("mouseover", function () {
					imageChange(this);
					return false;
				});
				
				$(".thumbnails A").bind("click", function () {
					imageChange(this);
					return false;
				});
			};
		
			$(function () {
				var isGallery = $(".product-gallery").size() > 0;
				if (isGallery) {
					hideImagesSlides();
					initialiseCarousel();
					wireEvents();
					initialiseLightbox();
				}
			});
		}
	};
	
	$(document).ready(function () {
		ND.clickable.init();
		ND.Slideshow.init();
		ND.ProductGallery.init();
	});
	
	
})(jQuery);
