$(window).bind('load', function(){
	$('div.mainbg').mouseGallerySlide({
		scrollElParent: 'div.gallery',
		scrollEl: 'img'
	});
	initAboutPage();
	$('.content-scroll').customScrollV({lineWidth: 7})
});

function initAboutPage(){
	
	$('div.content-wrap div.content-scroll').each(function(){
		var hold = $(this);
		var head = $('div.header').outerHeight(true);
		var menu = $('ul.menu').outerHeight(true);
		var main = $('div.mainblock')
		var footer = $('div.footer-place').outerHeight(true);
		
		if ($(window).height() - head -menu -footer - 64 > 86)
			hold.css({height: $(window).height() - head -menu -footer - 64});
		else
			hold.css({height: 86});
		
		main.css({height: $('#wrapper').height()});
		
		$(window).resize(function(){
			
			if ($(window).height() - head -menu -footer - 64 > 86)
				hold.css({height: $(window).height() - head -menu -footer - 64});
			else
				hold.css({height: 86});
			main.css({height: $('#wrapper').height()});
		});
	});
}

jQuery.fn.mouseGallerySlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		scrollElParent: 'ul',
		scrollEl: 'li'
	},_options);

	return this.each(function(){
		var _this = $(this);

		var _gWidth = _this.outerWidth();
		var _scrollElParent = jQuery(_options.scrollElParent,_this);
		var _scrollEl = jQuery(_options.scrollEl,_this);
		var _liWidth = _scrollEl.outerWidth(true);
		var _liSum = _scrollEl.length * _liWidth;
		
		var _sec = 30;
		var _maxMargin = _liSum - $('body').width();
		var _sss = Math.floor((_liSum - $('body').width())/100)+1;
		
		var _posHolder = _this.offset();
		var _width = _this.outerWidth();
		var _height = _this.outerHeight();
		
		var _chapter = _gWidth/12;
		var _speed = 0
		var _direction = 2;
		var _timerOut = false;
		var _time;
		
		_scrollElParent.css({marginLeft: -(_scrollElParent.outerWidth()-$('body').width())/2 });
		jQuery(window).resize(function(){
			_posHolder = _this.offset();
			_maxMargin = _liSum - $('body').width();
			_sss = Math.floor((_liSum - $('body').width())/100)+1;
			_scrollElParent.css({marginLeft: -(_scrollElParent.outerWidth()-$('body').width())/2 });
		});
		
		$('div.bg-nav > div.left').mouseover(function(){
			_direction = 1;
			_speed = 0.005/_sss;
			animateEl();
			_time = setTimeout(function(){
				_speed = 0.009/_sss;
				animateEl();
				_time = setTimeout(function(){
					_speed = 0.015/_sss;
					animateEl();
					_time = setTimeout(function(){
						_speed = 0.022/_sss;
						animateEl();
						_time = setTimeout(function(){
							_speed = 0.030/_sss;
							animateEl();
							
						}, 300);
					}, 400);
				}, 500);
			}, 600);
		});
		$('div.bg-nav > div.left').mouseout(function(){
			_scrollElParent.stop();
			if(_time) clearTimeout(_time);
		});
		$('div.bg-nav > div.right').mouseover(function(){
			_direction = 3;
			_speed = 0.005/_sss;
			animateEl();
			_time = setTimeout(function(){
				_speed = 0.009/_sss;
				animateEl();
				_time = setTimeout(function(){
					_speed = 0.015/_sss;
					animateEl();
					_time = setTimeout(function(){
						_speed = 0.022/_sss;
						animateEl();
						_time = setTimeout(function(){
							_speed = 0.030/_sss;
							animateEl();
							
						}, 300);
					}, 400);
				}, 500);
			}, 600);
		});
		$('div.bg-nav > div.right').mouseout(function(){
			_scrollElParent.stop();
			if(_time) clearTimeout(_time);
		});
		
		function animateEl() {
			if (_timerOut) clearTimeout(_timerOut);
			_scrollElParent.stop();
			var _curMargin = parseInt(_scrollElParent.css('marginLeft'));
			
			if (_direction == 1) {
				var k = -_curMargin/_maxMargin;
				_scrollElParent.stop()
					.animate(
						{marginLeft:0},
						{easing:'linear',duration:(_sec/_speed)*k, complete:function(){
							_scrollElParent.stop()
						}}
					);
			}
			if (_direction == 3) {
				var k = (_maxMargin + _curMargin)/_maxMargin;
				_scrollElParent.stop()
					.animate(
						{marginLeft:-_maxMargin},
						{easing:'linear',duration:(_sec/_speed)*k, complete:function(){
							_scrollElParent.stop()
						}}
					);
			}
		}
	});
}

/*--- mousewheel ---*/
var types = ['DOMMouseScroll', 'mousewheel'];
jQuery.event.special.mousewheel = {
	setup: function() {
		if ( this.addEventListener ) for ( var i=types.length; i; ) this.addEventListener( types[--i], handler, false );
		else this.onmousewheel = handler;
	},
	teardown: function() {
		if ( this.removeEventListener ) for ( var i=types.length; i; ) this.removeEventListener( types[--i], handler, false );
		else this.onmousewheel = null;
	}
};
jQuery.fn.extend({
	mousewheel: function(fn) {
		return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
	},
	unmousewheel: function(fn) {
		return this.unbind("mousewheel", fn);
	}
});
function handler(event) {
	var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;
	event = jQuery.event.fix(event || window.event);
	event.type = "mousewheel";
	if ( event.wheelDelta ) delta = event.wheelDelta/120;
	if ( event.detail     ) delta = -event.detail/3;
	args.unshift(event, delta);
	return jQuery.event.handle.apply(this, args);
}
/*--- custom scroll ---*/
jQuery.fn.customScrollV = function(_options){
var _options = jQuery.extend({
	lineWidth: 16 /* this parameter sets the width of the scroll*/
}, _options);
return this.each(function(){
	var _box = jQuery(this);
	if(_box.is(':visible')){
		if(_box.children('.scroll-content').length == 0){
			var line_w = _options.lineWidth;
			/*--- init part ---*/
			var scrollBar = jQuery('<div class="vscroll-bar"><div class="scroll-up"><img src="/Images/tishkovo/scroll.gif" alt="image description" /></div><div class="scroll-line"><div class="bg-b"></div><div class="scroll-slider jScrollPaneDrag"></div></div><div class="scroll-down"><img style="margin-top: -11px; display: block;" src="/Images/tishkovo/scroll.gif" alt="image description" /></div></div>');
			_box.wrapInner('<div class="scroll-content"><div class="scroll-hold"></div></div>').append(scrollBar);
			var scrollContent = _box.children('.scroll-content');
			var scrollSlider = scrollBar.find('.scroll-slider');
			var scrollSliderH = scrollSlider.parent();
			var scrollUp = scrollBar.find('.scroll-up');
			var scrollDown = scrollBar.find('.scroll-down');
			/*--- different variables ---*/
			var box_h = _box.height();
			var slider_h = 0;
			var slider_f = 0;
			var cont_h = scrollContent.height();
			var _f = false;
			var _f1 = false;
			var _f2 = true;
			var _t1, _t2, _s1, _s2;
			/*--- set styles ---*/
			_box.css({
				position: 'relative',
				overflow: 'hidden',
				height: box_h
			});
			scrollContent.css({
				position: 'relative',
				top: 0,
				left: 0,
				zIndex: 1,
				height: 'auto'
			});
			scrollBar.css({
				position: 'absolute',
				top: 0,
				right: 0,
				zIndex:2,
				width: line_w,
				height: box_h,
				overflow: 'hidden'
			});
			scrollUp.css({
				width: line_w,
				height: line_w,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			scrollDown.css({
				width: line_w,
				height: line_w,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			slider_h = scrollBar.height();
			if(scrollUp.is(':visible')) slider_h -= scrollUp.height();
			if(scrollDown.is(':visible')) slider_h -= scrollDown.height();
			scrollSliderH.css({
				position: 'relative',
				width: line_w,
				height: slider_h,
				overflow: 'hidden'
			});
			slider_h = 0;
			scrollSlider.css({
				position: 'absolute',
				top: 0,
				left: 0,
				width: line_w,
				height: slider_h,
				overflow: 'hidden',
				cursor: 'pointer'
			});
			box_h = _box.height();
			cont_h = scrollContent.height();
			if(box_h < cont_h){
				_f = true;
				slider_h = Math.round(box_h/cont_h*scrollSliderH.height());
				if(slider_h < 5) slider_h = 5;
				scrollSlider.height(slider_h);
				slider_h = scrollSlider.outerHeight();
				slider_f = (cont_h - box_h)/(scrollSliderH.height() - slider_h);
				_s1 = (scrollSliderH.height() - slider_h)/15;
				_s2 = (scrollSliderH.height() - slider_h)/3;
				scrollContent.children('.scroll-hold').css('padding-right', scrollSliderH.width());
			}
			else{
				_f = false;
				scrollBar.hide();
				scrollContent.css({width: _box.width(), top: 0, left:0});
				scrollContent.children('.scroll-hold').css('padding-right', 0);
			}
			var _top = 0;
			/*--- element's events ---*/
			scrollUp.mousedown(function(){
				_top -= _s1;
				scrollCont();
				_t1 = setTimeout(function(){
					_t2 = setInterval(function(){
						_top -= 4/slider_f;
						scrollCont();
					}, 20);
				}, 500);
				return false;
			}).mouseup(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			}).mouseleave(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			});
			scrollDown.mousedown(function(){
				_top += _s1;
				scrollCont();
				_t1 = setTimeout(function(){
					_t2 = setInterval(function(){
						_top += 4/slider_f;
						scrollCont();
					}, 20);
				}, 500);
				return false;
			}).mouseup(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			}).mouseleave(function(){
				if(_t1) clearTimeout(_t1);
				if(_t2) clearInterval(_t2);
			});
			scrollSliderH.click(function(e){
				if(_f2){
					_top = e.pageY - scrollSliderH.offset().top - scrollSlider.outerHeight()/2;
					scrollCont();
				}
				else{
					_f2 = true;
				}
			});
			var t_y = 0;
			var tttt_f = (jQuery.browser.msie)?(true):(false);
			scrollSlider.mousedown(function(e){
				t_y = e.pageY - jQuery(this).position().top;
				_f1 = true;
				return false;
			}).mouseup(function(){
				_f1 = false;
			});
			jQuery('body').mousemove(function(e){
				if(_f1){
					 _f2 = false;
					 _top = e.pageY - t_y;
					 if(tttt_f) document.selection.empty();
					 scrollCont();
				}
			}).mouseup(function(){
				_f1 = false;
			});
			document.body.onselectstart = function(){
				if(_f1) return false;
			}
			if(!_box.hasClass('not-scroll')){
				_box.bind('mousewheel', function(event, delta){
					if(_f){
						_top -=delta*_s1;
						scrollCont();
						if((_top > 0) && (_top+slider_h < scrollSliderH.height())) return false;
					}
				});
			}
			function scrollCont(){
				if(_top < 0) _top = 0;
				else if(_top+slider_h > scrollSliderH.height()) _top = scrollSliderH.height() - slider_h;
				scrollSlider.css('top', _top);
				scrollContent.css('top', -_top*slider_f);
			}
			this.scrollResize = function(){
				box_h = _box.height();
				cont_h = scrollContent.height();
				if(box_h < cont_h){
					_f = true;
					scrollBar.show();
					scrollBar.height(box_h);
					slider_h = scrollBar.height();
					
					if(scrollUp.is(':visible')) slider_h -= scrollUp.height();
					if(scrollDown.is(':visible')) slider_h -= scrollDown.height();
					scrollSliderH.height(slider_h);
					slider_h = Math.round(box_h/cont_h*scrollSliderH.height());
					if(slider_h < 5) slider_h = 5;
					scrollSlider.height(slider_h);
					slider_h = scrollSlider.outerHeight();
					slider_f = (cont_h - box_h)/(scrollSliderH.height() - slider_h);
					if(cont_h + scrollContent.position().top < box_h) scrollContent.css('top', -(cont_h - box_h));
					_top = - scrollContent.position().top/slider_f;
					scrollSlider.css('top', _top);
					_s1 = (scrollSliderH.height() - slider_h)/15;
					_s2 = (scrollSliderH.height() - slider_h)/3;
					scrollContent.children('.scroll-hold').css('padding-right', scrollSliderH.width());
				}
				else{
					_f = false;
					scrollBar.hide();
					scrollContent.css({top: 0, left:0});
					scrollContent.children('.scroll-hold').css('padding-right', 0);
				}
			}
			
			setInterval(function(){
				if(_box.is(':visible') && cont_h != scrollContent.height()) _box.get(0).scrollResize();
			}, 200);
			
		}
		else{
			this.scrollResize();
		}
		var t = this;
		$(window).resize(function(){
			t.scrollResize();
		});
	}
})}

	
