var PhotoPreview = {
	// Costanti Immagini e Posizionamento
	defaultBigImageSize: 300,
	defaultSmallImageSize: 60,

	xPosWidthBase: 970,
	xPosXBase: 185 + 100,
	yPosYBase: - 60 - (navigator.product == "Gecko" ? 16 : 0),

	// Reference
	_Container: null, _BigImage: null, _SmallImagesContainer: null,

	// variabili delle immagini
	productId: 0, imagesIds: null, productUrl: null,

	// Timeout di visualizzazione
	hideTimeout: false, showTimeout: false,
	showMillisecWait: 200,
	hideMillisecWait: 600,
	loadingImage: false
};



// *********************
//		Disposition Methods
PhotoPreview.showProductPreview = function (imageId)
{
	this.showTimeout = false;

	if (typeof this.productId == 'string')
		this.setupBigImage (this.productId);

	else
	{
		if (!imageId)
			imageId = this.imagesIds[0];

		var loc = "/catalog/vetrine/default/img_tmp/" + this.productId + "_" + imageId + "_" + this.defaultBigImageSize + "_" + this.defaultBigImageSize + ".png";
		this.setupBigImage (loc);
	}

	this.setupSmlImages ();
	this.setupContainer ();
}
PhotoPreview.setupBigImage = function (loc)
{
	var img = this._BigImage;

	img.style.display = "none";
	img.src = loc;
	this.loadingImage = true;

	if (PhotoPreview.productUrl)
		img.parentNode.href = PhotoPreview.productUrl;
	else
		img.parentNode.href = "javascript: PhotoPreview.hide ();";

	img.onload = function ()
	{
		this.style.display = "block";
		PhotoPreview.checkContainer ();
		PhotoPreview.loadingImage = false;
	}
}
PhotoPreview.setupSmlImages = function ()
{
	var smls = this._SmallImagesContainer, smlCont, img, imgId;
	smls.innerHTML = "";

	if (this.imagesIds.length > 1)
	for (i in this.imagesIds)
	{
		imgId = this.imagesIds[i];
		img = document.createElement ("img");
		img.style.display = "none";
		img.src = "/catalog/vetrine/default/img_tmp/" + this.productId + "_" + imgId + "_" + this.defaultSmallImageSize + "_" + this.defaultSmallImageSize + ".png";
		img.imgId = imgId;
		img.defaultBigImageSize = this.defaultBigImageSize;
		img.productId = this.productId;
		img.style.cursor = "pointer";
		img.onmouseover = function ()
		{
			this.parentNode.className = "smlContOver";
		}
		img.onmouseout = function ()
		{
			this.parentNode.className = "smlCont";
		}
		img.onload = function ()
		{
			this.style.display = "inline";
		}
		img.onclick = function ()
		{
			var src = "/catalog/vetrine/default/img_tmp/" + this.productId + "_" + this.imgId + "_" + this.defaultBigImageSize + "_" + this.defaultBigImageSize + ".png";

			PhotoPreview.setupBigImage (src);
		}
		img.className = "imgThumb";

		smlCont = document.createElement ("span");
		smlCont.className = "smlCont";
		smlCont.appendChild (img);
		smls.appendChild (smlCont);
	}
}
PhotoPreview.setupContainer = function ()
{
	var cont = this._Container;

	cont.style.display = "block";
	cont.style.zIndex = "1000";
	// cont.style.width = this.defaultBigImageSize + 10 + (this.imagesIds.length > 1 ? this.defaultSmallImageSize + 20 : 0) + "px";
	// cont.style.height = this.defaultBigImageSize + 10 + "px";

	var sizes = window.getSize (), bodyY = document.body.scrollTop;
	var m = _Mouse.y + this.yPosYBase;
	var b = sizes[1] + bodyY - cont.offsetHeight + this.yPosYBase;
	cont.style.top = Math.min (_Mouse.y + this.yPosYBase, sizes[1] + bodyY - cont.offsetHeight + this.yPosYBase) - (this.imagesIds.length > 1 ? this.defaultSmallImageSize + 20 : 0) + "px";

	var bodyW = document.body.offsetWidth;
	cont.style.left = this.xPosXBase + Math.round ((bodyW - this.xPosWidthBase) / 2) + "px";
}
PhotoPreview.checkContainer = function ()
{
	var cont = this._Container;

	cont.style.display = "block";
	cont.style.zIndex = "1000";
	// cont.style.width = this._BigImage.offsetWidth + 10 + "px"; // (this.imagesIds.length > 1 ? this.defaultSmallImageSize + 20 : 0) +
	// cont.style.height = this._BigImage.offsetHeight + 10 + "px";

	var sizes = window.getSize (), bodyY = document.body.scrollTop;
	var m = _Mouse.y + this.yPosYBase;
	var b = sizes[1] + bodyY - cont.offsetHeight + this.yPosYBase;
	cont.style.top = Math.min (_Mouse.y + this.yPosYBase, sizes[1] + bodyY - cont.offsetHeight + this.yPosYBase) + "px";

	var bodyW = document.body.offsetWidth;
	cont.style.left = this.xPosXBase + Math.round ((bodyW - this.xPosWidthBase) / 2) + "px";
}



// *********************
//		Show / Hide Methods
PhotoPreview.start = function (productId, allImagesId, productUrl)
{
	this.assureWillNotCloseWindow ();

	this.productId = productId;
	if (!allImagesId)
		allImagesId = [];
	this.imagesIds = allImagesId;
	this.productUrl = productUrl;

	this.showTimeout = window.setTimeout ("PhotoPreview.showProductPreview ()", this.showMillisecWait);
}
PhotoPreview.stop = function ()
{
	if (this.loadingImage)
		return;

	if (this.showTimeout)
	{
		window.clearTimeout (this.showTimeout);
		this.showTimeout = false;
	}
	else
		this.hideTimeout= window.setTimeout ("PhotoPreview.hide ();", this.hideMillisecWait);
}
PhotoPreview.hide = function ()
{
	this._Container.style.display = "none";
	this._BigImage.src = '';
}
PhotoPreview.assureWillNotCloseWindow = function ()
{
	if (this.hideTimeout)
	{
		window.clearTimeout (this.hideTimeout);
		this.hideTimeout = false;
	}
}
