// Забирает окно 
function getItemPopup(id)
{
	$.get('/catalog/wininfo/'+id+'/?cache',false,function(data)
	{		
		// Убираем старое окно
		removeItemPopup();
		
		// Добавляем тень
		$('body').prepend('<div id="shadow" onclick="removeItemPopup()"></div>');
		var shadow = $('#shadow')
		
		// Растягиваем в высоту всего документа
		shadow.height($(document).height());
		
		// Делаем прозрачным
		shadow.css('opacity','0.8');
		
		// Делаем видимым
		//shadow.css('visibility','visible');
		shadow.fadeIn('slow',function()
		{
			
			// Только после появления тени показываем содержимое
			// Добавляем вновь сварившийся div
			$('body').prepend(data);
			
			// Чтобы каждый раз не опрашивать DOM
			var popupItem = $('#popupItem');
			
			// Определяем его ширину и высоту
			var popupWidth = popupItem.width();
			var popupHeight = popupItem.height();
					
			// Если ширина меньше 600, делаем ровно 600
			if(popupWidth < 500)
			{
				popupItem.width(500);
				popupWidth = 500;
			}
			
			// Размещаем его по середине, относительно взгляда пользователя
			// Пара служебных функций
			var arrayPageScroll = getPageScroll();
			var arrayPageSize = getPageSize();
			
			// Выравниваем по ширине и высоте
			popupItem.css('left','50%');
			popupItem.css('margin-left',-1*popupWidth/2);
			popupItem.css('top',arrayPageScroll[1] + ((arrayPageSize[3] - 35 - popupHeight) / 2));

			$('div.white_bg').width(popupWidth);
			
			// Показываем
			popupItem.css('visibility','visible');
			shadow.height($(document).height());
			});
	},'html');
}

// Удаляет окно и тень
function removeItemPopup()
{
	// Убираем старый div и тень
	$('#popupItem').remove();
	$('#shadow').remove();
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
