var g_AreaWidth,g_AreaTop,g_AreaLeft;
var g_ItemList;
var g_PosList;
var g_TopPos;
var g_BottomPos;
var g_LeftPos;
var g_ItemLeft;
var g_ItemTop;
var timerID;
var changeTimerID;
var g_MaxLoop;

function init()
{
	var idx;
	var area_obj = document.getElementById('deckarea');
	g_AreaTop = area_obj.offsetTop;
	g_AreaLeft = area_obj.offsetLeft;
	g_AreaWidth = area_obj.offsetWidth;
	var parent_obj = area_obj.offsetParent;
	if(parent_obj.id != ''){
		while(parent_obj.id != ''){
			g_AreaTop += parent_obj.offsetTop;
			g_AreaLeft += parent_obj.offsetLeft;
			parent_obj = parent_obj.offsetParent;
		}
	}

	var item_obj = document.getElementsByName('deckitem');
	g_ItemList = new Array(item_obj.length);
	g_ItemLeft = new Array(item_obj.length);
	g_ItemTop = new Array(item_obj.length);
	for(idx=item_obj.length-1; idx>=0; idx--){
		g_ItemList[idx] = item_obj[idx];
		g_ItemList[idx].style.left = item_obj[idx].offsetLeft+'px';
		g_ItemList[idx].style.top = item_obj[idx].offsetTop+'px';
		g_ItemList[idx].style.position = 'absolute';
	}
	g_PosList = new Array();
	g_PosList[0] = new Array(0,0,1);
	g_TopPos = 0;
	g_BottomPos = 0;
	g_LeftPos = 0;

	set_items();
}

function resize_items()
{
	var area_obj = document.getElementById('deckarea');
	g_AreaTop = area_obj.offsetTop;
	g_AreaLeft = area_obj.offsetLeft;
	g_AreaWidth = area_obj.offsetWidth;
	var parent_obj = area_obj.offsetParent;
	if(parent_obj.id != ''){
		while(parent_obj.id != ''){
			g_AreaTop += parent_obj.offsetTop;
			g_AreaLeft += parent_obj.offsetLeft;
			parent_obj = parent_obj.offsetParent;
		}
	}

	g_PosList = new Array();
	g_PosList[0] = new Array(0,0,1);
	g_TopPos = 0;
	g_BottomPos = 0;
	g_LeftPos = 0;

	set_items();
}

function set_items()
{
	var idx,position;

//	var tagdebug = document.getElementById('debug');
//	tagdebug.innerHTML = 'g_AreaWidth='+g_AreaWidth+'<br>';

	for(idx=0; idx<g_ItemList.length; idx++){
//tagdebug.innerHTML += 'idx='+idx+', width='+g_ItemList[idx].offsetWidth+', height='+g_ItemList[idx].offsetHeight+'<br>';
		position = get_pos(g_ItemList[idx].offsetWidth,g_ItemList[idx].offsetHeight);
		set_item_pos(idx,position[0],position[1]);
	}
	var area_obj = document.getElementById('deckarea');
	area_obj.style.height = g_BottomPos+'px';
	g_MaxLoop = 30;
	timerID = setTimeout('redraw_func()',50);
}

function redraw_func()
{
	var idx, ret;

	clearTimeout(timerID);
	ret = 0;
	for(idx=0; idx<g_ItemList.length; idx++){
		ret += move_item(idx,0);
	}

	g_MaxLoop--;
	if(g_MaxLoop <= 0){
		ret = 0;
	}

	if(ret > 0){
		timerID = setTimeout('redraw_func()',50);
	}
	else{
		timerID = setTimeout('fit_items()',50);
	}
}

function fit_items()
{
	var idx;

	clearTimeout(timerID);
	for(idx=0; idx<g_ItemList.length; idx++){
		move_item(idx,1);
	}
	changeTimerID = setTimeout('change_order()',15000);
}

function change_order()
{
	var idx, saveItem;

	clearTimeout(changeTimerID);
	saveItem = g_ItemList[0];
	for(idx=0; idx<g_ItemList.length-1; idx++){
		g_ItemList[idx] = g_ItemList[idx+1];
	}
	g_ItemList[g_ItemList.length-1] = saveItem;
	resize_items();
}

function set_item_pos(idx,left,top)
{
	g_ItemLeft[idx] = left;
	g_ItemTop[idx] = top;
}

function move_item(idx,is_fit)
{
	var cur_left, cur_top, deltax, deltay;

	cur_left = g_ItemList[idx].offsetLeft;
	cur_top = g_ItemList[idx].offsetTop;

	if(is_fit == 0){
		deltax = (g_ItemLeft[idx] - cur_left)/5;
		cur_left += deltax;
		deltay = (g_ItemTop[idx] - cur_top)/5;
		cur_top += deltay;
		if((Math.abs(deltax) < 0.5) && (Math.abs(deltay) < 0.5)){
			is_fit = 1;
		}
	}
	if(is_fit == 0){
		g_ItemList[idx].style.left = cur_left+'px';
		g_ItemList[idx].style.top = cur_top+'px';
		return 1;
	}
	else{
		g_ItemList[idx].style.left = g_ItemLeft[idx]+'px';
		g_ItemList[idx].style.top = g_ItemTop[idx]+'px';
	}
	return 0;
}

function get_pos(width,height)
{
	var left,top;
	if(g_LeftPos+width > g_AreaWidth){
		g_LeftPos = 0;
	}
	if(g_BottomPos < g_TopPos+height){
		g_BottomPos = g_TopPos+height;
	}
	left = g_LeftPos;
	g_LeftPos = g_LeftPos+width;
	top = get_top_point(left,g_LeftPos-1);
	add_point(left,g_LeftPos-1,top+height);
	return [g_AreaLeft+left,g_AreaTop+top];
}

function get_top_point(left,right)
{
	var idx,flag,top;
//var debug = '';
	top = 0;
	flag = 0;
//debug += 'lengh='+g_PosList.length+"<br>\n";
	for(idx=0; idx<g_PosList.length; idx++){
//debug += 'left='+left+',right='+right+','+g_PosList[idx][0]+' , '+g_PosList[idx][1]+' , '+g_PosList[idx][2]+"<br>\n";
		if((flag == 0) && (g_PosList[idx][0] > left)){
			if((idx+1 < g_PosList.length) && (g_PosList[idx][2]==1) && (g_PosList[idx][0]-2<=left)){
				continue;
			}
//debug += 'flag = 1'+"<br>\n";
			flag = 1;
			if((g_PosList[idx][2] == 1) && (g_PosList[idx][0] > right)){
				top = g_PosList[idx][1];
			}
		}
		if(g_PosList[idx][0] > right){
//debug += 'break'+"<br>\n";
			break;
		}
		if((flag == 0) && (g_PosList[idx][0] >= left)){
			top = g_PosList[idx][1];
//debug += 'flag0 top = '+top+"<br>\n";
		}
		if((flag == 1) && (top < g_PosList[idx][1])){
			top = g_PosList[idx][1];
//debug += 'flag1 top = '+top+"<br>\n";
		}
	}
	if(flag == 0){
		top = g_TopPos;
	}
//debug += 'g_leftPos'+g_LeftPos+'<br>';
//alert(debug+top);
//	var tagdebug = document.getElementById('debug');
//	tagdebug.innerHTML += debug+top+"<br>\n";
	return top;
}

function add_point(left,right,bottom)
{
	var idx, flag;
	var new_list = new Array();

	flag = 0;
	for(idx=0; idx<g_PosList.length; idx++){
		if(g_PosList[idx][0] < left){
			new_list.push(new Array(g_PosList[idx][0],g_PosList[idx][1],g_PosList[idx][2]));
		}
		else if(flag == 0 && g_PosList[idx][0] == left){
			new_list.push(new Array(left,bottom,0));
			flag++;
		}
		else if(flag == 0 && g_PosList[idx][0] > left){
			new_list.push(new Array(left,bottom,0));
			if(g_PosList[idx][0] > right){
				new_list.push(new Array(right,bottom,1));
				new_list.push(new Array(g_PosList[idx][0],g_PosList[idx][1],g_PosList[idx][2]));
				flag++;
			}
			flag++;
		}
		else if(flag == 1){
			if(g_PosList[idx][0] < right){
			}
			else if(g_PosList[idx][0] == right){
				new_list.push(new Array(right,bottom,1));
				flag++;
			}
			else if(g_PosList[idx][0] > right){
				new_list.push(new Array(right,bottom,1));
				new_list.push(new Array(g_PosList[idx][0],g_PosList[idx][1],g_PosList[idx][2]));
				flag++;
			}
		}
		else if(flag == 2){
			new_list.push(new Array(g_PosList[idx][0],g_PosList[idx][1],g_PosList[idx][2]));
		}
	}
	if(flag == 0){
		new_list.push(new Array(left,bottom,0));
		flag++;
	}
	if(flag == 1){
		new_list.push(new Array(right,bottom,1));
	}
	g_PosList = new_list;
	g_TopPos = 0;
	g_BottomPos = 0;
//var debug = 'start ---- '+left+','+right+','+bottom+'<br>';
	for(idx=0; idx<g_PosList.length; idx++){
//debug += g_PosList[idx][0]+' , '+g_PosList[idx][1]+' , '+g_PosList[idx][2]+"<br>";
//		if((g_TopPos==0) || (g_TopPos > g_PosList[idx][1])){
//			g_TopPos = g_PosList[idx][1];
//		}
		if(g_BottomPos < g_PosList[idx][1]){
			g_BottomPos = g_PosList[idx][1];
		}
	}
//var deb_obj = document.getElementById('debug');
//deb_obj.innerHTML += debug;
//alert(debug);
}



function addResizeEvent(func)
{	
	if(typeof window.addEventListener == 'function'){
		window.addEventListener('resize', func, false);
		return true;
	}
	else if(typeof window.attachEvent == 'object'){
		window.attachEvent('onresize', func);
		return true;
	}
	var oldonresize = window.onresize;
	if (typeof window.onresize != 'function'){
    	window.onresize = func;
	} else {
		window.onresize = function(){
		oldonresize();
		func();
		}
	}
}

function addLoadEvent(func)
{	
	if(typeof window.addEventListener == 'function'){
		window.addEventListener('load', func, false);
		return true;
	}
	else if(typeof window.attachEvent == 'object'){
		window.attachEvent('onload', func);
		return true;
	}
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

addLoadEvent(init);
addResizeEvent(resize_items);

