
(function ( $ ) {
    // Transfer context
    if ( ! Function.prototype.context ) {
        Function.prototype.context = function( context /*, arg1, arg2... */ ) {
            'use strict';
            if ( typeof this !== 'function' ) {
                throw new TypeError();        
            }
            var _arguments = Array.prototype.slice.call( arguments, 1);
            var _this = this;
            var _concat = Array.prototype.concat;
            var _function = function() {
                return _this.apply(
                    ( this instanceof _dummy ) ? this : context,
                    _concat.apply( _arguments, arguments ) 
                );
            };
            var _dummy = function() {};
            _dummy.prototype = _this.prototype;
            _function.prototype = new _dummy();
            return _function;
        };
    }                      
    
    // Object organizing scrolling layer
    function Scroller ( elem, options ) {
        // Layer that needs to be scrolled
        this.scroll = $( elem ).find( '.scroll-block' )[0];
        
        // Button to turn down
        this.down = $( elem ).find( '.scroll-up' )[0];
        
        // Button to turn up
        this.up = $( elem ).find( '.scroll-down' )[0];
        
        // Object holds the scroll function
        this.interval = null;               
        
        // Swig scroll 
        this.delta = 0;
        
        // Step scroll
        this.step = options.step;
        
        // Update interval 
        this.refresh = options.interval;
        
        // Transparency of the original
        this.opacityInitial = options.opacityInitial;
        
        // Transparency hover
        this.opacityHover = options.opacityHover;
        
        // 
        this.scrollButtonHeight = options.scrollButtonHeight;
        
        // Handling events falling in the area of ​​the down
		$( this.down ).click (function() {
				this.scrollDown(); 
				
		} .context( this ),
		function() {
				    this.scrollStop();
            }.context( this ) 
		);
       /* $( this.down ).hover(
            function() {
                $( this.down ).css( 'opacity', '0.4' );
                this.scrollDown();
            }.context( this ),
        
            function() {
                $( this.down ).css( 'opacity', '1' );
                this.scrollStop();
            }.context( this )
        ); */
        
        // Handling events falling in the area of ​​the up
       
	   $( this.up ).click (function() {       
                this.scrollUp();
            }.context( this ),
			function() {
				    this.scrollStop();
            }.context( this ) 
		);
        
            /* function() {                              
                $( this.up ).css( 'opacity', '1' );
                this.scrollStop();
            }.context( this )
        ); */
    
    }
    
    // Scroll Up
    Scroller.prototype.scrollUp = function () {
          this.interval = window.setInterval( function () {
              this.delta -= this.step;
              if( ( this.delta - this.scrollButtonHeight ) > 0 ) {
                  $( this.scroll ).css( { 'margin-top' : ( this.delta * -1 ) + 'px' } );
				  
                 } else {
                     this.scrollStop();
                 }
         }.context( this ), this.refresh );
    };
    
    // Scroll Down
    Scroller.prototype.scrollDown = function () {
        this.interval = window.setInterval( function () { 
             this.delta += this.step;                       
             
             var childrens_height = 0;
             var childrens = $(this.scroll).children();
             var childrens_count = childrens.length;
             
             for( var i = 0; i < childrens_count; i++ ) {
                 childrens_height += $(childrens[i]).height();
             }
             
             var maxdelta = ( childrens_height ) - $(this.scroll).height() + this.scrollButtonHeight;
             
             if( this.delta <= maxdelta ) {
                 $(this.scroll).css( { 'margin-top' : ( this.delta * -1 ) + 'px' } );
             } else {
                 this.scrollStop();
             }
         }.context( this ), this.refresh );
    
    };                   
    
    // Stop scrolling
    Scroller.prototype.scrollStop = function () {
        window.clearInterval( this.interval );
    };
    
    
    $.fn.scrollDiv = function( options ) {
        // Configuration defaul
        var defaults = {
            interval       : 1,
            step           : 1,
            opacityInitial : '1',
            opacityHover   : '1',
            scrollButtonHeight : -1
        };                                      
        
        // Additional user preferences
        var settings = $.extend( defaults, options );
        
        // Apply the plugin to a set of bits and pieces
        return this.each( function( elem ) {
            new Scroller( this, settings ) 
        });
    };

})($);

function switchTab(elementID, className, boxClassName)
{
    $('.'+className+'-active').removeClass(className+'-active');
    $('#'+elementID+'-tab').addClass(className+'-active');
    $('.'+boxClassName).css('display', 'none');
    $('#'+elementID+'-box').css('display', 'block');
}
 

