(function($) {
	$(document).ready(function() {
		
		getPhotogallery = function(Code) {
			window.scroll(0,0);
			var widthImg;
			var heightImg;
			$.ajax({
				url:'/ajax.php',
				type:'GET',
				dataType: 'json',
				data:'Code='+Code+'&action=getSizes',
				success:function(response) {
					widthImg = parseInt(response.width);
					heightImg = parseInt(response.height);
				}
			});
			
			$.ajax({
				url:'/ajax.php',
				type:'GET',
				data:'Code='+Code+'&action=gallery',
				success:function(response) {
					$('#gallery').html(response);
					$('#shadow').show();
					$('#loader').show();
					image = new Image();
					image.src = $('#mainPhoto').attr('src');
					$(image).load(function() {
						$('#loader').hide();
						$('#leftArrow,#rightArrow').css('top',(heightImg+140));
						heightImg = parseInt(heightImg+230);
						$('#gallery').animate({
							height:heightImg+'px'
						},{
							queue:false,
							duration:1500
						}).fadeIn('slow');
					});
				}
			})
		}
		
		changePhoto = function(Code,T_Code) {
			height = $('#mainPhoto').height();
			$('#imageBlock').html('<div class="loader" style="height:'+height+'px;"><img src="/images/ajaxLoader.gif" alt="Загрузка" /></div>');
			$.ajax({
				url:'/ajax.php',
				type:'GET',
				dataType: 'json',
				data:'Code='+Code+'&action=getSizes',
				success:function(response) {
					widthImg = parseInt(response.width);
					heightImg = parseInt(response.height);
					src = response.src;
					$.ajax({
				url:'/ajax.php',
				type:'GET',
				data:'Code='+Code+'&action=photo',
				success:function(response) {
					image = new Image();
					image.src = src;
					$(image).load(function() {
						$('.loader').remove();
						$('#imageBlock').html(' ');
						$('#imageBlock').html(response);
						$('#leftArrow,#rightArrow').css('top',(heightImg+140));
						heightImg = parseInt(heightImg+230);
						$('#gallery').animate({
							height:heightImg+'px'
						},{
							queue:false,
							duration:1500
						}).fadeIn('slow');
					});
					}
				})
				}
			});
		}
		
		$('#shadow').live('click',function() {
			$('#shadow').hide();
			$('.popupWindow').hide();
		})
		
		$('.close').live('click',function(){
			$('#shadow').hide();
			$('.popupWindow').hide();
		})
		
		counter = 0;
		$('#rightArrow').live('mouseover',function() {
			var animateLeft = $('#photosList ul').width();
			if(animateLeft > 750)
				animateLeft = Math.abs(animateLeft-750);
			var marginLeft = Math.abs(parseInt($('#photosList ul').css('margin-left')));
			var speed = parseInt(2*(animateLeft-marginLeft));
			if(speed == 0) 
				speed = parseInt(2*animateLeft);
			$('#photosList ul').animate({
				marginLeft:'-'+animateLeft+'px'
			},{
				queue:false,
				duration:speed
			});
		});
		
		$('#leftArrow').live('mouseover',function() {
			var animateLeft = $('#photosList ul').width();
			if(animateLeft > 750)
				animateLeft = Math.abs(animateLeft-750);
			var marginLeft = Math.abs(parseInt($('#photosList ul').css('margin-left')));
			var speed = parseInt(2*marginLeft);
			if(speed == 0) 
				speed = parseInt(2*animateLeft);
			$('#photosList ul').animate({
				marginLeft:'0px'
			},{
				queue:false,
				duration:speed
			});
		})
		
		$('#leftArrow, #rightArrow').live('mouseout',function() {
			$('#photosList ul').stop();
		})
	})
})(jQuery);
