﻿var PhotoGrid =
{
	delayhide: null,
	tr: null,
	tbl: null,
	transLayer: null,
	popup: null,
	url: '/photo/list.aspx?dossier=true',
	title: null,
	imgSmuss: null,
	cellspacing: 10,

	Init: function(tbl, popup, transLayer, w, h, timespan) {
		var i, k, td, tr, img;
		PhotoGrid.popup = popup = document.getElementById(popup);
		PhotoGrid.tbl = tbl = document.getElementById(tbl);
		PhotoGrid.transLayer = transLayer = document.getElementById(transLayer);
		tbl.onmouseout = function() { PhotoGrid.Restore(timespan) };

		//PhotoGrid.imgSmuss = document.createElement("img");
		//PhotoGrid.imgSmuss.setAttribute('src','/images/common/smuss.gif');
		//PhotoGrid.imgSmuss.onclick = function(){PhotoGrid.ShowDetails(this)};

		PhotoGrid.tr = tr = GetXChildNodes(tbl, 'TBODY[0]/TR');
		for (i = 0; i < tr.length; i++) {
			td = GetChildNodes(tr[i], 'TD');
			for (k = 0; k < td.length; k++) {
				td[k].style.width = w + 'px';
				td[k].style.height = h + 'px';
				//PhotoGrid.Smuss(td[k]);
				td[k].onmouseover = function() { PhotoGrid.ShowToolTip(this, false, w, h) };
				td[k].onclick = function() { PhotoGrid.ShowDetails(this) };
			}
		}
		transLayer.style.width = popup.style.width = tbl.offsetWidth + 'px';
		transLayer.style.height = popup.style.height = tbl.offsetHeight + 'px';
	},

	ShowToolTip: function(sender, restore, w, h) {
		if (PhotoGrid.delayhide) {
			clearTimeout(PhotoGrid.delayhide);
			PhotoGrid.delayhide = null;
		}
		var tr = PhotoGrid.tr, i, k, tdlist, td, tooltip, tag;
		for (i = 0; i < tr.length; i++) {
			tdlist = GetChildNodes(tr[i], 'TD');
			for (k = 0; k < tdlist.length; k++) {
				td = tdlist[k];
				td.innerHTML = (td == sender || restore) ? "&nbsp;" : "<div style='height:" + (td.rowSpan == 2 ? ((h * 2) + PhotoGrid.cellspacing) : h) + "px' class='opaque'></div>";
				//PhotoGrid.Smuss(td);
				if (td == sender) {
					tooltip =
						k < tdlist.length - 2 || (k < tdlist.length - 1 && tdlist[k + 1].colSpan == 2) ? tdlist[k + 1] :
						tdlist[k - 1].colSpan == 2 ? tdlist[k - 1] :
						tdlist[k - 2];
					tag = td.getAttribute('tag');
				}
			}
		}
		if (tooltip)
			tooltip.innerHTML = "<div class='tooltip' style='height:" + h + "px; width:" + (10 + 2 * w) + "px'><p>" + tag + "</p></div>";
	},

	Restore: function(timespan) { if (!PhotoGrid.delayhide) PhotoGrid.delayhide = setTimeout('PhotoGrid.ShowToolTip(0,true)', timespan); },

	ShowDetails: function(sender) {
		var popup = PhotoGrid.popup;

		var td = GetXChildNodes(popup, 'DIV[0]/TABLE[0]/TBODY[0]/TR[0]/TD');
		//td[0].style.backgroundImage=sender.style.backgroundImage.replace('small','medie');

		td[0].style.backgroundImage = 'url(\'' + sender.getAttribute('copertinaId').replace('grandi', 'medie') + '\')';
		//alert(sender.getAttribute('copertinaId'));
		//alert(td[0].style.backgroundImage);
		td[1].innerHTML = sender.getAttribute('tag') + '<br/><br/><div align="right"><a href="' + PhotoGrid.url + '&title=' + sender.getAttribute('title') + '&photosetId=' + sender.getAttribute('pid') + '"><b>[go to photos]</b></a></div>';
		PhotoGrid.Restore(0);

		$(PhotoGrid.transLayer).show('fast');
		$(popup).show('fast');
	},

	HideDetails: function() {
		$(PhotoGrid.popup).hide('fast');
		$(PhotoGrid.transLayer).hide('fast');
	},

	Smuss: function(sender, opaque) {
		var w = sender.offsetWidth + 'px', h = sender.offsetHeight + 'px', img = PhotoGrid.imgSmuss.cloneNode(false);
		img.style.width = w;
		img.style.height = h;
		sender.innerHTML = opaque ? "<div style='height:" + h + "px' class='opaque'></div>" : "";
		if (opaque)
			sender = sender.firstChild;
		sender.appendChild(img);
	}
}
