(function($) {
    $.fn.highlightcarousel = function(options) {
        var s = $.extend({
            slides: '#m-banner-thumbs li', //the jquery object of items
            animtimeout: 1000, //use this to control length of animation
            pausetimeout: 5600, //use this to control how long between timed animations
            nextbtn: '#m-banner-bottom', //next button
            prevbtn: '#m-banner-top', //previous button
            slider: '#m-banner-thumbs ul', //the thing that is slid
            descholder: '#m-banner-detail-text',
            classholder: '#m-banner-detail',
            imgholder: '#m-banner-detail-img',
            width: 200, //width of slide
            height: 70,
            trigger: '',
            autostart: true,
            popup: false
        }, options || {});
        return this.each(function() {
            var count = $(s.slides).length;
            var slide = 0;
            var t = setTimeout(nextslide, s.pausetimeout);
            //$(s.slider).css({ width: count * s.width });
            $(s.slider).css({ height: count * s.height });

            $(s.slider).hover(
                function() {
					if (count > 5){
						$(s.nextbtn).css('display', 'inline');
					}
                },
                function() {
                    $(s.nextbtn).css('display', 'none');
                }
            );

            $(s.nextbtn).hover(
                function() {
					if (count > 5){
						$(s.nextbtn).css('display', 'inline');
					}
                },
                function() {
                    $(s.nextbtn).css('display', 'none');
                }
            );

            $(s.slides).click(function(e) {
                e.preventDefault();
                displayslide($(s.slides).index(this));
            });
            $(this).bind('play', function() {
                slide = 0;
                displayslide(0);
                $(s.slider).css({ left: 0 });
                if (s.popup) {
                    $(s.imgholder).popup({
                        width: 640,
                        height: 360,
                        type: 'video'
                    });
                } else {
                    $(s.imgholder).unbind('click');
                }
            });
            $(this).bind('pause', function() {
                clearTimeout(t);
            });
            if (s.autostart) {
                $(this).trigger('play');
            } else {
                clearTimeout(t);
            }
            function nextslide() {
                var _org = slide;
                if (slide == count - 1) {
                    slide = 0;
                } else {
                    slide = Math.min(count - 1, slide + 1);
                }
                if (slide > 4) {                
                    gotopage(slide-4);
                } 
                if (_org == count - 1){
                    gotopage(slide);
                }
                displayslide(slide);
            }
            function displayslide(number) {
                slide = number;
                var selected = $(s.slides).eq(number);
                var imglink = selected.find('a').attr('href');
                var imgpath = selected.find('img').attr('alt');
                var imgdesc = selected.find('.dn').html();
                $(s.imgholder + ' img').attr('src', imgpath);
                $(s.imgholder).attr('href', imglink);
                $(s.descholder).html(imgdesc);
                selected.addClass('active').siblings().removeClass('active');
                clearTimeout(t);
                t = setTimeout(nextslide, s.pausetimeout + s.animtimeout);
            }
            function gotopage(number) {
                $(s.slider).stop().animate({ top: -number * s.height }, s.animtimeout, 'easeOut');
            }
            $(s.nextbtn).click(function(e) {
                e.preventDefault();
                nextslide();
            });
		});
    };
})(jQuery);

// easing
jQuery.extend(jQuery.easing,
{
    easeIn: function(x, t, b, c, d) {
        return c * (t /= d) * t * t * t * t + b;
    },
    easeOut: function(x, t, b, c, d) {
        return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
    },
    easeInOut: function(x, t, b, c, d) {
        if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
        return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
    }
});

