/*
	© Arseniy's lab
	jArs version 2.0
*/
var gb={};
// стандартные функции
function $(n){return document.getElementById(n)}
Function.prototype.bind=function(object){var __method=this;return function(){return __method.apply(object,arguments);}}
window.ie=!!window.ActiveXObject;

// плавная прокрутка по вертикали
var scrl=function() {
	var from,to,timer,e,startTime,duration;
	return{
		t:function(){return window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0},
		l:function(){return window.pageXOffset||document.documentElement.scrollLeft||document.body.scrollLeft||0},
		to:function(nm,millisec) {
			duration=millisec;
			if(typeof nm=='string') {
				e=$(nm);
				to=0;
				while(e){to+=e.offsetTop;e=e.offsetParent;}
			}
			else
			to=nm;
			from=scrl.t();
			startTime=(new Date).getTime();
			clearInterval(timer);
			timer=setInterval(this.on.bind(this), 20);
			return false;
		},
		on:function() {
			var time = (new Date).getTime(),wo;
			if (time >= duration+startTime) {
				clearInterval(timer);
				scroll(scrl.l(), to);				
			}
			else
				scroll(scrl.l(),from + -(Math.cos(Math.PI*(time-startTime)/duration)-1)/2*(to-from));
		}
	}
}();

// анимация + ajax
var anim=function(el){
	this.gotresponse=0;
	this.duration=500;
	this.el=$(el);
}
anim.prototype={
	ajax:function(pth)	{
		if(this.timer!=null)return 0;
		new ajax(pth,
			function(res){
				if(this.timer)this.gotresponse=res
				else {this.el.style.height='0px';this.el.innerHTML=res;this.custom(0,this.el.scrollHeight);}
			}.bind(this)
		);
		this.el.offsetHeight>0?this.custom(this.el.offsetHeight,0):this.custom(0,this.el.scrollHeight);
	},
	setOpacity:function(opacity){
		window.ie?
			(this.el.style.filter=opacity==1?"":"alpha(opacity="+Math.round(opacity*100)+")")
			:
			(this.el.style.opacity=opacity);
		this.el.style.visibility=opacity==0?"hidden":"visible"
	},
	custom:function(from,to){
		this.from=from;
		this.to=to;
		this.startTime=(new Date).getTime();
		this.timer=setInterval(this.step.bind(this), 30);
	},
	step:function(){
		var time=(new Date).getTime(),wo;
		if(time>=this.duration+this.startTime){
			this.now=this.to;
			clearInterval(this.timer);
			this.timer=null;
			// закончили анимацию
			if(this.gotresponse){
				this.el.innerHTML=this.gotresponse;
				this.gotresponse=null;
				this.custom(0, this.el.scrollHeight);
				return;
			}
			// закончили анимацию, а аякс нам ещё не ответил
			else if(this.now==0){
				this.el.innerHTML='<img src="http://online.garant-broker.ru/i/clocks.gif" width="32" height="32">'
				this.el.height='32px';
				this.now=32;
				this.setOpacity(0);
			}
			this.setOpacity(1);
		}
		else{
			wo=(-Math.cos((time-this.startTime)/(this.duration)*Math.PI)/2)+0.5;
			this.now=Math.round(wo*(this.to-this.from)+this.from);
			this.setOpacity(this.now?(this.to?wo:(1-wo)):0);
		}
		this.el.style.height=this.now+"px";
	}
}

function getXHR(){return window.ie?new ActiveXObject("MSXML2.XMLHTTP"):(window.XMLHttpRequest?new XMLHttpRequest():null);}

// url, callback, data
function ajax(u,f,d){
	var r=getXHR();
	if(!r){
		alert("Ваш обозреватель интернета не поддерживает ajax");return;
	}
	r.onreadystatechange=function(){
		if(r.readyState==4)	{
			f(r.status==200?r.responseText:"<div style='font-size:1.3em;color:red;margin:3em 0'>Ошибка при исполнии запроса ("+r.status+")</div>");
			r.abort();
			r=null;
		}
	}
	r.open(d?'POST':'GET',u,true);
	r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=windows-1251");
	r.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	r.send(d||null);
}


function path(p){
	var s=document.location.pathname.match(/(\/\w+\/\w+\/)/);
	return '/ajax2'+(s?s[0]:'')+(p?(p+'/'):'');
}

window.onload=function(){
	gb.left=new anim('info');
	gb.right=new anim('history');
}

function get_data(n,ind){
	window.scrollTo(0,0);
	gb.left.ajax(path(n));
	if(ind)
		gb.right.ajax(path('index/'+n));
	return false;
}

function get_data4(n){
	window.scrollTo(0,0);
	gb.right.ajax(path('index/'+n));
	return false;
}
function show(o){o.className=o.className.replace(' dn','');}
function hide(o){o.className+=' dn'}
function resize(n){$(n).style.height='auto';/*$(n).scrollHeight+"px";*/}
function get_sdel(obj,n){
	var buf=obj.parentNode.parentNode;
	while(buf.nextSibling){
		if(buf.nodeName=="TR" && buf.className.indexOf('sdel')!=-1)
			break;
		buf=buf.nextSibling;
	}
	if(buf){
		var td=buf.cells[0];
		if(!td.innerHTML.length){
			td.innerHTML='<b>загрузка...</b>';
			td.colSpan=4;
			show(buf);
			new ajax('/ajax2/history/sdelki/'+n+'/',function(res){td.innerHTML='<div style="margin-left:90px">'+res+'</div>';resize('history');});
			obj.innerHTML='Сделки &uarr;';
		}
		else {
			if(buf.className.indexOf('dn')!=-1){show(buf);obj.innerHTML='Сделки &uarr;';}
			else{hide(buf);obj.innerHTML='Сделки &darr;';}
		}
	}
	return false;
}
// кнопка 'i'
function info(a,b,c){
	var ret; // true - делали show(), false hide()
	for(var i=0,items=document.getElementsByTagName(a),cnt=items.length;i<cnt;i++)
		with(items[i])
			if(className.indexOf(b)!=-1)
				if(className.indexOf(' dn')==-1){hide(items[i]);ret=false}
				else{show(items[i]);ret=true}
	if(c)resize(c);
	return ret;
}
// описание сделки в индексе
function sx(o){
	var a=o.parentNode.rows[o.rowIndex+1];
	a.className.indexOf(' dn')!=-1?show(a):hide(a);
}
// В рот мне ноги! Еманарод! Печеньки!
function setCookie(c_name,value){
	var dt=new Date();
	dt.setFullYear(dt.getFullYear()+1);// храним куку 1 год :)
	document.cookie=c_name+"="+escape(value)+";path=/;expires="+dt.toGMTString();
}

function getCookie(c_name){
	var dc=document.cookie;
	c_start=dc.indexOf(c_name+"=");
	if(c_start!=-1){ 
		c_start+=c_name.length+1; 
		c_end=dc.indexOf(";",c_start);
		if(c_end==-1)c_end=dc.length;
		return unescape(dc.substring(c_start,c_end));
	}
	return null;
}
// увеличение графика
function doexpand(d){
	var o=$('chart');	
	if(o.className==''){
		o.className='expanded_chart';
		d.innerHTML='&rarr;Уже&larr;';
	}
	else{
		o.className='';
		d.innerHTML='&larr;Шире&rarr;';
	}
}