function setWrapHeight() {
	$('.main .wrap-inner').height('auto').height($('.main').height());
}

$(window).resize(function() {
	setWrapHeight();	
});

$(window).load(function() {
	setWrapHeight();		
});

$(function() {
	if ($('.main .carousel').length > 0) {
		function carouselControl(carousel) {
			var conainer = $('#main-carousel');
			
			function setArrowsState(pos, size) {
				conainer.find('.arrow.inactive').removeClass('inactive');
				if (pos == 1)
					conainer.find('.arrow.l').addClass('inactive');
				else if (pos == size)
					conainer.find('.arrow.r').addClass('inactive');
			}
			
			conainer.find('.items a').click(
				function() {
			        carousel.scroll($.jcarousel.intval($(this).attr('href')));
			        conainer.find('.items a.active').removeClass('active');
			        $(this).addClass('active');
			        setArrowsState(carousel.last, carousel.size());
			        return false;
				}
			);
			conainer.find('.arrow.r').click(
				function() {
			        carousel.next();
			        //size
					if (carousel.size() >= carousel.last) {
						conainer.find('.items a[href="' + carousel.last + '"]').click();
						setArrowsState(carousel.last, carousel.size());
					}
			        return false;
				}
			);
			conainer.find('.arrow.l').click(
				function() {
			        carousel.prev();
					if (carousel.first <= carousel.last) {
						conainer.find('.items a[href="' + carousel.last + '"]').click();
						setArrowsState(carousel.last, carousel.size());
					}
			        return false;
				}
			);
			
			/*if (conainer.find('.desc').length > 0) {
				conainer.find('.desc a').click(
					function() {
						var item = $(this).parents('.jcarousel-item');
						var id = $(this).attr('href');
						item.find('.img:visible').fadeOut(
							'fast',
							function() {
								item.find('div' + id).fadeIn('fast');
							}
						);
						return false;
					}
				);
			}*/
		}
		
		$('.main .carousel > ul').jcarousel({
			visible: 1,
			scroll: 1,
			initCallback: carouselControl,
			buttonNextHTML: null,
	        buttonPrevHTML: null
		});
	}
	
	if ($('.header .smenu li').length > 0) {
		$('.header .smenu li').hover(
			function() {
				var dd = $(this).find('.dd');
				if (dd.length > 0 && !$(this).hasClass('active')) {
					if (dd.is(':visible'))
						dd.hide();
					else
						dd.show();
				}
			}
		);
	}
	
	if ($('.main .bottom-wrap .list').length > 0) {
		
		$('.main .bottom-wrap .list > li').bind('mouseenter', function(){
			var self = $(this);
			var short = self.find('.over');
			var full = self.find('.full');			
			
			if (full.length > 0) {
				self.addClass('vis');
				short.hide();
				full.show();	
			}		
		});
		
		
		$('.main .bottom-wrap .list > li').bind('mouseleave', function(){
			var self = $(this);
			var short = self.find('.over');
			
			
			var full = self.find('.full');			
			
			if (full.length > 0) {
				self.removeClass('vis');
				full.hide();
				short.show();	
			}		
		});		

		$('.bottom-wrap .list .title').bind('click', function(){
			$(this).parents('li').find('.full').hide();
			$(this).parents('li').find('.over').show();
		})
		
		/*
		$('.main .bottom-wrap .list > li').hover(
			function() {
				
			
				var self = $(this);
				var short = self.find('.over');
				var full = self.find('.full');
				if (full.length > 0) {
					self.toggleClass('vis');
					if (full.is(':hidden')) {
						short.hide();
						full.show();
					}
					else {
						full.hide();
						short.show();
					}
				}
			}
		);
		
		
*/
	}
	
	if ($('.main .submenu li').length > 0) {
		$('.main .submenu li').hover(function() { $(this).toggleClass('hover'); })
	}
	
	if ($('.header .subscribe_over').length > 0) {
		var subscribeSubmit = function() {
			$.ajax({
				url: subscriberAddPath,
				dataType: 'json',
				data: {'email': $('#subscribe .email').val()},
				type: 'POST',
				success: function(data, textStatus, XMLHttpRequest) {
					if (data.error == 'email_err')
						alert(subscribeDialogEmailErr);
					else if (data.error == 'email_exists')
						alert(subscribeDialogEmailExists);
					else {
						$('#subscribe .email').val('');
						$('#subscribe').dialog('close');
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {},
				complete: function(XMLHttpRequest, textStatus) {}
			});
		};
		
		$('#subscribe').dialog({
			autoOpen: false,
			modal: true,
			width: 324,
			height: 135,
			title: subscribeDialogTitle,
			closeText: '',
			draggable: false,
			create: function(event, ui) {
				$(this).parents('.ui-dialog').addClass('subscribe_window');
			},
			open: function(event, ui) {
				$('#subscribe .email').focus();
			},
			buttons: [
				{
					text: subscribeDialogButton,
					click: subscribeSubmit
				}
			]
		});
		$('.header .subscribe_over').click(function() {
			$('#subscribe').dialog('open');
			return false;
		});
	}
	
	if ($('.header .feedback_over').length > 0) {
		var feedbackSubmit = function() {
			var error = false;
			var arData = {};
			$('#feedback').find('input, textarea, select').each(function() {
				if (!error) {
					var val = $(this).val();
					arData[this.name] = val;
					if ($(this).parents('td').hasClass('req') && $.trim(val).length == 0) {
						error = true;
						alert('Не заполнено обязательное поле "' + $(this).parents('td').find('.title span').text() + '"');
					}
				}
			});
			
			if (!error) {
				$.ajax({
					url: feedbackPath,
					dataType: 'json',
					data: arData,
					type: 'POST',
					success: function(data, textStatus, XMLHttpRequest) {
						if (data.error == 'email_err')
							alert(subscribeDialogEmailErr);
						else {
							$('#feedback .fields').find('input, textarea, select').val('');
							$('#feedback').dialog('close');
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {},
					complete: function(XMLHttpRequest, textStatus) {}
				});
			}
			else {
				return false;
			}
		};
		
		$('#feedback').dialog({
			autoOpen: false,
			modal: true,
			width: 504,
			height: 441,
			title: feedbackDialogTitle,
			closeText: '',
			draggable: false,
			create: function(event, ui) {
				$(this).parents('.ui-dialog').addClass('feedback_window');
			},
			open: function(event, ui) {
				$('#feedback input[name="surname"]').focus();
			},
			buttons: [
				{
					text: feedbackDialogButton,
					click: feedbackSubmit
				}
			]
		});
		$('.header .feedback_over').click(function() {
			$('#feedback').dialog('open');
			return false;
		});
	}
});
