(function($) {
	$.fn.makeGallery = function(o) {
		o = $.extend( {
			interval : 5000, /* интервал вращения 1000 = 1секунда */
			speed : 300, /* скорость перемещения 1000 = 1секунда */
			gallery_frame : '.visual-list',
			gallery_holder : 'ul',
			gallery_item : 'li'
		}, o || {});
		var steps = 1;
		$(this).each(
				function() {
					main_holder = $(this);
					var item_class = main_holder.find(o.gallery_item).attr('class');
					var step = ($(this).find(o.gallery_item).width()+parseInt($(this).find(o.gallery_item).css('margin-right'))+parseInt($(this).find(o.gallery_item).css('margin-left'))) * steps;
					main_holder.find(o.gallery_holder).css('width',main_holder.find(o.gallery_item).size()*($(this).find(o.gallery_item).width()+parseInt($(this).find(o.gallery_item).css('margin-right'))+parseInt($(this).find(o.gallery_item).css('margin-left'))));
					main_holder.bind('mouseover',function(){
						clearTimeout(t);
					});
					main_holder.bind('mouseleave',function(){
						t = setTimeout( oneStep, o.interval);
					});
					t = setTimeout(oneStep, o.interval);
					
					function oneStep () {
						main_holder.find(o.gallery_holder).animate({marginLeft:step*(-1)}, o.speed, function(){
							$(this).append('<'+o.gallery_item+' class="'+item_class+'">'+$(this).find(o.gallery_item+':first').html()+'</'+o.gallery_item+'>');
							$(this).find(o.gallery_item+':first').remove();
							$(this).css('margin-left','0');
						});
						t = setTimeout(oneStep, o.interval);
					};
					
				});
	};
})(jQuery);

(function($) {
	$.fn.makeTabset = function(o) {
		o = $.extend( {
			tab : '.tab',
			control : '.tabset',
			animate : false,
			animate_holder : '.holder'
		}, o || {});
		$(this).each(
				function() {
					tabset_holder = $(this);
					tabset_holder.find(o.control + ' li').each(function(){
						$(this).find('a').attr('href','tab'+($(this).index()+1));
					});
					var index = 1;
					tabset_holder.find(o.tab).each(function(){
						$(this).addClass('tab'+index);
						index += 1;
					});
					tabset_holder.find(o.control + ' li a').bind('click',function(){
						if (!$(this).parent().is('.active')) {
							if (o.animate == true) {
								tabset_holder.find(o.animate_holder).height(tabset_holder.find(o.animate_holder).height());
								tabset_holder.find(o.tab + '.' + tabset_holder.find(o.control + ' .active a').attr('href')).animate({
									opacity: 0.1
								},200, function(){
									$(this).css('position','absolute').css('left','-9999px');
								});
								tabset_holder.find(o.control + ' .active').removeClass('active');
								$(this).parent().addClass('active');
								tabset_holder.find(o.tab + '.' + $(this).attr('href')).css('width', tabset_holder.find(o.animate_holder).width()).css('position','absolute').css('left', '-9999px');
								var heightTo = tabset_holder.find(o.tab + '.' + tabset_holder.find(o.control + ' .active a').attr('href')).height();
								tabset_holder.find(o.animate_holder).animate({
									height: heightTo
								}, 500, function(){
									tabset_holder.find(o.tab + '.' + tabset_holder.find(o.control + ' .active a').attr('href')).css('width','100%').css('heigth','auto').css('position','relative').css('left', '0').css('opacity','0.01');
									tabset_holder.find(o.tab + '.' + tabset_holder.find(o.control + ' .active a').attr('href')).animate({opacity : 1}, 200);
									$(this).css('height','auto');
								});
							}
							else {
								tabset_holder.find(o.control + ' .active').removeClass('active');
								tabset_holder.find(o.tab).hide();
								$(this).parent().addClass('active');
								tabset_holder.find(o.tab + '.' + $(this).attr('href')).show();
							}
						}
						return false;
					});
				});
	};
})(jQuery);

$(document).ready(function(){
	$('.data-holder').makeTabset({
		control : '.data-set',
		tab : '.holder',
		animate : true,
		animate_holder : '.data-frame'
	});
	$('body').mousemove(function(e){
		var x_amplitude = 40;
		x_delta = Math.round( ($(this).width()/2 - e.pageX)/$(this).width() * x_amplitude);
		$(this).css('background-position',x_delta-20+'px 0px');
	});
	$('.visual-list').makeGallery();
	$('.btn-quest1').click(function(){
		popupBg();
		$('.quest-popup').fadeIn(200);
		return false;
	});
	$('.popup .close, .popup .bg').click(function(){
		$('.popup').fadeOut(200);
		return false;
	})
	popupBg();
});
$(window).resize(function(){
	popupBg();
});
function outhere () {
	$('.scrollable:not(.scrollable:first)').parents('.holder').css('position','absolute').css('left:-999px').css('height','864px');
};
function popupBg () {
	if ($(window).height() > $('#wrapper').height()+51) {
		$('.popup .bg').css('height',$(window).height())
	} else {
		$('.popup .bg').css('height',$('#wrapper').height()+51)
	}
	$('.popup .light-box').css('top',$(window).scrollTop()+40);
}
$(function(){
	initSlideShow();
});
function initSlideShow() {
	$('div.gallery').fadeGallery({
		slideElements:'div.gallery-frame > ul  > li',
		pauseOnHover:true,
		autoRotation:true,
		switchTime:6000,
		duration:650,
		event:'click'
	})
}
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'div.slideset > div',
		pagerLinks:'ul.swicher a',
		/*btnNext:'a.link-next',*/
		btnNext:'div.gallery-frame',
		btnPrev:'a.link-next',
		btnPlayPause:'a.play-pause',
		pausedClass:'paused',
		playClass:'playing',
		activeClass:'active',
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:false,
		switchTime:4000,
		duration:650,
		event:'click'
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _slides = jQuery(_options.slideElements, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _btnPlayPause = jQuery(_options.btnPlayPause, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _pausedClass = _options.pausedClass;
		var _playClass = _options.playClass;
		var _autoHeight = _options.autoHeight;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;

		var _hover = false;
		var _prevIndex = 0;
		var _currentIndex = 0;
		var _slideCount = _slides.length;
		var _timer;
		if(!_slideCount) return;
		_slides.hide().eq(_currentIndex).show();
		if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);
		else _this.removeClass(_playClass).addClass(_pausedClass);

		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentIndex != _ind) {
						_prevIndex = _currentIndex;
						_currentIndex = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		if(_btnPlayPause.length) {
			_btnPlayPause.bind(_controlEvent,function(){
				if(_this.hasClass(_pausedClass)) {
					_this.removeClass(_pausedClass).addClass(_playClass);
					_autoRotation = true;
					autoSlide();
				} else {
					if(_timer) clearTimeout(_timer);
					_this.removeClass(_playClass).addClass(_pausedClass);
				}
				return false;
			});
		}

		function prevSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else _currentIndex = _slideCount-1;
			switchSlide();
		}
		function nextSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex < _slideCount-1) _currentIndex++;
			else _currentIndex = 0;
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
			_slides.eq(_prevIndex).removeClass(_activeClass);
			_slides.eq(_currentIndex).addClass(_activeClass);
		}
		function switchSlide() {
			_slides.eq(_prevIndex).fadeOut(_duration);
			_slides.eq(_currentIndex).fadeIn(_duration);
			refreshStatus();
			autoSlide();
		}

		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		refreshStatus();
		autoSlide();
	});
}
function hideFormText() {
	var _inputs = document.getElementsByTagName('input');
	var _txt = document.getElementsByTagName('textarea');
	var _value = [];
	
	if (_inputs) {
		for(var i=0; i<_inputs.length; i++) {
			if (_inputs[i].type == 'text' || _inputs[i].type == 'password') {
				
				_inputs[i].index = i;
				_value[i] = _inputs[i].value;
				
				_inputs[i].onfocus = function(){
					if (this.value == _value[this.index])
						this.value = '';
				}
				_inputs[i].onblur = function(){
					if (this.value == '')
						this.value = _value[this.index];
				}
			}
		}
	}
	if (_txt) {
		for(var i=0; i<_txt.length; i++) {
			_txt[i].index = i;
			_value['txt'+i] = _txt[i].value;
			
			_txt[i].onfocus = function(){
				if (this.value == _value['txt'+this.index])
					this.value = '';
			}
			_txt[i].onblur = function(){
				if (this.value == '')
					this.value = _value['txt'+this.index];
			}
		}
	}
}
if (window.addEventListener)
	window.addEventListener("load", hideFormText, false);
else if (window.attachEvent)
	window.attachEvent("onload", hideFormText);

$(document).ready(function() {
	$("div.visual-list ul li a").attr('href','#');
	$("ul.subnav li a").each(function() {
		$(this).after('<span>'+$(this).attr('title')+'</span>');
  	});
})
