
/*

	News Rotator - jQuery plug-in
	
	

	-----------------------------------------------------------------------------------------------------------------------------

*/

(function ($) {



    $.fn.NewsRotator = function (options) {

        var o = jQuery.extend({//Default values
            interval: 3000,
            items_query: '.items',
            item_query: '.item',
            image_query: '.image',
            orientation: 'horizontal',
            current_class: 'current',
            duration: 1000,
            easing: "linear",
            beforeStart: function () { },
            beforeSlide: function () { },
            afterSlide: function () { }

        }, options);

        var oInterval = null;

        function horizontal()//checks if the orientation of the slideshow is landscape/horizontal
        {
            if (o.orientation.toLowerCase().indexOf('horizontal') != -1) return true;
            return false;
        }



        function next(obj) {
            if (typeof $(obj).find('.' + o.current_class).next()[0] != 'undefined') {
                return $(obj).find('.' + o.current_class).next();
            } else {
                return $(obj).find(o.item_query + ':first');
            }
        }

        function previous(obj) {
            if (typeof $(obj).find('.' + o.current_class).previous()[0] != 'undefined') {
                return $(obj).find('.' + o.current_class).previous();
            } else {
                return $(obj).find(o.item_query + ':last');
            }
        }

        this.oInterval = null;

        this.moveTo = function (x) {

            $(this).find(o.items_query).stop();
            clearInterval(oInterval);
            oInterval = null;

            $('#home-pods .items .item').removeClass('current').eq(x).addClass('current');

            var obj = this;

            $(obj).find(o.items_query).animate({
                marginLeft: -(x * ($(obj).find('.' + o.current_class).width()))
            }, o.duration, o.easing, function () {
                $(obj).find('.' + o.current_class + ' .image').css({
                    marginTop: 0
                });
                var nextItem = next(obj);
                o.afterSlide();
            });

            oInterval = setInterval(function () {
                o.beforeSlide();
                $(obj).find(o.item_query).each(function ($key, $value) {
                    if ($(this).hasClass(o.current_class)) x = $key + 1;
                });
                if (x == $(obj).find(o.item_query).length) x = 0;

                $(obj).find(o.items_query).animate({
                    marginLeft: -(x * ($(obj).find('.' + o.current_class).width()))
                }, o.duration, o.easing, function () {
                    $(obj).find('.' + o.current_class + ' .image').css({
                        marginTop: 0
                    });
                    var nextItem = next(obj);
                    $(obj).find(o.item_query).removeClass(o.current_class);
                    $(nextItem).addClass(o.current_class);
                    o.afterSlide();
                });
            }, o.interval);

        };

        return this.each(function () {

            //Set up the news rotator
            $(this).css({
                position: 'relative',
                'z-index': 1,
                overflow: 'hidden'
            });

            //Set up the items holder
            $(this).find(o.items_query).css({
                height: $(this).find(o.item_query).outerHeight(),
                width: ($(this).find(o.item_query).outerWidth() * $(this).find(o.item_query).length),
                overflow: 'hidden',
                position: 'relative'
            });

            if (horizontal()) {
                $(this).find(o.item_query).css('float', 'left');
            }

            $(this).find(o.item_query + ':first').addClass(o.current_class);

            var obj = this;

            o.beforeStart();

            oInterval = setInterval(function () {
                o.beforeSlide();
                var curPos = 0;
                $(obj).find(o.item_query).each(function ($key, $value) {
                    if ($(this).hasClass(o.current_class)) curPos = $key + 1;
                });
                if (curPos == $(obj).find(o.item_query).length) curPos = 0;

                $(obj).find(o.items_query).animate({
                    marginLeft: -(curPos * ($(obj).find('.' + o.current_class).width()))
                }, o.duration, o.easing, function () {
                    $(obj).find('.' + o.current_class + ' .image').css({
                        marginTop: 0
                    });
                    var nextItem = next(obj);
                    $(obj).find(o.item_query).removeClass(o.current_class);
                    $(nextItem).addClass(o.current_class);
                    o.afterSlide();
                });
            }, o.interval);

        });


    };

})(jQuery);
































