infuso_current_prefix = 0;
infuso_heap = Array();
window.onload = infuso_onload;

function infuso_onload()
{
	var id_prefix = 'infuso_background';
	for(var i in infuso_heap)
	{
		var element = document.getElementById(infuso_heap[i]['id']);
		infuso_heap[i]['element']  = element;
	}

	setInterval("infuso_onresize()",1000)
	infuso_onresize();
}

function infuso_onresize()
{

	for(var i=0;i<infuso_heap.length;i++)
	{
		var element = infuso_heap[i]['element'];

		if(1)
		{
		    // Отступы от краев
			var margin = Array (0,0,0,0);
			if(infuso_heap[i]['margin']) margin = infuso_heap[i]['margin'];


			// Расчитываем координаты
			var top_left = document.getElementById( infuso_heap[i]['top_left_id'] );
			var _right = document.getElementById( infuso_heap[i]['bottom_right_id'] );
			var _bottom = document.getElementById( infuso_heap[i]['bottom_right_id'] );

			var top_left_position = getElementPosition(top_left);
			var left = top_left_position.left + margin[3] ;
			var top = top_left_position.top + margin[0];

			var _right_position = getElementPosition(_right);
			var _bottom_position = getElementPosition(_bottom);


			// Расчитываем размеры фоновой картинки
			var width = (_right_position.left - top_left_position.left ) + _right.offsetWidth - (margin[1] + margin[3]);
			var height = (_bottom_position.top - top_left_position.top ) +  _bottom.offsetHeight - (margin[0] + margin[2]);

			element.style.top = top;
			element.style.left = left;
			if(width>=0 && height>=0)
			{
				element.style.width = width;
				element.style.height = height;
				element.style.display = "block";
			}
			else
			{
				element.style.display = "none";
			}
		}

		if(infuso_heap[i]['type']=='spacer')
		{
			element.parentNode.style.height = infuso_heap[i]['height'];
		}
	}
}

function $bg(url,top_left,bottom_right,styles,margin)
{
	var style = "";
	style += "position:absolute;";
	style += "background-image:url('"+url+"');";
	style += "font-size:0px;";
	style += styles;
	
	var id_prefix='infuso_background';
	var id = id_prefix + infuso_current_prefix;
	infuso_current_prefix++;
	
	document.write('<div id="'+id+'" style="'+style+'">');
	document.write('</div>');
	
	if(!margin) margin = Array(0,0,0,0);

	infuso_heap.push({'id':id,'top_left_id':top_left,'bottom_right_id':bottom_right,'margin':margin});

}

function getElementPosition(offsetTrail)
{
    var offsetLeft = 0;
         var offsetTop = 0;

    while(offsetTrail)
    {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }

    return { left:offsetLeft, top:offsetTop }
}

function get_child_nodes_by_tag_name(parent,node_name)
{
	var items = Array();
	for(var i=0;i<parent.childNodes.length;i++)
		if(parent.childNodes[i].nodeType==1)
			if(parent.childNodes[i].nodeName==node_name)
				items.push(parent.childNodes[i]);

	return items;
}



