var d=document;
function get(id){
	return d.getElementById(id);
}

var isShownFeedbackConfirm=false;
var intervalFeedbackConfirm=0;
var countFeedbackConfirm=0;
var animFeedbackSteps=12;
function animFeedbackConfirm(){
	countFeedbackConfirm++;
	var w=((Screen.width-400)/animFeedbackSteps)*(animFeedbackSteps-countFeedbackConfirm)+400;
	var h=((Screen.height-100)/animFeedbackSteps)*(animFeedbackSteps-countFeedbackConfirm)+100;
	var a=(.50/animFeedbackSteps)*countFeedbackConfirm;
	var s=document.body.scrollTop;
	var c=d.getElementById("feedbackConfirm");
	c.style.left=Screen.width/2-w/2;
	c.style.top=s+Screen.height/2-h/2;
	c.style.width=w+"px";
	c.style.height=h+"px";
	var f=d.getElementById("fade");
	if(countFeedbackConfirm>=animFeedbackSteps){
		clearInterval(intervalFeedbackConfirm);
		intervalFeedbackConfirm=0;
		c.className="done";
		CSS.setOpacity(c,1);
		CSS.setOpacity(f,0.6);
		setTimeout("closeFeedbackConfirm()",3500);
	}
	else {
		CSS.setOpacity(c,a);
		CSS.setOpacity(f,a+0.1);
	}
}
function closeFeedbackConfirm(){
	var d=document;
	var c=d.getElementById("feedbackConfirm");
	var f=d.getElementById("fade");
	c.style.display="none";
	f.style.display="none";
	isShownFeedbackConfirm=false;
}
function feedbackShowForm(){
	var d=document;
	d.getElementById("feedbackForm").style.display="block";
}
function feedbackFormSubmit(f){
	/*if(f.name.value==""){
		alert("Пожалуйста, укажите Ваше имя.");
		f.name.focus();
		return false;
	}
	if(f.email.value==""){
		alert("Пожалуйста, укажите Ваш e-mail.");
		f.email.focus();
		return false;
	}*/
	if(f.msg.value==""){
		alert("Пожалуйста, введите Ваше сообщение.");
		f.msg.focus();
		return false;
	}
	var d=document;
	var p=d.getElementById("feedback");
	var l=d.getElementById("feedbackLoading");
	l.style.top="0";
	l.style.left="0";
	l.style.width="100%";
	l.style.height=(p.offsetHeight-2)+"px";
	l.style.display="block";
	l.style.paddingTop=Math.round((p.offsetHeight-2)/2-8)+"px";
	var ajax=new Ajax();
	var r="isInstant=1"
		+"&url="+encodeURI(f.url.value)
		+"&h1="+encodeURI(f.h1.value)
		+"&name="+encodeURI(f.name.value)
		+"&email="+encodeURI(f.email.value)
		+"&msg="+encodeURI(f.msg.value);
	ajax.onResponse=function(x){
		//f.reset();
		d.getElementById("feedbackLoading").style.display="none";
		d.getElementById("feedbackForm").style.display="none";
		//alert("Спасибо!\nВаше мнение очень важно для нас!");
		isShownFeedbackConfirm=true;
		var s=document.body.scrollTop;
		var c=d.getElementById("feedbackConfirm");
		intervalFeedbackConfirm=setInterval("animFeedbackConfirm()",25);
		c.style.display="block";
		c.style.top=s;
		c.style.left=0;
		c.style.width=Screen.width+"px";
		c.style.height=Screen.height+"px";
		CSS.setOpacity(c,0);

		var f=d.getElementById("fade");
		f.style.display="block";
		f.style.top=s;
		f.style.left=0;
		f.style.width=Screen.width+"px";
		f.style.height=Screen.height+"px";
		CSS.setOpacity(f,0);
	}
	ajax.send("/InstantMsg.html",r);//?rnd"+Math.random());
	return false;
}

var PublicPage={
	lastMouseX:0
	,lastMouseY:0
	,decoratedClick:null

	,init:function(){
		BrowserDetect.init();

		PublicPage.onWResize();
		onEvent(window,"resize",PublicPage.onWResize);
		onEvent(window,"scroll",PublicPage.onWScroll);

		var all=d.getElementsByTagName("div");
		for(var j=0;j<all.length;j++){
			var el=all[j];
			if(!el.className)continue;
			if(el.className.indexOf("decoratedClickItem")==-1)continue;
			eventOn(el,"mousemove",PublicPage.moveDecoratedClick);
			eventOn(el,"mouseover",PublicPage.showDecoratedClick);
			eventOn(el,"mouseout",PublicPage.hideDecoratedClick);
		}
	}
	,moveDecoratedClick:function(e){
		if(!PublicPage.decoratedClick)return;
		var el=eventTarget(e);
		if(BrowserDetect.browser=="Explorer"){
			var x=event.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
			var y=event.clientY+document.body.scrollTop+document.documentElement.scrollTop;
			if(y>101)y-=101;// IE bug around div.contentBoxOver{border-top:101px}
		}else{
			if(!e){
				var x=PublicPage.lastMouseX;
				var y=PublicPage.lastMouseY;
			}
			else{
				var x=e.pageX;
				var y=e.pageY;
			}
			PublicPage.lastMouseX=x;
			PublicPage.lastMouseY=y;
		}  
		if (x<0)x=0;
		if (y<0)y=0;
		x-=Screen.absOffset(el,"offsetLeft");
		y-=Screen.absOffset(el,"offsetTop");

		if(y<el.offsetHeight-PublicPage.decoratedClick.offsetHeight)PublicPage.decoratedClick.style.top=(y)+"px";
		else PublicPage.decoratedClick.style.top=(y-24)+"px";
		
		if(x<el.offsetWidth-PublicPage.decoratedClick.offsetWidth)PublicPage.decoratedClick.style.left=(x+12)+"px";
		else PublicPage.decoratedClick.style.left=(x-108)+"px";
	}
	,showDecoratedClick:function(e){
		var el=eventTarget(e);
		///if(PublicPage.decoratedClick)el.removeChild(PublicPage.decoratedClick);
		var c=d.createElement('div');
		el.appendChild(c);
		c.className="decoratedClick";
		PublicPage.decoratedClick=c;
		PublicPage.moveDecoratedClick(e);
	}
	,hideDecoratedClick:function(e){
		var el=eventTarget(e);
		var children=el.getElementsByTagName("div");
		while(children.length>0){
			el.removeChild(children[0]);
		}
	}
}
PublicPage.onWResize=function(){
	Screen.getSize();
	var x=document.body;
	if(Screen.width<1200)x.className="w1024";
	else if(Screen.width<=1500)x.className="w1280";
	else x.className="w1680";

	if(isShownFeedbackConfirm){
		var d=document;
		var c=d.getElementById("feedbackConfirm");
		var s=document.body.scrollTop;
		c.style.top=s+Screen.height/2-50;
		c.style.left=Screen.width/2-200;
		c.style.width="400px";
		c.style.height="100px";
		var f=d.getElementById("fade");
		f.style.top=s;
		f.style.left=0;
		f.style.width=Screen.width+"px";
		f.style.height=Screen.height+"px";
	}

	PublicPage.placeCallRequestForm();
}
PublicPage.onWScroll=function(){
	if(isShownFeedbackConfirm){
		// pos feedback confirm:
		var s=document.body.scrollTop;
		var d=document;
		var c=d.getElementById("feedbackConfirm");
		c.style.top=s+Screen.height/2-50;
		var f=d.getElementById("fade");
		f.style.top=s;
		f.style.left=0;
		f.style.width=Screen.width+"px";
		f.style.height=Screen.height+"px";
	}

	PublicPage.placeCallRequestForm();
}
PublicPage.showCallRequestForm=function(){
	// show form:
	PublicPage.isShownCallRequestForm=true;
	var p=d.getElementById("callRequestForm");
	p.style.display="block";
	var p=d.getElementById("callRequestFormMain");
	p.style.display="block";
	// show fade:
	var p=d.getElementById("fade");
	p.style.display="block";
	// pos form & fade:
	PublicPage.placeCallRequestForm();
}
PublicPage.closeCallRequestForm=function(ccpId){
	// hide form:
	PublicPage.isShownCallRequestForm=false;
	var p=d.getElementById("callRequestForm");
	p.style.display="none";
	// hide fade:
	var p=d.getElementById("fade");
	p.style.display="none";
}
PublicPage.placeCallRequestForm=function(ccpId){
	if(!PublicPage.isShownCallRequestForm)return;

	Screen.getSize();
	Screen.getScroll();

	var c=d.getElementById("callRequestForm");
	var s=Screen.scrollTop;
	//alert(s+" : "+Screen.height/2 + " : "+ c.offsetHeight);
	c.style.top=Math.round(s+Screen.height/2-c.offsetHeight/2)+"px";
	c.style.left=Math.round(Screen.width/2-c.offsetWidth/2)+"px";
	var f=d.getElementById("fade");
	f.style.top=s+"px";
	f.style.left=0;
	f.style.width=Screen.width+"px";
	f.style.height=Screen.height+"px";
}
PublicPage.submitCallRequestForm=function(f){
	if(f.phone.value==""){
		alert("Пожалуйста, укажите Ваш телефон.");
		f.phone.focus();
		return false;
	}
	var p=d.getElementById("callRequestFormMain");
	p.style.display="none";
	var l=d.getElementById("callRequestFormLoading");
	l.style.display="block";
	var ajax=new Ajax();
	var r="name="+encodeURI(f.name.value)
		+"&phone="+encodeURI(f.phone.value)
		+"&msg="+encodeURI(f.msg.value);
	ajax.onResponse=function(x){
		//f.reset();
		d.getElementById("callRequestFormLoading").style.display="none";
		d.getElementById("callRequestForm").style.display="none";
		d.getElementById("fade").style.display="none";
		var r=eval("("+x.responseText+")");
		if(r.status)alert("Спасибо!\nНаш менеджер свяжется с Вами в ближайшее время!");
		PublicPage.isShownCallRequestForm=false;
	}
	ajax.send("/PublicPage.json",r);//?rnd"+Math.random());
	return false;
}
onReadys.push(PublicPage.init);

