$(document).ready(
function ()
{    
	$('.scrollable').jScrollPane({showArrows:true , scrollbarWidth:23, scrollbarMargin:1 }).parent().css('float','left');
	
	$.expandingTA($('textarea.expanding'));  
	//zapneme modul slideshow ^^
	jQuery().slideshow();

});

$.expandingTA = function(elems) 
{
    // turn each element into an expanding text area
    elems.each( function()  
    {       
        var _interval = null;
        var _dummy = null;
        var _self = this;
        var _prevHeight = $(_self).height();
        var _min_height;
        var scrollable2 = $('.scrollable2');
		
		scrollable2.jScrollPane2({showArrows:true , scrollbarWidth:23, scrollbarMargin:1 });
		scrollable2.parent().css({'border': '1px dotted #BFBFBF','float': 'left','margin': '0px 0px 8px 0px'});
        // magically expand the textarea
        $(_self)
        	.bind('focus', __checkExpand)
            .bind('blur', __stopExpand)
            //.bind('mousedown', __stopExpand)
            .css('overflow', 'hidden');

        function __checkExpand()
        {
            _min_height = $(_self).height();

            _interval = setInterval(function() { 
                __expandUpdate(); 
            }, 150);
        }

        function __stopExpand()
        {
            clearInterval(_interval);   
        }

        function __expandUpdate()
        {
            var line_height = 13;   // 13px assumed

            if ( _dummy == null ) { // create dummy
                _dummy = $('<div></div>')
                _dummy.css( {
                                'font-size':    '13px',
                                'font-family':  $(_self).css('font-family'),
                                'width':        parseInt($(_self).css('width'))+12+'px',
                                'padding':      $(_self).css('padding'),
                                'line-height':  '13px',
                                'overflow-x':   'hidden',
                                'overflow-y':   'scroll',
                                'display':      'none'
                            }).appendTo('body');
            }

            // update html in dummy div
            var newHtml = $(_self).val().replace(/\n/g, '<br>');
			
            if( _dummy.html() != newHtml ) {

                _dummy.html( newHtml );
                var newHeight = Math.max(_min_height, _dummy.height() + line_height);
	
	            if (_prevHeight != newHeight && _prevHeight != 0) {
                    $(_self).height(newHeight);
                    //$(_self).animate({height:(newHeight)}, 100);    // animate?
                    _prevHeight = newHeight;
                    //alert('fet');
                     scrollable2.jScrollPane2({showArrows:true , scrollbarWidth:23, scrollbarMargin:1 });
                     //scrollable2.parent().css({'border': '1px dotted #BFBFBF','float': 'left','margin': '0px 0px 8px 0px'});
                     //scrollable2[0].scrollTo(); 
                     return false;
                }
            }
        }
    });

}
