var gallery_image_count = 1;

function $(i) {
	return document.getElementById(i);
}

function strip_px(str) {
	return parseInt(str.replace(/[^0-9\.]/g, ''));
}

function load_gallery(id, src, img_width, img_height) {
	if($(id)) {
		var img_bg = document.createElement('div');
			img_bg.className = 'gallery_thumb_bg';
		var img = document.createElement('img');
		img.src = src;
		img.width = img_width;
		img.height = img_height;
		var holder_height = strip_px($(id).style.height);
		if(isNaN(holder_height) || holder_height < img_height) {
			$(id).style.height = img_height + 'px';
		}
		var holder_width = strip_px($(id).style.width);
		if(isNaN(holder_width) || holder_width < img_width) {
			$(id).style.width = img_width + 'px';
		}
		img_bg.style.zIndex = gallery_image_count;
		img_bg.appendChild(img);
		$(id).appendChild(img_bg);
		gallery_image_count++;
	}
}

function change_image(id, n) {
	if($(id)) {
		var _img = $(id).getElementsByTagName('img');
		if(_img[n]) {
			_img[n].parentNode.style.zIndex = ++gallery_image_count;
			_img[n].parentNode.style.top = '0px';
			_img[n].parentNode.style.left = '0px';
			
			// Vertical Align
			var holder_height = strip_px($(id).style.height);
			if(_img[n].height < holder_height) {
				_img[n].style.marginTop = Math.floor((holder_height - _img[n].height) / 2) + 'px';
			}
		}
	}
}