(function($) {
	$.fn.makeGallery = function(o) {
		o = $.extend( {
			interval : 5000, /* интервал вращения 1000 = 1секунда */
			speed : 1000, /* скорость перемещения 1000 = 1секунда */
			gallery_frame : '.slideshow',
			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 () {
						step=984;
						main_holder.find("ul").animate({marginLeft: (step*(-1))}, 1000, 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);

$(document).ready(function(){
	
	$('.slideshow').makeGallery();
});
function autoPos ( popup ) {
	$(popup).css('top',$(window).scrollTop()+40);
}
$(document).ready(function(){
	$('.side-menu li').hover(function(){
		var itemOffset = $(this).offset();
		var menuOffset = $('.side-menu').offset();
		if ($(this).find('.level1').height() > itemOffset.top - menuOffset.top + 38) {
			$(this).find('.level1').css('top',(-1)*(itemOffset.top - menuOffset.top + 38));
		} else {
			$(this).find('.level1').css('top','-20px');
		}
		
	});
	$('.multimedia-visual').each(function(){
		$(this).width($(this).find('img').width());
	});
	$('.multimedia-visual1').each(function(){
		$(this).width($(this).find('img').width());
	});
	$('.poll-button').click(function(){
		autoPos ($('.poll'));
		$('.poll').fadeIn(200);
		return false;
	});
	$('.poll .close').click(function(){
		$(this).parents('.poll').fadeOut(200);
		return false;
	});
	$('.poll .button').click(function(){
		$(this).parents('.poll').fadeOut(200);
		autoPos ($('.thanks'));
		$(this).parents('body').find('.thanks').fadeIn(200);
		return false;
	});
	$('.thanks .back').click(function(){
		$(this).parents('.thanks').fadeOut(200);
		return false;
	});
	
	$('.multimedia-visual a, .main-video .video a').fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'hideOnContentClick': true
	});
	$('.main-video .video a').fancybox({
		'transitionIn'	:	'fade',
		'transitionOut'	:	'fade',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false,
		'hideOnContentClick': true
	});
})
$(function(){
	initSlideShow();
});
function initSlideShow() {
	$('div.slideshow').fadeGallery({
		slideElements:'.frame ul li',
		pauseOnHover:true,
		autoRotation:true,
		switchTime:2000,
		duration:650,
		event:'click'
	})
}
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'div.slideset > .frame ul li',
		pagerLinks:'ul.control a',
		btnNext:'a.next',
		btnPrev:'a.prev',
		btnPlayPause:'a.play-pause',
		pausedClass:'paused',
		playClass:'playing',
		activeClass:'active',
		pauseOnHover:true,
		autoRotation:false,
		autoHeight:false,
		switchTime:3000,
		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 initPage()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);
