personSliders = new Class({
	Implements: [Options, Chain],
	options: {
		counter: 5
	},
	initialize: function(elements, curlink, options){
		this.setOptions(options);
		this.elements = $$(elements);
		if (this.elements.length < 2) return; // Если меньше двух элементов, то слайдить просто смешно!
		this.curElm = 0;
		this.lastElm = this.elements.length - 1;
		this.myFx = [];
		this.myUrl = [];
		this.inProc = false; // В процессе какого-либо спец. эффекта
		this.elements.each(function(elm, id){
			this.myUrl[id] = elm.get('title');
			this.myFx[id] = new Fx.Tween(elm, {duration: 600, link: 'cancel', transition: Fx.Transitions.Quad.easeIn});
		}, this);
		this.period = this.nextSecond.periodical(1000, this);
		this.seconds = this.options.counter;
		this.linkbtn = document.getElement(curlink);
		this.linkbtn.addEvent('click', this.goLink.bind(this));
	},

	slideNext: function(){
		if (this.inProc) return;
		this.inProc = true;
		var nextId = (this.curElm == this.lastElm)?0:(this.curElm + 1);
		var oldElm = this.curElm;
		this.elements[nextId].inject(this.elements[this.curElm], 'after').setStyles({'margin-left': 0, 'display': 'block'});
		this.renewCounter();
		this.myFx[this.curElm].start('margin-left', -385).chain(function(){
			this.inProc = false;
			this.elements[oldElm].setStyle('display', 'none');
			this.curElm = nextId;
		}.bind(this));
	},

	slidePrev: function(){
		if (this.inProc) return;
		this.inProc = true;
		var prevId = (this.curElm == 0)?this.lastElm:(this.curElm - 1);
		var oldElm = this.curElm;
		this.elements[prevId].inject(this.elements[this.curElm], 'before').setStyles({'margin-left': -385, 'display': 'block'});
		this.renewCounter();
		this.myFx[prevId].start('margin-left', 0).chain(function(){
			this.inProc = false;
			this.elements[oldElm].setStyle('display', 'none');
			this.curElm = prevId;
		}.bind(this));
	},

	renewCounter: function(){
		this.seconds = this.options.counter;
	},

	nextSecond: function(){
		if (this.seconds == 1){
			this.seconds = this.options.counter;
			this.slideNext();
		}else{
			this.seconds--;
		}
	},

	remove: function(){
		$clear(this.period);
	},

	goLink: function(){
		self.location.href = '#:' + this.myUrl[this.curElm];
	}
});

Fx.Slide = new Class({

	Extends: Fx,

	options: { mode: 'vertical' },

	initialize: function(element, options){
		this.addEvent('complete', function(){
			this.open = (this.wrapper['offset' + this.layout.capitalize()] != 0);
			if (this.open && Browser.Engine.webkit419) this.element.dispose().inject(this.wrapper);
		}, true);
		this.element = this.subject = $(element);
		this.parent(options);
		var wrapper = this.element.retrieve('wrapper');
		this.wrapper = wrapper || new Element('span', {
			styles: $extend(this.element.getStyles('margin', 'position'), {'overflow': 'hidden', 'position': 'absolute', 'display': 'block', 'width': 100, 'height': 50})
		}).wraps(this.element);
		this.element.store('wrapper', this.wrapper).setStyle('margin', 0);
		this.now = [];
		this.open = true;
	},

	vertical: function(){
		this.margin = 'margin-top';
		this.layout = 'height';
		this.offset = this.element.offsetHeight;
	},

	horizontal: function(){
		this.margin = 'margin-left';
		this.layout = 'width';
		this.offset = this.element.offsetWidth;
	},

	set: function(now){
		this.element.setStyle(this.margin, now[0]);
		this.wrapper.setStyle(this.layout, now[1]);
		return this;
	},

	compute: function(from, to, delta){
		var now = [];
		var x = 2;
		x.times(function(i){
			now[i] = Fx.compute(from[i], to[i], delta);
		});
		return now;
	},

	start: function(how, mode){
		if (!this.check(arguments.callee, how, mode)) return this;
		this[mode || this.options.mode]();
		var margin = this.element.getStyle(this.margin).toInt();
		var layout = this.wrapper.getStyle(this.layout).toInt();
		var caseIn = [[margin, layout], [0, this.offset]];
		var caseOut = [[margin, layout], [-this.offset, 0]];
		var start;
		switch (how){
			case 'in': start = caseIn; break;
			case 'out': start = caseOut; break;
			case 'toggle': start = (this.wrapper['offset' + this.layout.capitalize()] == 0) ? caseIn : caseOut;
		}
		return this.parent(start[0], start[1]);
	},

	slideIn: function(mode){
		return this.start('in', mode);
	},

	slideOut: function(mode){
		return this.start('out', mode);
	},

	hide: function(mode){
		this[mode || this.options.mode]();
		this.open = false;
		return this.set([-this.offset, 0]);
	},

	show: function(mode){
		this[mode || this.options.mode]();
		this.open = true;
		return this.set([0, this.offset]);
	},

	toggle: function(mode){
		return this.start('toggle', mode);
	}

});
Element.Properties.slide = {
	set: function(options){
		var slide = this.retrieve('slide');
		if (slide) slide.cancel();
		return this.eliminate('slide').store('slide:options', $extend({link: 'cancel'}, options));
	},
	get: function(options){
		if (options || !this.retrieve('slide')){
			if (options || !this.retrieve('slide:options')) this.set('slide', options);
			this.store('slide', new Fx.Slide(this, this.retrieve('slide:options')));
		}
		return this.retrieve('slide');
	}
};


indexShow = new Class({
	initialize: function(elements){
		// elements are [link1, body1, link2, body2, mainbody]
		this.elements = $$(elements);
		this.elements.each(function(elm){

			var mybl = elm.getElement('span');
			var myanch = elm.getElement('a.bigablock');
			if (!mybl || !myanch) return;
			myanch.$mybl = mybl;

			var mySlide = new Fx.Slide(myanch.$mybl, {'mode': 'horizontal', 'duration': 150, 'wait': false, 'transition': Fx.Transitions.Sine.easeIn});
			mySlide.hide();
			//			myanch.$mybl.set('slide', {'duration': 200, 'mode': 'horizontal', 'wait': false})
			myanch.addEvents({
			'mouseover': function(e){
				mySlide.slideIn();
			},
			'mouseout': function(e){
				mySlide.slideOut();
			}});
			myanch.$mybl.setStyle('visibility', 'visible');
		})
	}
});

coolCard = new Class({
	Implements: [Options, Events],
	options: {

	},
	initialize: function(elements, options){
		elements.each(function(elm){
			var child = elm.getChildren();
			if (child.length != 4) return;
			var mfx = new Fx.Morph(elm, {duration: 200, transition: Fx.Transitions.linear, wait: false});
			mfx.stage = 0;
			var closer = elm.getElement('input.closecard');
			closer.addEvent('click', function(event, timered){
				if (timered && !mfx.beIn) return;
				mfx.beIn = false;
				if (mfx.inProcess){
					closer.fireEvent('click', [event, true], 1000);
					return;
				}
				if (mfx.stage == 1){
					mfx.inProcess = true;
					mfx.start({'height': 115}).chain(function(){
						child[1].addClass('bussines_form_hidden');
						child[2].removeClass('bussines_card_hidden');
						mfx.start({'height': 240 });
					}).chain(function(){
						mfx.stage = 0;
						mfx.inProcess = false;
					});
				}
			});
			elm.addEvents({'mouseenter': function(event){ mfx.beIn = true; if (mfx.inProcess) return; if (mfx.stage == 0) { mfx.inProcess = true; mfx.start({'height': 115}).chain(function(){ child[1].removeClass('bussines_form_hidden'); child[2].addClass('bussines_card_hidden'); mfx.start({'height': 414 }); }).chain(function(){ mfx.stage = 1; mfx.inProcess = false; }); }; }});
		});
	}
});

topSlogan = new Class({
	Implements: [Options, Chain],
	options: {
		interval: 5000,
		duration: 200
	},
	stage: 0,
	initialize: function(elements){
		tSg=this;
		this.elements = $$(elements);
		if (this.elements.length < 2) return; // Делать слайдшоу просто бессмысленно
		this.elements.each(function(elm){
			elm.setStyles({'opacity': 0, 'display': 'block'}).set('morph', {'duration': tSg.options.duration});
		});
		this.maxNum = this.elements.length * 2 - 1;
		this.nextEvent();
	},
	nextEvent: function(){
		var actelm = (this.stage / 2).toInt();
		var action = this.stage % 2;
		if (action == 0){ // show!
			this.showElm(actelm);
			this.nextEvent.delay(this.options.interval + this.options.duration, this);
		}else{ // hide!
			this.hideElm(actelm);
			this.nextEvent.delay(50, this);
		}
		this.stage = (this.stage == this.maxNum) ?  0 : this.stage + 1;
	},
	showElm: function(id){
		this.elements[id].morph({'opacity': 1});
	},
	hideElm: function(id){
		this.elements[id].morph({'opacity': 0});
	}
})

topMenu = new Class({
	Implements: [Options],
	options: {
		activeElm: 0
	},
	initialize: function(container, options){
		ttopMenu = this;
		container = document.getElement(container);
		this.liElements = $$(container.getElements('li'));
		this.elements = [];
		this.pnames = [];
		this.liElements.each(function(elm, id){
			this.elements[id] = elm.getElement('a');
			ttopMenu.pnames[id] = parseInt(this.elements[id].title);
			this.elements[id].erase('title');
		}, this);
	},
	setActive: function(pathname){
		var myInd = this.pnames.indexOf(pathname);
		if (myInd == -1) return;
		if (myInd == this.options.activeElm) return;
		this.liElements[this.options.activeElm].removeClass('act');
		this.liElements[myInd].addClass('act');
		this.options.activeElm = myInd;
	}
});

myTooltip = new Class({
	Implements: [Events, Options],
	options: {
	},

	initialize: function(container){
		this.container = $(container);
		this.container.setStyles({'opacity': 0, 'left': -1000, 'top': -1000, 'display': 'block'});
		this.fx = new Fx.Morph(this.container, {duration: 200, wait: false});
		this.image = this.container.getElement('.photoplace');
		this.imageelm = this.container.getElement('.tooltip-image');
		if (Browser.Engine.trident) this.imageelm.addEvents({'mouseenter': this.addIn.bindWithEvent(this, this.imageelm), 'mouseleave': this.addOut.bindWithEvent(this, this.imageelm)});
		this.text = this.container.getElement('.textplace');
		if (Browser.Engine.gecko) this.imageelm.setStyle('top', '0px');
		this.state = 0; // Курсор: 0 - за пределами элементов, 1 - находится внутри основного элемента, 2 - находится внутри всплывающего элемента (IE only)
	},

	attach: function(elements){
		$$(elements).each(function(element, id){
			var mytext = element.retrieve('tip:mytext', element.get('title'));
			var enter = element.retrieve('tip:enter', this.elementEnter.bindWithEvent(this, element));
			var leave = element.retrieve('tip:leave', this.elementLeave.bindWithEvent(this, element));
			element.addEvents({'mouseenter': enter, 'mouseleave': leave});
			var myRow = 1000 - (parseInt(id / 5) + 2) * 10;
			element.setStyle('z-index', myRow);
			element.store('tip:num', id%5);
			element.store('tip:row', myRow);
			element.store('tip:native', element.get('title'));
			element.erase('title');
			element.erase('alt');
		}, this);
		return this;
	},

	addIn: function(event, element){ this.state = 2; },

	addOut: function(event, element){
		if (Browser.Engine.trident){ this.state = 0; this.hide.delay(50, this); }
	},

	detach: function(elements){
		$$(elements).each(function(element){
			element.removeEvent('mouseenter', element.retrieve('tip:enter') || $empty);
			element.eliminate('tip:enter');
		});
		return this;
	},

	elementEnter: function(event, element){
		this.state = 1;
		this.container.setStyle('z-index', element.retrieve('tip:row') - 5);
		$A(this.image.childNodes).each(Element.dispose);
		$A(this.text.childNodes).each(Element.dispose);
		var title = element.retrieve('tip:mytext');
		var html = $(title).get('html');
		element.clone().injectInside(this.image);
		this.text.set('html', html);

		this.position(element.getPosition(), element.retrieve('tip:num'));
		this.timer = $clear(this.timer);
		this.show();
	},

	elementLeave: function(event, element){
		this.state = 0;
		if (!Browser.Engine.trident) this.hide(); else this.hide.delay(50, this);
	},

	position: function(poss, tipnum){
		this.container.className = 'tooltip ttip' + tipnum;
		var mtop = poss.y - 10;
		var mleft = poss.x - 10 - 100*tipnum;
		if (!Browser.Engine.gecko) { mtop-=2; mleft-=1; }
		this.container.setStyles({'top': mtop, 'left': mleft});
	},

	show: function(){
		this.fx.start({'opacity': 1});
	},

	hide: function(){
		if (Browser.Engine.trident && this.state != 0) return;
		this.fx.start({'opacity': 0});
	}
});

fullAjax = new Class({
	Implements: [Events, Options, Chain],
	options: {
		/*
		beforeRollIn: $empty, // Constructor 1
		afterRollIn: $empty, // Constructor 2
		beforeRollOut: $empty, // Destructor 1
		afterRollOut: $empty // Destructor 2
		*/
		activeElm: 0,
		historyKey: ':',
		defHKey: ['/', '/index/'],
		nHeight: '70px',
		defTitle: ''
	},
	initialize: function(bodyelm, options){
		var tfA = this;
		this.tmpdiv = new Element('div');
		this.bodyelm = bodyelm;
		this.bodyfx = new Fx.Morph(this.bodyelm, {duration: 350, transition: Fx.Transitions.linear, wait: false});
		this.setOptions(options);

		this.baseURL = (arr=/^http:\/\/([a-z0-9\-\.]+)/.exec(document.URL))?arr[1]:'';

		this.adoptAnchors(document.body); // !!!
		this.phiden = 0; // stage of effects 0 - elements are shown; 1 - elements are moving out; 2 - elements are moving in
		this.ploaded = true; // stage of loading false - still loading; true - loaded
		this.aPath = ""; // active path

		this.HStarted = false;
		this.scriptLoaded = false; // Is the script loaded properly ?

		this.history = HistoryManager.register(// arguments are (key, defaults, onMatch, onGenerate, regexp, options)
		this.options.historyKey,
		// Array with default values, here index 0 is the current page index
		this.options.defHKey,
		// onMatch, callback when the state changed
		function(values) {
			if (!tfA.HStarted && values[0] == "/") return;
			tfA.open(values[0]);
		}.bind(this),
		// onGenerate, callback that returns the string for the hash
		function(values) {
			return [tfA.options.historyKey, '', values[0], ''].join('');
		}.bind(this),
		// RegExp for the hash value, works
		this.options.historyKey + '(([a-z0-9_\\/]+))');
		this.$events = {};
	},
	open: function(path){
		if (!this.ploaded || (this.phiden != 0)){ this.history.setValue(0, this.aPath); return; }
		this.ploaded = false; this.phiden = 1;
		this.fireEvent('beforeRollOut');
		var newPage = new Request({'url': '/getajax.php', 'method': 'post', 'data': {'p': path}, 'evalScripts': true, 'onComplete': function(text){
			this.aPath = path;
			this.tmpdiv.set('html', text);
			this.adoptAnchors(this.tmpdiv);
			this.ploaded = true;
			this.showattempt();
		}.bind(this)}).send();
		this.scriptLoaded = false;
		var screenX = window.getSize().x;
		//		var wdt1 = (screenX + 940)/2, wdt2 = (screenX + 820)/2;
		//		tfA.headfx.start({'left': wdt1}).chain(function(){ document.body.className = 'preload'; tfA.headelm.setStyle('left', '-' + wdt1 + 'px'); });
		this.bodyfx.start({'opacity': 0}).chain(function(){
			this.phiden = 2;
			// ↓ Вы стали свидетелем танцев с бубном над IE!
			if (!document.documentElement.className.contains('preloadme', ' ')) document.documentElement.className = (document.documentElement.className + ' ' + 'preloadme').clean();
			//			document.documentElement.addClass('preloadme');
			this.fireEvent('afterRollOut');
			this.showattempt();
		}.bind(this));
	},
	adoptAnchors: function(elm){
		var tfA = this;
		var noext='((\.[a-z]{2,4})$|^javascript\:|https\:|http\:\/\/)'
		var anchors = $$(elm.getElementsByTagName('a'));
		anchors.each(function(elm){
			var mhref = elm.getProperty('href');
			if (arr=/^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(mhref)) if (arr[6] == tfA.baseURL) mhref = arr[8];
			//			if (mhref.test('.\'))
			if (!mhref) return;
			if (!mhref.test(noext)) elm.setProperty('href', '#:' + mhref); else elm.setProperty('target', '_blank');
		});
	},
	showattempt: function(){
		if ((this.phiden != 2) || !this.ploaded || !this.scriptLoaded) return;
		this.phiden = 3;
		var elms = $$(this.tmpdiv.getChildren()).filter(function(item, index){ return item.tagName == "DIV"; });
		//		tfA.headelm.setHTML(elms[0].innerHTML);
		this.bodyelm.set('html', elms[0].innerHTML);
		this.bodyelm.set('class', elms[0].className); // Наследуем также и класс
		this.bodyelm.setStyle('opacity', 0);
		document.documentElement.className = document.documentElement.className.replace(new RegExp('(^|\\s)' + 'preloadme' + '(?:\\s|$)'), '$1');
//		document.documentElement.removeClass('preloadme');
		this.fireEvent('beforeRollIn');
		//		tfA.headfx.start({'left': 0});
		this.bodyfx.start({'opacity': 1}).chain(function(){
			document.documentElement.className = document.documentElement.className.replace(new RegExp('(^|\\s)' + 'preloadme' + '(?:\\s|$)'), '$1');			
//			document.documentElement.removeClass('preloadme');
			if (this.phiden != 3) return;
			this.phiden = 0;
			this.fireEvent('afterRollIn');
		}.bind(this));
		if ($mbox && $mbox.opened) $mbox.close();
		if ($mbox && $mbox.overlay) $mbox.overlay.container.setStyles({'height': 1, 'display': 'none'});
	},
	setTitle: function(newTitle){ document.title = ((newTitle != '')?(newTitle + " :: "):'') + this.options.defTitle; }
});

function sendmail(mform){
	mform = $(mform);
	var elm = mform.getParent().getElement('.reqresult');
	elm.set('html', '<img src="/images/pr1.gif" />');
	elm.getParent().addClass('cresp');
	var myAjax = new Request({url: '/_admindata/run/sendmail.php', method: 'post', data: {'myname': mform.myname.value, 'myemail': mform.mymail.value, 'message': mform.message.value, 'person': mform.person.value}, onComplete: function(data){ elm.set('html', data); elm.getParent().addClass('cresp'); }}).send();
}

function sendmailx(mform){
	mform = $(mform);
	var elm = mform.getParent().getElement('.reqresult');
	elm.set('html', '<img src="/images/pr1.gif" />');
	elm.getParent().addClass('cresp');
	var myAjax = new Request({url: '/_admindata/run/sendmailx.php', method: 'post', data: {'myname': mform.myname.value, 'mycompany': mform.mycompany.value, 'mycountry': mform.mycountry.value, 'myphone': mform.myphone.value, 'myemail': mform.myemail.value, 'projname': mform.projname.value, 'trtype': mform.trtype.value, 'wordcount': mform.wordcount.value, 'comments': mform.comments.value, 'person': mform.person.value}, onComplete: function(data){ elm.set('html', data); elm.getParent().addClass('cresp'); }}).send();
}

function sendq(mform){
	mform = $(mform);
	var elm = mform.getParent().getElement('.reqresult');
	elm.set('html', '<img src="/images/pr4.gif" />');
	elm.getParent().addClass('cresp');
	var myAjaxx = new Request({url: '/_admindata/run/askquestion.php', method: 'post', data: {'fld1': mform.fld1.value, 'fld2': mform.fld2.value, 'fld3': mform.fld3.value, 'formid': mform.formid.value}, onComplete: function(data){ elm.set('html', data); elm.getParent().addClass('cresp'); }}).send();
}

var myTop, myAjax, mySlogan, $mbox = false, mtooltip, pSliders;

function sendfrm(mform){
	mform = $(mform);
	var elm = mform.getParent().getElement('.reqresult');
	elm.set('html', '<img src="/images/pr4.gif" />');
	elm.getParent().addClass('cresp');
	var myAjaxx = new Request({url: '/_admindata/run/sendorder.php', method: 'post', data: {'fld1': mform.fld1.value, 'fld2': mform.fld2.value, 'fld3': mform.fld3.value, 'formid': mform.formid.value, 'apath': myAjax.aPath || location.href}, onComplete: function(data){ elm.set('html', data); elm.getParent().addClass('cresp'); }}).send();
}

function bform(elm){
	var eform = $('cardform' + elm).getParent();
	eform.removeClass('cresp');
}



var myColor = '#FFFFFF';

window.addEvent('domready', function(){
	// Top menu
	myTop = new topMenu('.navigation');
	// And footer:
	//	myFoot = new myFooter($('ftoggle'), $('footblock'));

	// Image slider:
	pSliders = new personSliders('.onePhoto', '.book_menu .photo');

	// And the whole class
	myAjax = new fullAjax($('content'), {});
	//	mtooltip = new myTooltip($('mytooltip'));
	HistoryManager.start();
	myAjax.HStarted = true;

	$$('.paspartu div').each(function(elm){
		elm.addEvent('click', function(e){
			document.body.className = this.className;
		})
	})
});
