﻿(function($) {
    var _intervalTimer, $current, $next, $settings, $sponsors;

    $.fn.sponsors = function(settings) {

        var _sponsors = this;

        this.each(function(i) {
            var $parent = $(this);

            this.playId = null;
            this.playFlag = false;
            this.length = 0;
            this.inited = new Array();
            this.titles = new Array();

            this.build = function() {

                var _self = this;
                $parent.addClass('sponsors-content');
                $parent.wrap('<div class="sponsors-wrapper"></div>');
                $parent = $('.sponsors-wrapper');

                this.length = $parent.find('.sponsors-content > *').length;
                $parent.find('.sponsors-content > *').each(function() {
                    $(this).addClass('sponsors-slide');
                });

                this.events();

                // init slide (replace by ajax etc)
                //this.init(this.options.index);

                // show slide
                $parent.find('.sponsors-slide:eq(' + this.options.index + ')').show();

                return true;
            };

            /**
            * Init N-slide
            * @method
            * @param {Integer} index
            */
            this.init = function(index) {
                // initialize only ones
                for (var i = 0, loopCnt = this.inited.length; i < loopCnt; i++) {
                    if (this.inited[i] === index) {
                        return true;
                    }
                }

                // index to inited stack
                this.inited.push(index);

                // current slide
                slide = $parent.find('.sponsors-slide:eq(' + index + ')');

                return false;
            };

            this.play = function() {
                var _self = this;
                this.playFlag = true;
                this.playId = setTimeout(function() { _self.next() }, this.options.speed);
            };

            this._play = function() {
                var _self = this;

                // if it last frame
                if (this.options.index == (this.length - 1)) {
                    this.stop();
                    // should be restart sponsors
                    if (this.options.loop) {
                        this.play();
                    }
                    return false;
                }
                this.playId = setTimeout(function() { _self.next(); }, this.options.speed);
                return true;
            };

            this.stop = function() {
                this.playFlag = false;

                clearTimeout(this.playId);
                this.playId = null;
            };

            this.events = function() {
                var _self = this;

                $parent.hover(function() {
                    if (_self.playId) {
                        _self.stop();
                    }
                }, function() {
                    if (!_self.playId) {
                        _self.play();
                    }
                });

                $parent.find('.sponsors-content > *').each(function() {
                    $(this).hover(function() {
                        if (_self.playId) {
                            _self.stop();
                        }
                    }, function() {
                        if (!_self.playId) {
                            _self.play();
                        }
                    });
                });
            };

            this.next = function() {
                if (this.options.index == (this.length - 1)) {
                    i = 0;
                } else {
                    i = this.options.index + 1;
                }
                this.goToSlide(i);
            };

            /**        
            * Goto N-slide
            * @method
            * @param {Integer} n
            */
            this.goToSlide = function(n) {
                if (this.options.index == n) return;

                //if (!this.init(n)) return;

                var next = $parent.find('.sponsors-content > *:eq(' + n + ')');
                var prev = $parent.find('.sponsors-content > *:eq(' + this.options.index + ')');

                // restore next slide after all effects, set z-index = 0 for prev slide
                prev.css({ zIndex: 0 });
                next.css({ zIndex: 1, top: 0, left: 0, opacity: 1 });

                this.options.index = n;

                prev.css({ zIndex: 0, opacity: 1 });
                next.css({ zIndex: 1, opacity: 0 });

                prevAni = { opacity: 0 };

                var _self = this;

                prev.animate(prevAni, this.options.effecttime);

                // play next slide animation, hide prev slide, update label, update counter
                next.show().animate({ top: 0, left: 0, opacity: 1 }, this.options.effecttime, function() {
                    prev.hide();
                    _self._play();
                });
            };


            this.options = $.extend({}, $.fn.sponsors.options, settings);

            this.build();
        });

        _sponsors.playSlide = function(callback) {
			_sponsors.each(function() {
				this.play();
				if (typeof callback == 'function') 
					return callback(this);
			})
		};

        return this;
    };

    $.fn.sponsors.options = {
        effecttime: 1000,
        index: 0,
        loop: true,
        play: true,
        speed: 2500
    };

})(jQuery);
