/*
 * tripedia.org
 * 
 * (c) Jan Poeschko
 * 
 */

function imageLinkDestination(destination, format) {
	var name = destination.name;
	//var caption = '';
	var image = destination.images[format];
	var a = new Element('a', {
		'class': 'image',
		'href': destination.url,
		'title': destination.name
	});
	var img = new Element('img', {
		'src': image.url,
		'width': image.width,
		'height': image.height,
		'alt': destination.name
	});
	a.appendChild(img);
	//a.appendChild(document.createTextNode(caption));
	return a;
}

function linkDestination(destination) {
	var a = new Element('a', {'href': destination.url}).setText(destination.name);
	return a;
}

function linkCountry(country) {
	var a = new Element('a', {'href': country.url, 'class': 'iconed'}).setText(country.name);
	a.style.backgroundImage = 'url(' + country.image['icon'].url;
	return a;
}

function timerange(start, end) {
	function formatDate(date, fmt) {
		//var syntax = /(^|.|\r|\n)(\<%=\s*(\w+)\s*%\>)/;
		var syntax = /([NjY])/;
		return fmt.interpolate({
			'N': MONTH_NAMES_SHORT[date.getMonth()],
			'j': date.getDate(),
			'Y': date.getFullYear()
		});
	}
	
	function formatDates(fmt1, fmt2) {
		return formatDate(start, fmt1) + " - " + formatDate(end, fmt2);
	}
	
	if (start.getFullYear() == end.getFullYear())
		if (start.getMonth() == end.getMonth())
			if (start.getDate() == end.getDate())
				return formatDate(start, 'N j, Y');
			else
				return formatDates('N j', 'j, Y');
		else
			return formatDates('N j', 'N j, Y');
	else
		return formatDates('N j, Y', 'N j, Y');
}

function commafy(value) {
	//return value;
	value = String(value);
	var result = '';
	for (var index = value.length - 1; index >= 0; --index) {
		if ((value.length - index) % 3 == 1 && index < value.length - 1)
			result = ',' + result;
		result = value.charAt(index) + result;
	}
	return result;
}

function pluralize(count) {
	if (count == 1)
		return "";
	else
		return "s";
}