var my_div;
var last_type = "";
var last_params = "";
var div_name = "my_float_div";
var div_opacity = 0;
var drag_div = false;

function draw_float_div(elem, css_class, type, params, redraw){
	var maxDivWidth = 500;
	var maxDivHeight = 200;
	var x = 0;
    var y = 0;
    while (elem){
        x += elem.offsetLeft;
        y += elem.offsetTop;
        elem = elem.offsetParent;
    }
	
	var screenWidth = getClientWidth();
	var screenHeight = getClientHeight();
	// var diff = screenWidth - x;
	// if( diff < maxDivWidth ){
		// diff = maxDivWidth - diff;
		// x = x - diff;
		// if( x < 0 ) x = 0;
	// }
	
	x = Math.round(screenWidth/2 - maxDivWidth/2);
	//y = Math.round(screenHeight/2 - maxDivHeight/2);
	
	if( !document.getElementById(div_name) ){
		create_div(x,y,css_class);
		fill_div(type, params);
	}
	else if( type == last_type && redraw != 1 ){
		update_div(x,y,type,params);
	}
	else{
		position_div(x,y);
		fill_div(type, params);
	}
	
	last_type = type;
	last_params = params;
}

function draw_float_div_position(x,y, css_class, type, params, redraw){
	var maxDivWidth = 500;
	
	var screenWidth = getClientWidth();
	var diff = screenWidth - x;
	if( diff < maxDivWidth ){
		diff = maxDivWidth - diff;
		x = x - diff;
		if( x < 0 ) x = 0;
	}
	
	if( !document.getElementById(div_name) ){
		create_div(x,y,css_class);
		fill_div(type, params);
	}
	else if( type == last_type && redraw != 1){
		update_div(x,y,type,params);
	}
	else{
		position_div(x,y);
		fill_div(type, params);
	}
	
	last_type = type;
	last_params = params;
}

function position_div(x,y){
	my_div.style.top = y+"px";
	my_div.style.left = x+"px";
}

function update_div(x,y,type,params){
	my_div.style.display = 'none';
	position_div(x,y);
	
	update_div_content(type, params);
	
	div_opacity = 0;
	my_div.style.opacity = div_opacity;
	my_div.style.display="block";
	smooth_div_show();
}

function create_div(x,y,css_class){
	my_div = document.createElement("DIV");
	my_div.style.top = y+"px";
	my_div.style.left = x+"px";
	my_div.className = css_class;
	my_div.setAttribute("id", div_name);
}

function fill_div(type,params){
	JsHttpRequest.query("/jquery/js_div/float_div.php",{'type':type, 'params':params}, fill_div_res);
}

var fill_div_res = function(result, errors){
	my_div.innerHTML = "<div align='right'>";
	my_div.innerHTML += "<a href='#' onClick='document.getElementById(\""+div_name+"\").style.display=\"none\"; return false;'><img src='/jquery/js_div/close.gif' border='0' align='right'></a>"; 
	my_div.innerHTML += "</div><br>";
	my_div.innerHTML += result;
	
	
	if( !document.getElementById(div_name) ){
		div_opacity = 0;
		my_div.style.opacity = div_opacity;
		document.body.appendChild(my_div);
		smooth_div_show();
	}
	else{
			div_opacity = 0;
			my_div.style.opacity = div_opacity;
			my_div.style.display="block";
			smooth_div_show();
		}
}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function get_elem_x(elem){
	var x = 0;
    while (elem){
        x += elem.offsetLeft;
        elem = elem.offsetParent;
    }
	return x;
}

function get_elem_y(elem){
    var y = 0;
    while (elem){
        y += elem.offsetTop;
        elem = elem.offsetParent;
    }
	return y;
}

function smooth_div_show(){
	my_div.style.filter="alpha(opacity="+div_opacity*100+")";
	my_div.style.opacity = div_opacity
	if( div_opacity >= 1 ){
		div_opacity = 0;
		return;
	}
	div_opacity += 0.03;
	setTimeout("smooth_div_show()",5);
}

/******************************************************************************************/
/*** В эту функцию надо писать код для каждого типа слоя, для статического обновления слоя. ***/
/******************************************************************************************/
function update_div_content(type, params){
	switch(type){
		case 'guestbook_write':
			break;
	}
}


