groups = [];
sections = [];

group_names = [];
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;
section.prototype.detect_resize = detect_resize;
section.prototype.reposition = reposition;

function section(id,group,startx,starty,x,y,w,h,direction)
{
	this.id=id;
	this.group=group;
	
	this.static = true;
	this.original_w = w;
	this.original_h = h;
	this.original_x = x;
	this.original_y = y;
	
	this.startx = startx;
	this.starty = starty;
	
	if(isNumeric(w))
	{
		this.w=w;
	}
	else
	{
		this.static = false;
		if(w.indexOf('width') >=0)
		{
			this.w = Math.round(eval(w.replace('width', browser_size()['width'])));
		}
		else
		{
			this.w = ID(id).offsetWidth;
		}
	}
	
	if(isNumeric(h))
	{
		this.h=h;
	}
	else
	{
		this.static = false;
		if(h.indexOf('height') >=0)
		{
			this.h = Math.round(eval(h.replace('height', browser_size()['height'])));
		}
		else
		{
			this.h = ID(id).offsetHeight;
		}
	}
	
	if(isNumeric(x))
	{
		this.x=x;
	}
	else
	{
		this.static = false;
		
		if(x.indexOf('left') >=0)
		{
			this.x = eval(x.replace('left',0));
		}
		else if(x.indexOf('center') >=0)
		{
			this.x = eval(x.replace('center', Math.round(browser_size()['width']/2) - Math.round(this.w/2)));
		}
		else if(x.indexOf('right') >=0)
		{
			this.x = eval(x.replace('right', Math.round(browser_size()['width']) - Math.round(this.w)));
		}
	}
	
	if(isNumeric(y))
	{
		this.y=y;
	}
	else
	{
		this.static = false;
		
		if(y.indexOf('top') >=0)
		{
			this.y = eval(y.replace('top',0));
		}
		else if(y.indexOf('middle') >=0)
		{
			this.y = eval(y.replace('middle', Math.round(browser_size()['height']/2) - Math.round(this.h/2)));
		}
		else if(y.indexOf('bottom') >=0)
		{
			this.y = eval(y.replace('bottom', Math.round(browser_size()['height']) - Math.round(this.h)));
		}
	}
	
	if(direction == 'down')
	{
		this.starty = -20-this.h;
		this.startx = this.x;
	}
	else if(direction == 'right')
	{
		this.starty = this.y;
		this.startx = -20-this.w;
	}	
	
	ID(this.id).style.width = this.w;
	ID(this.id).style.height = this.h;
	ID(this.id).style.left = this.startx;
	ID(this.id).style.top = this.starty;	
	
	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)
		{
			group_names.push(group_array[i]);
			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()));
		}
	}
}


function detect_resize()
{
	//document.getElementsByTagName('body')[0].onresize = function(){alert('boom')};
	for(g=0;g<group_names.length;g++)
	{
		for(s=0;s<groups[group_names[g]].length;s++)
		{
			obj=eval(groups[group_names[g]][s]+'_section');
			
			if(!obj.static)
			{
				obj.reposition();
			}
		}
	}
}

function reposition()
{
	at_start = (position(ID(this.id)).x == this.startx && position(ID(this.id)).y == this.starty ? true : false);
	
	//trace(this.id+' : '+position(ID(this.id)).x+' == '+this.startx+' && '+position(ID(this.id)).y+' == '+this.starty);
	
	if(this.original_x.indexOf('left') >=0)
	{
		this.x = eval(this.original_x.replace('left',0));
	}
	else if(this.original_x.indexOf('center') >=0)
	{
		this.x = eval(this.original_x.replace('center', Math.round(browser_size()['width']/2) - Math.round(this.w/2)));
	}
	else if(this.original_x.indexOf('right') >=0)
	{
		this.x = eval(this.original_x.replace('right', Math.round(browser_size()['width']) - Math.round(this.w)));
	}
	
	if(this.original_y.indexOf('top') >=0)
	{
		this.y = eval(y.replace('top',0));
	}
	else if(this.original_y.indexOf('middle') >=0)
	{
		this.y = eval(this.original_y.replace('middle', Math.round(browser_size()['height']/2) - Math.round(this.h/2)));
	}
	else if(this.original_y.indexOf('bottom') >=0)
	{
		this.y = eval(this.original_y.replace('bottom', Math.round(browser_size()['height']) - Math.round(this.h)));
	}
	
	if(this.original_w.indexOf('width') >=0)
	{
		this.w = Math.round(eval(this.original_w.replace('width', browser_size()['width'])));
	}
	
	if(this.original_h.indexOf('height') >=0)
	{
		this.h = Math.round(eval(this.original_h.replace('height', browser_size()['height'])));
	}
	
	if(this.direction == 'down')
	{
		this.starty = -20-this.h;
		this.startx = this.x;
	}
	else if(this.direction == 'right')
	{
		this.starty = this.y;
		this.startx = -20-this.w;
	}		
	
	if(at_start)
	{
		ID(this.id).style.left = this.startx;
		ID(this.id).style.top = this.starty;
	}
	else
	{
		ID(this.id).style.left = this.x;
		ID(this.id).style.top = this.y;
	}
	
	ID(this.id).style.width = this.w;
	ID(this.id).style.height = this.h;
}

