/* (c) Priit Kallas kallaspriit@gmail.com 2006 */
var bs_pointers = new Array();

var BlockScroller = function(block_id, speed, window_top_margin, max_scroll)
{
	this.block_id          = block_id          || 'block';
	this.speed             = speed             || 0.2;
	this.window_top_margin = window_top_margin || 10;
	this.max_scroll        = max_scroll        || 0;
	
	this.block = null;
	this.block_y = 0;
	this.target_margin = 0;
	this.last_top_scroll = -1;
	this.margin_interval = null;
	
	if((document.all) && this.window_top_margin > 0)
	{
		this.window_top_margin += 15;
	}
	
	bs_pointers[block_id] = this;
}

BlockScroller.prototype.init = function()
{
	this.block = document.getElementById(this.block_id);
	
	if(this.block !== null)
	{
		this.block_y = this.getElementTopPos(this.block);
		
		var parent_height = this.block.offsetParent.offsetHeight;
		
		if(parent_height > 0)
		{
			if(this.max_scroll == 0)
			{
				var self_height = this.block.offsetHeight;
				
				if(self_height > 0)
				{
					//this.max_scroll = parent_height-self_height;
				}
			}
		}
		
		var global_block_id = this.block_id;
		
		window.setInterval(function(){checkBlockScroll(global_block_id)}, 50);
	}
}

function checkBlockScroll(block_id)
{
	var bs = bs_pointers[block_id];
	
	var top_scroll = document.documentElement.scrollTop;
	
	if(top_scroll != bs.last_top_scroll)
	{
		bs.target_margin = top_scroll - bs.block_y + bs.window_top_margin;
		
		if(bs.target_margin < 0)
		{
			bs.target_margin = 0;
		}
		
		if(bs.max_scroll != 0 && bs.target_margin > bs.max_scroll)
		{
			bs.target_margin = bs.max_scroll;
		}
		
		if(bs.block.style.marginTop != bs.target_margin)
		{
			if(bs.margin_interval != null)
			{
				window.clearInterval(bs.margin_interval);
			}
			
			bs.margin_interval = window.setInterval(function(){applyBlockMargin(block_id)}, 1);
		}
		
		bs.last_top_scroll = top_scroll;
	}
}

function applyBlockMargin(block_id)
{
	var bs = bs_pointers[block_id];
	
	var current_margin_txt = bs.block.style.marginTop;
	
	var current_margin = 0;
	
	if(current_margin_txt != '')
	{
		current_margin = parseInt(current_margin_txt.substr(0, current_margin_txt.length-2));
	}
	
	if(current_margin == bs.target_margin)
	{
		window.clearInterval(bs.margin_interval);
	}
	
	var distance = Math.abs(bs.target_margin-current_margin);
	
	var step = Math.ceil(bs.speed * distance);
	
	var new_margin = current_margin;
	
	if(bs.target_margin > current_margin)
	{
		new_margin = current_margin+step;
	}
	else if(bs.target_margin < current_margin)
	{
		new_margin = current_margin-step;
	}
	
	if(new_margin < 0)
	{
		new_margin = 0;
	}
	
	bs.block.style.marginTop = new_margin+'px';
}

BlockScroller.prototype.getElementTopPos = function(obj)
{
	var top_pos = 0;
	
	if (obj.offsetParent) 
	{
		top_pos = obj.offsetTop;
		
		while(obj = obj.offsetParent) 
		{
			top_pos += obj.offsetTop;
		}
	}
	
	return top_pos;
}

BlockScroller.prototype.debug = function(txt)
{
	debug_element = document.getElementById('debug');
	
	if(txt.substr(0,1) == '+')
	{
		txt = '<span class="message">'+txt+'</span>';
	}
	else if(txt.substr(0,1) == '-')
	{
		txt = '<span class="error">'+txt+'</span>';
	}
		
	debug_element.innerHTML += txt+'<br />';
	
	current_scroll = debug_element.scrollTop;
	debug_element.scrollTop = current_scroll+500;
}
