/**
 *
 * @author alecksey
 * @date 08.08.11
 * @time 12:28
 * @package
 * @version 0.1
 */
(function($)
{
    var YF_Image_Rotate_Element = function(sUrl, oNext, oPrev, oOptions){
        var th = this;

        this._prev = null;
        this._next = null;

        this._node = null;
        this._options = { fade_delay : 2000, beforeFade : 0, afterFade : 0, vars : {}, onload : null};
        this._text = '';

        this.fadeOut = function(f){
            if('function' == typeof this.getOption('beforeFade'))
                this.getOption('beforeFade')(this);

            $(this._node).fadeOut(this.getOption('fade_delay'), f);

            if('function' == typeof  this.getOption('afterFade'))
                this.getOption('afterFade')(this);
        };

        this.fadeIn = function(f){
            if('function' == typeof this.getOption('beforeFade'))
               this.getOption('beforeFade')(this);

            $(this._node).fadeIn(this.getOption('fade_delay'), f);

            if('function' == typeof  this.getOption('afterFade'))
                this.getOption('afterFade')(this);
        };

        this.setOption = function(sName, sVal){
            if(this._options[sName] != undefined)
                this._options[sName] = sVal;
            return th;
        };

        this.getOption = function(sName){
            return (this._options[sName] !== undefined) ? this._options[sName] : null;
        };

        this.getNode = function(){
            return this._node;
        };

        this.getNext = function(){
            return this._next;
        };

        this.getPrev = function(){
            return this._prev;
        };

        this.setNext = function(oNext){
            this._next = oNext;
            return this;
        };

        (function(){
            if(oOptions) for(var i in oOptions){
                th.setOption(i, oOptions[i]);
            }

        if(sUrl != undefined && sUrl != '')
            th._node = $("<img>").load(oOptions['onload']).attr('src', sUrl)
                    .css('left', 0)
                    .css('top', 0)
                    .css('position', 'absolute');
        })();

        th._next = oNext;
        th._prev = oPrev;
    };

    var YF_Image_Rotate = function(dNode, aOptions)
    {
        var th = this,
            _flushCssChanges = function(){
                th.flushCssChanges();
            },
            _rotate = function(){
                th.rotate();
            },
            _loaded = 0;

        this.delay = 2000;
        this.onChangeComplete = null;
        this.circle = true;

        this._current = null;

        this._items = [];
        this._node = null;


        this.rotate = function(){
            if(this._current === null) this._current = this._items[this._items.length - 1];
            if(this._current.getPrev() == null)
            {
                this._current = this._items[this._items.length - 1];
                this._current.fadeIn(_flushCssChanges);
            }
            else
            {
                this._current.fadeOut();
                this._current = this._current.getPrev();

            }
            if(this.onChangeComplete != null)
                    this.onChangeComplete(this._current);
            setTimeout(_rotate, this.delay);
        };

        this.flushCssChanges = function(){
            for(var i = this._items.length - 1; i >= 0 ; i--){
                $(this._items[i].getNode()).css('display', 'block');
            }
        };
        (function(){

            if(aOptions.delay) th.delay = aOptions.delay;
            th._node = dNode;
            if(!aOptions.list) return;
            if(aOptions.list.length == 0) return;
            if(aOptions.onChangeComplete) th.onChangeComplete = aOptions.onChangeComplete;

            var _oElement = null, _iPos = 0;
            for(var i = aOptions.list.length - 1; i >= 0 ; i--)
            {
                _iPos = th._items.push(new YF_Image_Rotate_Element(aOptions.list[i].image, null, _oElement, $.extend({vars : aOptions.list[i], onload : function(e){
                   if((_loaded++) == (aOptions.list.length - 1)){
                       $(th._node).css('display', 'block');
                       setTimeout(_rotate, th.delay);
                   }
                }}, aOptions)));
                if(_oElement !== null) _oElement.setNext(th._items[_iPos-1]);
                _oElement = th._items[_iPos-1];
                _oElement.getNode().appendTo(th._node);
            }

        })();
    };

    var _process        = function(opt)
    {
        var ret = null;
        if ($.nodeName(this, 'div'))
        {
            ret = $(this).data('yf_image_rotate');
            if (!ret)
            {
                ret = new YF_Image_Rotate(this, opt);
                $(this).data('yf_image_rotate', ret)
            }
        }
        return ret
    };


    $.fn.YF_ImageRotate  = function(opt)
    {
        if (this.length == 1)
        {
            return _process.apply(this.get(0), arguments)
        }
        else if (this.length > 0)
        {
            this.each(_process, arguments)
        }
        return this
    };
})(jQuery);
