﻿var popupStatus = 0;

function loadPopup(){
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popup").fadeIn("slow");
		popupStatus = 1;
	}
}


function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popup").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup").height();
	var popupWidth = $("#popup").width();
	var from_top = ($.browser.webkit) ? window.scrollY : $("html").scrollTop(); //В Хроме надо scrollY
	$("#popup").css({
		"position": "absolute",
		"top": (((windowHeight/2-popupHeight/2)>0)?windowHeight/2-popupHeight/2:0) + from_top,
		"left": windowWidth/2-popupWidth/2
	});
		//"top": (((windowHeight/2-popupHeight/2)>0)?windowHeight/2-popupHeight/2:0) + $("html").scrollTop(),
		//"top": 100,
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

$(document).ready(function(){
	$(".nomer").click(function(){
		$('#popup_img_1').attr('src',$(this).find('img').attr('src'));
		$('#nomer_src').val($(this).find('img').attr('src'));
		ts_title = $(this).find('.ts_title').text();
		nomer_title = $(this).find('.title_text').text();
		nomer_type = $(this).find('.nomer_type').text();
		$('#nomer_type').val(nomer_type);
		$("#popupContent").find('h1').text(nomer_title);
		nomer_description1 = ($(this).find('.nomer_description').html()) ? $(this).find('.nomer_description').html() : '';
		$("#nomer_description").html(nomer_description1);
		$("#popupContent").data('nomer_type',nomer_type);
		
		$("#popupContent .reg_block, #popupContent .ind_block, #popupContent .suv_block, #popupContent .transit_block").hide().find('input, select').attr('disabled','disabled');
		$('#popupContent .' + $("#popupContent").data('nomer_type') + '_block').show().find('input, select').removeAttr('disabled');
		$('#popupContent a.deactivated').removeClass('deactivated');
		centerPopup();
		loadPopup();
		return false;
	});
				
	$("#popupClose")
		.click(function(){
			disablePopup();
			$("#popupForm").hide();
			$('#popupForm select option:first').attr('selected','selected');
			$('#file_list, #popup_docs').text('');
			$('#popup_docs_caption, #popup_contacts').hide();
			$('#make_order_container').show();
			$('#submit_container').hide();
			files_num = 1;
		});
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

	//"top", parseInt($("html").scrollTop()) + parseInt(((windowHeight/2-popupHeight/2)>0)?windowHeight/2-popupHeight/2:0) + 'px'

	$(".order")
		.hover(function(){$(this).css('text-decoration','underline')},function(){$(this).css('text-decoration','none')})		
		.click(function(){
			files_num = 1;						
			var from_top = ($.browser.webkit) ? window.scrollY : $("html").scrollTop(); //В Хроме надо scrollY
			$("#popupForm").slideDown('fast', function(){
					var windowHeight = document.documentElement.clientHeight;
					var popupHeight = $("#popup").height();
					$("#popup").css(
						"top", from_top + parseInt(((windowHeight/2-popupHeight/2)>0)?windowHeight/2-popupHeight/2:0) + 'px'
					);
			   });
			return false;
		})
	
});
