window.onresize=___getPageSize;


$(document).ready(function(){
    $(".openPopup").click(function(){
        var info_id = $(this).attr("href").substr(1)-0;
        openPopup($(this),info_id,1);
        return false;
    });
    
	$(document).keypress(function(e){
		if(e.keyCode==27){
			closePopup();
		}
	});
});


//Растягиваем человечиков лева и справа)
$(document).ready(function(){
    resize_cheloveki();
})
$(document).resize(function(){
    resize_cheloveki();
});
function resize_cheloveki(){
    var height_td = $("#cheloveki1").parents("td").height();
    $("#cheloveki1").css("height",height_td-322);
    $("#cheloveki2").css("height",height_td-378);
}
//-------------------------------------


function gif_over(img) {
	img.src=img.src.replace('.gif','r.gif');
};
function gif_out(img) {
	img.src=img.src.replace('r.gif','.gif');
};

function openPopup(obj,info_id,show) {
    var url = "/show_file/?id="+info_id;
    var insert_div = '<div id="popup_all"><div id="popup"><img src="/img/preloader.gif" alt="" onclick="closePopup();" /></div></div><div id="backgroundPopup" style="display:none; position:absolute; height:100%; width:100%; top:0; left:0; background:#000000; z-index:50;" onclick="closePopup();"></div>';
    if(show){
        $("body").append(insert_div);
        centerPopup();
    	$("#popup_all").fadeIn("fast");
    	$("#popup").fadeIn("fast");
        $("#backgroundPopup").css({
    	   "opacity": "0.7"
    	});
        $("#backgroundPopup").fadeIn("fast");
    }else{
        $("#popup").html('<img src="/img/preloader.gif" width="103" height="24" alt="" />');
        centerPopup();
        //return false;
    }
    $("#popup").load(url,function(){
        centerPopup();
        if($("#player").html() != null){
            flowplayer("player", "/img/flash/flowplayer-3.1.1.swf", { clip: { autoPlay:true } } );
        }
        $("#close").css("display","block");
    });
}

function closePopup() {
	$("#popup_all").fadeOut("fast",function(){
	   $("#popup_all").remove();
	   $("#popup").remove();
	});
    $("#backgroundPopup").fadeOut("fast",function(){
	   $("#backgroundPopup").remove();
	});
}

function navPopup(info_id){
    $("#close").css("display","none");
    openPopup("",info_id,0)
}

function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};


function centerPopup(){
    $("#popup").css("width",$("#pic_content").width());
    $("#title").css("width",$("#pic_content").width());
    $("#bottom_popup").css("width",$("#pic_content").width());
	var popupHeight = $("#popup").height();
	var popupWidth = $("#popup").width();
	$("#popup").css({
        "margin-top": -popupHeight/2,
		"margin-left": -popupWidth/2
	});	
    var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var arrPageSizes = ___getPageSize();
	$("#backgroundPopup").css({
		width:arrPageSizes[0],
		height:arrPageSizes[1]
	});
}
