function format_number(x) 
{
	x = x.toString().split("").reverse().join("").replace(/(?=\d*\.?)(\d{3})/g,"$1.");
	x= x.split("").reverse().join("").replace(/^[\.]/,"");
	return x	
}

function format_00(x) {return ((x>9)?"":"0")+x}
function format_000(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}
function format_time(seconds)
{
	one_day = 86400;
	days = 0;
	if (seconds > one_day)
		days = parseInt(seconds / one_day);

	var tm=new Date(seconds*1000);
	var hours=tm.getUTCHours(); 
	var minutes=tm.getUTCMinutes(); 
	var seconds=tm.getUTCSeconds(); 
	
	str = format_00(hours) + ":" + format_00(minutes) + ":" + format_00(seconds);
	if (days > 0)
		str = days + "d, " + str
	return str
}

//------------------------------------------------
// Manager
//------------------------------------------------
var Manager = Class.create(
{
	// initialize
	//------------------------------------------------
	initialize: function() 
	{
		this.callbacks = []
		this.elapsedSeconds = 0;
		this.actions = []
	},
	// start
	//------------------------------------------------
	start: function() 
	{
		for (i=0;i<GI.actions.list.length;i++) 
		{
			var a = GI.actions.list[i];
    		this.actions.push(new Action(a.id, a.duration, a.remaining_time, a.c));		
		}
	
		new PeriodicalExecuter(this.timer, 1);
	},
	// add_callback
	//------------------------------------------------
	add_callback: function(f) 
	{
		this.callbacks.push(f)
	},
	// update_actions
	//------------------------------------------------
	update_actions: function() 
	{
		for (i=0;i<MANAGER.actions.length;i++)
		{ 
			a = MANAGER.actions[i];
			a.timer();
			$$('#AID_P_' + a.id).each(function(n)
			{
				n.innerHTML = a.percent;
			});
			$$('#AID_R_' + a.id).each(function(n)
			{
				n.innerHTML = format_time(a.remaining_time);
			});
		}
	},
	// timer
	//------------------------------------------------
	timer: function() 
	{
		MANAGER.timer_do();
	},
	// timer_do
	//------------------------------------------------
	timer_do: function() 
	{
		if (GI.actions.refresh_time > 0)
		{
			this.elapsedSeconds++;
			this.remaining = GI.actions.refresh_time - this.elapsedSeconds;
	
			this.callbacks.each(function(c){
				eval(c + '();');
			});
			
			if (this.remaining <= 0)
		    	setTimeout("document.location.reload()",1000);
		}
	}
});
var MANAGER = new Manager();

//------------------------------------------------
// start
//------------------------------------------------
function start()
{
	MANAGER.add_callback('game_timer_do')
	MANAGER.start();
}

//------------------------------------------------
// game_timer_do
//------------------------------------------------
function game_timer_do()
{
	MANAGER.update_actions()
}

//------------------------------------------------
// Action
//------------------------------------------------
var Action = Class.create(
{
	// initialize
	//------------------------------------------------
	initialize: function(id, duration, remaining_time, is_construction) 
	{
		this.id = id;
		this.duration = duration;
		this.remaining_time = remaining_time;
		this.is_construction = is_construction
		this.percent = 0;
		
		this.recalculate(); 
	},
	// recalculate
	//------------------------------------------------
	recalculate: function() 
	{
		if (this.remaining_time < 1)
			this.percent = 100;
		else 
			this.percent = parseInt(((this.duration - this.remaining_time) / this.duration)*100);
	},
	// timer
	//------------------------------------------------
	timer: function() 
	{
		if (this.remaining_time > 0)
			this.remaining_time--;
		this.recalculate();
	}
});

//------------------------------------------------
// show_map_info
//------------------------------------------------
function show_map_info(name, city_level, population, username, user_rank, user_rank_name) 
{
	pluralize = population>1?"s":""
	
	$('MI_cityname').innerHTML = name;
	$('MI_citypop').innerHTML = city_level + ' (' + population + ' habitante' + pluralize + ')';
	$('MI_username').innerHTML = username;
	$('MI_stars').innerHTML = '<div class="s_ss s_ss' + user_rank + '"></div>';
	$('MI_rank').innerHTML = user_rank_name;
	
	$('MI').show();
}

//------------------------------------------------
// hide_map_info
//------------------------------------------------
function hide_map_info(name) 
{
	$('MI').hide();
}

//------------------------------------------------
// show_title
//------------------------------------------------
function show_title(txt, level)
{
	$('pob_name').innerHTML = txt;
	if (level=='')
		$('pob_level').hide();
	else
	{
		$('pob_level').innerHTML = level;
		$('pob_level').show();
	}
	$('pob_titulo').show();
}

//------------------------------------------------
// hide_title
//------------------------------------------------
function hide_title()
{
	$('pob_titulo').hide();
}


// pop-up function
var newWin = null;
function closeWin()
{
	if (newWin != null)
	{
		if(!newWin.closed)
			newWin.close();
	}
}
function popUp(strURL,strType,strHeight,strWidth) 
{
	closeWin();
	var strOptions="";
	if (strType=="console") strOptions="scrollbars,height="+strHeight+",width="+strWidth;
	if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
	newWin = window.open(strURL, 'newWin', strOptions);
	newWin.focus();
}

