function showMagicZoom(w, h, a, ow, oh){
	var im = document.getElementById('magicZoom');
	if(!im){
		return false;
	}
	im.href = a.href;
	originalWidth = ow;
	originalHeight = oh;
	im = im.firstChild;
	im.width=w; 
	im.height=h; 
	
	im.src = a.href;
	im = document.getElementById('enlargeImage');
	if(im)
		im.href = a.href;
	MagicZoom.refresh();
	return false;
};

Math.sgn = function(num){
	if(num==0)
		return 0;
	if(num>0)
		return 1;
	return -1;
}

var scrollWidth;
var scroll;
var scrollOffset;
var timer = false; 

function scrollImagesHoriz(direction){
	if(timer)
		return;
	if(imgs.length==0)
		return;
	var im = document.getElementById("img0");
	scrollWidth = im.offsetWidth;
	scroll = 0;
	var table = document.getElementById("imagesTable");
	scrollOffset = parseInt(table.style.left);
	target = scrollOffset+scrollWidth*direction;
	if(target>0){
		direction = -direction;
		target = -(imgs.length-1)*scrollWidth;
	} else if(target<=-imgs.length*scrollWidth){
		direction = -direction;
		target = 0;
	}
	moveImagesTableHoriz(target, direction);
}

function moveImagesTableHoriz(target, direction){
	var table = document.getElementById("imagesTable");
	scroll += direction;
	if(Math.sgn(target-scrollOffset-scroll)!=Math.sgn(direction)){
		table.style.left = target+"px";
		setOpacity(Math.round(-target/scrollWidth));
		timer = false;
		return;
	}
	table.style.left = (scrollOffset+scroll)+"px";
	direction += Math.sgn(direction);
	timer = setTimeout("moveImagesTableHoriz("+target+","+direction+")", 15);
}

function setOpacity(selected){
	currentImg = selected;
	for(var i=0; i<imgs.length; i++){
		var im = document.getElementById("img"+(i+1)).firstChild.firstChild;
		if(i==selected){
			im.style.opacity = 1;
			im.style.filter = "alpha(opacity=100)";
			im.style.cursor = "pointer";
		}else{
			im.style.opacity = 0.3;
			im.style.filter = "alpha(opacity=30)";
			im.style.cursor = "default";
		}
	}
	var itms = items[selected];
	var div = document.getElementById("buyTheLook");
	if(div)
		div.style.visibility = (itms?"visible":"hidden");
	div = document.getElementById("items");
	if(div){
		div.style.visibility = (itms?"visible":"hidden");
		var s = '';
		if(itms) for(i=0; i<itms.length; i++){
			if(i)
				s += '&nbsp; | &nbsp;';
			s += '<a href="'+itms[i][1]+'">'+itms[i][0]+'</a>';
		} else s='&nbsp;';
		div.innerHTML = s;
	}
}

function showSelectedImg(sel){
	if(!sel)
		sel = currentImg;
	if(sel!=currentImg)
		return false;
	var im = document.getElementById("img"+(sel+1));
	window.open(im.firstChild.href,"img","width="+(imgs[sel][1]+14)+",height="+(imgs[sel][2]+20));
}

var selectedImg = 0;
var imageWindow = false;
function showSelectedImgs(){
	if(imageWindow)
		imageWindow.close();
	var im = document.getElementById('magicZoom');
	imageWindow = window.open(im.href,"img","width="+(originalWidth+14)+",height="+(originalHeight+20));
}

/////////////////////////////////////////////////////////////

function scrollImagesPress(direction){
	if(timer)
		return;
	if(imgs.length==0)
		return;
	var im = document.getElementById("img0");
	scrollWidth = im.offsetWidth;
	scroll = 0;
	var table = document.getElementById("imagesTable");
	scrollOffset = parseInt(table.style.left);
	target = scrollOffset+scrollWidth*direction;
	if(target>0){
		direction = -direction;
		target = -(imgs.length-1)*scrollWidth;
	} else if(target<=-imgs.length*scrollWidth){
		direction = -direction;
		target = 0;
	}
	moveImagesTablePress(target, direction);
}

function moveImagesTablePress(target, direction){
	var table = document.getElementById("imagesTable");
	scroll += direction;
	if(Math.sgn(target-scrollOffset-scroll)!=Math.sgn(direction)){
		table.style.left = target+"px";
		setCookie("pressScroll",target);
		timer = false;
		return;
	}
	table.style.left = (scrollOffset+scroll)+"px";
	direction += Math.sgn(direction);
	timer = setTimeout("moveImagesTablePress("+target+","+direction+")", 15);
}


/////////////////////////////////////////////////////////////
function updatePopupUrl(url){
	return url + selectedImg+"/";
}
////////////////////////////////////////////////////////////

var currentImg = 0;
function showItemImages(direction){
	currentImg += direction;
	if(currentImg<0)
		currentImg += imgs.length;
	if(currentImg>=imgs.length)
		currentImg -= imgs.length;
	var ci = currentImg;
	for(var i=0; i<3;i++){
		var td = document.getElementById("img"+i);
		if(td){
			td.innerHTML = imgs[ci++];
			if(ci>=imgs.length)
				ci -= imgs.length;
		}
	}
}


var scrollHeight;
function scrollImages(direction){
	if(timer)
		return;
	if(imgs.length==0)
		return;
	var im = document.getElementById("img0");
	scrollHeight = im.offsetHeight;
	scroll = 0;
	moveImagesTable(direction);
}

function moveImagesTable(direction){
	var table = document.getElementById("imagesTable");
	scroll -= direction;
	if(Math.abs(scroll)>=scrollHeight){
		table.style.top = "0px"; 
		showItemImages(Math.sgn(direction));
		timer = false;
		return;
	}
	table.style.top = scroll+"px";
	direction += Math.sgn(direction);
	timer = setTimeout("moveImagesTable("+direction+")", 15);
}
