$(function(){
	var g2 = new initGallery(jQuery('.gallery'),{
		list: '.holder ul',
		switcher: 'ul.switch > li',
		prev: 'a.link-prev',
		next: 'a.link-next',
		effect: 'slide',
		autoRotation: 4000
	});
	initHoverFade();
	initLogoSlider({
		holder:'#header .logo-box',
		element:'.logo',
		animateNumber:9
		
	});
	initSliderButton({
		holder:'.link-start',
		element:'.link-holder',
		animateNumber:14
	});
	initFadeBlockHover();
	initSlideIcon();
});

//init hover fade
function initHoverFade(){
	var duration = 500;
	$('.catalog,.catalog-inner').each(function(){
		var holder = $(this);
		var liItems = holder.find('li');
		
		liItems.each(function(){
			var currentLi = $(this);
			var currentLink = currentLi.find('.link');
			currentLi.hover(function(){
				currentLink.show().css({opacity:0}).stop().animate({opacity:0.96},{
					duration:duration,
					queue:true,
					complete:function(){
						currentLink.css({display:'block'});
					}
				});
			},
			function(){
				currentLink.stop().animate({opacity:0},{
					duration:duration,
					queue:true,
					complete:function(){
						currentLink.css({display:'none'});
					}
				});
			});
		});
	});
}

//init logo slider
function initLogoSlider(options){
	var duration = 300;
	$(options.holder).each(function(){
		var holder = $(this);
		var slideBlock = holder.find(options.element);
		var marginTop = slideBlock.css('marginTop');
		slideBlock.hover(function(){
			slideBlock.stop().animate({marginTop:options.animateNumber},{duration:duration});
		},
		function(){
			slideBlock.stop().animate({marginTop:marginTop},{duration:duration});
		});
	});
}

//init content button slider
function initSliderButton(options){
	var duration = 300;
	$(options.holder).each(function(){
		var holder = $(this);
		var slideBlock = holder.find(options.element);
		var marginTop = slideBlock.css('marginTop');
		slideBlock.hover(function(){
			slideBlock.stop().animate({marginTop:options.animateNumber},{duration:duration});
		},
		function(){
			slideBlock.stop().animate({marginTop:marginTop},{duration:duration});
		});
	});
}

// init fade block hover
function initFadeBlockHover(){
	var duration = 350;
	$('.boxes .box').each(function(){
		var holder = $(this);
		var continer = holder.find('.title');
		
		continer.each(function(){
			var current = $(this);
			var currentEl = current.find('h4:eq(1)').show().css({opacity:0,zIndex:1});
			var notHover = current.find('h4:eq(0)').show();

			currentEl.hover(function(){
				currentEl.stop().animate({opacity:1},{duration:duration});
			},
			function(){
				if(currentEl.hasClass('hover')){
					currentEl.stop().animate({opacity:0},
					{
						duration:duration,
						complete:function(){
							notHover.css({opacity:1}).show();
						}
					});
				}
			});
		});
	});
}

// init slide icon
function initSlideIcon(){
	$('.social-networks').each(function(){
		var holder = $(this);
		var spans = holder.find('span');
		
		spans.each(function(){
			var timer;
			var currentSpan = $(this);
			currentSpan.hover(function(){
				clearTimeout(timer);
				timer = setTimeout(function(){
					currentSpan.stop().animate({marginTop:-66},{duration:400,queue:false});
				},250);
			},
			function(){
				clearTimeout(timer);
				timer = setTimeout(function(){
					currentSpan.animate({marginTop:-99},{
						duration:250,
						queue:false,
						complete:function(){
							currentSpan.css({marginTop:33});
							currentSpan.animate({marginTop:0},{
								duration:250
							});
						}
					});
				},50);
				
			});
			
		});
		
	});
}

// main gallery module
function initGallery(context, options){this.init(context, options)}
(function( $ ){
	initGallery.prototype = {
		autoRotation: false,
		disableBtn: false,
		list: 'ul.g1',
		switcher: false,
		prev: false,
		next: false,
		effect: false,
		event:'click',
		onStart: function(){},
		beforeChange: function(){},
		onChange: function(){},
		switcherClick: function(){},
		
		init: function(context, options){
			for ( var i in options ) this[i] = options[i]; 
			this.holder = $(context);
			if(this.holder == undefined) return;
			
			this.list = this.holder.find(this.list);
			this.animation = true, this.active = this.list.children().filter('.active').length ? this.list.children().index(this.list.children().filter('.active')) : 0;
			this.prevActive = this.active, this.wait;
			this.count = this.list.children().length,
			this.w = this.list.children().eq(0).outerWidth(true);
			this.holdW = this.list.parent().width();
			this.visEl = Math.ceil(this.holdW/this.w);
			
			if(this.count <= this.visEl) this.animation = false;
			
			if(this.effect == 'fade' && !(jQuery.browser.msie && jQuery.browser.version < 9)) this.list.children().css('opacity', 0).eq(this.active).css('opacity', 1).addClass('active');
			else if(this.effect == 'fade' && jQuery.browser.msie && jQuery.browser.version < 9) this.list.children().hide().eq(this.active).show().addClass('active');
			else if(this.effect == 'slide') this.list.css('marginLeft', -this.w*this.active);
			else if(this.effect == 'slideY') {
				this.h = this.list.children().eq(0).outerHeight(true);
				this.list.css('marginTop', -this.h*this.active);
			}

			this.initControls(this);
			
			this.toggleState.listItem(this);
			if(this.switcher) this.toggleState.switchItem(this);
			
			this.onStart(this, this.list, this.active);
			
			if(this.autoRotation && this.animation) this.runTimer(this);
		},
		initControls: function(_this){
			if(_this.prev && _this.next){
				_this.prev = _this.holder.find(_this.prev).attr('rel', 'prev').click(function(e){
					_this.beforeChange(_this, e);
					_this.refreshState(e);
					return false;
				});
				_this.next = _this.holder.find(_this.next).attr('rel', 'next').click(function(e){
					_this.beforeChange(_this, e);
					_this.refreshState(e);
					return false;
				});
			}
			if(_this.switcher){
				_this.switcher = _this.holder.find(_this.switcher);
				_this.toggleState.switchItem(_this);
				_this.switcher.bind(_this.event, function(){
					var ind = _this.switcher.index($(this));
					_this.switcherClick(_this);
					_this.refreshState(ind);
					return false;
				});
			}
			if(this.disableBtn) this.disabledConrtol();
		},
		toggleState:{
			listItem: function(_this){
				_this.list.children().eq(_this.prevActive).removeClass('active');
				_this.list.children().eq(_this.active).addClass('active');
			},
			switchItem: function(_this){
				_this.switcher.eq(_this.prevActive).removeClass('active');
				_this.switcher.eq(_this.active).addClass('active');
			}
		},
		disabledConrtol: function(){
			if(this.active == 0) {
				this.prev.parent().addClass('disabled');
				this.next.parent().removeClass('disabled');
			}
			else if(this.active == this.count-1 || (this.visEl > 2 && this.active + this.visEl == this.count)) {
				this.prev.parent().removeClass('disabled');
				this.next.parent().addClass('disabled');
				this.autoRotation = false;
			}
			else {
				this.prev.parent().removeClass('disabled');
				this.next.parent().removeClass('disabled');
			}
		},
		runTimer: function(_this){
			_this.beforeChange(_this);
			this.wait = setTimeout(function(){_this.refreshState('next')}, this.autoRotation)
		},
		stop: function(){
			if(this.wait) clearTimeout(this.wait)
		},
		play: function(){
			if(this.wait) clearTimeout(this.wait);
			if(this.autoRotation) this.runTimer(this);
		},
		changeSlide:{
			fade:function(_this){
				if(jQuery.browser.msie && jQuery.browser.version < 9){
					_this.list.children().eq(_this.prevActive).hide();
					_this.list.children().eq(_this.active).show();
					_this.play();
					_this.onChange(_this, _this.list, _this.active);
				}
				else {
					_this.list.children().eq(_this.prevActive).animate({opacity:0}, {queue:false, duration:700});
					_this.list.children().eq(_this.active).animate({opacity:1}, {queue:false, duration:700, complete:function(){
						_this.play();
						_this.onChange(_this, _this.list, _this.active);
					}});
				}
			},
			slide:function(_this){
				if(_this.active + _this.visEl > _this.count) _this.active = 0;
				_this.list.animate({marginLeft:-_this.w*_this.active}, {queue:false, duration:500, complete:function(){
					_this.play();
					_this.onChange(_this, _this.list, _this.active);
				}});
			},
			slideY:function(_this){
				_this.list.animate({marginTop:-_this.h*_this.active}, {queue:false, duration:500, complete:function(){
					_this.play();
					_this.onChange(_this, _this.list, _this.active);
				}});
			}
		},
		refreshState: function(e){
			if(this.animation){
				this.prevActive = this.active;
				if(typeof e == 'string' && e == 'next') this.active++;
				else if(typeof e == 'number') this.active=e;
				else{
					if(e.currentTarget.rel == 'next') this.active++;
					else if(e.currentTarget.rel == 'prev') this.active--;
				}
				if(this.wait) clearTimeout(this.wait);
				if(this.active == this.count) this.active = 0;
				else if(this.active == -1) this.active=this.count - 1;
				
				this.toggleState.listItem(this);
				if(this.switcher) this.toggleState.switchItem(this);
				
				if(this.disableBtn) this.disabledConrtol();
				
				if(this.effect == 'fade') this.changeSlide.fade(this);
				else if(this.effect == 'slide') this.changeSlide.slide(this);
				else if(this.effect == 'slideY') this.changeSlide.slideY(this);
			}
		}	
	}
})( jQuery );

