/*
---
description: 
	- jQuery Carousel Plugin (http://vkurseweba.ru/blog/jquery-carousel-plugin)

authors:
	- Bekbulatov Alexander ( alexander@bekbulatov.ru, http://vkurseweba.ru/ )

license:
	- MIT-style license

requires:
	- jquery-1.3.2.js
	- jquery.scrollTo.js
...
*/

(function($){
    $.fn.carousel = function(options) {
        var defaults = {
            mode: 'horizontal',
            fadeDuration: 300,
            transitionDuration: 700,
            step: 300,
            opacity: 0.5,
            center: false
        };

        var options = $.extend(defaults, options);
	
        return this.each(function() {
		
            var obj = $(this);
		
            var topButton = obj.children('div.topButton'),
            bottomButton = obj.children('div.botomButton'),
            scrollDiv = obj.children('div.scroll'),
            ul = scrollDiv.children('ul'),
            li = ul.children('li'),
            scrollLimit = 0,
            position = 0;
		
            li.each(function(){
                scrollLimit += (options.mode == 'horizontal') ?
                $(this).outerWidth() : $(this).outerHeight();
            });
		
            if (options.mode == 'horizontal') {
                ul.width(scrollLimit);
            }
            else {
                ul.height(scrollLimit);
            }

            var max = (options.mode == 'horizontal') ?
            ul.outerWidth() - scrollDiv.outerWidth() :
            ul.outerHeight() - scrollDiv.outerHeight();
		
            li.hover(function() {
                $(this).siblings(':not(.current)').stop().fadeTo(options.fadeDuration, options.opacity);
            }, function() {
                $(this).siblings().stop().fadeTo(options.fadeDuration, 1);
            });
		

            var scrollingTo = function(destination) {

                if (typeof destination == "number") {
                    scrollDiv.stop().scrollTo( destination, options.transitionDuration );
                }
                else if(typeof destination == "object")	{
                    var x;
                    if (options.mode == 'horizontal') {
                        x = destination.position().left - ul.position().left;
                        x -= (scrollDiv.outerWidth() - destination.outerWidth()) / 2;
                    }
                    else {
                        x = destination.position().top - ul.position().top;
                        x -= (scrollDiv.outerHeight() - destination.outerHeight()) / 2;
                    }
                    if (x < 0)
                        x = 0;
                    scrollDiv.stop().scrollTo( x, options.transitionDuration );
                    position = x;
                }

                changeActivity();
            };

            var changeActivity = function() {
                topButton.toggleClass('disabled', position <= 0);
                bottomButton.toggleClass('disabled', position >= max);
            };

            topButton.click(function(){
                position -= options.step;
                if (position < 0)
                    position = 0;
                scrollingTo(position);
            });
		
            bottomButton.click(function(){
                position += options.step;
                if (position > max)
                    position = max;
                scrollingTo(position);
            });
		
            if (options.center) {
                ul.children('li').click(function(){
                    $(this).siblings().removeClass('current');
                    $(this).addClass('current');
                    scrollingTo($(this));
                });
            }
		
            var current = $('li.current');
            if (current.length) {
                scrollingTo(current);
            }
        });

    };
})(jQuery);
