Function.prototype.bind = function(object) {
    var method = this
    return function() {
        return method.apply(object, arguments) 
    }
}

function VPA_vscroller(name,current_cell_index,show_cells,smooth)
{
	this.singleton_name=name;
	this.name=name;
	this.show_cells=show_cells;
	this.offset=0;
	this.smooth=smooth;
	this.timer=null;
	this.velocity=3;
	this.obj=document.getElementById(this.name);
	if (!this.obj) return false;
	this.scroll_obj=this.obj.getElementsByTagName('table')[0];
	this.scroll_obj_left=this.scroll_obj.offsetLeft;
	this.cells=this.scroll_obj.rows[0].cells.length;
	this.cell_first=this.scroll_obj.rows[0].cells[0];
	this.cell_last=(this.cells>0) ? this.scroll_obj.rows[0].cells[this.cells-1] : 0;
	this.current_cell_index=current_cell_index;
	this.cell_current=this.scroll_obj.rows[0].cells[this.current_cell_index];
	this.left_link_cell=this.obj.rows[0].cells[0];
	this.left_link=this.obj.rows[0].cells[0].getElementsByTagName('a')[0];
	this.left_link.setAttribute('obj', this.singleton_name);
	this.right_link_cell=this.obj.rows[0].cells[2];
	this.right_link=this.obj.rows[0].cells[2].getElementsByTagName('a')[0];
	this.right_link.setAttribute('obj', this.singleton_name);
	if (this.cells<=show_cells)
	{
		this.left_link_cell.style.visibility='hidden';
		this.right_link_cell.style.visibility='hidden';
	}
	
	this.set_cell=function (cell)
	{
		
		if (cell>=0 && this.cells-cell>=this.show_cells)
		{
			this.current_cell_index=cell;
			this.cell_current=this.scroll_obj.rows[0].cells[this.current_cell_index];
			this.right_link_cell.style.visibility=(this.current_cell_index+this.show_cells==this.cells) ? 'hidden' : 'visible';
			this.left_link_cell.style.visibility=(this.cell_current==this.cell_first) ? 'hidden' : 'visible';
			this.left=-this.cell_current.offsetLeft;
			this.offset=this.scroll_obj.offsetLeft;
			this.slow_motion(Math.abs(this.offset-this.left)/2);
		}
	}
	

	this.slow_motion=function (start_delta)
	{
		this.start_delta=start_delta;
		this.timer=window.setInterval(this.tick.bind(this),5);
	}
	
	this.tick=function()
	{
		if (this.offset!=this.left)
		{
			var dl=this.offset-this.left;
			if (this.smooth)
			{
				var delta=Math.abs(dl);
				this.velocity+=delta>this.start_delta ? 1 : -1;
			}
			var desc=(dl>=0) ? this.velocity : -this.velocity;
		
			this.offset-=desc;
			if ((desc>0 && this.offset<this.left) || (desc<0 && this.offset>this.left))
			{
				this.offset=this.left;
			}
			this.scroll_obj.style.left=this.offset+'px';
		}
		else
		{
			this.scroll_obj.style.left=this.offset+'px';
			window.clearInterval(this.timer);
		}
	}

	this.left_link.onclick=function ()
	{
		this.set_cell(this.current_cell_index-1);
	}.bind(this)

	this.right_link.onclick=function ()
	{
		this.set_cell(this.current_cell_index+1);
	}.bind(this)

	this.set_cell(this.current_cell_index);
}

VPA_vscroller.prototype.toString=function() { return 'Object VPA_vscroller\n\nDeveloped by Andrey Pahomov (andrey.pahomov@gmail.com)';};

/**
* obj - сворачиваемый/разворачиваемый объект
* swither - объект-переключатель, при клике по которому происходит действие (картинка)
* min_height - минимальная высота объекта
* max_height - максимальная высота объекта
* switch_ico - иконка включенного положения
* key - если несколько менюшек - то при открытии надо хакрывать уже открытые, поэтому key - порядковый номер текушей менюшки
**/
function vpa_dropdown(obj,min_height,max_height,key)
{
	this.obj=obj;
	this.min_height=min_height;
	this.max_height=max_height;
	this.status=0;
	this.key=key;
	this.scroller;
	this.timer=0;
	this.off=0;
	this.velocity=0;
	this.switch_off='';
	
	this.init=function (switcher,switch_ico) {
		this.switcher=switcher;
		this.switch_on=switch_ico;
		var dm_head=this.switcher;
		this.switch_off=this.switcher.src;
		this.scroller=this.obj.getElementsByTagName('div')[0];
		dm_head.onclick=function() {
			this.start();
		}.bind(this);
	}
	
	this.start=function()
	{
		if (!this.timer)
		{
			if (this.status==0)
			{
				if (this.switcher) this.switcher.src=this.switch_on;
				if (window['current_mn']!=-1 && window['current_mn']!=this.key)
				{
					if (window.mns[window['current_mn']].status==1)
					{
						window.mns[window['current_mn']].start();
					}
				}
				window['current_mn']=this.key;
			}
			this.obj.parentNode.style.display='';
			this.velocity=1;
			this.off=this.obj.offsetHeight;
			this.timer=window.setInterval(this.motion.bind(this),10);
		}
		else this.stop();
	}
	
	
	this.motion=function()
	{
		//var delta=(this.max_height-this.off)/this.min_height/6;
		var delta=1;
		this.velocity+=delta;
		this.velocity=this.velocity>0 ? this.velocity : 1;
		
		if (this.status==0)
		{
			if (this.off<=this.max_height)
			{
				this.off=this.off+this.velocity;
				this.obj.style.height=(this.off<this.max_height ? this.off : this.max_height)+'px';
			}
			else
				this.stop();
		}
		else if (this.status==1)
		{
			if (this.off>=this.min_height)
			{
				this.off=this.off-this.velocity;
				this.obj.style.height=(this.off>this.min_height ? this.off : this.min_height)+'px';
			}
			else
				this.stop();
		}
	}
	
	this.stop=function()
	{
		this.obj.style.height=(this.status==0 ? this.max_height : min_height)+'px';
		this.status=1-this.status;
		this.timer=window.clearTimeout(this.timer);
		//this.obj.parentNode.style.display=(this.status==1)	'' : 'none';
		if (this.switcher) this.switcher.src=(this.status) ? this.switch_on : this.switch_off;
	}
	
}

vpa_dropdown.prototype.toString=function() { return 'Object VPA_dropdown \n\nDeveloped by Andrey Pahomov (andrey.pahomov@gmail.com)';};

function init_banners()
{
 var scr=new VPA_vscroller('scroller',0,1,true);
}

function init_galls()
{
	window['mns']=Array();
	window['current_mn']=-1;
	var ex_gal=new VPA_vscroller('new_gallery',0,4,true);
	var v_exd=document.getElementById('new_dropdown');
	window.mns[0]=new vpa_dropdown(v_exd,0,360,0);
}

function init_info()
{
	window['mns']=Array();
	window['current_mn']=-1;
	var used=document.getElementById('used_info');
	var switcher=null;
	var obj=null;
	var k=0;
	for (var i=0;i<used.rows.length;i++)
	{
		var cells=used.rows[i].cells;
		if(cells.length==7)
		{
			var im=cells[6].getElementsByTagName('img')[0];
			if (im.src.indexOf('bk_o')!=-1)
			{
				switcher=im;
			}
		}
		if(cells.length==1)
		{
			var obj=cells[0].getElementsByTagName('div')[0];
			window.mns[k]=new vpa_dropdown(obj,0,317,k);
			window.mns[k].init(switcher,'/i/bk_on.gif');
			init_used_gal(obj);
			k++;
		}
	}
}

function init_used_gal(obj)
{
	var tb=obj.getElementsByTagName('table')[0];
	var gal_id=tb.id;
	var c_gal=new VPA_vscroller(gal_id,0,3,false);
}

function show_img(id,img)
{
	document.getElementById(id).src='/upload/'+img;
}

function open_gal(obj_id,img,alt)
{
	var obj=window.mns[obj_id];
	var im=document.getElementById('ex_gal_image');
	image_scroll(im,im.src,'/img/new_big/'+img,alt);
	obj.status=0;
	obj.start();
}

function image_scroll(obj,old_img,new_img,alt)
{
	this.old_obj=obj;
	this.root=obj.parentNode;
	var imgs=root.getElementsByTagName('img');
	if (imgs.length==2)
	{
		this.root.removeChild(imgs[1]);
	}
	this.new_obj=new Image;
	this.new_obj.src=new_img;
	this.old_obj.style.position='absolute';
	this.old_obj.style.top='0px';
	this.old_obj.style.left='0px';
	this.old_obj.style.zIndex=0;
	this.root.appendChild(this.new_obj);
	this.new_obj.style.position='absolute';
	this.new_obj.style.top='0px';
	this.new_obj.style.left=this.old_obj.offsetWidth+'px';
	this.new_obj.style.zIndex=20;
	this.old_img=old_img;
	this.new_img=new_img;
	this.new_left=this.old_obj.offsetWidth;
	this.old_left=0;
	this.timer=0;
	this.step=1;
	this.delta=this.new_left/2;
	this.alt=alt;
	
	this.start=function()
	{
		this.timer=window.setInterval(this.motion.bind(this),10);
		var o_alt=document.getElementById('alt');
		o_alt.style.display='none';
	}
	
	this.motion=function()
	{
		if (this.new_left>0)
		{
			this.step+=(this.new_left>this.delta) ? 1 : -1;
			this.old_left-=this.step;
			this.new_left=(this.new_left>this.step) ? this.new_left-this.step : 0;
			this.old_obj.style.left=this.old_left+'px';
			this.new_obj.style.left=this.new_left+'px';
		}
		else
		{
			this.stop();
		}
	}
	
	this.stop=function()
	{
		this.timer=window.clearTimeout(this.timer);
		this.old_obj.src=this.new_obj.src;
		this.old_obj.style.left='0px';
		this.new_obj.style.display='none';
		var o_alt=document.getElementById('alt');
		o_alt.style.display='block';
		o_alt.innerHTML='';
		o_alt.appendChild(document.createTextNode(alt));
	}
	
	this.start();
}