
function si() {
  var i,j=0,x,a=si.arguments; for(i=0;i<a.length;i++)
   if ((x=MM_findObj(a[i]))!=null){if(!x.oSrc) x.oSrc=x.src; x.src=x.src.replace(/_(.)\./g, "_$1o.");}
   //if ((x=MM_findObj(a[i]))!=null){if(!x.oSrc) x.oSrc=x.src; x.src=x.src.replace(/_u\./g, "_uo\.");}
}
function sir() {
  var i,x,a=sir.arguments; for(i=0;i<a.length&&(x=MM_findObj(a[i]))&&x.oSrc;i++) x.src=x.oSrc;
}

function si2() {
  var i,j=0,x,a=si2.arguments; for(i=0;i<a.length;i++)
   if ((x=MM_findObj(a[i]))!=null){if(!x.oSrc) x.oSrc=x.src; x.src=x.src.replace(/_u\.gif/g, "_uo.jpg");}
}




function f_drag_y(l, t, obj){
	obj.autoScroll = false;
	obj.Yscroll_drag = true;
	obj.Yscroll(Math.round(-(obj.clip.top-(t-obj.scrol_bar_top)/obj.k1)));
}

function Yscroll_bar(obj_name, layer_name, scroll_bar_name, scrol_bar_height){
	this.my_name = obj_name;
	this.body = MM_findObj(layer_name);
	if(!this.body) return false;
	this.scroll_bar = MM_findObj(scroll_bar_name);
	this.scrol_bar_clientHeight = scrol_bar_height;
	//this.scrol_bar_top = NS ? this.scroll_bar.top : this.scroll_bar.style.pixelTop;
	this.scrol_bar_top = get_relative_top(this.scroll_bar);
	this.autoScroll = false;
	this.Yscroll_drag = false;

	//q this.scrollHeight = NS ? this.body.document.height : this.body.scrollHeight; //Пълната височина на документа
	this.scrollHeight = get_absolute_height(this.body);

	this.Yscroll = Yscroll;
	this.scroll_up = scroll_up;
	this.scroll_down = scroll_down;
	this.scroll_stop = scroll_stop
	this.dump = dump
	this.capture_events = capture_events;

	this.clip = get_clip_rec(this.body)

	this.clientHeight = this.clip.bottom - this.clip.top;
	if(this.clientHeight>this.scrollHeight) this.scrollHeight = this.clientHeight;
	this.scrol_bar_height = Math.round((this.scrol_bar_clientHeight * this.clientHeight) / this.scrollHeight);
	this.scroll_bar_max_bottom = this.scrol_bar_clientHeight - this.scrol_bar_height;
	if(this.scrol_bar_height>this.scrol_bar_clientHeight) this.scrol_bar_height = this.scrol_bar_clientHeight;
	if(this.scrol_bar_height<10) this.scrol_bar_height = 10;
	if(NS){
		this.scroll_bar.clip.height = this.scrol_bar_height;
	} else {
		this.scroll_bar.style.height = this.scrol_bar_height;
	}

	this.k1 = this.scrol_bar_clientHeight / this.scrollHeight;

	this.key_scroll_step = Math.round(this.clientHeight/8);
	this.page_scroll_step = Math.round(this.clientHeight*0.9);
	this.btn_scroll_step = Math.round(this.clientHeight/16);

	MM_dragLayer(scroll_bar_name,'',0,0,0,0,true,true,0,this.scroll_bar_max_bottom,0,0,false,false,0,'',false,'f_drag_y(newLeft, newTop, '+this.my_name+')');
	//MM_dragLayer(scroll_bar_name,'',0,0,0,0,true,true,0,this.scroll_bar_max_bottom,0,0,false,false,0,'',false,'');
}


function tmp4(e){
	alert(e.keyCode);
	//alert(e.which);
	e.returnValue = false;
	return false;
}


function capture_events(){
	var i,x,a=capture_events.arguments;

	if(1) {
		var tmp = this.my_name;
		for(i=0; i<a.length; i++)
    	if( x=MM_findObj(a[i]) ){

    		if(NS){
    			x.captureEvents(Event.Focus);
    			x.onFocus = tmp4;
				}

				if(NS6){
					document.addEventListener("keydown",tmp4,true);
				}

				if(IE)
    		x.onkeydown = x.onmousewheel = function(){

    			var obj = eval(tmp);
					var scroll_step = 0;
    			switch(event.keyCode){
    				case 40: scroll_step = obj.key_scroll_step; break; //40 strelka na dolu
    				case 38: scroll_step = -obj.key_scroll_step; break; //38 strelka nagore
    				case 34: scroll_step = obj.page_scroll_step; break; //34 PageDown
    				case 33: scroll_step = -obj.page_scroll_step; break; //33 PageUp
    			}
					if(event.wheelDelta!=0) scroll_step = obj.key_scroll_step*(-event.wheelDelta/120);
					if(scroll_step!=0){
						obj.autoScroll = false;
						obj.Yscroll_drag = false;
						event.returnValue = false;
						obj.Yscroll(scroll_step);
					}
				};

    	}
	}
}

function debug(msg){
		document.forms[0].t1.value += msg+'\n';
}


function dump(){
	alert(
	'height: '+this.scrollHeight+'\n'+
	'width: '+this.scrollWidth+'\n'+
	'clientHeight: '+this.clientHeight+'\n'+
	'clientWidth: '+this.clientWidth+'\n'+
	'scrol_bar_top: '+this.clip_top*this.k1+'\n'+
	'clip_top: '+this.clip_top+'\n'+
	'clip_left: '+this.clip_left+'\n'+
	'clip_bottom: '+this.clip_bottom+'\n'+
	'clip_right: '+this.clip_right+'\n'+
	'AutoScroll: '+this.autoScroll+'\n'+
	'');
}

function scroll_stop(){
	this.autoScroll = false;
	clearTimeout(this.timer1);
}

function scroll_up(){
	this.autoScroll = true;
	this.Yscroll_drag = false;
	this.Yscroll(-this.btn_scroll_step);
}

function scroll_down(){
	this.autoScroll = true;
	this.Yscroll_drag = false;
	this.Yscroll(this.btn_scroll_step);
}

function Yscroll(offset){
	if( (this.clip.top+offset) <= 0) offset = 0 - this.clip.top;
	if( (this.clip.bottom+offset) >= this.scrollHeight) offset = this.scrollHeight - this.clip.bottom;

	this.clip.top += offset;
	this.clip.bottom += offset;

	move_layer_by(this.body, 0, -offset);
	set_clip_rec(this.body, this.clip);
  if(!this.Yscroll_drag) move_layer_to_y(this.scroll_bar, this.clip.top*this.k1+this.scrol_bar_top);

	if(this.autoScroll) this.timer1 = setTimeout(this.my_name+'.Yscroll('+offset+')', 50);
}



// ---------------------------------- X SCROLL -----------------------

function f_drag_x(l, t, obj){
	obj.autoScroll = false;
	obj.Xscroll_drag = true;
	obj.Xscroll(Math.round(-(obj.clip_left-l/obj.k1)));
}

function Xscroll_bar(obj_name, layer_name, scroll_bar_name, scrol_bar_width){
	this.my_name = obj_name;
	this.body = MM_findObj(layer_name);
	if(!this.body) return;
	this.scroll_bar = MM_findObj(scroll_bar_name);
	this.scrol_bar_clientWidth = scrol_bar_width;
	this.scrol_bar_left = NS ? this.scroll_bar.left : this.scroll_bar.style.pixelLeft;
	this.autoScroll = false;
	this.Xscroll_drag = false;

	this.scrollWidth = NS ? this.body.document.width : this.body.scrollWidth; //Пълната височина на документа

	this.Xscroll = Xscroll;
	this.scroll_left = scroll_left;
	this.scroll_right = scroll_right;
	this.scroll_stop = scroll_stop
	this.dump = dump
	this.capture_eventsx = capture_eventsx;

	if(NS){
		this.clip_top = this.body.clip.top;
		this.clip_left = this.body.clip.left;
		this.clip_bottom = this.body.clip.bottom;
		this.clip_right = this.body.clip.right;
	} else {
		var s = this.body.style.clip;
		s = s.slice(5);
		s = s.split(" ", 4);

		this.clip_top = parseInt(s[0]);
		this.clip_left = parseInt(s[3]);
		this.clip_bottom = parseInt(s[2]);
		this.clip_right = parseInt(s[1]);
	}

	this.clientWidth = this.clip_right - this.clip_left;
	if(this.clientWidth>this.scrollWidth) this.scrollWidth = this.clientWidth;
	this.scrol_bar_width = Math.round((this.scrol_bar_clientWidth * this.clientWidth) / this.scrollWidth);

	if(this.scrol_bar_width>this.scrol_bar_clientWidth) this.scrol_bar_width = this.scrol_bar_clientWidth;
	if(this.scrol_bar_width<10) this.scrol_bar_width = 10;

	if(NS){
		this.scroll_bar.clip.width = this.scrol_bar_width;
	} else {
		this.scroll_bar.style.width = this.scrol_bar_width;
	}

	this.k1 = this.scrol_bar_clientWidth / this.scrollWidth;

	this.key_scroll_step = Math.round(this.clientWidth/16);
	this.page_scroll_step = Math.round(this.clientWidth*0.9);
	this.btn_scroll_step = Math.round(this.clientWidth/32);

	MM_dragLayer(scroll_bar_name,'',0,0,0,0,true,true,0,0,0,this.scrol_bar_clientWidth - this.scrol_bar_width,false,false,0,'',false,'f_drag_x(newLeft, newTop, '+this.my_name+')');

}

function capture_eventsx(){

	var i,x,a=capture_eventsx.arguments;

	if(!NS){
		var tmp =
			this.my_name+".autoScroll = false;\n"+
			this.my_name+".Xscroll_drag = false;\n"+
			"if(event.keyCode==39) "+this.my_name+".Xscroll("+this.my_name+".key_scroll_step);\n"+ //40 strelka na diasno
			"if(event.keyCode==37) "+this.my_name+".Xscroll(-"+this.my_name+".key_scroll_step);\n"+ //38 strelka liavo
			"if(event.keyCode==34) "+this.my_name+".Xscroll("+this.my_name+".page_scroll_step);\n"+ //34 PageDown
			"if(event.keyCode==33) "+this.my_name+".Xscroll(-"+this.my_name+".page_scroll_step);\n"+ //33 PageUp
			"if(event.wheelDelta!=0) "+this.my_name+".Xscroll("+this.my_name+".key_scroll_step*(-event.wheelDelta/120));\n";


    for(i=0; i<a.length; i++)
    	if( x=MM_findObj(a[i]) ){
    		x.onkeydown = x.onmousewheel = function(){
					event.returnValue = false;
					eval(tmp);
				};
    	}
	}
}

function scroll_left(){
	this.autoScroll = true;
	this.Xscroll_drag = false;
	this.Xscroll(-this.btn_scroll_step);
}

function scroll_right(){

	this.autoScroll = true;
	this.Xscroll_drag = false;
	this.Xscroll(this.btn_scroll_step);
}

function Xscroll(offset){
	if( (this.clip_left+offset) <= 0) offset = 0 - this.clip_left;
	if( (this.clip_right+offset) >= this.scrollWidth) offset = this.scrollWidth - this.clip_right;

	this.clip_left += offset;
	this.clip_right += offset;

	if(!this.Xbody_drag) move_layer_by(this.body, -offset, 0);

	if (NS) {
		this.body.clip.left = this.clip_left;
		this.body.clip.right = this.clip_right;
	} else {
  	this.body.style.clip ='rect(' + this.clip_top + ' ' + this.clip_right + ' ' + this.clip_bottom + ' ' + this.clip_left + ')';
  }

  if(!this.Xscroll_drag) move_layer_to_x(this.scroll_bar, this.clip_left*this.k1);

	if(this.autoScroll) this.timer1 = setTimeout(this.my_name+'.Xscroll('+offset+')', 50);
}