	
	var is_ie6 = false;
	var is_ie7 = false;
	jQuery.each(jQuery.browser, function(i, val) {
		if(i=="msie" && jQuery.browser.version.substr(0,3)=="6.0"){is_ie6 = true;}
		if(i=="msie" && jQuery.browser.version.substr(0,3)=="7.0"){is_ie7 = true;}
	});
	
	
	var scr, scrbar, block, content, block_height, content_height, content_delta, scroller_theight, scroller_height, scroller_delta, delta, canDrag, shift_y;
	
	// Slideshow
	function slideSwitch() {
		var $active = $('#slideshow div.active');
		if ( $active.length == 0 ) $active = $('#slideshow div:last');
		var $next =  $active.next().length ? $active.next()
			: $('#slideshow div:first');

		$active.addClass('last-active');
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});
	}
	$(function() {
		setInterval( "slideSwitch()", 7500 );
	});
	
	
	// Slider
	$(document).ready(function() {
		if ($('div.slider')) {
			$('div.slider img').each(function(i){
				if (i == 0) {
					$(this).addClass('visible');
				} else {
					$(this).addClass('invisible');
				}
			});
			// Next
			$('div.slider a.next').click(function(){
				if ($('div.slider').length > 0){
					img1 = $('div.slider img:visible');
					$('div.slider img:visible').fadeOut(300, function(){
						if ($(this).next().html() != null) {
							$(this).next().fadeIn(300);
						} else {
							$(this).parent().children('img:first').fadeIn(300);
						}
					});
				}
				return false;
			});
			// Prev
			$('div.slider a.prev').click(function(){
				if ($('div.slider').length > 0){
					$('div.slider img:visible').fadeOut(300, function(){
						if ($(this).prev().hasClass('next')) {
							$(this).parent().children('img:last').fadeIn(300);
						} else {
							$(this).prev().fadeIn(300);
						}
					});
				}
				return false;
			});
		}
	});
	
	
	// Gallery
	$(document).ready(function() {
		if ($('#slideshow').length > 0 || $('div.body div.gallery').length > 0) {
			$('div.body').addClass('preloader');
		}
		$('#slideshow').fadeTo(0, 0);
		$('div.gallery div.left-block').fadeTo(0, 0.2);
		$('div.gallery div.right-block').fadeTo(0, 0.2);
		$('div.body div.gallery').fadeTo(0, 0);
		var allImages = '';
		allImages = $('div.gallery div.left-block div').html() + $('div.gallery div.middle-block').html() + $('div.gallery div.right-block div').html();
		if ($('div.gallery div.left-block img').length < 4) {
			$('div.gallery div.left-block div').prepend(allImages);
		}
		if ($('div.gallery div.right-block img').length < 4) {
			$('div.gallery div.right-block div').append(allImages);
		}
		var img_ids = new Array();
		$('div.gallery img').each(function(index) {
			//img_ids[] = $(this).attr('id');
			img_ids.push($(this).attr('id')); 
		});
		var current_photo = $('div.gallery div.middle-block img').attr('id');
		for ( keyVar in img_ids ) {
			if (current_photo == img_ids[keyVar]){
				$('#img_li'+current_photo).css('padding', '0 2px 6px 0');
				$('#img_li'+current_photo).css('position', 'relative');
				$('#img_li'+current_photo).css('left', '-2px');
				$('#img_li'+current_photo+' img').css('border', '2px solid orange');
			}else{
				$('#img_li'+img_ids[keyVar]).css('padding', '0 6px 10px 0');
				$('#img_li'+img_ids[keyVar]).css('position', '');
				$('#img_li'+img_ids[keyVar]).css('left', '');
				$('#img_li'+img_ids[keyVar]+' img').css('border', '0px solid orange');
			}
		}

		// Next
		$('div.gallery div.right-block').click(function() {
			$('div.gallery div.left-block div').append($('div.gallery div.middle-block').html());
			$('div.gallery div.middle-block').html($('div.gallery div.right-block img:first'));
			var current_photo = $('div.gallery div.middle-block img').attr('id');
			for ( keyVar in img_ids ) {
				if (current_photo == img_ids[keyVar]){
					$('#img_li'+current_photo).css('padding', '0 2px 6px 0');
					$('#img_li'+current_photo).css('position', 'relative');
					$('#img_li'+current_photo).css('left', '-2px');
					$('#img_li'+current_photo+' img').css('border', '2px solid orange');
				}else{
					$('#img_li'+img_ids[keyVar]).css('padding', '0 6px 10px 0');
					$('#img_li'+img_ids[keyVar]).css('position', '');
					$('#img_li'+img_ids[keyVar]).css('left', '');
					$('#img_li'+img_ids[keyVar]+' img').css('border', '0px solid orange');
				}
			}
			var middleBlockWidth = $('div.gallery div.middle-block img').width();
			var arrowsBlockWidth = (921 - middleBlockWidth) / 2;
			var leftBlockWidth = 0;
			var leftBlockMargin = 0;
			var rightBlockWidth = 0;
			$('div.gallery div.left-block img').each(function(){
				leftBlockWidth += $(this).width() + 1;
			});
			leftBlockMargin = (leftBlockWidth - arrowsBlockWidth) * -1;
			$('div.gallery div.right-block img').each(function(){
				rightBlockWidth += $(this).width() + 1;
			});
			$('div.gallery div.left-block').width(arrowsBlockWidth);
			$('div.gallery div.right-block').width(arrowsBlockWidth);
			$('div.gallery div.middle-block').width(middleBlockWidth);
			$('div.gallery div.left-block div').css({'margin-left':leftBlockMargin});
			$('div.gallery div.left-block div').width(leftBlockWidth);
			$('div.gallery div.right-block div').width(rightBlockWidth);
			if ($('div.gallery div.right-block img').length < 4) {
				$('div.gallery div.right-block div').append(allImages);
			}
			$('div.gallery div.left-block').fadeTo(500, 0.2);
			$('div.gallery div.right-block').fadeTo(500, 0.2);
			$('div.gallery div.middle-block img').fadeTo(0, 0);
			$('div.gallery div.middle-block img').fadeTo(500, 1);
		});
		// Prev
		$('div.gallery div.left-block').click(function() {
			$('div.gallery div.right-block div').prepend($('div.gallery div.middle-block').html());
			$('div.gallery div.middle-block').html($('div.gallery div.left-block img:last'));
			var current_photo = $('div.gallery div.middle-block img').attr('id');
			for ( keyVar in img_ids ) {
				if (current_photo == img_ids[keyVar]){
					$('#img_li'+current_photo).css('padding', '0 2px 6px 0');
					$('#img_li'+current_photo).css('position', 'relative');
					$('#img_li'+current_photo).css('left', '-2px');
					$('#img_li'+current_photo+' img').css('border', '2px solid orange');
				}else{
					$('#img_li'+img_ids[keyVar]).css('padding', '0 6px 10px 0');
					$('#img_li'+img_ids[keyVar]).css('position', '');
					$('#img_li'+img_ids[keyVar]).css('left', '');
					$('#img_li'+img_ids[keyVar]+' img').css('border', '0px solid orange');
				}
			}
			var middleBlockWidth = $('div.gallery div.middle-block img').width();
			var arrowsBlockWidth = (921 - middleBlockWidth) / 2;
			var leftBlockWidth = 0;
			var leftBlockMargin = 0;
			var rightBlockWidth = 0;
			$('div.gallery div.left-block img').each(function(){
				leftBlockWidth += $(this).width() + 1;
			});
			leftBlockMargin = (leftBlockWidth - arrowsBlockWidth) * -1;
			$('div.gallery div.right-block img').each(function(){
				rightBlockWidth += $(this).width() + 1;
			});
			$('div.gallery div.left-block').width(arrowsBlockWidth);
			$('div.gallery div.right-block').width(arrowsBlockWidth);
			$('div.gallery div.middle-block').width(middleBlockWidth);
			$('div.gallery div.left-block div').css({'margin-left':leftBlockMargin});
			$('div.gallery div.left-block div').width(leftBlockWidth);
			$('div.gallery div.right-block div').width(rightBlockWidth);
			if ($('div.gallery div.left-block img').length < 4) {
				$('div.gallery div.left-block div').prepend(allImages);
			}
			$('div.gallery div.left-block').fadeTo(500, 0.2);
			$('div.gallery div.right-block').fadeTo(500, 0.2);
			$('div.gallery div.middle-block img').fadeTo(0, 0);
			$('div.gallery div.middle-block img').fadeTo(500, 1);
		});
		$('div.gallery a.next').click(function(){
			$('div.gallery div.right-block').trigger('click');
			return false;
		});
		$('div.gallery a.prev').click(function(){
			$('div.gallery div.left-block').trigger('click');
			return false;
		});
	});
	
	
	// Navigation
	$(document).ready(function() {
		var ulEnter = false;
		$('div.navigation li.selected').each(function(){
			$(this).children('a').children('img').css({'margin-top':'-10px'});
		});
		$('div.navigation li').mouseenter(function(){
			if ($(this).children('ul').length > 0) {
				$(this).parent().find('ul').fadeOut(0);
				$(this).children('ul').fadeIn(0);
				if ($(this).children('ul').hasClass('level-3')) {
					$('div.nblock').css({'height':'96px'});
				}
				if ($(this).children('ul').hasClass('level-4')) {
					$('div.nblock').css({'height':'128px'});
				}
				if ($(this).children('ul').hasClass('level-5')) {
					$('div.nblock').css({'height':'160px'});
				}
				if ($(this).children('ul').hasClass('level-6')) {
					$('div.nblock').css({'height':'192px'});
				}
			}
		});
		$('div.navigation li').mouseleave(function(){;
			$(this).parent().find('ul').fadeOut(0);
			if ($(this).parent().hasClass('level-1') && $(this).hasClass('selected')) {
				$(this).children('ul').fadeIn(0);
			}
			if ($(this).parent().find('ul').hasClass('level-6')) {
				$('div.nblock').css({'height':'160px'});
			}
			if ($(this).parent().find('ul').hasClass('level-5')) {
				$('div.nblock').css({'height':'128px'});
			}
			if ($(this).parent().find('ul').hasClass('level-4')) {
				$('div.nblock').css({'height':'96px'});
			}
			if ($(this).parent().find('ul').hasClass('level-3') || $(this).parent().find('ul').hasClass('level-2') || $(this).parent().find('ul').hasClass('level-1')) {
				$('div.nblock').css({'height':'64px'});
			}
		});
		$('div.navigation ul.level-1').children('li').mouseleave(function(){;
			$(this).parent().find('ul').fadeOut(0);
			$('div.navigation ul.level-1 li.selected').children('ul').fadeIn(0);
		});
	});
	
	
	// Window Onload
	window.onload = function() {
		var middleBlockWidth = $('div.gallery div.middle-block img').width();
		var arrowsBlockWidth = (921 - middleBlockWidth) / 2;
		var leftBlockWidth = 0;
		var leftBlockMargin = 0;
		var rightBlockWidth = 0;
		$('div.gallery div.left-block img').each(function(){
			leftBlockWidth += $(this).width() + 1;
		});
		leftBlockMargin = (leftBlockWidth - arrowsBlockWidth) * -1;
		$('div.gallery div.right-block img').each(function(){
			rightBlockWidth += $(this).width() + 1;
		});
		$('div.gallery div.left-block').width(arrowsBlockWidth);
		$('div.gallery div.right-block').width(arrowsBlockWidth);
		$('div.gallery div.middle-block').width(middleBlockWidth);
		$('div.gallery div.left-block div').css({'margin-left':leftBlockMargin});
		$('div.gallery div.left-block div').width(leftBlockWidth);
		$('div.gallery div.right-block div').width(rightBlockWidth);
		if ($('div.body').hasClass('preloader')) {
			$('div.body').removeClass('preloader');
		}
		$('div.body div.gallery').fadeTo(500, 1);
		$('#slideshow').fadeTo(500, 1);

		
		// Zoom
		$('div.zoom div.preview img').fadeTo(0, 0.83);
		var imageSrc = $('div.zoom div.preview img').attr('src');
		imageSrc = 'url('+imageSrc+')';
		$('div.zoom div.preview div.visible').css({'background-image':imageSrc, 'background-position':'0 0'});
		var startDrag = false;
		var startDragMini = false;
		var startPosition = 0;
		var imagePosition = parseFloat($('div.zoom div.fullsize img').css('left'));
		var imageSize = $('div.zoom div.fullsize img').width();
		var previewSize = $('div.zoom div.preview img').width();
		var visibleSide = parseFloat($('div.zoom div.fullsize').css('width'));
		var scale = Math.round((visibleSide / imageSize)*100)/100;
		var bigDiff = imageSize - visibleSide;
		var borderPercent = Math.round(scale*100);
		if (borderPercent > 100) borderPercent = 100;
		var borderPixel = parseInt(previewSize * borderPercent / 100);
		
		$('div.zoom div.visible').css('width', borderPixel+'px');
		
		var smallDiff = previewSize - borderPixel;
		var smallScale = Math.round((smallDiff / bigDiff)*100)/100;
		var bigScale = Math.round((bigDiff / smallDiff)*100)/100;
		var maxLeft = (imageSize - 911) * -1;
		if (imageSize > 911) {
			$('div.zoom div.fullsize img').mousedown(function(e){
				imagePosition = parseFloat($('div.zoom div.fullsize img').css('left'));
				startDrag = true;
				startPosition = e.pageX - imagePosition;
				if (!e){
					e = window.event;
				}
				blockEvent(e);
				return false;
			});
			
			$('div.zoom div.preview div.visible').mousedown(function(e){
				imagePosition = parseFloat($('div.zoom div.preview div.visible').css('left'));
				startDragMini = true;
				startPosition = e.pageX - imagePosition;
				if (!e){
					e = window.event;
				}
				blockEvent(e);
				return false;
			});
			
			
			$(document).mouseup(function(e){
				startDrag = false;
				startDragMini = false;
				startPosition = 0;
			});
			
			
			$(document).mousemove(function(e){
				if (startDrag || startDragMini) {
					if (!e){
						e = window.event;
					}
					blockEvent(e);
					if (startDrag) {
						imagePosition = parseFloat($('div.zoom div.fullsize img').css('left'));
						mouseRange = e.pageX - startPosition;
						if (mouseRange <= 0 && mouseRange >= maxLeft) {
							$('div.zoom div.fullsize img').css({'left':mouseRange});
							var borderPosition = Math.round((mouseRange*smallScale));
							//alert(borderPosition+':'+smallDiff);
							$('div.zoom div.visible').css({'left':-borderPosition,'background-position':borderPosition});
						} else {
							startPosition = e.pageX - imagePosition;
						}
					} else {
						imagePosition = parseFloat($('div.zoom div.preview div.visible').css('left'));
						mouseRange = e.pageX - startPosition;
						if (mouseRange >= 0 && mouseRange <= smallDiff) {
							$('div.zoom div.visible').css({'left':mouseRange,'background-position':-mouseRange});
							var imgPosition = Math.round(mouseRange*bigScale);
							$('div.zoom div.fullsize img').css({'left':-imgPosition});
							if ((mouseRange - smallDiff) > -0.5) {
								
								$('div.zoom div.fullsize img').css({'left':maxLeft});
							}
						} else {
							startPosition = e.pageX - imagePosition;
						}
					}
					return false;
				}
			});
			
			/*
			$('div.zoom div.preview div.visible').mousemove(function(e){
				if (startDrag) {
					if (!e){
						e = window.event;
					}
					blockEvent(e);
					imagePosition = parseFloat($('div.zoom div.preview div.visible').css('left'));
					mouseRange = e.pageX - startPosition;
					if (mouseRange >= 0 && mouseRange <= smallDiff) {
						$('div.zoom div.visible').css({'left':mouseRange});
						var imgPosition = Math.round(mouseRange*(scale*10));
						$('div.zoom div.fullsize img').css({'left':-imgPosition});
					} else {
						startPosition = e.pageX - imagePosition;
					}
					return false;
				}
			});
			*/
		}
		
		
		// Onload for Scroll
		scroll_html = '<div class="scrollbar"><div class="scroll"><div></div></div></div>';
		
		$('div.scrollable').each(function(){
			if ($(this).height() > $(this).parent().height()) {
				$(this).prepend(scroll_html);
			}
			
			scr = $(this).find('div.scrollbar');
			scrbar = scr.find('div.scroll');
			block = $(this).parent();
			content = block.find('div.scrollable');
			
			block_height = block.height();
			content_height = content.height();
			content_delta = (content_height - block_height) * -1;
			scroller_theight = scr.height();
			scroller_height = Math.round( (block_height * scroller_theight) / content_height );
			scroller_delta = scroller_theight - scroller_height;
			delta = (content_delta / scroller_delta) * -1;
			scrbar.css({'top':'0px','height':scroller_height});
			canDrag = false;
			shift_y = 0;
			
			scrbar.mousedown(function(e){
				if (!e){
					e = window.event;
				}
				
				scr = $(this).parent();
				scrbar = $(this);
				block = $(this).parent().parent().parent();
				content = $(this).parent().parent();
				block_height = block.height();
				content_height = content.height();
				content_delta = (content_height - block_height) * -1;
				scroller_theight = scr.height();
				scroller_height = Math.round( (block_height * scroller_theight) / content_height );
				scroller_delta = scroller_theight - scroller_height;
				delta = (content_delta / scroller_delta) * -1;
				
				canDrag = true;
				shift_y = e.clientY - parseInt($(this).css('top'));
				blockEvent(e);
				return false;
			});
			
			$(document).mousemove(function(e){
				if (!e){
					e = window.event;
				}
				if (canDrag){
					var aaa = e.clientY - shift_y;
					setPosition(aaa, scrbar, content);
					blockEvent(e);
				}
				return false;
			});
			
			$(document).mouseup(function(e){
				canDrag = false; 
			});
		});
	}
	
	function blockEvent(event) {
		if (!event) {
			event = window.event;
		}
		if(event.stopPropagation) event.stopPropagation();
		else event.cancelBubble = true;
		if(event.preventDefault) event.preventDefault();
		else event.returnValue = false;
	}
	
	function setPosition(newPosition, scrbar, content)
	{
		//alert(scrbar.height());
		if ( (newPosition <= scroller_theight - scroller_height) && (newPosition >= 0) )
		{
			pos = newPosition + "px";
			scrbar.css({'top':pos});
		}
		else if (newPosition > scroller_theight - scroller_height)
		{
			pos = (scroller_theight - scroller_height) + "px";
			scrbar.css({'top':pos});
		}
		else
		{
			scrbar.css({'top':'0px'});
		}
		
		var mtop = Math.round( parseInt(scrbar.css('top'))  * delta * (-1) );
		
		if (mtop < content_delta)
		{
			pos = content_delta + "px";
			content.css({'margin-top':pos});
		}
		else
		{
			pos = mtop + "px";
			content.css({'margin-top':pos});
		}
		return false;
	}
	
	
	
	/*
	if (block.addEventListener)
	{
		block.addEventListener('DOMMouseScroll', wheel, false);
	}
	block.onmousewheel = wheel;
	
	function wheel(event)
	{
		// Переменная, в которой будем хранить сдвиг
		var wheelDelta = 0;

		// Шаг меню при прокрутке
		var step = 30;
		if (!event)
		{
			event = window.event;
		}
		if (event.wheelDelta)
		{
			// В IE и Opera при сдвиге колеса на один шаг event.wheelDelta принимает значение 120
			// Значения сдвига в этих двух браузерах совпадают по знаку.
			wheelDelta = -event.wheelDelta/120;
		} 
		else if (event.detail)
		{
			// В Mozilla, значение wheelDelta отличается по знаку от значения в IE.
			// Сдвиг колеса на один шаг соответствует значению 3 параметра event.detail
			wheelDelta = event.detail/3;
		}
		// При скроллинге вверх wheelDelta > 0
		// При скролинге вниз - wheelDelta < 0
		if (wheelDelta)
		{
			var currentPosition = parseInt(scrbar.css('top'));
			var newPosition = wheelDelta * step + currentPosition;
			setPosition(newPosition);
		}

		if (event.preventDefault)
		{
			event.preventDefault();
		}
		event.returnValue = false;
		blockEvent(event);
	}
	*/
