var gallery_image_count = 1;

if("undefined" == typeof($)) {
	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_alt = (arguments[4])? arguments[4] : '';
		var position_top = (arguments[5])? arguments[5] : 0;
		var img_caption = (arguments[6])? arguments[6] : '';
		var img_bg = document.createElement('div');
			img_bg.className = 'gallery_thumb_bg';
		var img = document.createElement('img');
		img.src = src + '?scale=0&amp;width='+ img_width +'&amp;height=' + img_height;
		img.alt = img_alt;
		img.title = img_alt;
		img.width = img_width;
		img.height = img_height;
		if(position_top) {
			img.style.position = 'relative';
			img.style.top = position_top + 'px';
		}
		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);
		if(img_caption) {
			caption = document.createElement('span');
			caption.className = 'caption';
			caption.innerHTML = img_caption;
			img_bg.appendChild(caption);
		}
		
		$(id).appendChild(img_bg);
		gallery_image_count++;
	}
}

function change_image(id, n) {
	if($(id)) {
		var _img = $(id).getElementsByTagName('img');
		
		// Reset all zIndex
		if(gallery_image_count > 25) {
			for(i=0; i<_img.length; i++) {
				_img[i].parentNode.style.zIndex = 0;
			}
			gallery_image_count = 0;
		}
		
		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.top) {
				_img[n].style.marginTop = Math.floor((holder_height - _img[n].height) / 2) + 'px';
			}
		}
	}
}

function set_selected(obj) {
	id_str = (arguments[1])? arguments[1] : 'selected';
	if($(id_str)) {
		$(id_str).removeAttribute('id');
	}
	obj.id = id_str;
}

function change_swf(id, n, swf) {
	if("undefined" == typeof(gallery_image_count))
		gallery_image_count = 10;
	$('product_360_holder').style.zIndex = ++gallery_image_count;
	window.setTimeout('swfobject.embedSWF("'+ swf +'", "product_360_inner", "500", "411", "9.0.0", false, {}, {quality: "high", wmode: "transparent", swfversion: "6.0.65.0", flashvars: ""}, {})', 5);
}
