function Slider(id)
{
	this.cont=$(id);
	this.moved=null;
	this._t=null;
	this.a=0.01;//ускорение(%от this.bwidth)
	this.dt=20;//интервал
	this.bwidth=100;
	this.items = new Array();
	this._o0=0; 
	this._o1=3;
	this._nb=0;//нужная позиция 
	this._cv=0;//текушая скорость
	this._cb=0;//текущая позицыя
	this._c0=0;//центральная точка (в которой происходит разварот ускорения)
	this._stoped=true;
	this._movitems=new Array();
	this.GetItem = function(i)
	{
		var n=this.items.length;
		if(n==0)return'';
		if(i<0)while(i<0)i=i+n;
		else i=i%n;
		return this.items[i];
	}
	this.resizeWindow = function()
	{
		this.bwidth = (document.body.clientWidth-160)/4;
		if(this.moved==null)return;
		var n=this._movitems.length;
		this.moved.style.width=(n*this.bwidth)+'px';
		for(var i=0;i<n;i++)
		{
			this._movitems[i].style.width=this.bwidth+'px';
		}
	};
	this.NextFrame = function()
	{
		//проверка в какую сторону изменять скорость
		
		if(this._cb<this._c0)this._cv=this._cv+this.a;
		else this._cv=this._cv-this.a;
		if((this._cv<this.a)&&(this._cv>-this.a))//приближаемся к точке и скорость упала до нуля
		{
			if(this._cb<this._nb)this._cv=this.a;
			else this._cv=-this.a;
		}
		var nx = this._cb+this._cv;
		if(((nx>=this._nb)&&(this._nb>this._cb))||(nx<=this._nb)&&(this._nb<this._cb))
		{
			this._cb=this._nb;
			this.Stop();
		}else
		{
			this._cb=nx;
		}
		if(this.moved==null)return;
		this.moved.style.left = -(this.bwidth*(this._cb-this._o0))+'px';
	};
	this.BuildStoped = function()
	{
		var h='';
		for(var i=0;i<4;i++)h=h+'<div class="contbox">'+this.GetItem(i)+'</div> ';
		this.cont.set('html',h);
		this.moved=null;
		initLightbox();
	};
	this.BuildMooved = function(min,max)
	{
		var h='';
		var id=this.cont.get('id')+'_move';
		for(var i=min;i<=max;i++)h=h+'<div class="contbox_move">'+this.GetItem(i)+'</div> ';
		if(this.moved==null)
		{
			this.cont.set('html','<div style="width:100%;overflow:hidden;position:relative;"><div id="'+id+'" style="left:'+(this.bwidth*this._o0)+'px">'+h+'</div></div>');
			this.moved = $(id);
		}else
		{
			this.moved.set('html',h);
			this.moved.style.left = -(this.bwidth*(this._cb-this._o0))+'px';
		}
		this._movitems = $$('#'+id+' div.contbox_move');
		this.resizeWindow();
		initLightbox();
	};
	this.LoadData = function(id)
	{
		var e = $$('#'+id+' div.contbox');
		var n = e.length;
		while(this.items.length!=0)this.items.pop();
		for(var i=0;i<n;i++)
		{
			//$(e[i]);//на всяк случай для ие
			this.items.push(e[i].get('html'));
		}
		this.Stop();
	}
	this.LoadData2 = function(arr)
	{
		this.items = arr;
		this.Stop();
	}
	this.Stop = function()
	{
		this._stoped=true;
		$clear(this._t);
		var n = this._nb+this.items.length;
		var a=new Array();
		for(var i=this._nb;i<n;i++)a.push(this.GetItem(i));
		this.items=a;
		this.BuildStoped();
		this._movitems.empty();
		this._o0=0; 
		this._o1=3;
		this._nb=0;
		this._cv=0;
		this._cb=0;
		this._c0=0;
	};
	this.Start = function()
	{
		this._t=this.NextFrame.periodical(this.dt,this);
	};
	this.Next = function()
	{
		this._nb++;
		if(this._nb>0)
		{
			this._c0 = (this._nb+this._cb-this._cv*this._cv/(2*this.a))/2
		}else
		{
			this._c0 = (this._nb+this._cb+this._cv*this._cv/(2*this.a))/2
		}
		this._o1++;
		this.BuildMooved(this._o0,this._o1);
		if(this._stoped)
		{
			this._stoped=false;
			this.Start();
		}
	};
	this.Prev = function()
	{
		this._nb--;
		if(this._nb>0)
		{
			this._c0 = (this._nb+this._cb-this._cv*this._cv/(2*this.a))/2
		}else
		{
			this._c0 = (this._nb+this._cb+this._cv*this._cv/(2*this.a))/2
		}
		this._o0--;
		this.BuildMooved(this._o0,this._o1);
		if(this._stoped)
		{
			this._stoped=false;
			this.Start();
		}
	};
	
}
function Droper(id)
{
	this.id=id;
	this.e=$(id);
	this.v=20;
	this.pos=0;
	this._t=null;
	this.c1=null;//Show complit
	this.c2=null;//Hide complit
	this.d=0;
	this.Frame=function()
	{
//		var t=self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
		var n=this.pos+this.d*this.v;
		if(this.d>0)
		{
			this.e.style.display='';
			this.e.style.height=n+'px';
			if(n>this.innerH)
			{
				this.pos=this.innerH
				$clear(this._t);
				if(this.c1!=null)this.c1();
			}else
			{
				this.pos=n;
			}
			this.e.style.height=this.pos+'px';
		}else
		{
			if(n<=0)
			{
				this.e.style.display='none';
				this.pos=0;
				$clear(this._t);
				if(this.c2!=null)this.c2();
			}else
			{
				this.e.style.height=n+'px';
				this.pos=n;
			}
		}
//		window.scrollTo(0,t);
	};
	this.Show=function()
	{
		this.d=1;
		if(this._t!=null)$clear(this._t);
		this._t=this.Frame.periodical(20,this);
	};
	this.Hide=function()
	{
		this.d=-1;
		if(this._t!=null)$clear(this._t);
		this._t=this.Frame.periodical(20,this);
	};
	this.getInnerH=function()
	{
		var n=this.e.childNodes.length
		var ret=0;
		for(var i=0;i<n;i++)
		{
			if(this.e.childNodes[i].offsetHeight!=null)
			ret=ret+this.e.childNodes[i].offsetHeight;
		}
		return ret;
	}
	this.e.style.display='';
	this.innerH = this.e.offsetHeight;
	this.e.style.display='none';
	this.e.style.overflow='hidden';
}
function mousePageXY(e)
{
  var x = 0, y = 0;

  if (!e) e = window.event;

  if (e.pageX || e.pageY)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }

  return {"x":x, "y":y};
}
function blockEvent(event)
{
    if (!event)
    {
        event = window.event;
    }
    if(event.stopPropagation) event.stopPropagation();
    else event.cancelBubble = true;
    if(event.preventDefault) event.preventDefault();
    else event.returnValue = false;
}
function wheel(e)
{
	var y=0;
	if(!e)e=window.event;
	if(e.wheelDelta) y=-e.wheelDelta/120;
	else if(e.detail)y=e.detail/3;
	if(this.sb!=null)this.sb.onwheel(y);
	blockEvent(e);
}
function ScrollBar(id)
{
	this.scrolbar=$(id);
	this.e=null;
	this.scrolbar.style.display='none';
	this.top=0;
	this.cont=null;
	this.hs=0;
	this.hc=0;
	this.mousey=0;
	this.pres=false;
	this.rmy=0;
	this.rmt=0;
	this.Init = function(c,d)//c-контейнер, d-контент
	{
		this.cont=$(d);
		var xx1= this.cont.scrollHeight;
		var xx2=$(c);
		
		this.hc=this.cont.offsetHeight-$(c).offsetHeight;
		this.cont.style.position='relative';
		this.cont.style.top="0";
		if(this.hc<=0)
		{
			this.scrolbar.style.display='none';
			return;
		}
		this.scrolbar.style.display='';
		var e=this.scrolbar.getElements('div')[0];
		this.hs=this.scrolbar.offsetHeight-e.offsetHeight;
		e.style.position='relative';
		e.style.top=0;
		this.e=e;
		e.sb=this;
		this.cont.sb=this;
		e.onmousedown=function(){this.sb.ondown();return false;};
		//колесо
		if(this.cont.addEventListener)
		{
			this.cont.addEventListener('DOMMouseScroll', wheel, false);
		}
		this.cont.onmousewheel = wheel;
		if(window.sb==null)
		{
			window.sb=new Array();
			document.onmousemove=function(e)
			{
				var c=mousePageXY(e);
				for(var i in window.sb)
				{
					if(window.sb[i]!=null)if(window.sb[i].onup!=null)window.sb[i].onmove(c.y);
				}
				if(document.selection) document.selection.empty();
			};
			document.onmouseup=function(e)
			{
				for(var i in window.sb)
				{
					if(window.sb[i]!=null)if(window.sb[i].onup!=null)window.sb[i].onup();
				}
			}
		}
		window.sb[this.scrolbar.get('id')]=this;
	}
	this.ondown=function()
	{
		this.rmy=this.mousey;
		this.rmt=this.top;
		this.pres=true;
	}
	this.onup=function(){this.pres=false;}
	this.onmove=function(y)
	{
		this.mousey=y;
		if(!this.pres)return;
		this.top=this.rmt+y-this.rmy;
		if(this.hs<this.top)this.top=this.hs;
		if(this.top<0)this.top=0
		this.e.style.top=this.top+'px';
		var ct=this.hc*this.top/this.hs;
		this.cont.style.top=-ct+'px';
	}
	this.onwheel=function(y)
	{
		var d=50;
		var cp = -parseInt(this.cont.style.top);
		cp = cp+y*d;
		if(cp>this.hc)cp=this.hc;
		if(cp<0)cp=0;
		this.cont.style.top=-cp+'px';
		this.top=this.hs*cp/this.hc;
		this.e.style.top=this.top+'px';
	}
}
var slider=null
var sliderdata=new Array();
var sliderselect=0;
var pmod=null;
//function InitSlider()
function SelectPortfolioTab(id)
{
	if(slider==null)return;
	sliderdata[sliderselect]=slider.items;
	sliderselect=id;
	slider.LoadData2(sliderdata[id]);
	var e=$$('ul.tabs li')
	if(id==0)e[0].className="current";
	else e[0].className="";
	if(id==1)e[1].className="current";
	else e[1].className="two";
	if(id==2)e[2].className="current";
	else e[2].className="";
}
function LoadPortfolio()
{
	var req = new Request.HTML({
		method: 'get',
		url: '/ajax_portfolio.php',
		onRequest: function() {  },
		onComplete: function(response) 
		{ 
			slider = new Slider('content');
			sliderdata[0]=new Array();
			sliderdata[1]=new Array();
			sliderdata[2]=new Array();
			
			var k,a,n;
			for(i in response)
			{
				switch(response[i].id)
				{
					case 'sliderdata_1':k=0;break;
					case 'sliderdata_2':k=1;break;
					case 'sliderdata_3':k=2;break;
					default: k=-1;
				}
				if(k!=-1)
				{
					a=response[i].getElements('div.contbox');
					n=a.length;
					for(var j=0;j<n;j++)
					{
						//$(e[i]);//на всяк случай для ие
						sliderdata[k].push(a[j].get('html'));
					}
				}
				
			}
			slider.LoadData2(sliderdata[0]);
			slider.resizeWindow();
			window.addEvent('resize', function(){slider.resizeWindow()});
			$('next').onclick = function(){slider.Next();return false;}
			$('prev').onclick = function(){slider.Prev();return false;}
		}
	}).send();
}
function showPortfolio()
{
	if(pmod==null)pmod=new Droper('portfolio');
	pmod.Show();
	pmod.c1=function(){$('shporfolio_link').onclick=function(){pmod.Hide()};};
	pmod.c2=function(){$('shporfolio_link').onclick=function(){pmod.Show()};};
}
window.addEvent('domready', LoadPortfolio );
var catlis=null;
function ShowPortfolioCat()
{
	if(catlis==null) 
	{
		var d=$('direction_list');
		var s=new ScrollBar('dir_scrol');
		d.style.display='';
		s.Init('dir_sub_c','dir_sub_d');
		d.style.display='none';
		catlis = new Droper('direction_list');
	}
	catlis.Show();
	$('directionup_a').onclick=function(){catlis.Hide()};
	catlis.c1=function(){$('directionup_a').onclick=function(){catlis.Hide()};};
	catlis.c2=function(){$('directionup_a').onclick=function(){catlis.Show()};};
	$('dir_sub_d').onclick=function(){catlis.Hide()};
}

