function isUA() {
	if (window.opera)
		return 'opera';
	else if (document.documentElement.getAttribute("style") == document.documentElement.style)
		return 'ie';
	else
		return null;
}

var itemGallery = {};
itemGallery.init = function() {
	var a = document.getElementById('image-list').getElementsByTagName('a');
	var p = document.getElementById('image-description').getElementsByTagName('p');
	if (a.length != p.length)
		return;

	for (var i = 0; i < a.length; i ++) {
		if (i == 0) {
			a[i].className = p[i].className = 'current';
		}
		a[i].id = 'tn' + i;
		p[i].id = (a[i].id = 'tn' + i) + '_desc';
		if (isUA() == 'ie')
			a[i].setAttribute('onclick', new Function('itemGallery.replace(this); return false;'));
		else
			a[i].setAttribute('onclick', 'itemGallery.replace(this); return false;');
	}
	itemGallery.loading_img = new Image();
	itemGallery.loading_img.src = 'http://www.traveling-furniture.com/img/loading.gif';
	itemGallery.loading_img.style.cssText = 'position:absolute; top:0; left:0; z-index:1;';
};
itemGallery.replaceCurrent = function(ary, id) {
	for (var i = 0; i < ary.length; i ++) {
		if (ary[i].id != id && ary[i].className == 'current')
			ary[i].className = null;
		else if (ary[i].id == id && ary[i].className != 'current')
			ary[i].className = 'current';
	}
};
itemGallery.replace = function(a) {
	if (!('id' in a) && (a.className == 'current'))
		return;

	var a_href = a.href;
	if (isUA() == 'ie' && a_href.indexOf('%') == -1) {
		a_href = escape(a_href);
		a_href = a_href.replace(/http%3A\/\//i, 'http://');
	}

	var current_img = document.getElementById('current-img');
	if (current_img) {
		var tmp_img = current_img.parentNode.appendChild(itemGallery.loading_img);
		var new_img = new Image();
		var onloadFnc = function() {
			var desc_div = document.getElementById('image-description');
			if (desc_div) {
				itemGallery.replaceCurrent(desc_div.getElementsByTagName('p'), a.id + '_desc');
				current_img.src = a_href;
				current_img.parentNode.removeChild(tmp_img);
			}
		};
		if (isUA() == 'opera')
			onloadFnc();
		else
			new_img.onload = onloadFnc;
		new_img.src = a_href;
		var tn_div = document.getElementById('image-list');
		if (tn_div)
			this.replaceCurrent(tn_div.getElementsByTagName('a'), a.id);

	}

};

if (window.addEventListener)
	window.addEventListener('load', itemGallery.init, false);
else if (window.attachEvent)
	window.attachEvent('onload', itemGallery.init);
else
	window.onload = itemGallery.init;

