(function($)
    {
        $.fn.verticalSlider = function(options)
        {
            return this.each( function( el )
            {
                var defaults = {
                    timeout : 7000,
                    next_button_selector : ".navigator .arrows .arrow.right",
                    previous_button_selector : ".navigator .arrows .arrow.left",
                    navi_selector : ".navigator .navi",
                    item_selector : ".mime",
                    slide_after_click : true,
                    auto_start : true,
                    slide_count:10
                }; 
                var current_count = 0;
                var current_index = 0;
                var auto_scroll_timeout;
                var left_arrow;
                var right_arrow;
                var navi;
                var element = $(this);
                var content_height = 980;
                setOptions();
                initialize();
		
                function initialize()
                {
                    current_count = element.find( options.item_selector ).length;
                    content_height = element.find( options.item_selector + ":first" ).outerHeight(true);
                    element.height( ( element.find( options.item_selector ).length + 1 ) * content_height );
                    content_height = content_height * options.slide_count;
                    if ( current_count > 1 )
                    {
                        left_arrow = element.parent().parent().find( options.previous_button_selector );
                        right_arrow = element.parent().parent().find( options.next_button_selector );
                        left_arrow.show();
                        left_arrow.bind( "click" , goToPrevious );
                        right_arrow.show();
                        right_arrow.bind( "click" , goToNext );
                        navi = element.parent().parent().find( options.navi_selector );
                        if ( options.auto_start )
                        {
                            element.bind( "mouseover" , handle_mouseover );
                            auto_scroll_timeout = setTimeout( goToNext , options.timeout );
                        }
                    }
                }
			
                function handle_mouseover( event )
                {
                    element.unbind("mouseover");
                    element.bind( "mouseout" , handle_mouseout );
                    clearTimeout( auto_scroll_timeout );
                }
			
                function handle_mouseout( event )
                {
                    element.unbind("mouseout");
                    element.bind("mouseover", handle_mouseover);
                    auto_scroll_timeout = setTimeout( goToNext , options.timeout );
                }
			
                function goToNext( event )
                {
                    current_index = current_index < current_count - 1 ? current_index + 1 : 0;
                    clearTimeout( auto_scroll_timeout );
                    left_arrow.unbind("click");
                    right_arrow.unbind("click");
                    element.height( ( element.find( options.item_selector ).length + options.slide_count ) * content_height );
                    for ( var i = 0;i<options.slide_count;i++)
                    {
                        element.find( options.item_selector).eq(i).clone().insertAfter( element.find(options.item_selector + ':last' ) );
                    }
                    element.animate({
                        top: -content_height
                    }, 500 , function() {
                        if ( navi )
                        {
                            navi.find("li").removeClass("active");
                            navi.find("li").eq( current_index ).addClass("active");
                        }
                        for ( var i = 0;i<options.slide_count;i++)
                        {
                            element.find( options.item_selector + ':first' ).remove();
                        }
                        element.height( element.find( options.item_selector ).length * content_height );
                        element.css( "top" , 0 );
                        left_arrow.bind( "click" , goToPrevious );
                        right_arrow.bind( "click" , goToNext );
                        if ( typeof event != "object" || options.slide_after_click)
                        {
                            auto_scroll_timeout = setTimeout( goToNext , options.timeout );
                        }
                    } );
                    return false;
                }
			
                function goToPrevious( event )
                {
                    current_index = current_index > 0 ? current_index - 1 : current_count - 1;
                    clearTimeout( auto_scroll_timeout );
                    left_arrow.unbind("click");
                    right_arrow.unbind("click");
                    element.height( ( element.find( options.item_selector ).length + options.slide_count ) * content_height );
                    for ( var i = 0;i<options.slide_count;i++)
                    {
                        element.find( options.item_selector).eq((element.find( options.item_selector ).length-1)-i).clone().insertBefore( element.find( options.item_selector + ':first' ) );
                    }
                    element.css( "top" , -content_height );
                    element.animate({
                        top: 0
                    }, 500, function(){
                        if ( navi )
                        {
                            navi.find("li").removeClass("active");
                            navi.find("li").eq( current_index ).addClass("active");
                        }
                        for ( var i = 0;i<options.slide_count;i++)
                        {
                            element.find(options.item_selector + ':last' ).remove();
                        }
                        element.height( element.find( options.item_selector ).length * content_height );
                        left_arrow.bind( "click" , goToPrevious );
                        right_arrow.bind( "click" , goToNext );
                        if ( typeof event != "object" || options.slide_after_click)
                        {
                            auto_scroll_timeout = setTimeout( goToNext , options.timeout );
                        }
                    }
                    );
                    return false;
                }
			
                function setOptions()
                {
                    options = $.extend({}, defaults, element.data('my_plugin:options'), options);
                    element.data('my_plugin:options', options);
                };
            });
        }
    })(jQuery);
