/**
 * Ashcroft Image Rotator
 */

if (window.jQuery) (function ($) {

	var selector = '.gallery.rotator';

	var createPrevNextNav = function (target) {
		var prev = $('<a class=prev href=#>Prev</a>'),
			next = $('<a class=next href=#>Prev</a>');

		$(target)
			.append(prev)
			.append(next);
	};

	var homeRotator = {
		introText : 'Announcing<br>the New Collection',
		introCapt : 'Shop this Collection',
		introHref : '/shop.html',
		otherCapt : 'Shop this Frame'
	};

	var config = {
		'home-rotator' : {
			introText : 'Announcing<br>the New Collection',
			introCapt : 'Shop this Collection',
			introHref : '/shop.html',

			otherText : '{title}',
			otherCapt : 'Shop this Frame',
			otherHref : '/shop/{sku}.html'
		}
	};

    $(function() {

		var selected = $(selector);

		if (!selected) return;

		/* Create the prev/next navigation 
		 */
		selected.each(function (i) {
			var that = $(this);
			createPrevNextNav(that.find('.nav'));
		});

		/* Initialize the gallery 
		 */
		selected.cycle({
    		fx: 'fade',
    		speed: 1500,
			timeout: 5000,

    		prev: selector + ' .nav .prev',
    		next: selector + ' .nav .next',

			slideExpr: 'li',

			pager: selector + ' .pager',
			pauseOnPagerHover: true,

			before : function (currSlideElement, nextSlideElement, options, forwardFlag) {
				var config = 'home-rotator',
					firstImage = $(selector).find('li:first-child img')[0],
					first = (firstImage === $(nextSlideElement).find('img')[0]);

				var img  = $(nextSlideElement).find('img'),
					text = img.attr('title').replace('\\n', '<br>'),
					capt = first ? 'Shop the Collection' : 'Shop this Frame',
					href = img.attr('data-href');

				$(selector).find('.caption h1').html(text);
				$(selector).find('.caption p a').attr('href', href)
				$(selector).find('.caption p a').text(capt);
					
			},

			prevNextClick : function (isNext, zeroBasedSlideIndex, slideElement) {
				$(selector).cycle('pause');
			}
    	});

    });
    
})(window.jQuery);

