var MooFade = new Class(nanofadeProto);

var MooTibeFade = new Class({ // some alternative class for flashAnim
    Implements: [Options, Events],
    /*Extends: product,*/
    /* standart egenskap */
    options: {

    },

    /* -- constructor -- */
    initialize: function($options) {
        this.setOptions($options);

    }

});

var MooFadeOutFadeIn = new Class({
    /*Implements: [Options, Events],*/

    Extends: MooFade,

    stack : function() {
        this.parent();
        this.nextEl.style.opacity = 0;
        if (this.transType == 'filter') {
            this.nextEl.style['filter'] = 'alpha(opacity=0)';
        }
    },
    filterFade : function(el, from, to, dur, after){
        var start =+ new Date, finish = start+dur;
        var self = this;
        var reOpacity = /alpha\(opacity=([^\)]+)\)/;
        el.set('morph', {
            duration : dur,
            onComplete : after.bind(this)
        });
        el.morph({'opacity': to});
        // later fix for IE9
        /*var interval = setInterval(function(){
            var time = +new Date, pos = time>finish ? 1 : (time-start)/dur;
            var value = ((from+(to-from)*pos).toFixed(3));
            var es = el.style;
            if (reOpacity.test(es.filter)) {
                value = value >= 0.9999 ? '' : ('alpha(opacity=' + (value * 100) + ')');
                es.filter = es.filter.replace(reOpacity, value);
            }
            else {
                es.filter += ' alpha(opacity=' + (value * 100) + ')';
            }
            if(time>finish) {
                clearInterval(interval);
                after && self.schedule(after, 1);
            }
        },10);*/
    },
    fade : function() {
        //console.log(this.nextEl, this.getImg(this.nextEl).complete);
        if (!this.getImg(this.nextEl).complete) {
            this.schedule(this.fade, 500);
            return false;
        }
        if (this.transType == 'css') {
            this.cssFade(this.currentEl, 1, 0, this.duration);
        }
        else if (this.transType == 'filter') {
            this.filterFade(this.currentEl, 1, 0, this.duration, this.stack);
        }
        else {
            this.animFade(this.currentEl, 1, 0, this.duration);
        }
        this.currentEl = this.nextEl;
        this.current = (this.current < this.images.length-1) ? this.current+1 : 0;
        this.schedule(this.fadeIn, 750);
    },
    fadeIn : function() {
        if (this.transType == 'css') {
            this.cssFade(this.currentEl, 0, 1, this.duration);
        }
        else if (this.transType == 'filter') {
            this.filterFade(this.currentEl, 0, 1, this.duration, this.stack);
        }
        else {
            this.animFade(this.currentEl, 0, 1, this.duration, this.stack);
        }
        this.schedule(this.fade, this.slidetime);
    }
});


var MooFadeExt = new Class({
    /*Implements: [Options, Events],*/

	Extends: MooFade,
	texts : null,
	caption1: null,
	caption2: null,
	currentCaption: null,
	stack : function() {
		this.parent();
		this.nextEl.style.opacity = 0;
		if (this.transType == 'filter') {
			this.nextEl.style['filter'] = 'alpha(opacity=0)';
		}
		var caption = (this.currentEl == this.el1) ? this.caption2 : this.caption1;
		caption.set('html', this.texts[this.current]);
	},
	initialize : function(el, data, options) {
		var captionClass = options.captionClass || 'fade_caption';
		var captionProp = {
			'class': captionClass
		};
		this.caption1 = new Element('div', captionProp);
		this.caption2 = new Element('div', captionProp);
		this.texts = data.texts;
		this.parent(el, data.img, options);
		if (this.caption1.style['opacity'] !== undefined) {
			this.transType = 'anim';
		}
		this.caption1.injectInside(this.el1);
		this.caption1.zIndex = 100;
		this.caption2.injectInside(this.el2);
		this.caption1.zIndex = 10;
		this.caption1.set('html', data.texts[0]);
		this.currentCaption = this.caption1;
	},
	filterFade : function(el, from, to, dur, after){
		var start = +new Date, finish = start+dur;
		var self = this;
		var reOpacity = /alpha\(opacity=([^\)]+)\)/;
		var interval = setInterval(function(){
			var time = +new Date, pos = time>finish ? 1 : (time-start)/dur;
			var value = ((from+(to-from)*pos).toFixed(3));
			var es = el.style;
			if (reOpacity.test(es.filter)) {
				value = value >= 0.9999 ? '' : ('alpha(opacity=' + (value * 100) + ')');
				es.filter = es.filter.replace(reOpacity, value);
			}
			else {
				es.filter += ' alpha(opacity=' + (value * 100) + ')';
			}
			if(time>finish) {
				clearInterval(interval);
				after && self.schedule(after, 1);
			}
		},10);
	},
  	fade : function() {
		if (!this.getImg(this.nextEl).complete) {
			this.schedule(this.fade, 500);
			return false;
		}
		if (this.transType == 'css') {
			this.cssFade(this.currentEl, 1, 0, this.duration);
		}
		else if (this.transType == 'filter') {
			this.filterFade(this.currentEl, 1, 0, this.duration, this.stack);
		}
		else {
			this.animFade(this.currentEl, 1, 0, this.duration);
		}
		this.currentEl = this.nextEl;
		this.current = (this.current < this.images.length-1) ? this.current+1 : 0;
		this.schedule(this.fadeIn, 750);
	},
	fadeIn : function() {
		if (this.transType == 'css') {
			this.cssFade(this.currentEl, 0, 1, this.duration);
		}
		else if (this.transType == 'filter') {
			this.filterFade(this.currentEl, 0, 1, this.duration, this.stack);
		}
		else {
			this.animFade(this.currentEl, 0, 1, this.duration, this.stack);
		}
		this.schedule(this.fade, this.slidetime);
	}
});
