//=========
// Eugene Ivanov, 20081001
// Объект для работы с управлением ScrollBar
// вход: el - элемент, куда встроить создаваемый элемент (полностью заменяет)
//       parameters - входные параметры для настройки

function ScrollBar() {}
//-------
ScrollBar.prototype.create = function(el, parameters) {
 if (!el) return false;

 this.this_object = this;

 var options = {};
 this.options_set_default();
 var parameters_inline = {};

 this.is_import = 1;

 this.element = el;
 this.id = el.id;
 this.height_thumb = 0;
 this.thumb_drag_start = 0;
 this.drag_lastx = 0;
 this.drag_lasty = 0;

 this.updown_hold = 0;
 this.updown_timer_var = null;
 this.updown_timer_value = 70;

 this.updown_timer_wait_var = null;
 this.updown_timer_wait_value = 200;

 this.update_timer_var = null;
 this.update_timer_value = 1000;

 if (isset(parameters))
 {
  this.parse_options(parameters);
 }

 if (this.is_import)
 {
  var is = this._import();

  options = {
	width:is['width'],
	height:is['height'],
    scrollTop_original:is['scrollTop'],
    scrollLeft_original:is['scrollLeft']
  };

  var pis = el.getAttribute('parameters');
  if (pis) parameters_inline = pis.evalJSON();
 }

 this.parse_options(options);
 if (isset(parameters))
 {
  this.parse_options(parameters);
 }
 this.parse_options(parameters_inline);

 this.construct();
 this.set_events();

 var o = this.options;

 var scroll_top = o['scrollTop_original']||0;
 var scroll_left = o['scrollLeft_original']||0;
 if (scroll_top || scroll_left)
 {
  this.scrollTo(scroll_left, scroll_top);
 }

}
//-------
ScrollBar.prototype.options_set_default = function() {
 var select_none_style = select_none_getstring();
 var select_none_style_clear = select_none_style.replace(/style="(.*?)"/i, '$1');
 var select_none_style_clear = select_none_style_clear.replace(/unselectable="on"/i, '');
 var select_none_style_ie = (isIE)?'unselectable="on"':'';

 this.options = {
    width:200,
    width_scroll:7,
    height:300,
    step_for_updown:15, // шаг перемещения для кнопок
 	template:'<div class="x-scrollbar fr" style="width: #width#; height: #height#; '+select_none_style_clear+'" '+select_none_style_ie+'>\
    <div class="x-scrollbar-up" '+select_none_style+' '+select_none_style_ie+'></div>\
    <div id="#id_thumb#" class="x-scrollbar-thumb" style="height: #height_thumb#; '+select_none_style_clear+'" '+select_none_style_ie+'></div>\
    <div class="x-scrollbar-down" '+select_none_style+' '+select_none_style_ie+'></div>\
    </div>'
 };
}
//-------
ScrollBar.prototype.parse_options = function(added) {
 var i;
 for (i in added)
 {
  this.options[i] = added[i];
 }
}
//-------
ScrollBar.prototype.scrollTo = function(x, y) {
 var o = this.options;

 var el = this.element;
 var id = this.id;
 var id_thumb = id+'_thumb';
 var el_thumb = $(id_thumb);

 var el_inner = this.inner;

 var scroll_top_max = this.height_all-o['height'];
 if (scroll_top_max < 0) scroll_top_max = 0;

 if (x < 0) x = 0;
 if (y < 0) y = 0;
 if (y > scroll_top_max) y = scroll_top_max+1;

 el_inner.scrollLeft = x;
 el_inner.scrollTop = y;

 var height_thumb_wo = o['height']-1;
 var thumb_scroll_top = Math.round((y/this.height_all)*height_thumb_wo)+7;

 el_thumb.style.top = dom_size_css_get(thumb_scroll_top);
}
//-------
ScrollBar.prototype.scrollBy = function(dx, dy, is_from_timer) {
 is_from_timer = isset(is_from_timer)?is_from_timer:0;

 var this_object = this;
 var o = this.options;

 var el = this.inner;

 var x = el.scrollLeft + dx;
 var y = el.scrollTop + dy;

 if (is_from_timer != 'timer' || (is_from_timer == 'timer' && this.updown_hold))
 {
  this.scrollTo(x, y);
 }

 if (is_from_timer == 'timer' && this.updown_hold)
 {
  clearInterval(this.updown_timer_var); this.updown_timer_var = null;
  this.updown_timer_var = setInterval(function(){return this_object.scrollBy(dx, dy)}, this.updown_timer_value);
 }
}
//-------
ScrollBar.prototype.check_update = function() {
 var o = this.options;

 var el = this.element;
 var id = this.id;
 var original_el = $(id);

// var el_sizes = Element.getDimensions(original_el);

 el_sizes = {};
 el_sizes['height'] = original_el.clientHeight;
 if (el_sizes['height'] != this.height_all)
 {
// mprint([el_sizes['height'], this.height_all]);
  this.height_all = el_sizes['height'];
  this.thumb_size_set();
  this.scrollBy(0, 0);
 }

}
//-------
ScrollBar.prototype.construct = function() {
 var o = this.options;

 var this_object = this;

 var h = o['template']; if (!isset(h)) return false;

 var el = this.element;
 var id = this.id;
 var id_thumb = id+'_thumb';

 var height_thumb = 4;

 h = h.replace(/#width#/ig, dom_size_css_get(o['width_scroll']));
 h = h.replace(/#height#/ig, dom_size_css_get(o['height']));
 h = h.replace(/#height_thumb#/ig, dom_size_css_get(height_thumb));
 h = h.replace(/#id_thumb#/ig, id_thumb);
 //h = h.replace(/##/ig, );

 var ref_width = 10;
 if (isIE) ref_width += 2;
 
 var el_width = o['width']-o['width_scroll'];

 var t_outer = dom_element_create('div', id+'_outer');
 t_outer.style.width = dom_size_css_get(o['width']);

 var t_inner = dom_element_create('div', id+'_inner');

 t_inner.style.overflow = 'hidden';
 t_inner.style.height = dom_size_css_get(o['height']);
 t_inner.style.width = dom_size_css_get(el_width);

 var parent = el.parentNode;
 parent.insertBefore(t_outer, el);
 html_set(t_outer, h);
 t_inner.appendChild(el);
 t_outer.appendChild(t_inner);

 el.style.overflow = 'visible';
 el.style.width = dom_size_css_get(el_width-ref_width);
 el.style.height = 'auto';

 el['ScrollBar'] = this_object;

 var sizes = {};
 if (!isIE)
 {
  sizes = Element.getDimensions(el);
 }
 else
 {
  sizes['height'] = el.clientHeight;
  sizes['width'] = el.clientWidth;
 }

 this.height_all = sizes['height'];
 this.thumb_size_set();

 this.element = t_outer;
 this.inner = t_inner;
}
//-------
ScrollBar.prototype.thumb_size_set = function() {
 var o = this.options;

 var id = this.id;
 var id_thumb = id+'_thumb';

 var el_thumb = $(id_thumb);
 var h_k = o['height']/this.height_all;

 var height_thumb_max = o['height']-18;
 if (isIE)
 {
  height_thumb_max = height_thumb_max + 2;
 }
 var height_thumb = Math.floor(height_thumb_max*h_k);

 if (height_thumb > height_thumb_max) height_thumb = height_thumb_max;
 this.height_thumb = height_thumb;
 el_thumb.style.height = dom_size_css_get(height_thumb);
}
//-------
ScrollBar.prototype._import = function() {
 var o = this.options;
 var c = [], i, j, t, ih, cc;

 var el = this.element;

 var scrollTop = el.scrollTop;
 var scrollLeft = el.scrollLeft;

 var sizes = {};
 if (!isIE)
 {
  var old_overflow = el.style.overflow;
  el.style.overflow = 'hidden';
  el.scrollTop = 0;
  el.scrollLeft = 0;
  sizes = Element.getDimensions(el);
 }
 else
 {
  sizes['height'] = el.clientHeight;
  sizes['width'] = el.clientWidth;
 }
 if (!isIE)
 {
  el.style.overflow = old_overflow;
 }
 else
 {
  sizes['width'] += 16;
 }

 return {width:sizes['width'], height:sizes['height'], scrollTop:scrollTop, scrollLeft:scrollLeft}
}
//-------
ScrollBar.prototype.destroy = function() {
 var o = this.options;
 var el = this.element;
 var el_inner = this.inner;
 var original_el = $(this.id);

 if (el && original_el)
 {
  this.clear_events();

  var scroll_top = el_inner.scrollTop;
  var scroll_left = el_inner.scrollLeft;

  var parent = el.parentNode;
  parent.insertBefore(original_el, el);

  original_el.style.overflow = 'auto';
  original_el.style.width = dom_size_css_get(o['width']-10);
  original_el.style.height = dom_size_css_get(o['height']-10);

  dom_element_del(this.id+'_outer');

  if (isIE)
  {
   setTimeout(function(){
   if (scroll_left)
   {
    original_el.scrollLeft = scroll_left;
   }
   if (scroll_top)
   {
    original_el.scrollTop = scroll_top;
   }
   }, 0
   );
  }
  else
  {
   if (scroll_left)
   {
    original_el.scrollLeft = scroll_left;
   }
   if (scroll_top)
   {
    original_el.scrollTop = scroll_top;
   }
  }

 }
}
//-------
ScrollBar.prototype.event_mousedown = function(event) {
 event = event||window.event;
 var event_element = Event.element(event);

 var this_object = this;

 var o = this.options;

 var event_el_class = event_element.className;

 if (event_el_class)
 {
  var dy = o['step_for_updown'];
  var dy = (event_el_class == 'x-scrollbar-up')?-dy:dy;

  if (event_el_class == 'x-scrollbar-up' || event_el_class == 'x-scrollbar-down')
  {
   this.scrollBy(0, dy);
   clearInterval(this.updown_timer_var); this.updown_timer_var = null;
   clearTimeout(this.updown_timer_wait_var); this.updown_timer_wait_var = null;
   this.updown_timer_wait_var = setTimeout(function(){return this_object.scrollBy(0, dy, 'timer')}, this.updown_timer_wait_value);
   this.updown_hold = 1;
  }
  if (event_el_class == 'x-scrollbar-thumb')
  {
   this.thumb_drag_start = 1;

   this.drag_lastx = event.clientX||event.pageX;
   this.drag_lasty = event.clientY||event.pageY;
  }
 }
}
//-------
ScrollBar.prototype.event_mouseup = function(event) {
 event = event||window.event;
 var event_element = Event.element(event);

 var o = this.options;

 var event_el_class = event_element.className;

 if (event_el_class)
 {
  if (event_el_class == 'x-scrollbar-up' || event_el_class == 'x-scrollbar-down')
  {
   clearInterval(this.updown_timer_var); this.updown_timer_var = null;
   this.updown_hold = 0;
  }

  if (event_el_class == 'x-scrollbar-thumb')
  {
   this.thumb_drag_start = 0;
  }
 }

 if (this.thumb_drag_start) this.thumb_drag_start = 0;

}
//-------
ScrollBar.prototype.event_mousemove = function(event) {
 event = event||window.event;
 var event_element = Event.element(event);

 var o = this.options;

 var event_el_class = event_element.className;

 //===
 var drag_lastx = event.clientX||event.pageX;
 var drag_lasty = event.clientY||event.pageY;

 var dx = drag_lastx-this.drag_lastx;
 var dy = drag_lasty-this.drag_lasty;

 this.drag_lastx = drag_lastx;
 this.drag_lasty = drag_lasty;
 //===

 if (this.thumb_drag_start)
 {
  if (1||event_el_class == 'x-scrollbar-thumb')
  {
   var height_thumb_wo = o['height']-16;
   var h_k = Math.ceil(height_thumb_wo/(this.height_thumb));
   this.scrollBy(0, dy*h_k);
  }
 }
}
//-------
ScrollBar.prototype.event_mouseover = function(event) {
 event = event||window.event;
 var event_element = Event.element(event);

 var o = this.options;
}
//-------
ScrollBar.prototype.event_mouseout = function(event) {
 event = event||window.event;
 var event_element = Event.element(event);

 var o = this.options;
}
//-------
ScrollBar.prototype.clear_events = function() {
 var this_object = this;
 var o = this.options;
 var el = this.element;

// Event.stopObserving(name_input, 'keyup', function(e) {this_object.keyup(e);});
// Event.stopObserving(name_input, 'keydown', function(e) {this_object.keydown(e);});
// Event.stopObserving(name_input, 'blur', function(e) {this_object.hide_timer(e);});

 Event.stopObserving(el, 'mousedown', function(e) {this_object.event_mousedown(e);});
 Event.stopObserving(document, 'mouseup', function(e) {this_object.event_mouseup(e);});
 Event.stopObserving(document, 'mousemove', function(e) {this_object.event_mousemove(e);});

 //Event.stopObserving(name+'_list', 'mouseover', function(e) {this_object.event_mouseover(e);});
 //Event.stopObserving(name+'_list', 'mouseout', function(e) {this_object.event_mouseout(e);});
// Event.stopObserving(window, 'resize', function(e) {this_object.event_resize(e);});

 if (this.update_timer_var)
 {
  clearInterval(this.update_timer_var); this.update_timer_var = null;
 }
}
//-------
ScrollBar.prototype.set_events = function() {
 var this_object = this;

 var o = this.options;
 var el = this.element;

// Event.observe(name_input, 'keyup', function(e) {this_object.keyup(e);});
// Event.observe(name_input, 'keydown', function(e) {this_object.keydown(e);});
// Event.observe(name_input, 'blur', function(e) {this_object.hide_timer(e);});
 
// Event.observe(document, 'click', function(e) {this_object.document_click(e);});

 Event.observe(el, 'mousedown', function(e) {this_object.event_mousedown(e);});
 Event.observe(document, 'mouseup', function(e) {this_object.event_mouseup(e);});
 Event.observe(document, 'mousemove', function(e) {this_object.event_mousemove(e);});

 //Event.observe(name+'_list', 'mouseover', function(e) {this_object.event_mouseover(e);});
 //Event.observe(name+'_list', 'mouseout', function(e) {this_object.event_mouseout(e);});
// Event.observe(window, 'resize', function(e) {this_object.event_resize(e);});

 this.update_timer_var = setInterval(function(){return this_object.check_update()}, this.update_timer_value);

 //this.resize();
}


