//<--
/*

	@author: SWAT (Rostovtsev Ruslan)
	@url: http://www.jsysteam.ru
	@version: 1.0.0

*/


var sWindow = new Class({
						
	initialize: function(type, content) {
        
		this.type = type;
		this.node = $('window_' + this.type);
		this.state = false;
		this.content = content;
		this.wInner = this.node.getElement('.wInner');
		this.req = $empty;
		this.timer = $empty;
		
		
		switch(this.type) {
			
			case 'tl':
				this.node.set('morph', {duration: 'long', transition: 'quint:out', link: 'cancel'});
				break;
				
			case 'tr':
				this.node.set('morph', {duration: 'long', transition: 'cubic:out', link: 'cancel'});
				break;

			case 'br':
				this.node.set('morph', {duration: 'long', transition: 'cubic:out', link: 'cancel'});
				break;
			default:
				break;
		}

		this.hide();
		this.addClose();
    },

	makeScroll: function() {
		
		if(!this.scroller && $chk(this.node.getElement('.scrollArea'))) {
			
			this.scroller = new UvumiScrollbar(this.node.getElement('.scrollArea'));
			this.scroller.update();
			
		} else {
			this.toggleScroll('show');
		}
	},
	
	
	destroyScroll: function() {
		
		if(this.scroller) {
			if($chk(this.node.getElement('.scrollArea'))) this.node.getElement('.scrollArea').destroy();
			this.scroller = null;
		}
		
	},
	
	toggleScroll: function(m) {
		
		try {
			
			if(this.scroller && $chk(this.node.getElement('.scrollBar'))) {
				
				if((m && m == 'hide')/* || this.node.getElement('.scrollBar').getStyle('display') == 'block'*/) {
					
					//if(!Browser.Engine.trident) this.node.getElement('.scrollBar').fade(0.0);
					this.node.getElement('.scrollBar').setStyle('display', 'none');
									
				} else if((m && m == 'show')/* || this.node.getElement('.scrollBar').getStyle('display') == 'block'*/) {
					
					this.node.getElement('.scrollBar').setStyle('display', 'block');
					//if(!Browser.Engine.trident) this.node.getElement('.scrollBar').fade(1.0);
					this.scroller.update();
					
				} else {
					return false;
				}
				
				
			} else {
				
				if($chk(this.node.getElement('.scrollArea'))) this.makeScroll();
			}
		
		} catch(E) {
			
		}
	},
	
	
	showContent: function() {
		return this.toggleContent('show');
	},
	
	toggleContent: function(m) {
		
		if(m == 'hide') {
			
			//if(!Browser.Engine.trident) this.wInner.tween('opacity', 1.0, 0.0);
			this.wInner.setStyle('display', 'none');

			this.toggleScroll(m);
			this.state = 0;
							
		} else if(m == 'show') {
			
			//if(!Browser.Engine.trident) this.wInner.tween('opacity', 0.0, 1.0);
			this.wInner.setStyle('display', 'block');
			
			this.toggleScroll(m);
			this.state = 1;
			
		} else {
			//
		}
		
	},
	
	

	addClose: function() {
		
		if($chk(this.node.getElement('.close'))) {
			
			if(this.type == 'tl') {
				
				this.node.getElement('.close').set('html', $('window_close').innerHTML.substitute({onclick: 'site.windows.hideAll();'}));
				
			} else if(this.type == 'br') {
				
				this.node.getElement('.close').set('html', $('window_close').innerHTML.substitute({onclick: 'site.windows.hide2();'}));
				
			} else {
			
				this.node.getElement('.close').set('html', $('window_close').innerHTML.substitute({onclick: 'site.windows.' + this.type + '.hide();'}));
			}
		}
	},
	
	
	needModifyLink: function(url) {
		
		var l = url.split('.');
		l = l[l.length - 1];
		
		if(url.lastIndexOf('mailto:') >= 0) {
		
			return false;
		
		} else if(l.length < 5 && l.lastIndexOf('/') < 0) {
			
			switch(l) {
				
				case 'php': 
					return true;
				case 'html':
					return true;
				case 'txt':
					return true;
				default:
					return false;
			}
			
		} else if(l.lastIndexOf('/') >= 0) {
		
			return true;
		
		} else {
			
			return false;	
		}
		
	},
	
	modifyLinks: function() {
		
		var ctx = this;
		
		this.wInner.getElements('a').addEvent('click', function(event) {
											 
				if(ctx.needModifyLink(this.getProperty('href'))) {

					if(this.getProperty('target') != '_blank') {
					
						event.stop();
						eval('try{ site.windows.' + (this.getProperty('target').length == 2 ? this.getProperty('target') : 'tl') + 
							 '.setHref("' + this.getProperty('href') + '"); } catch(E) { site.windows.tl.setHref("' + this.getProperty('href') + '"); }');
					} else {
					
						if(Browser.Engine.trident) {
							window.open(this.getProperty('href'));
						}
					
					}
					
					return false;
				}
		});
		
		if(Browser.Engine.trident && Browser.Engine.version == 4 && $chk(this.wInner.getElements('.ie6fix'))) this.wInner.getElements('.ie6fix').ie6fix();
	},
	
	setLoading: function(info) {

		this.wInner.set('html', $('window_loading').get('html').substitute({info: info ? info : 'Загрузка...'}));
		this.node.getElement('.wlInd').setProperty('src', 'images/progress/loading_' + this.type + '.gif').getParent().setStyle('padding-top', (this.getParams().h / 2) - (this.type == 'tr' ? 35 : 70));
	
		if(this.state == 0) this.show();
		else if(this.state == 1) this.toggleContent('show');
		else return false;
	},
	
	setContent: function(content) {
		
		this.toggleContent('hide');
		this.destroyScroll();
		this.wInner.set('html', content);
		this.modifyLinks();
		
		if(this.state == 0) this.show();
		else if(this.state == 1) this.toggleContent('show');
		else return false;
		
		return false;
	},
	
	appendContent: function(content) {
		
		this.wInner.set('html', this.wInner.get('html'), content);
		
		if(this.scroller) this.scroller.update();
		else this.makeScroll();
	},
	
	
	setHref: function(url) {

		if(this.type == 'br' && site.windows.tr.state == 1) site.windows.tr.hide();
		if(this.type == 'tl' && site.windows.br.state == 1) site.windows.br.hide();
		
		this.setLoading();

		var win = this;
		
		this.req = new Request.HTML({
									
									 headers: {'X-Request': 'HTML'},
									 //encoding: 'utf-8',
									 evalScripts: true, 
								 
									 onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
										 
										 if(responseHTML.lastIndexOf('<body>') >= 0) {
											responseHTML = responseHTML.split('<body>')[1].split('</body>')[0];
										 }
										
										
										if(win.state < 0) {
											
											$clear(win.timer);
											win.timer = (function() { 
													  
													  if(this.ctx.state < 0) return;

													  this.ctx.setContent(this.data);
													  $clear(this.ctx.timer);
													  
											}).periodical(100, { ctx: win, data: responseHTML });
											
										} else {
											win.setContent(responseHTML);
										}
										 
									 },
									 
									 onFailure: function(xhr) { 
										
										if(confirm('Ошибка загрузки, попробовать еще раз?')) {
											return win.setHref(url);
										}
										
										win.hide();
									 }
		});
		
		this.req.get(url);
		
		if(this.type == 'tl' && url.split('/')[1]) site.cat.change('/images/tRight/' + url.split('/')[1] + '.jpg');
		site.pagi.history.setValue(0, url.replace(/^http:\/\/[^\/]+/, '') + '?win=' + this.type);
		
		return false;
	},
	
	
	getParams: function(type) {
	
		var coord = $('container').getPosition();
		var width = 503;
		var height = 487;
		
		
		switch(type ? type : this.type) {
			
			case 'tl':
				
				if(Browser.Engine.trident) {
					
					coord.y += 70;
					if(Browser.Engine.version == 4) coord.x -= 4;
					else coord.x -= 5;
					
				} else {
					coord.y -= 78;
					coord.x -= 5;
					if(Browser.Engine.gecko) coord.x += 2;
				}
				
				width = 503;
				height = 487;
				
				break;
				
			case 'tr':
				
				if(Browser.Engine.trident) {
					
					coord.y += 150;
					coord.x += 499;
					
				} else {
					coord.y += 1;
					coord.x += 500;
				}

				width = 217;
				height = 217;
				
				break;

			case 'br':
				
				
				if(Browser.Engine.trident) {
					coord.y += 415;
					
					if(Browser.Engine.version == 4) coord.x += 499;
					else coord.x += 500;
					
				} else {

					coord.y += 266;
					coord.x += 500;
				}
				
				width = 433;
				height = 377;
				
				break;
			
			default:
				
				break;
		}
		
		return {w: width, h: height, l: coord.x, t: coord.y};
	
	},
	
	hideNode: function() {
		return this.node.setStyle("visibility", "hidden");
	},
	

	show: function() {

		this.state = -1;
		
		if(this.type == 'tl') site.textImg.hide();
		if(!Browser.Engine.trident) this.node.fade(1.0);

		var p = this.getParams();
		this.showContent.delay(600, this);
		
		return this.node.morph({
				left: p.l + 'px',
				top: p.t + 'px',
				width: p.w + 'px',
				height: p.h + 'px', 
				fontSize: '13px',
				visibility: 'visible'
			});
		
	},
	

	
	hide: function() {

		this.state = -1;
		
		if(this.type == 'tl') site.textImg.show();
		
		if(!Browser.Engine.trident) this.node.fade(0.0);
		
		this.toggleContent('hide');
		
		var left = -200;
		var top = -200;
		
		switch(this.type) {
			
			case 'tl':
				
				break;
			case 'tr':
					
				var el = $('tRight');
				top = el.getPosition().y + (el.getStyle('height').toInt() - $('logo').getStyle('height').toInt() + 30);
				left = el.getPosition().x;
				
				break;
			
			case 'br':

				var pos = $('bRight').getPosition();
				top = pos.y;
				left = pos.x;
				
				break;
			
			default:
				
				break;
		}
		
		this.hideNode.delay(350, this);
		
		return this.node.morph({
			left: left + 'px',
			top: top + 'px',
			width: '0px',
			height: '0px',
			fontSize: '0px'
		});
		
	},
	
	
	toggle: function() {
		
		if(this.state) {
			
			this.hide();
			
		} else {
			
			this.show();
		}
		
		return false;
	}
					 
});




var sGallery = new Class({
						
	initialize: function(el) {
        
		try {
			
			this.node = $(el);
			//this.left = this.node.getElement('.arLeft');
			//this.right = this.node.getElement('.arRight');
			this.imagesNode = this.node.getElement('.images');
			this.images = this.imagesNode.getElements('img');
			this.nums = this.node.getElement('.numImg').getElements('span');
			this.scrollFx = new Fx.Scroll(this.imagesNode, {duration: 500, wait: false, transition: Fx.Transitions.Quad.easeInOut});
			
			this.step = this.images[0].getProperty('width').toInt() + 14;
			this.maxStep = (this.images.length-1) * this.step;
			this.pos = {l: 0, t: 0};
			
			this._reset();

			this.images.each(function(item, index) {
									  
									 item.addEvent('click', function(event) {
											 
											 event.stop(); 
											 Slimbox.open(item.getProperty('alt'), 0, {overlayOpacity: 0.9});
									 }); 
							});
			
		
		} catch (E) {
			alert(E);
		}
    },
	
	start: function() {
		this.scrollFx.start(this.pos.l, this.pos.t);
		//this.scrollFx.toElement(this.images[this.pos.l / this.step]);
		this.update();
	},
	
	update: function() {
		/*
		if(this.pos.l >= this.maxStep) {
			
			this.right.addClass('.arRight_disabled');
			
		} else {
			
			if(this.right.hasClass('.arRight_disabled')) this.right.removeClass('.arRight_disabled');	
		}
		
		
		if(this.pos.l <= 0) {
			
			this.right.addClass('.arLeft_disabled');
			
		} else {
			
			if(this.right.hasClass('.arLeft_disabled')) this.right.removeClass('.arLeft_disabled');	
		}
		*/
		
		for(var i = 0; i < this.images.length; i++) {
			
			if(this.nums[i].hasClass('active')) {
				
				if(this.pos.l != (this.step  * i)) this.nums[i].removeClass('active');
				
			} else {
				
				if(this.pos.l == (this.step  * i)) this.nums[i].addClass('active');
			}
		}
		
	},
	
	show: function(num) {
		
		if(this.pos.l == this.step * num) return false;
		
		this.pos.l = this.step * num;
		this.start();
		
	},
	/*
	sRight: function() {
		
		if(this.pos.l < this.maxStep) {
			
			this.pos.l += this.step;
			this.start();
		} 
	},
	
	sLeft: function() {
		
		if(this.pos.l > 0) {
			
			this.pos.l -= this.step;
			this.start();
		} 
	},*/
	
	_reset: function() {
		
		this.pos.l = 0;
		this.pos.t = 0;
		this.start();
		this.nums[0].addClass('active');
	}
	
});



Element.implement({
				  
	ie6fix: function() {
		
		if(!Browser.Engine.trident && Browser.Engine.version != 4) return this;
		
		var img;
		
		if(this.get('tag') == 'img') {
			
			img = "'" + this.get('src') + "'";
			this.set('src', '/images/blank.gif');
			
		} else {
			
			var bg = this.getStyle('background-image');
			if(bg && bg != 'none') img = bg.match(/\(([^)]+)\)/)[1];
		}
		
		if(img) {
			
			if(this.getStyle('display') == 'inline' && !['input', 'textarea', 'button'].contains(this.get('tag'))) {
				
				this.setStyles({
					'display': 'block',
					'width': this.getStyle('width')
				});
				
			}
				
			this.setStyles({
				'background': '',
				'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src=" + img + ", sizingMethod='crop')"
			});
			
		}
		
		return this;
	}
	
});

 
 
var PaginationClass = new Class({

	initialize: function(firstPage) {

		this.historyKey = '';
		this.first = true;
		this.history = HistoryManager.register(
			this.historyKey,
			[firstPage],
			function(values) {
				this.to(values[0]);
			}.bind(this),
			function(values) {
				return [this.historyKey, values[0]].join('');
			}.bind(this),
			this.historyKey + '(.+)'
		);

	},

	to: function(page) {
		if (page === undefined || page === null) return;
		
		page = page.replace(/^http:\/\/[^\/]+/, '').split('?win=');
		this.page = page[0];

		if(this.first) {
			
			this.first = false;
			
		} else {
			
			if(this.page && this.page != '' && this.page != '/'  && this.page != '/index.php') { 
			
				eval('try{ site.windows.' + page[1] + '.setHref("' + this.page + '"); } catch(E) { site.windows.tl.setHref("' + this.page + '"); }');
			
			} else {
				
				site.windows.hideAll();
			}
		}

		//this.history.setValue(0, this.page + '&win=' + page[1]);
	}
}); 
 
 
 

var site = {};
site.windows = {};
site.pagi = null;


site.windows.showAll = function() {

	try {
		
		var t = -600;
		
		for(var i in site.windows) {

			if(i.length == 2) eval('site.windows.' + i + '.toggle.delay('+t+', site.windows.'+i+');'); 
			t+=300;
		}
		
		
	} catch(E) {
		alert(E);	
	}
}


site.windows.hideAll = function() {

	try {
		
		for(var i in site.windows) {

			if(i.length == 2) eval('site.windows.' + i + '.hide();'); 
		}
		
		//site.cat.change('/images/tRight/main.jpg');
		site.pagi.history.setValue(0, '');
		
		
	} catch(E) {
		alert(E);	
	}
}


site.windows.hide2 = function() {

	site.windows.br.hide();
	site.windows.tr.hide();
}



site.windows.reShowAll = function(start) {
	
	site.windows.showAll.delay(start ? start : 300);
	site.windows.showAll.delay(start ? start+150 : 300);
}


site.show_step2 = function() {
	
	$('tLeft').morph('.tLeft');
	$('tRight').morph('.tRight');
	$('bLeft').morph('.bLeft');
	$('bRight').morph('.bRight');
	
	$('logo').morph('.logo');
	if(Browser.Engine.gecko) $('logo').morph('.moz-logo');
	
	site.windows.reShowAll();
}


site.show = function() {
	
	//site.textImg.rand();
	
	if(Browser.Engine.trident && Browser.Engine.version == 4) {
		
		$$('.ie6fix').ie6fix();
		$('loading').setStyle('display', 'none');
		
	} else {
		
		$('loading').destroy();
	}
	
	site.textImg.node.fade(1);
	site.textImg.node.morph('.abtext');
	site.show_step2.delay(200);

}

site.hide = function() {
	// TODO ?
}



site.textImg = {};
site.textImg.state = true;
site.textImg.node = null;
site.textImg.img = new Array();
site.textImg.first = true;


site.textImg.load = function() {
	
	$('textImgData').getElements('li').each(function(item, index) {
													 
													site.textImg.img.push(item.get('html')); 
											});
}

site.textImg.rand = function() {
	
	if(this.first) {
		return this.first = false;
	}
	
	return this.node.getElement('img').setProperty('src', this.img[$random(0, this.img.length-1)]);
}


site.textImg.show = function() {
	
	if(this.state) return this.state;
	this.state = true;
	this.node.fade(1);
}

site.textImg.hide = function() {
	
	if(!this.state) return this.state;
	this.state = false;
	this.node.fade(0);
	this.rand.delay(500, this);
}

site.textImg.toggle = function() {

	if(this.state) {
		
		this.hide();
		
	} else {
		
		this.show();
	}

}



site.cat = {};
site.cat.img = '';

site.cat.hide = function() {
	$('tRight').tween('background-position', '-300px 300px');
}

site.cat.show = function() {
	
	var pos = '0px 1px';
	
	if(Browser.Engine.trident) pos = '0px 0px';

	$('tRight').tween('background-position', pos);
}

site.cat.setImg = function() {
	
	$('tRight').tween('background-image', 'url(' + this.img + ')');
}

site.cat.change = function(img) {

	try {
		
		if(!img || img.length < 5 || 
		   ($chk($('tRight').getStyle('background-image')) && $('tRight').getStyle('background-image').lastIndexOf(img) > -1) || 
		   ($chk($('tRight').getStyle('background')) && $('tRight').getStyle('background').lastIndexOf(img) > -1)) return false;

		this.img = img;
		this.hide();
		this.setImg.delay(400, this);
		this.show.delay(500, this);
		
		
	} catch(E) {
		alert(E);	
	}
}


site.checkRedirect = function() {

	if(Cookie.read("sHistory")) {
	
		var h = JSON.decode(Cookie.read("sHistory"));
		
		if(h.tl) site.windows.tl.setHref(h.tl);
		if(h.tr) site.windows.tr.setHref(h.tr);
		if(h.br) site.windows.br.setHref(h.br);
		
		Cookie.dispose('sHistory');
	}
	
	
	if(document.location.href.lastIndexOf('#') > -1) {
		
		var url = document.location.href.split('#');
		
		if(url[1] && url[1] != '/') {
			
			url = url[1].split('?win=');
			eval('try{ site.windows.' + url[1] + '.setHref("' + url[0] + '"); } catch(E) { site.windows.tl.setHref("' + url[0] + '"); }');
		}
		
	}
}


site.modifyLinks = function() {
		
		$('container').getElements('a').addEvent('click', function(event) {
											 
    			if(this.getProperty('href').lastIndexOf('mailto:') < 0) {
				
					event.stop(); 
					
					eval('try{ site.windows.' + (this.getProperty('target').length == 2 ? this.getProperty('target') : 'tl') + 
						 '.setHref("' + this.getProperty('href') + '"); } catch(E) { site.windows.tl.setHref("' + this.getProperty('href') + '"); }');
					
					return false;
				}
		});
}



site.sendForm = function(form) {
	
	form = $(form);
	
	var req = new Request.HTML({
						url: form.getProperty('action'),
						evalScripts: false,
						//encoding: 'windows-1251',
						onComplete: function() {
							site.windows.tr.setContent('<br />Запрос отправлен, обработка...');
						},
						
						onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
										 
										site.windows.tr.hide.delay(3000, site.windows.tr);
										var y = false;
			
										responseElements.each(function(item, index) { 
											
											if(item.hasClass('form_msg')) {
												site.windows.tr.setContent(item.get('html'));
												y = true;
											}
										});	 
										
										
										if(!y) site.windows.tr.setContent('Ошибка<br />Не ответа от сервера.');
										
						},
						
						onFailure: function(xhr) { 
										
										if(confirm('Произошла ошибка, попробовать еще раз?')) {
											req.post(form);
										}
						}
						});
	
	site.windows.tr.setContent('<br />Отправка запроса...');
	req.post(form);
}





site.gallery = null;


site.initialize = function() {
	
	site.textImg.node = $('abtext');
	
	site.modifyLinks();
	site.textImg.load();
	
	site.windows.tl = new sWindow('tl', 'Загрузка...');
	site.windows.tr = new sWindow('tr', 'Загрузка...');
	site.windows.br = new sWindow('br', 'Загрузка...');
	
	window.addEvent('resize', function(event) {
		site.windows.reShowAll(1);
	});

	site.textImg.node.fade('hide');
	$('loading').fade(0);
	site.show.delay(300);
	
	
	HistoryManager.initialize();
	site.pagi = new PaginationClass('');
	HistoryManager.start();
	
	site.checkRedirect.delay(1200, site);

}



//window.addEvent('domready', 
window.onload = function() {   

    try {
		
		site.initialize();
		
	} catch(E) {
		
		alert(E);
	}
	
}//); 



//-->
