function correctGalleryHeight() {

	h = $(window).height() - $('.top').height() - 50;
	
	/*
	if ($('.b-sub-navigation').length == 0) {
		h -= 24;
	}
	*/
	
	if (h < 566) {
		h = 566;
	}
	pt = Math.floor((h - 566) / 2);
	$('.b-items').css('height',h + 'px');
	$('.b-items li').css('padding-top',pt);

}

$(document).ready(function(){

	$(document).click(function(){
		$('.b-sizes-small-open').removeClass('b-sizes-small-open');
		$('.b-sizes-open').removeClass('b-sizes-open');
	});
	
	if ($('.b-items').length) {

		correctGalleryHeight();
		$(window).resize(function(){
			correctGalleryHeight();
		});
		
		window.blockW = 0;
		$('.b-items li').each(function(){
			window.blockW += parseInt($(this).css('width').replace('px','')) + 80;
		});
		$('.b-items').css('width',window.blockW);
		$('.b-items-block-in').css('width',$(window).width() + 'px');
		
		$('.b-sizes-small b').live('click',function(){
			$(this).parent().toggleClass('b-sizes-small-open');
			return false;
		});
		
		$('.b-sizes-small-dropdown dl dd a').click(function(){
			$(this).parent().parent().find('.active').removeClass('active');
			$(this).parent().addClass('active');
			$(this).parents('.b-sizes-small:eq(0)').find('b').html($(this).text());
			$(this).parents('.b-sizes-small:eq(0)').toggleClass('b-sizes-small-open');
			$(this).parents('.b-sizes-small:eq(0)').parent().next().find('dt').html($(this).attr('title'));
			return false;
		});
	}
	
	$('.price .btn').click(function(){
		
		price = $.trim($(this).parent().prev().html());
		if ($(this).parents('li:eq(0)').length) {
			hint = $.trim($(this).parent().prev().parent().prev().find('p').text());
			name = $.trim($(this).parent().prev().parent().prev().prev().find('a').attr('longdesc'));
			if ($.trim($(this).parent().prev().parent().prev().find('.b-sizes-small b').text()) != '') {
				hint = $.trim($(this).parent().prev().parent().prev().find('.b-sizes-small b').text()) + ', ' + hint;
			}
			src = $(this).parents('li:eq(0)').find('.image img').attr('src');
			w = parseInt($(this).parents('li:eq(0)').find('.image img').attr('width'));
			h = parseInt($(this).parents('li:eq(0)').find('.image img').attr('height'));
		} else {
			hint = $.trim($(this).parent().parent().prev().find('.made').text());
			name = $('.b-crumbs dd:eq(2)').text() + ': ' + $.trim($(this).parent().parent().prev().prev().text());
			if ($.trim($('.b-sizes b').text()) != '') {
				hint = $.trim($('.b-sizes b').text()) + ', ' + hint;
			}
			src = $('.b-item-photo img').attr('src');
			w = parseInt($('.b-item-photo img').attr('width'));
			h = parseInt($('.b-item-photo img').attr('height'));			
		}
		if (w > h) {
			icode = '<img src="' + src + '" width="100" />';
		} else {
			icode = '<img src="' + src + '" height="100" />';
		}
		$('.b-pp-order-item h2').text(name);
		$('.b-pp-order-item p:eq(0)').text(hint);
		$('.b-pp-order-item p:eq(1)').text(price);
		$('.b-pp-order .image').html(icode);
		$('.overlay').show();

		$('.b-pp-order .pop-up-content:eq(0)').show();
		$('.b-pp-order .pop-up-content.confirm').hide();
		$('.b-pp-order').show();

		return false;
	});

	$('.b-pp-order-form-btn .btn').click(function(){
		$('#buy-form').submit();
		return false;
	});

	$('#buy-form').submit(function(){
		window.errorsFlag = 0;
		$(this).find('.inp-txt').each(function(){
			if ($.trim($(this).val()) == '') {
				$(this).parent().addClass('error');
				window.errorsFlag = 1;
			} else {
				$(this).parent().removeClass('error');
			}
		});
		if (!window.errorsFlag) {
			postData = {'Name' : $(this).find('.inp-txt:eq(0)').val(), 'Phone' : $(this).find('.inp-txt:eq(1)').val(), 'Address' : $(this).find('.inp-txt:eq(2)').val(), 'ItemName' : $('.b-pp-order-item .description h2').text(), 'ItemPrice' : $('.b-pp-order-item .description .price').text(), 'ItemHint' : $('.b-pp-order-item .description p:eq(0)').text()};
			$.post('?', postData,function(data){
				if (data != 'ok') {
					window.confirm('К сожалению, у нас на сайте произошла техническая ошибка.' + "\n" + 'Попробуйте загрузить страницу заново и заказать нужный товар снова.');
				} else {
					$('.pop-up-content:visible').hide();
					$('.pop-up-content.confirm').show();
				}
			});
		}
		return false;
	});

	
	$('.pop-up-close').mousedown(function(){
		$(this).addClass('pop-up-close-click');
	}).mouseup(function(){
		$(this).removeClass('pop-up-close-click');
		$('.overlay').hide();
		$('.pop-up:visible').hide();
	}).mouseout(function(){
		$(this).removeClass('pop-up-close-click');
	});
	
	$('.pop-up-close-btn,.overlay,.b-pp-order-message-btn-close .btn,.b-pp-tech-btn .btn').click(function(){
		$('.overlay,.pop-up').hide();
		return false;
	});
	
	$('.btn-2-wh').mouseover(function(){
		$(this).addClass('btn-2-wh-hover');
	}).mouseout(function(){
		$(this).removeClass('btn-2-wh-hover btn-2-wh-click');
	}).mousedown(function(){
		$(this).removeClass('btn-2-wh-hover').addClass('btn-2-wh-click');
	}).mouseup(function(){
		$(this).removeClass('btn-2-wh-click').addClass('btn-2-wh-hover');
	});	
	
	$('.btn-3').mouseover(function(){
		$(this).addClass('btn-3-hover');
	}).mouseout(function(){
		$(this).removeClass('btn-3-hover btn-3-click');
	}).mousedown(function(){
		$(this).removeClass('btn-3-hover').addClass('btn-3-click');
	}).mouseup(function(){
		$(this).removeClass('btn-3-click').addClass('btn-3-hover');
	});
	
	$('.btn-2').mouseover(function(){
		$(this).addClass('btn-2-hover');
	}).mouseout(function(){
		$(this).removeClass('btn-2-hover btn-2-click');
	}).mousedown(function(){
		$(this).removeClass('btn-2-hover').addClass('btn-2-click');
	}).mouseup(function(){
		$(this).removeClass('btn-2-click').addClass('btn-2-hover');
	});
	
	$('.btn-1').mouseover(function(){
		$(this).addClass('btn-1-hover');
	}).mouseout(function(){
		$(this).removeClass('btn-1-hover btn-1-click');
	}).mousedown(function(){
		$(this).removeClass('btn-1-hover').addClass('btn-1-click');
	}).mouseup(function(){
		$(this).removeClass('btn-1-click').addClass('btn-1-hover');
	});
	
	$('.q-icon').mouseout(function(){
		$(this).removeClass('q-icon-click');
	}).mousedown(function(){
		$(this).addClass('q-icon-click');
	}).mouseup(function(){
		$(this).removeClass('q-icon-click');
	});
	
	$('.show-help').click(function(){
		$('.pop-up').hide();
		$('.overlay').show();
		classes = $(this).attr('class').split(' ');
		for(j=0;j<classes.length;j++) {
			if (classes[j].indexOf('help-') === 0) {
				cl = classes[j];
				break;
			}
		}
		$('#' + cl).css('margin-left',($(document).scrollLeft() - 400) + 'px').show();
		return false;
	});
	
	if ($('.b-item-description').length) {
			
		$('.b-sizes b').live('click',function(){
			$(this).parent().toggleClass('b-sizes-open');
			return false;
		});
		
		$('.b-sizes-dropdown dl dd a').click(function(){
			$(this).parent().parent().find('.active').removeClass('active');
			$(this).parent().addClass('active');
			$(this).parents('.b-sizes:eq(0)').find('b').html($(this).text());
			$(this).parents('.b-sizes:eq(0)').toggleClass('b-sizes-open');
			$(this).parents('.b-sizes:eq(0)').parent().next().find('dt').html($(this).attr('title'));
			return false;
		});
	}
	
	$('.dots a').click(function(){
		if ($(this).hasClass('dots-active')) {
			return false;
		}
		if ($('.dots-active:visible').length == 0) {
			return false
		}
		window.n = $(this).prevAll().length;
		$('.dots-active:visible').removeClass('dots-active');
		
		$('.pop-up-content:visible i:visible').fadeOut(300,function(){
			$('.pop-up-content:visible i:eq(' + window.n + ')').css('visibility','visible').hide().fadeIn(300,function(){
				$('.dots:visible a:eq(' + window.n + ')').addClass('dots-active');
			});
		});
		return false;
	});
	
	
	if ($('.b-item-photo').length) {
		
		oneItemResize();
		$(window).resize(function(){
			oneItemResize();
		});
		
	} else {
	
		
		$('.b-items-ar-left').hover(function(){
			window.interval = window.setInterval("scrollToLeft()",7);
		},function(){
			window.interval = window.clearInterval(window.interval)
		});
	
		$('.b-items-ar-right').hover(function(){
			window.interval = window.setInterval("scrollToRight()",7);
		},function(){
			window.interval = window.clearInterval(window.interval)
		});
		
		$('.target-blank').attr('target','_blank');
	
		$(document).mousewheel(function (event, delta) {
		/*
	      if ( (!this.scrollLeft && delta > 0) || (this.scrollLeft == $(this).data('scrollLeft') && delta < 0) ) {
	         return true;
	      }
	      $(this).data('scrollLeft', this.scrollLeft);
	      this.scrollLeft -= (delta * 300);
	      */
		  sl = $(document).scrollLeft() + delta * 70;
		  $(document).scrollLeft(sl);
	      return false;
	   });
	
	}	  
	
});

function scrollToRight() {
	
	sl = $(document).scrollLeft() + 3;
	$(document).scrollLeft(sl);
	
}

function scrollToLeft() {
	
	sl = $(document).scrollLeft() - 3;
	$(document).scrollLeft(sl);
	
}

function oneItemResize() {

	maxW = $('#container').width() - 600;
	maxH = $(window).height() - 95;
	
	realW = parseInt($('.b-item-photo img').css('width').replace('px',''));
	realH = parseInt($('.b-item-photo img').css('height').replace('px',''));
	
	r = realW / realH;
	
	if (maxH * r > maxW) {
		newW = maxW;
		newH = Math.round(maxW / r);
	} else {
		newW = Math.round(maxH * r);
		newH = maxH;
	}
	
	minH = $('.b-item-description').height();
	if (newH < minH) {
		newH = minH;
		newW = Math.round(newH * r);
		if (newW > maxW) {
			newW = maxW;
			newH = Math.round(newW / r);
		}
	}
	
	$('.b-item-photo img').css('width',newW + 'px');
	$('.b-item-photo img').css('height',newH + 'px');

	h1 = $('.b-item-photo img').height();
	h2 = $('.b-item-description').height();
	pt = Math.round((h1 - h2) / 2);
	if ($('.b-item-description .text-editor p').length) {
		pt += 6;
	}
	$('.b-item-description').css('padding-top',pt + 'px');
	
}
