jQuery(document).ready(function() {

	var cyclePause = jQuery('#cycle-pause').bind('click',function(e){
		e.preventDefault();
		var paused = slideshow.is(':paused');
		slideshow.cycle(paused ? 'resume' : 'pause', true);
	});

	var slideshow = jQuery("#cycle").cycle({
		fx: "fade",
		pager: '#cycle-nav',
		pagerAnchorBuilder: function(idx, slide) { 
			return '<li><a href="#"></a></li>'; 
		},
		paused: function(cont, opts, byHover) {
			!byHover && cyclePause.html('Resume');
			jQuery('#cycle-pause').addClass('paused');
		},
		resumed: function(cont, opts, byHover) {
			!byHover && cyclePause.html('Pause');
			jQuery('#cycle-pause').removeClass('paused');
		}
	});

	jQuery('#product-images').cycle({
	    fx: 'fade',
	    speed: 'fast',
	    timeout: 0,
	    pager: '#product-images-thumbs',

	    // callback fn that creates a thumbnail to use as pager anchor
	    pagerAnchorBuilder: function(idx, slide) {
	    	var img = $(slide).find("img").attr("src");
	        return '<li class="vignette"><a href="#"><img src="' + img + '" width="70" height="48"></a></li>';
	    }
	});
	
	jQuery('#product-images li a').fancybox();

	jQuery("#carousel-container").jCarouselLite({
		btnNext: "#carousel-next",
        btnPrev: "#carousel-prev",
	    visible: 2,
		scroll: 2,
		speed: 800
	});
	
	jQuery("#carousel li:odd").addClass("odd");	

	jQuery("#products-gallery .carousel-product-wrap").jCarouselLite({
        btnNext: "#products-gallery .carousel-next",
        btnPrev: "#products-gallery .carousel-prev",
	    visible: 4,
	 	scroll: 2,
		speed: 800,
		start: 0
	});
	
	jQuery("#products-gallery .carousel li a").first().addClass("active");
	
	jQuery("#cat-landing > .carousel-product").each(function(n,el){
		var $carousel = jQuery(el);
		var id = $carousel.attr("id");
		if(!id) return true;

		// Attach the actual carousel
		jQuery("#"+id+" .carousel-product-wrap").jCarouselLite({
			btnNext: "#"+id+" .carousel-next",
	        btnPrev: "#"+id+" .carousel-prev",
		    visible: 4,
		 	scroll: 2,
			speed: 800
		});
	});

	jQuery("#accordion").accordion();
	jQuery("#resources-accordion").accordion({ 
		header: 'h3',
		collapsible: true,
		autoHeight: false
	});

	jQuery('#tabs').tabs();

	jQuery("#product-search-product").change(function() {
	   window.location = '/product/' + jQuery("#product-search-product option:selected").val() + '/';
	})
	jQuery("#product-search-category").change(function() {
	   window.location = '/products/categories/' + jQuery("#product-search-category option:selected").val() + '/';
	})
	jQuery("#product-search-brand").change(function() {
	   window.location = '/products/' + jQuery("#product-search-brand option:selected").val() + '/';
	})

	function productPanelClick(e){
		e.preventDefault();
		var $link = jQuery(this);
		var product = parseInt($link.data("product"));
		
		jQuery("#products-gallery li a").removeClass("active");
		jQuery(this).addClass("active");
				
		if(!product) return;

		jQuery("#main .panel.show")
			.removeClass("show")
			.addClass("hide");
		jQuery("#product-panel-"+product)
			.removeClass("hide")
			.addClass("show");
	}

	jQuery("#products-gallery.carousel-product a").live("click", productPanelClick);

});
