//<--
/*

	@author: SWAT (Rostovtsev Ruslan)
	@url: http://www.jsysteam.ru
	@version: 1.0.0

*/


var pgallery = new Class({
	
	Implements: Options,

	options: {
		page:null
	},
	
	menu: null,
	gallery: null,
	
	initialize: function(options) {
		
		try {
			
			this.setOptions(options);
			var o = this.options;
			
			this.menu = o.page.getElement('.submenu').getElements('li');
			this.gallery = o.page.getElements('.pgallery');
			
			this.menu.each(function(item, index) {
			
				item.addEvent('click', function(event) {
					
					event.stop();
					
					try {
						$$(this.gallery).setStyle('display', 'none');
						$$(this.menu).removeClass('active');
						this.gallery[index].setStyle('opacity', 0);
						this.gallery[index].setStyle('display', 'block');
						this.gallery[index].tween('opacity', 1);
						item.toggleClass('active');
					} catch(E) {
						
					}
					
				}.bind(this));
									
			}.bind(this));
			
			
			var pevent = function(event) {
				event.stop();
				this.ul.getElements('li').removeClass('active');
				this.li.addClass('active');
				
				var h = ((this.gallery.getElement('li').getSize().y + 41) * 2);
				if(this.page > 1) h -= 2;
				//alert(h + ' ' + this.gallery.getSize().y);
				
				this.fx.start(0, h*this.page);
				this.ul.tween('top', h*this.page + 23);
			}
			
			this.gallery.each(function(item, index) {
				
				try {
				
					var count = item.getElements('li').length / 6;
	
					if(count > 1) {
						
						var ul = new Element('ul', {'class': 'paging', 'html': '<li class="title">Страницы:</li>'});
						
						var fx = new Fx.Scroll(item, {
							duration: 1000,
							wait: false
						});
						
						for(var i = 0; i < count; i++) {
							var li = new Element('li', {'html': i+1});
							if(!i) li.addClass('active');
							li.addEvent('click', pevent.bind({gallery: item, li: li, ul: ul, fx: fx, page: i}));
							ul.adopt(li);
						}
						item.adopt(ul);
					}
				
				} catch(E) {
					//alert(E);	
				}
			});
			
			
		} catch(E) {
			//alert(E);
		}
	}

});





var fly = new Class({
	
	Implements: Options,

	options: {
		pages: null,
		left: null,
		right: null,
		transition: Fx.Transitions.Expo.easeOut,
		duration: 1500
	},
	
	pages: null,
	cur:0,
	scroll: null,
	drag:null,
	oldScroll: 0,
	form:null,
	pg:null,
	gallery:null,
	
	initialize: function(options) {
		
		try {
			
			this.setOptions(options);
			var o = this.options;
			
			this.pages = o.pages.getChildren('li');
			
			o.left.addEvent('click', this.prev.bind(this));
			o.right.addEvent('click', this.next.bind(this));
			
			this.scroll = new Fx.Scroll(o.pages, {
				duration: o.duration,
				transition: o.transition,
				wait: false
			});
			
			o.left.setStyle('opacity', 0);
			
			var l = o.left.getElement('div').setStyle('opacity', 0);
			var r = o.right.getElement('div').setStyle('opacity', 0);
			
			o.right.addEvents({
				mouseenter: function() { r.tween('opacity', 1); }, 
				mouseleave: function() { r.tween('opacity', 0); }
			});
			
			o.left.addEvents({
				mouseenter: function() { l.tween('opacity', 1); }, 
				mouseleave: function() { l.tween('opacity', 0); }
			});
			
			this.resize(true);
			this.arrows();
			$(window).addEvent('resize', this.resize.bind(this));
			
			this.makeContacts();
			
			if(NotAuthorized == false) {
				
				this.pg = new pgallery({page: $('portfPage')});
				this.gallery = new UvumiGallery({container:'gallery', thumbSize:110, spacing:20});

			}
							

			
			
			
			$('pages').getElements('.mainmenu').each(function(item, index) {
				
				item.getElements('li').each(function(li, idx) {
					li.addEvent('click', function() { this.show(idx); }.bind(this));
				}.bind(this));
															  
			}.bind(this));
			

			$('pages').getElement('.menu').getElements('li').each(function(item, index) {
				
				item.addEvent('click', function() { this.show(index+1); }.bind(this));
															  
			}.bind(this));



// Косяк зарыт здесь:
			this.form = new FormCheck('contactForm', {
							display : {
								showErrors : 1,	
								closeTipsButton : 1,
								flashTips : 1
							},
							submitByAjax: true,
							ajaxResponseDiv: $('fResult'),
							onAjaxRequest: function() { 
								$('fResult').set('html', '<img src="/images/ajax-loader.gif" width="220" height="19" /><br />Подождите...');
							}
			});


		
		} catch(E) {
//			alert(E);	
		}

	},
	
	
	makeContacts: function() {
		
		var c = $('contacts');
		
		c.getElement('.ph').addEvent('click', function(event) {
		
			event.stop();
			var e = c.getElement('.ph_cont');
			
			if(e.getStyle('display') == 'none') {
				
				e.setStyle('opacity', 0);
				e.setStyle('display', 'block');	
				e.tween('opacity', 1);
				c.getElement('.wr_cont').setStyle('display', 'none');
				
			} else {
				e.tween('opacity', 0);
				(function() { this.setStyle('display', 'none'); }).delay(800, e);	
			}							   
		});
		
		c.getElement('.wr').addEvent('click', function(event) {
		
			event.stop();
			var e = c.getElement('.wr_cont');
			
			if(e.getStyle('display') == 'none') {
				
				e.setStyle('opacity', 0);
				e.setStyle('display', 'block');	
				e.tween('opacity', 1);
				c.getElement('.ph_cont').setStyle('display', 'none');
				
			} else {
				e.tween('opacity', 0);
				(function() { this.setStyle('display', 'none'); }).delay(800, e);	
			}							   
		});
		
		
		c.getElement('.ab').addEvent('click', function(event) {
		
			event.stop();
			this.showAbout();
										   
		}.bind(this));
		
		
	},
	
	/*
	onDrag: function() {
		
		var w = $(window).getSize().x;
		var s = this.options.pages.scrollLeft;
		
		this.cur = (s / w).round();
		this.arrows();
		
		var p = this.oldScroll <= s ? s - this.oldScroll : this.oldScroll - s;
		var ns = this.oldScroll <= s ? (s + p/4) : (s - p/4);
		
		this.oldScroll = s;
		this.scroll.start(ns, 0);
	},*/
	
	
	resize: function(first) {

		if(!first) $('loader').setStyles({display: 'block', opacity: 1});
		
		var w = $(window).getSize();
		this.pages.each(function(item, index) {
			item.setStyles({width: w.x, height:w.y, left: (index ? w.x * index : 0)});
		});
		this.center();
		
		if(!first) {
			this.show(this.cur);
			$('loader').tween('opacity', 0);
			(function() { $('loader').setStyle('display', 'none'); }).delay(1000);
		}
	},
	
	arrows: function(page) {
		
		var o = this.options;
		
		if(this.cur >= this.pages.length - 1) {
			o.right.tween('opacity', 0);
		} else {
			if(o.right.getStyle('opacity').toInt() < 1) o.right.tween('opacity', 1);
		}
		
		if(this.cur <= 0) {
			o.left.tween('opacity', 0);
		} else {
			if(o.left.getStyle('opacity').toInt() < 1) o.left.tween('opacity', 1);
		}
	},
	
	showAbout: function() {
		this.cur = 1;
		this.scroll.toElement($('bigBall'));
		this.arrows();
	},
	
	
	show: function(index) {
		this.cur = index;

//		this.scroll.toElement(this.pages[index]); // bug in IE :(
		this.scroll.start($(window).getSize().x * this.cur, 0);
		this.arrows();
	},
	
	next: function() {
		this.show(++this.cur);
	},
	
	prev: function() {
		this.show(--this.cur);
	},
	
	
	//Utils
	
	center: function() {
	
		var els = $(document.body).getElements('.ctr');
		
		els.each(function(item, index) {
		
			var p = item.getParent().getSize();
			var i = item.getSize();
			
			if(item.hasClass('vt')) {
				item.setStyle('top', (p.y / 2) - (i.y / 2));
			} else if(item.hasClass('hr')) {
				item.setStyle('left', (p.x / 2) - (i.x / 2));
			} else {
				item.setStyles({left: (p.x / 2) - (i.x / 2), top: (p.y / 2) - (i.y / 2)});
			}
		});
	}				   
});


var site = site || {};


//window.addEvent('domready', function() { 
window.onload = function() {

	site = new fly({pages: $('pages'), left: $('leftArrow'), right: $('rightArrow')});	

	$('loader').tween('opacity', 0);
	(function() { $('loader').setStyle('display', 'none'); }).delay(1000);
}
//});



//-->