
function scroll_down(e)
{
  if (e.scrollTop() + scroll_length > scrollsize)
    e.scrollTop(scrollsize);
  else
    e.scrollTop(e.scrollTop() + scroll_length);
    
  recalc_bar(e);
}

function scroll_up(e)
{
  if (e.scrollTop() - scroll_length > 0)
    e.scrollTop(e.scrollTop() - scroll_length);
  else
    e.scrollTop(0);
    
  recalc_bar(e);
}

function recalc_bar(e)
{
  degree = Math.ceil(e.scrollTop() / (scrollsize / 100));
  $(custom_scrollbar.bar +' .pos').css('top', val = Math.round((scroll_fullheight / 100) * degree)+31);
}

function drag_scroll()
{
  /* opera alatt túl lehet húzni bizonyos esetekben */
  now = $("#scrollbar .pos").position().top - 31;
  degree = Math.round(now / (scroll_fullheight / 100));
  $(custom_scrollbar.container).scrollTop(Math.round(scrollsize * degree/100));
}

var scrollsize;
var scroll_fullheight;
var scroll_length;

if (typeof console == 'undefined')
  console = {
    log: function (e) {
      alert(e);
    }
  }

custom_scrollbar = {
  container: null,
  bar: null,
  scrollInit: function ()
  {
    if (!$(custom_scrollbar.container).length)
      return;

    $(custom_scrollbar.container).css('overflow', 'hidden');
    // $(custom_scrollbar.container).width($(custom_scrollbar.container).width() - $(custom_scrollbar.bar).width());
    $(custom_scrollbar.bar).show();

    scrollsize = $(custom_scrollbar.container)[0].scrollHeight - $(custom_scrollbar.container).outerHeight();

    if (scrollsize <= 0)
    {
      $('#scrollbar').hide();
      return;
    }

    $("#scrollbar .pos").draggable({
      axis: 'y',
      containment: 'parent',
      drag: drag_scroll
    });

    scroll_fullheight = 300-(3*31);
    scroll_length = 20;

    $('#scrollbar .up').click(function () { scroll_up($(custom_scrollbar.container)); return false; });
    $('#scrollbar .down').click(function () { scroll_down($(custom_scrollbar.container)); return false; });

    $(custom_scrollbar.container).mousewheel(function(event, delta) {
      if (delta > 0)
        scroll_up($(this));
      else if (delta < 0)
        scroll_down($(this));
      return false; // prevent default
      });

    recalc_bar($(custom_scrollbar.container));
  },
  
  heightRecalc: function () {
    if (!$(custom_scrollbar.container).length)
      return;
    scrollsize = $(custom_scrollbar.container)[0].scrollHeight - $(custom_scrollbar.container).outerHeight();
  }
}

$(window).load(function () {
  custom_scrollbar.heightRecalc();
});

function close_win()
{
  $('#container').toggle('drop');
  $('#container').effect("transfer", {
    to: '#main_menu li.'+ $('#close').attr('rel') +' a'
  });
  return false;
}

$(document).ready(function () {
  custom_scrollbar.container = '#texts';
  custom_scrollbar.bar = '#scrollbar';
  custom_scrollbar.scrollInit();

  if ($.browser.msie && $.browser.version < 7)
  {
    $('body div').hide();
    $('#wrongie').show();
    return;
  }
  
  $('#close').click(close_win);
  
});
