groups = [];
sections = [];

current_group = [];

ready = true;

section.prototype.slide_in = slide_in;
section.prototype.slide_out = slide_out;
section.prototype.load_section = load_section;
section.prototype.unload_section = unload_section;

function section(id,group,startx,starty,x,y,w,h,direction)
{
	this.id=id;
	this.group=group;
	this.startx = startx;
	this.starty = starty;
	this.x=x;
	this.y=y;
	this.w=w;
	this.h=h;
	this.direction=direction;
	sections.push(this.id);
	add_to_group(group,id);
}

function load_section()
{
	clearInterval(this.interval);
	ID(this.id).style.display='block';
	this.interval = setInterval(this.id+"_section.slide_in()",1000/fps)
	ready = false;
}

function slide_in()
{
	current_top = Number(ID(this.id).style.top.toString().split('px')[0]);
	current_left = Number(ID(this.id).style.left.toString().split('px')[0]);
	current_width = Number(ID(this.id).style.width.toString().split('px')[0]);
	current_height = Number(ID(this.id).style.height.toString().split('px')[0]);
	ready = false;
	
	if(this.direction == 'down')
	{
		shift_by = ((this.y-current_top)/5)
		
		if(current_top < this.y && shift_by > 1)
		{
			ID(this.id).style.top = current_top+shift_by;
		}
		else if(current_top < this.y && shift_by <= 1)
		{
			ID(this.id).style.top = current_top+1;
		}
		else
		{
			ID(this.id).style.top = this.y;
			ready = true;
			clearInterval(this.interval);
		}
	}
	else if(this.direction == 'right')
	{
		shift_by = ((this.x-current_left)/5)
		
		if(current_left < this.x && shift_by > 1)
		{
			ID(this.id).style.left = current_left+shift_by;
		}
		else if(current_left < this.x && shift_by <= 1)
		{
			ID(this.id).style.left = current_left+1;
		}
		else
		{
			ID(this.id).style.left = this.x;
			ready = true;
			clearInterval(this.interval);
		}
	}
}

function unload_section()
{
	clearInterval(this.interval);
	this.interval = setInterval(this.id+"_section.slide_out()",1000/fps);
	ready = false;
}

function slide_out()
{
	current_top = Number(ID(this.id).style.top.toString().split('px')[0]);
	current_left = Number(ID(this.id).style.left.toString().split('px')[0]);
	ready = false;
	
	if(this.direction == 'down')
	{
		shift_by = ((current_top-this.starty)/5)
		
		if(current_top > this.starty && shift_by > 1)
		{
			ID(this.id).style.top = current_top-shift_by;
		}
		else if(current_top > this.starty && shift_by <= 1)
		{
			ID(this.id).style.top = current_top-1;
		}
		else
		{
			ID(this.id).style.top = this.starty;
			ready = true;
			clearInterval(this.interval);
			ID(this.id).style.display='none';
		}/**/
	}
	else if(this.direction == 'right')
	{
		shift_by = ((current_left-this.startx)/5)
		
		if(current_left > this.startx && shift_by > 1)
		{
			ID(this.id).style.left = current_left-shift_by;
		}
		else if(current_left > this.startx && shift_by <= 1)
		{
			ID(this.id).style.left = current_left-1;
		}
		else
		{
			ID(this.id).style.left = this.startx;
			ready = true;
			clearInterval(this.interval);
		}
	}
}

function add_to_group(group_list,id)
{
	group_array = group_list.split(',');
	
	for(i=0;i<group_array.length;i++)
	{
		if(groups[group_array[i]] == undefined)
		{
			groups[group_array[i]] = [];
		}
		
		groups[group_array[i]].push(id);
	}
}

function load_all_sections(group)
{
	for(i=0;i<groups[group].length;i++)
	{
		//clearInterval(eval(sections[i]+"_section.interval"));
		setTimeout(groups[group][i]+"_section.load_section()",Math.ceil(load_time*1000*Math.random()));
	}
	
	current_group = group;
}

function unload_all_sections(group)
{
	if(group == undefined)
	{
		group = current_group;
	}
	
	for(i=0;i<groups[group].length;i++)
	{
		//clearInterval(eval(sections[i]+"_section.interval"));
		obj=eval(groups[group][i]+'_section');
		
		if(ID(obj.id).style.top != obj.starty || ID(obj.id).style.left != obj.startx)
		{
			setTimeout(groups[group][i]+"_section.unload_section()",Math.ceil(load_time*1000*Math.random()));
		}
	}
}