/*
 file: jquery.Fader.js
 file usage: Featured Section fade slider
 Author: Speed730
 Author URI: http://themeforest.net/user/Speed730
 */
(function($){
    $.fn.fade = function(options){
        var defaults = {
            elements: '>div',
            textBox: '.textBox',
            textBackgroundOpacity: 0.6,            
            easing: 'easeOutExpo',
            rotationTime: 1500,
            rotationSpeed: 3, // duration between autorotation switch in Seconds
            autoFade: true,
            fadeSpeed: 900
        }, settings = $.extend({}, defaults, options);
        
        var Container = this.find('#fadeWrapper');
        element = Container.find(settings.elements), element_Count = element.length, textBox = Container.find(settings.textBox).append('<span/>').removeClass('textBox').addClass('wrapperText').css({
            width: element.width()
        }),
		wrappedText = Container.find('.wrapperText').find('span').addClass('textBox').css({
            opacity: settings.textBackgroundOpacity,
            position: "absolute",
            left: 0,
            width: element.width(),
            display: 'block'			
        });
				
		wrappedText.find('h3').css(
		{
			color : '#fff'			
		});
        
        $(textBox).each(function(i){
            $this = $(this);
            var title = $this.find("h3");
            var bgCover = $this.find(".textBox").insertBefore(title);
            var $thisHeight = $this.outerHeight();
            bgCover.css({
                height: $thisHeight,
                top: -5
            });
            $this.css({
                bottom: -(380 - $thisHeight - 5)
            });
        });
        
        (textBox.outerHeight(true))
        faderPrevBtn = $('.prevButton'), faderNextBtn = $('.nextButton'), 
		buttons_space = 20, // buttons space from container edge
 buttonsWidth = faderNextBtn.width(), interval = '', inContainer = false;
        
        element.each(function(i){
            $this_element = $(this);
            $this_element.css({
                zIndex: i + 1
            });
        });
        
        current_element = element.not(element[0]).css({
            opacity: 0.0
        });
        
        $(element[0]).addClass('currenShow').css({
            zIndex: element_Count + 1
        });      
        
        
		activateFader();
		
        function activateFader(){
            $(element).css({
                'background': 'none'
            });
            
            if (settings.autoFade) {
                interval = setInterval(function(){
                    fadeImages();
                }, (parseInt(settings.rotationSpeed) * settings.rotationTime));
            }
            
            function fadeImages(){
                var current_elem = Container.find('.currenShow');
                var next_elem = current_elem.next();
                if (next_elem.length == 0) {
					next_elem = $(element[0])
				}
				                
                current_elem.css({
                    zIndex: element_Count
                }).stop().animate({
                    opacity: 0
                }, settings.fadeSpeed, settings.easing)
				
				var currentFade = Container.find('.currenShow').find('.wrapperText');
                    var currentHeight = currentFade.outerHeight();
                    currentFade.stop().animate({
                        bottom: - currentHeight
                    });
				
				current_elem.removeClass('currenShow');				
                
                next_elem.addClass('currenShow').css({
                    zIndex: element_Count + 1
                }).stop().animate({
                    opacity: 1
                }, settings.fadeSpeed, settings.easing, function()
				{
					var nextFade = Container.find('.currenShow').find('.wrapperText');
                    var nextHeight = nextFade.outerHeight();
                    nextFade.stop().animate({
                        bottom: nextHeight + 5
                    })					
				});
            }
            
            function fadeImagesRevers(){
                var current_elem = Container.find('.currenShow');
                var prev_elem = current_elem.prev();
                if (prev_elem.length == 0) {
                    prev_elem = $(element[element_Count - 1])
                }
                
                current_elem.css({
                    zIndex: element_Count
                }).stop().animate({
                    opacity: 0
                }, settings.fadeSpeed, 'easeOutExpo');
				
				var currentFade = Container.find('.currenShow').find('.wrapperText');
                    var currentHeight = currentFade.outerHeight();
                    currentFade.stop().animate({
                        bottom: - currentHeight
                    });
				current_elem.removeClass('currenShow');
                
                prev_elem.addClass('currenShow').css({
                    zIndex: element_Count + 1
                }).stop().animate({
                    opacity: 1
                }, settings.fadeSpeed, 'easeOutExpo', function()
				{
					var nextFade = Container.find('.currenShow').find('.wrapperText');
                    var nextHeight = nextFade.outerHeight();
                    nextFade.stop().animate({
                        bottom: nextHeight + 5
                    })					
				});
            }
			
            faderPrevBtn.animate({
                left: -buttonsWidth
            }, 900, 'easeInExpo');
            
            faderNextBtn.animate({
                right: -buttonsWidth
            }, 900, 'easeInExpo');
			            
            //============================================================================//
            
            $(Container).bind('mouseover', function(){
                clearInterval(interval);
                $(faderNextBtn).trigger('mouseover', false);
                $(faderPrevBtn).trigger('mouseover', false);
                
                var currentFade = Container.find('.currenShow').find('.wrapperText');
                var currentHeight = currentFade.outerHeight();
                currentFade.stop().animate({
                    bottom: currentHeight + 5
                });
                
                inContainer = true;
            });
            
            $(Container).bind('mouseout', function(event){
                if (inContainer) {
                    if (settings.autoFade) {
                        interval = setInterval(function(){
                            fadeImages();
                        }, (parseInt(settings.rotationSpeed) * settings.rotationTime));
                    }
                    
                    faderPrevBtn.stop().animate({
                        left: -buttonsWidth
                    }, 250, 'easeOutExpo');
                    
                    faderNextBtn.stop().animate({
                        right: -buttonsWidth
                    }, 250, 'easeOutExpo');
                    
                    var currentFade = Container.find('.currenShow').find('.wrapperText');
                    var currentHeight = currentFade.outerHeight();
                    currentFade.stop().animate({
                        bottom: -(currentHeight)
                    });
                }
            });
            
            $(faderNextBtn).bind('mouseover', function(event, inContainer){
                if (!inContainer) {
                    faderNextBtn.stop().animate({
                        right: buttons_space
                    }, 'easeOutExpo');
                    clearInterval(interval)
                }
            });
            
            $(faderPrevBtn).bind('mouseover', function(event, inContainer){
                if (!inContainer) {
                    $(faderPrevBtn, faderNextBtn).stop().animate({
                        left: buttons_space
                    }, 'easeOutExpo');
                    clearInterval(interval);
                }
            });
            
            $(faderNextBtn, faderPrevBtn).bind('mouseout', function(event){
                clearInterval(interval);
            });
            
            // Click Events 
            faderNextBtn.click(function(){
                fadeImages();
            });
            
            faderPrevBtn.click(function(){
                fadeImagesRevers();
            });
        }
        return this;
    }
})(jQuery);
