// JavaScript Document

function replace(f, r, s){
	var ra = r instanceof Array, sa = s instanceof Array, l = (f = [].concat(f)).length, r = [].concat(r), i = (s = [].concat(s)).length;
	while(j = 0, i--)
		while(s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j < l);
	return sa ? s : s[0];
}

function site_url(uri)
{
	var ret_url = base_url + index_page + '/';	
	var ret_url_length = ret_url.length;
	
	if (ret_url.substr(ret_url.length - 2, 2) == '//')
	{
		return (base_url + uri);
	}
	else
	{
		return (ret_url + uri);
	}
	
}

function calculate_age(date)
{
	today    = current_date.split("-");
	birthday = date.split("-");
	
	birth_day   = birthday[2] * 1;
	birth_month = birthday[1] * 1;
	birth_year  = birthday[0] * 1;
	
	today_year  = today[0] * 1;
	today_month = today[1] * 1;
	today_day   = today[2] * 1;
	
	age = 0;
	
	if ((birth_year > 1900 && birth_year < today_year) && (birth_month >= 1 && birth_month <= 12) && (birth_day >= 1 && birth_day <= 31))
	{
		if ((today_month > birth_month) || (today_month == birth_month && today_day >= birth_day))
		{
			age = today_year - birth_year;
		}
		else if ((today_month < birth_month) || (today_month == birth_month && today_day < birth_day))
		{
			age = (today_year - 1) - birth_year;
		}
	}
	
	return age;
	
	//birthday = new Date(birthday);
	
	//return parseInt((today - birthday)/365/24/60/60/1000);	
}

//date format: 2009-10-10
function days_between(first_date, last_date)
{
	first_date = first_date.split("-");
	last_date = last_date.split("-");
	
	//Date(day, month, year)
	var from_date = new Date(first_date[1]+"/"+first_date[2]+"/"+first_date[0]);
	var to_date = new Date(last_date[1]+"/"+last_date[2]+"/"+last_date[0]);
	
	//Resta fechas y redondea
	var difference = to_date.getTime() - from_date.getTime();
	var days = Math.floor(difference / (1000 * 60 * 60 * 24));
	var seconds = Math.floor(difference / 1000);
	
	return days;
}

//date format: 2009-10-10
function is_up(first_date, last_date)
{
	if (days_between(first_date, last_date) > 0)
	{
		return true;
	}
	
	return false;
}

function image_hover(obj_id, image_name, ext)
{
	$("#"+obj_id).hover(
		function(){
			$(this).attr('src', site_url('public/images/'+image_name+'_ov.'+ext));
		}, 
		function(){
			$(this).attr('src', site_url('public/images/'+image_name+'.'+ext));
		}
	);	
}

