/* ------------------------------------------------------------------------
	WRF-Slider inheritet from s3Slider
	
	Developped By: Boban Karišik -> http://www.serie3.info/
        CSS Help: Mészáros Róbert -> http://www.perspectived.com/
    changed: Michael Zschutschke -> http://webraumfahrer.com/
        
	Version: 1.1
	
	Copyright: Feel free to redistribute the script/modify it, as
			   long as you leave my infos at the top.
------------------------------------------------------------------------- */


(function($){  

    $.fn.wrfTransition = function(vars) {       
        
        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 4000;
        var current     = null;
        var timeOutFn   = null;
        var faderStat   = true;
        var mOver       = false;
        var items       = $('#transitionContent').children();
        var public = {};
            
        var makeTransition = function() {
            current = (current != null) ? current : items[(items.length-1)];
            var currNo      = jQuery.inArray(current, items) + 1
            currNo = (currNo == items.length) ? 0 : (currNo - 1);
            var newMargin   = $(element).width() * currNo;
            stop_container_idx = items.length;
            
            // console.log(stop_container_idx);
            // console.log(current);
            // console.log(currNo);
            // console.log(current);
            
            public.transion_effect = function(currNo) {
                
                effect_name = "public.effect_"+$(items[currNo]).children().first($(this)).attr('class');
                // console.log(effect_name);
                if(typeof eval(effect_name) == 'function') {
                    // console.log('function exists');
                    eval(effect_name)(currNo);
                }
                else
                {
                   //console.log('function does not exists');
                   public.nextElement(currNo);
                   public.transion_effect(current); 
                }
            }
            
            public.effect_fade_in = function(currNo) {
                $(items[currNo]).fadeIn((timeOut), function() {
                                    if (public.nextElement(currNo)) 
                                    {
                                        public.transion_effect(current);
                                    };
                                });
            }

            public.effect_fade_out = function(currNo) {
                $(items[currNo]).prevAll(':visible').each(function(index) {
                    // console.log($(this));
                    if ($(this).hasClass('transition-item-text')) {
                        $(this).hide();
                    }
                    else {
                    // console.log($(items[currNo]).prevAll().last());
                    $(this).fadeOut((timeOut), function() {
                                    if (public.nextElement(currNo) && $(items[currNo]).prevAll().last()) 
                                    {
                                        public.transion_effect(current);
                                    };
                                });
                    }; 
                });
            }
            
            public.effect_slide_in = function(currNo) {
                $(items[currNo]).slideDown((timeOut), function() {
                                    // console.log('slide down done');
                                    if (public.nextElement(currNo)) 
                                    {
                                        public.transion_effect(current);
                                    };
                                });
            }
            
            
            public.nextElement = function(currNo) {
                
                if (currNo+2 >= stop_container_idx)
                {
                   // console.log('one loop end');
                   current = 0; 
                   return true;
                }
                else {
                   current = currNo + 1;
                   return true; 
                }
            }
            
            public.transion_effect(currNo);
            
        }
        
        makeTransition();

    };  

})(jQuery);
