/*
 * 	Seven Fader 0.1 - jQuery plugin.
 *
 * 	
 *	Seven Intellect, http://www.sevenintellect.com/
 *
 *	Copyright (c) 2011 Seven Intellect (http://www.sevenintellect.com)
 *	Licensed under the MIT License:
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */

/*
 * Example usage:
 * 
 * jQuery('div#images').sevenFader({
 *      paginationContainer: '#pagination-container',
 *      paginationUlClass:   'ul-class',
 *      paginationUlId:      'ul-id',
 *      fadeTime:           300,
 *      fadeInterval:       3000
 * });


/*
 *	Markup example:
 *
 * <!-- fader container -->
 * 
 * <div id="fader">
 *      <img src="img/image1.jpg" alt="" />
 *      <img src="img/image2.jpg" alt="" />
 *      <img src="img/image3.jpg" alt="" />
 * </div>
 *
 * <!-- pagination container -->
 * 
 * <div id="faderPagination">
 * </div>
 * 
 */

(function($) {

    $.fn.sevenFader = function(options){        
        var defaults = {
            paginationContainer: '#thumbnailsWrapper',
            paginationUlClass:   'homepageSlider',
            paginationUlId:      'sliderPagination',
            fadeTime:           300,
            fadeInterval:       4000
        };
		
        var options = $.extend(defaults, options);
        
        this.each(function() {
            var base = $(this);
            var fadeIntervalFunc = null;
            var $paginate = jQuery(options.paginationContainer);

            setPagination(base, $paginate, options.fadeTime, options.paginationUlClass, options.paginationUlId);

            jQuery('div', base).not(':first').hide();
            jQuery('div:first', base).addClass('current');

            fadeIntervalFunc = setInterval(function(){
                fadeNext(base, $paginate, options.fadeTime);
            }, options.fadeInterval);

            function fadeNext(base, $paginate, fadeTime, img) {
                jQuery('div.current', base).fadeOut(fadeTime, function() {
                    jQuery(this).removeClass('current');
                    
                    if (!img) {
                        $next = (jQuery(this).next().length > 0) ? jQuery(this).next() : jQuery('div:first', base);
                    } else {
                        $next = img;
                    }
                    var nextIndex = $next.index();
                    
                    $next.fadeIn(fadeTime).addClass('current');

                    jQuery('li a.current', $paginate).removeClass('current');
                    jQuery('li:eq('+nextIndex+') a', $paginate).addClass('current');
                });
            }

            function setPagination(base, $paginate, fadeTime, ulClass, ulId) {
                var imagesCount = jQuery('div', base).size();
                var element = '<li><a href=""></a></li>';
                var ul = '<ul id="'+ulId+'" class="'+ulClass+'"></ul>';
                $paginate.append(ul);
                
                for (i = 0; i < imagesCount; i++) {
                    jQuery('ul', $paginate).append(element);
                }

                jQuery('li:first a', $paginate).addClass('current');

                jQuery('li a', $paginate).bind('click', function(e){
                    e.preventDefault();
                    clearInterval(fadeIntervalFunc);
                    var liIndex = jQuery(this).parent().index();
                    if (jQuery('div:eq('+liIndex+')', base).is(':visible')) {
                        return false;
                    }
                    
                    fadeNext(base, $paginate, fadeTime, jQuery('div:eq('+liIndex+')', base));
                });
            }
        });
    }
})(jQuery);
