
$(document).ready(function() {						   
	var counter = 0;					   
						   
	$.ajax_upload('#uploadButton', {
		action : 'ajaxupload/upload.php',
		name : 'myfile',
		onSubmit : function(file, ext) {
			// показываем картинку загрузки файла
			$("img#load").attr("src", "ajaxupload/img/load.gif").css('display', 'inline');
			$("#uploadButton span").text('');
			//Выключаем кнопку на время загрузки файла
			this.disable();

		},
		onComplete : function(file, response) {
			// убираем картинку загрузки файла
			$("img#load").attr("src", "ajaxupload/img/loadstop.gif").css('display', 'none');;
			$("#uploadButton span").text('Загрузить');
			// снова включаем кнопку
			this.enable();
			counter++;
			// показываем что файл загружен
			$('#files').append('<div class="picture"><img src="ajaxupload/upload/small/' +response+ '"><span></span><input type="hidden" name="src_s_'+counter+'" value="ajaxupload/upload/small/'+response+'"><input type="hidden" name="src_b_'+counter+'" value="ajaxupload/upload/big/'+response+'"></div>');
			
			//alert(response+file);
		}
	});
	
	
	$('.picture span').live("click", function(){
		var img_src = $(this).parent('.picture').find('img').attr('src');
		$(this).parent('.picture').remove();
		
		$.post("ajaxupload/delete.php", { src: img_src }, function(data){
        });
	});
	
	$('.picture img, .request img').live("click", function(){
											 
		var src_small = $(this).attr('src');
		var src_big = src_small.replace(/small/, 'big');
		
		$('#img_window').html('<img src="'+src_big+'"><span></span>');		
		$('#img_window').css('display', 'block');
		
		$.post("ajaxupload/size.php", { src_big: src_big, size: 'w' }, function(data){
			var w_img_big = data;
			$('#img_window').css('margin-left', '-'+w_img_big+'px');
        });
		
		$.post("ajaxupload/size.php", { src_big: src_big, size: 'h' }, function(data){
																				
			var h_img_big = data;			
			
			var scroll_top = $('html').scrollTop();
			if(scroll_top == 0){var scroll_top = document.body.scrollTop;}//для google chrome
			
			var screen_height = screen.height;
			var top_m = ((screen_height - h_img_big) / 2) + (scroll_top - 70);
			
			$('#img_window').css('top', top_m+'px');
			
        });		
	});
	
	$('#img_window span').live("click",function(){
		$(this).parent('#img_window').empty();
		$('#img_window').css('display', 'none');
	});
	
	
});

