// JavaScript Document
function popup(id,title){
	var popup=todo.get(id),
		body=document.getElementsByTagName('body')[0];
	if(popup)return popup;
	popup=body.appendChild(todo.create('div',{'id':id,'class':'popup'},null,{'position':'fixed','left':'-100%'}));
	popup.appendChild(todo.create('input',{'type':'button','class':'close'})).onclick=function(){this.parentNode._close();};
	if(title)popup.appendChild(todo.create('h3',null,title));
	popup.appendChild(todo.create('div',{'class':'bot-bg'}));
	popup._content=popup.appendChild(todo.create('div'));
	popup._open=function(){this.style.left='50%';this._bg.style.display='block';};
	popup._close=function(){this.style.left='-100%';this._bg.style.display='none';};
	popup._bg=body.appendChild(todo.create('div',{'class':'popup-bg'},null,{'display':'none'}));
	return popup;
};
function addCSS(href){
	var h=document.getElementsByTagName('head')[0],
		l=h.getElementsByTagName('link');
	for(var i=0;i<l.length;i++)if(href==l[i].href)return;
	h.appendChild(todo.create('link',{'rel':'stylesheet','type':'text/css','href':href}));
};
function callbackPopup(){
	var p=popup('callbackPopup','Заказать звонок');
	addCSS('/callback/style.css');
	p._content.innerHTML='<form action="#"><h4>Укажите свой номер телефона</h4>'
		+'<input type="text" name="name" placeholder="ваше имя" class="placeholder"><br>'
		+'<input type="text" name="phone" placeholder="номер телефона" class="placeholder"><br>'
		+'<input type="text" name="subj" placeholder="тема" class="placeholder"><br>'
		+'<a href="javascript:void(0)" onmouseup="javascript:_gaq.push([\'_trackPageview\',\'/call\']);" class="button">Запросить звонок<span class="s1"></span><span class="s2"></span></a>'
		+'<p>или позвоните нам по телефону: +7 (495) 411-72-72</p></form>';
	todo.loop(p._content.getElementsByTagName('input'),function(){
		this.placeholder=this.placeholder||this.getAttribute('placeholder');
		if(!this.getAttribute('placeholder'))return;
		this.onfocus=function(){
			if(this.value==this.placeholder)this.value='';
			this.className=null;
		};
		this.onblur=function(){
			if(!this.value){
				this.value=this.placeholder;
				this.className='placeholder';
			}
		};
		this.onblur();
	});
	p._content.getElementsByTagName('a')[0].onclick=function(){
		var f=this.parentNode,
			p={'name':f.name.value,'phone':f.phone.value,'subj':f.subj.value},
			l=window.location,
			err=false;
		if(err=(!p.name||p.name==f.name.placeholder))alert('Введите имя');
		else if(err=!p.phone.match(/[0-9\- \+\(\)]{5,}/))alert('Введите верный номер телефона');
		if(!err){
			popup('callbackPopup')._content.innerHTML='<p style="text-align:center;"><img src="/callback/preloader.gif" alt="Загрузка..."></p>';
			todo.ajax(l.protocol+'//'+l.hostname+':'+l.port+'/callback/',function(text,xml){
				var p=popup('callbackPopup');
				if(xml)p._content.innerHTML='<p style="padding:0 5px;">'+xml.getElementsByTagName('response')[0].getAttribute('message')+'</p>';
				else p._content.innerHTML=text;
			},p,'post');
		}
	};
	p._open();
};
todo.onload(function(){
	todo._imgcache=[];
	todo.loop(['popup-bg.png','popup-bg-1.png','popup-close.png','preloader.gif'],function(i,file){
		var l=window.location;
		todo._imgcache[i]=new Image
		todo._imgcache[i].src=l.protocol+'//'+l.hostname+':'+l.port+'/callback/'+file;
	});
});
