$(function() {
	var parentAwards = $('.awards');
	var mAwardsPreview = $('.preview ins, .preview img');
	
	var photo_show = $('#photo_show');
	var gallery_bg = $('#gallery_bg');
	var gallery_frame = $('#gallery_frame');
	
	var layout = $('#layout');
	
	// coeffecient move in pics on stage 
	var moveinCoeff = 100;

	// opening show
	mAwardsPreview.click(function(){
		// created background
		photo_show.fadeIn("fast");
		var current_pic 	= $(this).parent().parent().find('p.picture img').clone();
				
		var previev_width 	= $(this).width()/2,
			previev_height 	= $(this).height()/2;
			
		var previev_top		= $(this).offset().top,
			previev_left 	= $(this).offset().left;
				

				
		//insert picture on stage
		gallery_frame.html(current_pic);
		var mainPicture = $('img', gallery_frame);
		
		var mainPicture_width = mainPicture.width()/2,
			mainPicture_height = mainPicture.height()/2;
		

		
			var layout_width = layout.width(),
				layout_height = layout.height();
			
			
			var summHeight = (previev_top + mainPicture_height) 
			var summWidth = (previev_left + mainPicture_width) 
						


			if((summHeight + 100) > layout_height){
				var h_ostatok = summHeight - layout_height; 
				previev_top -= h_ostatok + 120;
			}
			
			
			if((summWidth + 100) > layout_width){
				var w_ostatok = summWidth - layout_width; 
				previev_left -= w_ostatok + 120;
			} 
									
		gallery_frame.css('left', previev_left);
		gallery_frame.css('top', previev_top);
		gallery_frame.css('margin-left', (mainPicture_width-previev_width)*(-1));
		gallery_frame.css('margin-top', (mainPicture_height-previev_height)*(-1));
		
	});
	
	
	
	// close Show
	$('#gallery_bg, #gallery_frame img').click(function(){
		photo_show.css('display','none');
	});
	$('#gallery_frame img').live("click", function(){
		photo_show.css('display','none');
	});
});


// 
// // (не для фото) при ресайзе перерсчитываем высоту блоков DL
// $(window).resize(function(){
//  	var dl_elements = $('div.awards dl.column');
// 	dl_elements.each(function(){
// 		$(this).removeAttr('css');	
// 	});
// 	
// 	var someHeight = 0;
// 	var mostHeight = 0; 
// 	dl_elements.each(function(){
// 		someHeight = $(this).height();
// 		if(someHeight > mostHeight) {
// 			mostHeight = someHeight; 
// 		}	
// 	});
// 	
// 	dl_elements.each(function(){
// 		$(this).css('height', mostHeight); 	
// 	});
// });
// 








 
