$(function() {

		// Track newsletter downloads
	$(".newsletter a").bind("click", function() {
		_gaq.push(['_trackEvent', 'Newsletters', 'Download', $(this).attr('href')]);
	});

		// Top level headings
	$(".nav-shopdept").toggle(
		function() {
			$(this).css('height', 'auto') ;
			return false ;
		},
		function() {
			$(this).css('height', '18px') ;
			return false ;
		}
	) ;
	$(".nav-shopdept").hover(function() {}, function() {
		if (parseInt($(this).css('height'), 10) > 18) $(this).click() ;
		$(".nav-shopdept li.expanded").removeClass('expanded') ;
		$(".nav-shopcat").hide() ;
	}) ;
	$(".nav-shopdept > li > a").click(function() {
		if ($(this).parent().hasClass('expanded')) {
			$(this).parent().removeClass('expanded') ;
			$(this).next().slideUp('fast') ;
		}
		else {
			$(this).parent().addClass('expanded') ;
			$(this).next().slideDown('fast') ;
		}
		return false ;
	}) ;
	$(".nav-shopdept li li a").click(function() {
		window.location.href = $(this).attr('href') ;
		return false ;
	}) ;

		// Search box
	$("#searchkeywords").focus(function() {
		if ($(this).val() == 'Search') {
			$(this).val('') ;
			$(this).addClass('focused') ;
		}
	}) ;
	$("#searchkeywords").blur(function() {
		if ($(this).val() == '') {
			$(this).val('Search') ;
			$(this).removeClass('focused') ;
		}
	}) ;
	
		// Videos
	if ($("#content-video").length) {
		$("#videolink").colorbox({
			html: $('#content-video').get(0).innerHTML
		}) ;
	}

	if ($("#shop-video").length) {
		$("#extrainfo a.videolink").colorbox({
			html: $('#shop-video').get(0).innerHTML
		}) ;
	}
	
	$(".buyers_guide-brochurelist a.videolink").bind("click", function(e) {
		var $self = $(this);
		$.colorbox({
			html: $self.closest('div').find('.content-video').get(0).innerHTML
		});
		
		e.preventDefault();
	});

		// Home page popup
	if ($("#home-popupmessage").length) {
	$("a#home-popuplink").colorbox({
			html: $('#home-popupmessage').get(0).innerHTML
		}).click() ;
	}
	
		// Shop detail picture zoomer
	$("#product-detail #pictures").append('<p id="zoomnotice"></p>');
	$("#product-detail #pictures #zoom").after('<div id="zoomviewport"></div>');
	$("#product-detail #pictures #zoom img").hide();
	$("#product-detail #pictures #zoomviewport")
		.bind("click", function(e) {
			$.colorbox({ href: $("#product-detail #pictures #zoom img.active").attr("src") });
		})
		.bind("mouseenter", function(e) {
			$("#product-detail #pictures #zoom img.active").fadeIn('fast');
		})
		.bind("mouseleave", function(e) {
			$("#product-detail #pictures #zoom img.active").fadeOut('fast');
		})
		.bind('mousemove', function(e) {
			var _x = e.pageX - $(this).offset().left,
				_y = e.pageY - $(this).offset().top,
				_w = $(this).width(),
				_h = $(this).height(),
				__w = $(this).find("img.active").width(),
				__h = $(this).find("img.active").height(),
				x_percent = _x / _w,
				y_percent = _y / _h,
				w = __w - _w,
				h = __h - _h,
				x = x_percent * _w,
				y = y_percent * _h;

			if ($("#product-detail #pictures #zoom img.active").length) {
				$("#product-detail #pictures #zoom img.active").css({
					left: 0 - x,
					top: 0 - y
				}) ;
			}

			e.preventDefault();
			e.stopPropagation();
		});

		// Shop detail pictures
	$("#product-detail #pictures #fullsize img").hide();
	$("#product-detail #pictures #thumbnails a").bind("click", function(e) {
		var $thumbnail = $(this),
			$detail = $("#product-detail #fullsize img[data-picture-no='"+ $thumbnail.find('img:first').data("picture-no") +"']"),
			$zoom = $("#product-detail #zoom img[data-picture-no='"+ $thumbnail.find('img:first').data("picture-no") +"']");

			// Set the active state
		$("#product-detail #pictures #thumbnails a.active").removeClass("active");
		$(this).addClass("active");
		
			// Transition the picture
		$("#product-detail #pictures #fullsize img:visible").stop().fadeTo("slow", 0);
		$("#product-detail #pictures #fullsize img[data-picture-no='"+ $(this).find('img:first').data("picture-no") +"']").stop().fadeTo("slow", 1);
		
			// Add the correct zoom notice
		$("#product-detail #pictures #zoom img.active").removeClass('active');
		if ($zoom.length) {
			$zoom.addClass('active');
			$("#zoomnotice").text("* Hover to zoom, click to view large picture.");
		}
		else {
			$("#zoomnotice").text("* Zooming is not available for this picture.");
		}
		
		e.preventDefault();
	});
	$("#product-detail #pictures #thumbnails a:first").click();
	
		// Banners
		// Transition only if more then 1 banner-group
	if ($("#banners .banner-group").length > 1) {

			// Set the timer vars
		var bgInterval, bgFrameInterval;
		
			// Hide all the internal banner-group frames
		$("#banners .banner-group a").hide();

			// Show the first group, this will always be the welcome image
		$("#banners .banner-group:first a:first").show();
		$("#banners .banner-group:first").fadeIn('slow');
		
			// Loop over the groups
		bgTimer = setInterval(function() {
				// Get the currently visible banner-group
			var $cBannerGroup = $("#banners .banner-group:visible");
			var $nBannerGroup = $cBannerGroup.next('.banner-group');
			if ($nBannerGroup.length === 0)
				$nBannerGroup = $("#banners .banner-group:first");
			$nBannerGroup.find('a').hide();
			$nBannerGroup.find('a:first').show();

			$cBannerGroup.fadeOut('slow');
			$nBannerGroup.fadeIn('slow');
			$("#banners").data('active-banner-group', $nBannerGroup);
			$nBannerGroup.data('number-of-frames', $nBannerGroup.find('a').length); 
			
				// Transition between each frame within the group
			if ($nBannerGroup.data('number-of-frames') > 1) {
				$nBannerGroup.data('active-frame', 1);
				bgFrameInterval = setInterval(function() {
					var $bg = $("#banners").data('active-banner-group');
					var $cFrame = $bg.find('a:visible');
					var $nFrame = $cFrame.next('a');
					$cFrame.fadeOut('slow');
					$nFrame.fadeIn('slow');
					$bg.data('active-frame', ($bg.data('active-frame') + 1));
					
						// If its the last frame in the group, clear the interval
					if ($bg.data('active-frame') == $bg.data('number-of-frames')) {
						clearInterval(bgFrameInterval);
					}
				}, (20000 / $nBannerGroup.data('number-of-frames'))); // 20 seconds / number of images in group
			}
		}, 20000);
	}

});
