if (!BAGLAN) {
	var BAGLAN = {};
}

/**
Options hash:
	
@outer			- outer container from which height of inner block will be calculated 
@delta			-	vertical padding and margins between outer and inner blocks
@inner			- inner container which would limit the contents height
@contents		- contents container
@pagination	- pagination placeholder

*/

BAGLAN.Paginator = function( options ) {
	var that = this;

	var outer				= $(options.outer);
	var delta				= parseInt(options.delta);
	var inner				= $(options.inner);
	var contents		= $(options.contents);
	var pagination	= $(options.pagination);
	
	var pages = 1;
	var page = 0;
	var page_height = 0;
	
	var is_sliding = false;
	
	var is_visible = false;
		
	function set_page(p) {
		page = p;
		page = page>=pages?pages-1:page;
		page = page<0?0:page;
	}
	
	function jump_to_page(p) {
		set_page(p);
		contents.style.top = (-page*page_height)+'px';
	}
	
	function slide_to_page(p) {
		if (p==page)
			return; // Page didn't change

		set_page(p);
		render_pagination();
		
		is_sliding = true;
		new Effect.ScrollContainer(inner,page*page_height,{afterFinish:function(){is_sliding=false;}});
		
		// inner.scrollTop = page*page_height;
			
		/*
		new Effect.Move(contents,{
			mode:'absolute',
			x:0,
			y:-page*page_height,
			duration: 0.3,
			queue:'end'
		});
		*/
	}
	
	function render_pagination() {
		if (!pagination)
			return;
		
		if (pages<2)
		{
			pagination.innerHTML = '';
			is_visible = false;
			return;
		}
				
		var html = '<ul>';
		for (var i=0;i<pages;i++)
			html = html + '<li'+(i==page?' class="current"':'')+'>'+(i+1)+'</li>';
		html = html + '</ul>';
		
		pagination.innerHTML = html;
		
		var lis = pagination.getElementsByTagName('LI');
		for(var i=0;i<lis.length;i++)
			lis[i].onclick = function(pg){return function(){slide_to_page(pg)}}(i);
			
		var ul = pagination.getElementsByTagName('UL')[0];
		new Insertion.Top(ul,'<li class="prev"></li>');
		ul.firstChild.onclick = function(){return function(){slide_to_page(page-1)}}(i);
		
		new Insertion.Bottom(ul,'<li class="next"></li>');
		ul.lastChild.onclick = function(){return function(){slide_to_page(page+1)}}(i);
		
		if (!is_visible) {
			is_visible = true;
			new Effect.Pulsate(pagination,{pulses:3,duration:1.2});
		}
	}
		
	return {
		to_page: function(p) {
			slide_to_page(p)
		},
		
		render: function() {
			var line_height = parseInt(contents.getStyle('line-height'));
			var outer_lines = Math.floor((outer.getHeight()-delta)/line_height);
			var contents_lines = Math.ceil(contents.getHeight()/line_height);
			
			outer_lines = outer_lines<2?2:outer_lines;	// Show at least 2 lines - 1 will go as an overlap
			
			var real_page_height = outer_lines*line_height;
			page_height = (outer_lines-1)*line_height;
			
			var lines = outer_lines-1;
			if (lines<1)
				lines = 1;
			
			var ps = Math.ceil((contents_lines-1)/lines);
			if (ps!=pages)
			{
				pages = ps;
				jump_to_page(page);
				render_pagination();
			}
			
			// Manual scrolling
			if (!is_sliding) {
				var actual_page = Math.ceil(inner.scrollTop/page_height);
				if (actual_page!=page) {
					set_page(actual_page);
					render_pagination();
				}
			}
			
			inner.style.height = real_page_height+'px';

			/* shameless hack for IE placement */
			$('pagination-placeholder').style.top = $('datetable').getHeight()+"px";
		}
	};
}