function imageChange()
{
	var main = document.getElementById('main');
	var main_large = document.getElementById('main_large');
	var thumbs = document.getElementById('jsthumb');
	var thumb_images = thumbs.getElementsByTagName('img');

	if (!thumb_images[0]) {
		return false;
	}
	thumb_images[0].setAttribute("class","selected");
	for (i=0;i<thumb_images.length;i++) {
		thumb_images[i].style.cursor = 'pointer';
		thumb_images[i].onclick = function() {
			for (j=0;j<thumb_images.length;j++) {
				thumb_images[j].setAttribute("class","");
			}
			var split = this.src.split('/');
			var splitLength = split.length - 1;
			var fileName = split[splitLength];
			
			var source = '/_images/products/300/' + fileName;
			var source_large = '/_images/products/1200/' + fileName;
			main.setAttribute("src",source);
			main_large.setAttribute("href",source_large);
			this.setAttribute("class","selected");
			fix_hidden(source_large);
		}
	}	
}

function fix_hidden(href) {
	items = $j('.thumbs_hidden a');
	var i = 0;
	var pos = 0;
 	items.each(function () {
		this_href = $j(this).attr('href');
		if (this_href == href) {
			pos = i;
		}
		i++;
	});
	var string = "";
	string = "<a href='"+href+"'></a>";
	if (pos == 5) {
	}
	pos++;
	if (pos >= items.length) {
		pos = 0;
	}
	
	for (i=0;i<items.length-1;i++) {
		item = items[pos];
		string = string + "<a href='" + item + "' rel='lightbox[thumbs]'></a>";
		pos++;
		if (pos >= items.length){
			pos = 0;
		}
	}
	$j('.thumbs_hidden').html(string);
}

function imageClick()
{
	var click = document.createElement('p');
	var text = document.createTextNode('');
	click.appendChild(text);
	var thumbs = document.getElementById('jsthumb');
	var thumb_images = thumbs.getElementsByTagName('img');
	if (!thumb_images[0]) {
		return false;
	}
	var main = document.getElementById('main');
	
	thumbs.parentNode.insertBefore(click,thumbs);
}


addLoadEvent(imageChange);
addLoadEvent(imageClick);
