//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popup_status = 0;

function load_popup(id, id_background){
	if(popup_status==0){
		$("#" + id).css({
			"opacity": "1.0"
		});
		$("#" + id_background).fadeIn("slow");
		$("#" + id).fadeIn("slow");
		popup_status = 1;
	}
}

function disable_popup(id, id_background){
	if(popup_status==1){
		$("#" + id_background).fadeOut("slow");
		$("#" + id).fadeOut("slow");
		popup_status = 0;
	}
}

/**
	Установка позиции
	x,y - координаты
*/
function set_position( id, id_background, x, y ){
	$("#" + id).css({
		"position": "absolute",
		"top": y,
		"left": x
	});
	
/*	$("#" + id_background).css({
		"height": windowHeight
	});	*/
}


function center_popup(id, id_background){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#" + id).height();
	var popupWidth = $("#" + id).width();
	$("#" + id).css({
		"position": "absolute",
		"top": windowHeight-popupHeight,
		"left": windowWidth/2-popupWidth/2
	});
	
	$("#" + id_background).css({
		"height": windowHeight
	});
	
}

function center_screen_popup(id, id_background){
	
	var windowWidth = document.documentElement.clientWidth;
	var currentOffset = document.documentElement.scrollTop || document.documentElement.scrollTop;

	if (!($.browser.opera)) { windowHeight = document.documentElement.clientHeight } else { windowHeight = document.body.clientHeight }
	if (windowHeight < $("#" + id).height()) {
		currentOffset = (windowHeight - $("#" + id).height() + currentOffset - 10);
	} else {
		currentOffset2 = parseInt((windowHeight - $("#" + id).height()) / 2);
	}
	var popupHeight = $("#" + id).height();
	var popupWidth = $("#" + id).width();
	$("#" + id).css({
		"position": "absolute",
		"top": currentOffset+currentOffset2,
		"left": windowWidth/2-popupWidth/2
	});
	
	$("#" + id_background).css({
		"height": windowHeight
	});
}

function center_visit_popup(id, id_background){
	
	var windowWidth = document.documentElement.clientWidth;
	var currentOffset = document.documentElement.scrollTop || document.documentElement.scrollTop;

	if (!($.browser.opera)) { windowHeight = document.documentElement.clientHeight } else { windowHeight = document.body.clientHeight }
	if (windowHeight < $("#" + id).height()) {
		currentOffset = (windowHeight - $("#" + id).height() + currentOffset - 10);
	} else {
		currentOffset2 = parseInt((windowHeight - $("#" + id).height()) / 2);
	}
	var popupHeight = $("#" + id).height();
	var popupWidth = $("#" + id).width();
	$("#" + id).css({
		"position": "absolute",
		"top": 0,
		"left": (windowWidth/2-popupWidth/2)-550
	});
	
	$("#" + id_background).css({
		"height": windowHeight
	});
}

function show_popup_person(id, id_background)
{
    center_screen_popup(id, id_background);
    load_popup(id, id_background);
}

function show_popup( id, id_background, x, y )
{	
    set_position( id, id_background, x, y );
    load_popup( id, id_background );
}

function show_visit_popup( id, id_background)
{	
	center_visit_popup( id, id_background );
    load_popup( id, id_background );
}

function go_popup(id, id_background)
{
    center_popup(id, id_background);
    load_popup(id, id_background);
}

function feedback_save(f, id, id_background) 
{
	if (f.elements["feedback_text"].value) {
        $.post('/ajax/feedback/addFeedback.php', 
            {
                feedback_text: f.elements["feedback_text"].value,
                feedback_members_id: f.elements["feedback_members_id"].value,
                feedback_members_login: f.elements["feedback_members_login"].value,
                feedback_ip: f.elements["feedback_ip"].value,
                feedback_uri: f.elements["feedback_uri"].value,
                rand: Math.random()
            },
            function (data) {
                if (data == "ERROR") alert("Ошибка при добавлении сообщения!"); 
            }
        );
    } else {
        alert("Пожалуйста, введите текст сообщения!");
    }
	disable_popup(id, id_background);
}


