﻿$(document).ready(function () {

    /* Cycle */

    if ($('.a41-canvases').length > 0) {

        $('.a41-canvases').cycle({
            fx: 'fade',
            speed: 500,
            timeout: 5000,
            slideExpr: '.a41-canvas',
			pager: '.a41-banner-nav',
			// callback fn that creates a thumbnail to use as pager anchor 
			pagerAnchorBuilder: function (idx, slide) {
				return '<a class="a41-rotating-banner-btn" href="#">&nbsp;</a>';
			}
        });
    }

    if ($('.a41-brand-scroller').length > 0) {

        $('.a41-brand-scroller a').click(function () {
            $('.a41-rotating-banner').hide();
        });

        $('.a41-rotating-banner').cycle({
            fx: 'fade',
            speed: 500,
            timeout: 5000,
            pager: '.a41-banner-nav',
            slideExpr: 'img',

            // callback fn that creates a thumbnail to use as pager anchor 
            pagerAnchorBuilder: function (idx, slide) {
                return '<a class="a41-rotating-banner-btn" href="#">&nbsp;</a>';
            }
        });
    }

    $('.a41-rotating-banner-btn').click(function () {
        $('.a41-rotating-banner').cycle('pause');
    });


    /* Sign In Box */
    $('.a41-returning-customer a.a41-expand-me, .a41-returning-customer a.a41-close-me')
	.click(function () {

	    $('.a41-returning-form-container').toggleClass('a41-open');

	    if ($('.a41-returning-form-container').hasClass('a41-open')) {
	        $('.a41-returning-customer a.a41-expand-me').fadeOut(300, function () {
	            $('.a41-returning-form-container').animate({ height: 133 }, 300, function () {
	                $('.a41-returning-customer form').fadeIn(300);
	            });
	        });
	    } else {
	        $('.a41-returning-customer form').fadeOut(300, function () {
	            $('.a41-returning-form-container').animate({ height: 25 }, 300, function () {
	                $('.a41-returning-customer a.a41-expand-me').fadeIn(300);
	            })
	        });
	    }

	    return false;
	});


    /* Video lightboxes */
    $('a.a41-colorbox-video')
	.colorbox({
	    iframe: true,
	    innerWidth: 830,
	    innerHeight: 630
	});

	var a41VideoPlayer = $('.a41-video-player');
    var a41VideoCarousel = $('.a41-video-carousel');

    if (a41VideoPlayer.length > 0) {

        if (!$.flash.available) {

            // Show Flash Player download button
            $('.a41-video-player').find('a').show();

        } else {

            // Set up flash player vars
            var playerPath = '/content/videos/',
		    videoPath = '/content/videos/',
		    posterPath = '/content/videos/posters/',
		    thumbExtension = 'jpg',
		    videoExtension = null,
		    videoWidth = 720,
		    videoHeight = 400,
		    currentClass = 'a41-selected';

            // Change video on image click
            a41VideoCarousel.find('img:not(.a41-control-image)')
		    .click(function () {
		        videoExtension = $(this).attr('rel');
		        $(a41VideoCarousel).find('.' + currentClass).removeClass(currentClass);
		        $(this).addClass(currentClass);
		        // Get the new video and poster filenames from the thumb name and generate the swfobject vars
		        var src = $(this).attr('src'),
				    imgArray = src.split('/'),
				    img = imgArray.pop(),
				    fileName = img.replace(thumbExtension, ''),
				    a41VideoPlayerArgs = {
				        swf: playerPath + 'player.swf',
				        width: videoWidth,
				        height: videoHeight,
				        flashvars: {
				            v: videoPath + fileName + videoExtension,
				            poster: posterPath + fileName + thumbExtension
				        }
				    }

		        // Re-initiate the player
		        a41VideoPlayer.flash(a41VideoPlayerArgs);

		    });
        }
    }

    if (a41VideoCarousel.length > 0) {

        var thumbContainer = a41VideoCarousel.find('ul')
        thumbs = thumbContainer.find('img'),
	    thumbWidth = 125,
	    numThumbs = a41VideoCarousel.find('li').length,
	    visibleThumbs = 5,
	    thumbPosition = thumbContainer.position(),
	    maxThumbPositionLeft = thumbPosition.left,
	    maxThumbPositionRight = maxThumbPositionLeft - (thumbWidth * (numThumbs - visibleThumbs)),
	    animationSpeed = 300,
	    controls = a41VideoCarousel.find('.a41-control');

        // Grab video #1 and put it into the playing area.
        thumbs.filter(':first').click();

        thumbContainer.css({
            width: thumbWidth * numThumbs
        });

        if (numThumbs <= visibleThumbs) controls.hide();

        controls
	    .click(function () {

	        var currentThumbPosition = thumbContainer.position(),
			    currentThumbPositionLeft = currentThumbPosition.left,
			    scrollDirection = $(this).hasClass('a41-next') ? 'next' : 'previous',
			    newThumbPositionLeft = scrollDirection == 'next' ? currentThumbPositionLeft - thumbWidth : currentThumbPositionLeft + thumbWidth;

	        if (newThumbPositionLeft > maxThumbPositionLeft) {
	            newThumbPositionLeft = maxThumbPositionRight;
	        } else if (newThumbPositionLeft < maxThumbPositionRight) {
	            newThumbPositionLeft = maxThumbPositionLeft;
	        }

	        thumbContainer
			    .animate(
				    {
				        left: newThumbPositionLeft
				    },
				    animationSpeed
			    );
	        return false;
	    });

    }

});
