var mBanner = {

	oGallery : null,
	numSlides : 0,
	fBuzy : false,
	shown : true,
	scrollTime : 0,
	fInterval : 5000,
	fSlideshow : 0,
	fPaused : false,
	fIndex : 0,

	create : function (oCanvas,oNavy,oScroll) {

		this.oCanvas = oCanvas;
		this.oScroll = oScroll;
		this.oBack = document.getElementById("sliderBg");
		this.oInfo = document.getElementById("sliderInfo");
		this.oGallery = new Array();
		var items = oNavy.getElementsByTagName("img");	
		for (var i = 0; i < items.length; i++) {
			this.oGallery.push(this.createSlide(items[i]));
			this.numSlides++;
		} this.maxHeight = Math.ceil(this.oGallery.length/3) * 90;
		if (this.numSlides) {
			this.oScroll.onmouseover = function (e) {
				mBanner.paneIn();
			}; this.oScroll.onmouseout = function (e) {
				mBanner.paneOut();
			}; this.paneOut();
			if (this.numSlides > 1) this.play();
		}

	},

	play : function () {

		if (this.numSlides > 1) {
			if (!this.fSlideshow) { 
				this.fSlideshow = setTimeout("mBanner.play()",this.fInterval);
			} else {
				if (this.fPaused) return;
				this.fIndex++;
				if (this.fIndex >= this.numSlides) this.fIndex = 0;
				this.select(this.fIndex);
				this.fSlideshow = setTimeout("mBanner.play()",this.fInterval);
			}			
		}

	},

	paneOut : function () {

		if (this.shown) {
			var oScroll = this.oScroll;
			var oBack = this.oBack;
			var oDst = 390 - Math.round(this.numSlides / 3) * 90;
			var oMove = 410 - oDst;
			var anim = function (a) {
				var res = Math.round(oDst + mBanner.fCubicOut(a,0,oMove,20));
				oScroll.style.top = res+'px';
				oBack.style.top = res+'px';
			}; var cBack = function () {
				oScroll.style.top = '410px';
				oBack.style.top = '410px';
				mBanner.shown = false;
			}; var cDelay = function () {
				var a = mBanner.animator(20,40,anim,cBack);
				a.run();
			}; this.scrollTime = setTimeout(cDelay,1000);
		}

	},

	paneIn : function () {

		if (this.scrollTime) {
			clearTimeout(this.scrollTime);
			this.scrollTime = 0;
		} if (!this.shown) {
			this.shown = true;
			var oScroll = this.oScroll;
			var oBack = this.oBack;
			var oDst = 390 - Math.round(this.numSlides / 3) * 90;
			var oMove = 410 - oDst;
			var anim = function (a) {
				var res = Math.round(410 - mBanner.fCubicIn(a,0,oMove,10));
				oScroll.style.top = res+'px';
				oBack.style.top = res+'px';
			}; var cBack = function () {
				oScroll.style.top = oDst+'px';
				oBack.style.top = oDst+'px';
				mBanner.shown = true;
				if (mBanner.scrollTime) {
					clearTimeout(mBanner.scrollTime);
					mBanner.scrollTime = 0;
				}
			}; var a = mBanner.animator(10,40,anim,cBack);
			a.run();
		}

	},

	select : function (oId) {

		var oSrc, oDst;
		if (this.oGallery[oId].active) return false;
		for (var i = 0; i < this.oGallery.length; i++) {
			if (oId == i) {
				oDst = this.oGallery[i];
				oDst.picture.style.display = 'block';
				this.opacity(0,oDst.picture);
				oDst.picture.style.zIndex = 2;
			} else {
				if (this.oGallery[i].active) {
					oSrc = this.oGallery[i];
					oSrc.active = false;
					oSrc.preview.className = '';
				}
			}
		} var anim = function (a) {
			mBanner.opacity(a * 10,oDst.picture);
		}; var cBack = function () {
			mBanner.fBuzy = false;
			mBanner.opacity(100,oDst.picture);
			oSrc.picture.style.display = 'none';
			oDst.picture.style.zIndex = 1;
			oDst.preview.className = 'active';
			oDst.active = true;
			mBanner.oInfo.innerHTML = oDst.title;
		}; var a = this.animator(10,40,anim,cBack);
		this.fBuzy = true;
		a.run();

	},

	createSlide : function (oElem) {

		var oRef = {};
		oRef.id = this.numSlides;
		oRef.preview = oElem;
		oRef.title = oElem.getAttribute("title");
		if (!oRef.title.length) oRef.title = this.oInfo.innerHTML;
		oRef.picture = this.oCanvas.firstChild.childNodes[oRef.id];
		oRef.active = oRef.id ? false : true;
		if (oRef.active) oRef.preview.className = 'active';
		oRef.picture.style.display = oRef.id ? 'none' : 'block';
		oRef.picture.style.position = 'absolute';
		oRef.picture.style.top = 0;
		oRef.picture.style.left = 0;
		oRef.preview.onclick = function (e) {
			mBanner.fPaused = true;
			if (!mBanner.fBuzy) mBanner.select(oRef.id);
			return false;
		}; return oRef;

	},

	opacity : function (v,o) {

		if (o.filters) {
			if (!o.filters.length) {
				o.style.filter = "alpha(opacity=" + v + ")";
			} if (v == 100) {
				o.filters("alpha").enabled = false;
			} else {
				o.filters("alpha").opacity = v;
				o.filters("alpha").enabled = true;
			}
		} else {
			o.style.opacity = (v / 100);
		}

	},

	animator : function (f,u,c,k,z) {
		
		var o = {};
		o.f = f;
		o.u = u;
		o.c = c;
		o.k = k;
		o.z = z ? z : 0;
		o.a = function () {
			if (o.b == o.f) {
				clearInterval(o.i);
				if (o.k) {
					if (o.z) {
						setTimeout(o.k,o.z);
					} else {
						o.k();
					}
				} return;
			} o.c(o.b);
			o.b++;
		}; o.run = function () {
			o.b = 0;o.i = setInterval(o.a,o.u);
		}; return o;

	},

	fCubicIn : function (t, b, c, d) {
		
		return c*((t=t/d-1)*t*t + 1) + b;
		
	},
	
	fCubicOut : function (t, b, c, d) {
		
		return c*(t/=d)*t*t + b;

	}

}

var orderForm = {

	oForm : null,
	oBlock : null,
	sBtn : null,
	ready : false,
	fields : false,
	oInfo : false,
	pHref : false,
	
	init : function (oBlock,oForm) {

		this.oBlock = oBlock;
		this.oForm = oForm;
		this.oInfo = document.createElement('span');
		this.pHref = this.oForm.getAttribute("rel");
		this.oInfo.innerHTML = '<p><b>Ваш запрос отправлен.</b><br/>Мы обязательно свяжемся с вами в ближайшее время для уточнения деталей.</p>';
		this.oInfo = this.oForm.parentNode.insertBefore(this.oInfo,this.oForm);
		this.sBtn = oForm['sendBtn'];
		this.sBtn.onclick = function () {
			if (!orderForm.ready) return false;
			return orderForm.process();
		}; this.fields = new Array ();
		for (var i = 0; i < this.oForm.length; i++) {
			if (this.oForm[i].getAttribute("rel")) {
				this.fields.push({fld: this.oForm[i],rel: this.oForm[i].getAttribute("rel")});
			}
		} this.ready = true;

	},

	show : function () {

		if (this.ready) {
			this.reset();
			this.oBlock.style.display = 'block';
		}

	},

	hide : function () {

		if (this.ready) {
			this.reset();
			this.oBlock.style.display = 'none';
		}

	},

	reset : function () {

		this.oInfo.style.display = 'none';
		this.oForm.style.display = 'block';
		for (var i = 0; i < this.fields.length; i++) {
			this.fields[i].fld.value = '';
		} this.sBtn.disabled = false;

	},

	results : function (msg,state) {

		this.oForm.style.display = 'none';
		this.oInfo.style.display = 'block';
		this.ready = true;
		

	},

	process : function () {

		this.ready = false;
		for (var i = 0; i < this.fields.length; i++) {
			if (this.fields[i].rel == 'mandatory' && this.fields[i].fld.value == '') {
				this.ready = true;
				return false;
			}
		} this.sBtn.disabled = true;
		var data = new Array();
		for (var i = 0; i < this.fields.length; i++) {
			data.push(this.fields[i].fld.name+"="+this.fields[i].fld.value);
		} var cBack = function (msg,state) {
			orderForm.results(msg,state);
		}; sandyXml.postString(this.pHref,data.join('&'),cBack);
		return false;

	}

}

var fbForm = {

	oForm : null,
	sBtn : null,
	ready : false,
	fields : false,
	oInfo : false,
	pHref : false,
	
	init : function (oForm) {

		this.oForm = oForm;
		this.oInfo = document.createElement('span');
		this.pHref = this.oForm.getAttribute("rel");
		this.oInfo.innerHTML = '<p><b>Ваш запрос отправлен.</b></p>';
		this.oInfo.style.display = 'none';
		this.oInfo = this.oForm.parentNode.insertBefore(this.oInfo,this.oForm);
		this.sBtn = oForm['sendBtn'];
		this.sBtn.onclick = function () {
			if (!fbForm.ready) return false;
			return fbForm.process();
		}; this.fields = new Array ();
		for (var i = 0; i < this.oForm.length; i++) {
			if (this.oForm[i].getAttribute("rel")) {
				this.fields.push({fld: this.oForm[i],rel: this.oForm[i].getAttribute("rel")});
			}
		} this.ready = true;

	},

	results : function (msg,state) {

		this.oForm.style.display = 'none';
		this.oInfo.style.display = 'block';
		this.ready = true;
		

	},

	process : function () {

		this.ready = false;
		for (var i = 0; i < this.fields.length; i++) {
			if (this.fields[i].rel == 'mandatory' && this.fields[i].fld.value == '') {
				this.ready = true;
				return false;
			}
		} this.sBtn.disabled = true;
		var data = new Array();
		for (var i = 0; i < this.fields.length; i++) {
			data.push(this.fields[i].fld.name+"="+this.fields[i].fld.value);
		} var cBack = function (msg,state) {
			fbForm.results(msg,state);
		}; sandyXml.postString(this.pHref,data.join('&'),cBack);
		return false;

	}

}

var sandyXml = {

	fetchString:function(u){var r=this.c(u);if(r.e)return false;return r.d()},
	getString:function(u,f){var r=this.c(u,0,f);if(r.e)return false;return r.d()},
	sendString:function(u,d){var r=this.c(u,0,false,d);if(r.e)return false;r.d();return true},
	postString:function(u,d,f){var r=this.c(u,0,f,d);if(r.e)return false;r.d();return true},
	fetchXml:function(u){var r=this.c(u,1);if(r.e)return false;return r.d()},	
	getXml:function(u,f){var r=this.c(u,1,f);if(r.e)return false;r.d();return true},
	c:function(){
		var a=arguments;
		var r={e:false,u:a.length?a[0]:false,t:a.length>1?a[1]:0,a:a.length>2?true:false,c:a.length>2?a[2]:false,m:a.length>3?"POST":"GET",b:a.length>3?a[3]:null};
		try{r.x=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){try{r.x=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{r.x=new XMLHttpRequest()}catch(e){r.e=true;return false}}}
		r.d=function(){r.x.open(this.m,this.u,this.a);
		if(this.m == "POST"){r.x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");r.x.setRequestHeader("Content-length", this.b.length);r.x.setRequestHeader("Connection", "close")}
		if(this.a && this.c){r.x.onreadystatechange=function(){if(r.x.readyState != 4){return}clearTimeout(r.timeout);
		if(r.x.status != 200){if(r.timeout){r.c(new Error(101,'XML_BAD_REQUEST'),false)}else{r.c(new Error(102,'XML_SERVER_TIMEOUT'),false)}}else{r.c(r.t?r.x.responseXML:r.x.responseText,true)}};
		r.timeout=setTimeout(function(){r.timeout=false;r.x.abort()},15000);r.x.send(this.b);
		}else{r.x.send(this.b);if(r.x.status != 200){throw new Error(101,'XML_BAD_REQUEST')}return this.t?r.x.responseXML:r.x.responseText}
		}; return r;			
	} 

}


function mbannerInitScripts () {

	var a,b,c;
	if ((a = document.getElementById("sliderCanvas")) && (b = document.getElementById("sliderItems")) && (c = document.getElementById("sliderScroll"))) mBanner.create(a,b,c);
	if ((a = document.getElementById("invoiceFormBlock")) && (b = document.getElementById("invoiceForm"))) orderForm.init(a,b);
	if ((a = document.getElementById("fbForm"))) fbForm.init(a);

}

if (window.addActiveHook) {
	addActiveHook(mbannerInitScripts);
} else {
	window.onload = mbannerInitScripts;
}
