function GE(id)
{
	return document.getElementById(id);
}

function searchPage()
{
	var searchForm = GE("searchForm");
	var searchText = GE("searchText");
	
	var text = GE("textInput");
	text.value = searchText.value;
	searchForm.submit();
}

function createImgTable(imgArr, genre)
{
	var imgParent = GE("imgParent");

	var rc = imgArr.length / 3;
	var t = document.createElement("table");
	t.border = 0;
	t.cellSpacing = 10;
	t.cellPadding = 0;
	
	var counter = 0;
	for (var i = 0; i < rc; i++)
	{
		var row = t.insertRow(-1);
		for (var j = 0; j < 3; j++)
		{
			var cell = row.insertCell(-1);
			cell.className = "imagecell";
			cell.align = "center";

			if (imgArr[counter])
			{
				var href = "art.php?genre=" + genre + "&id=" + imgArr[counter][0];
				var imgSrc = "art/small/" + imgArr[counter][2] + ".jpg";
				var name = imgArr[counter][1];
				var year = imgArr[counter][3];
				
				var table = document.createElement("table");
				cell.appendChild(table);
				
				var innerCell = table.insertRow(-1).insertCell(-1);
				innerCell.style.border = "1px solid #666666";
				innerCell.style.backgroundColor = "#CCCCCC";

				var lnk1 = document.createElement("a");
				innerCell.appendChild(lnk1);
				lnk1.href = href;
				var img = document.createElement("img");
				img.src = imgSrc;
				img.hspace = 10;
				img.vspace = 10;
				img.border = 0;
				img.style.border = "1px solid #666666";
				lnk1.appendChild(img);

				var lnk = document.createElement("a");
				cell.appendChild(lnk);
				
				lnk.href = href;
				lnk.innerHTML = name + (year == 0 ? "" : (" " + year + "."));
			}
			
			counter++;
		}
	}
	
	imgParent.appendChild(t);
}


function showBigImage(piWidth, piHeight, id)
{
	var $windowName = "";
	if (piHeight > screen.height) piHeight = screen.height - 50;
	if (piWidth > screen.width) piWidth = screen.width - 50;

	window.open ("popup.php?id=" + id + "&h=" + piHeight + "&w=" + piWidth, $windowName, 	"scrollbars=yes,status=no,location=no,menubar=no,resizable=yes,toolbar=no,dependent=no,dialog=yes,minimizable=no,alwaysRaised=no, top=" + (screen.height - piHeight) / 2 + ", left=" + (screen.width - piWidth) / 2 +
		", width=" + piWidth + ", height=" + piHeight);
}

function fillLinks(listId)
{
	var list = GE(listId);
	for (var i = 0; i < links.length; i++)
	{
		var o = links[i];
		var li = document.createElement("LI");
		list.appendChild(li);
		var lnk = document.createElement("A");
		li.appendChild(lnk);
		lnk.href = o.href;
		lnk.innerHTML = o.title;
		lnk.target = "_blank";
		if (o.info != "")
		{
			var txt = document.createElement("SPAN");
			txt.innerHTML = " &ndash; " + o.info + ".";
			li.appendChild(txt);
		}
	}
}


function isStringEmpty(str)
{
	return (str == null || str == "");
}

String.prototype.format = function()
{
	var str = new String(this);
	for (var i = 0; i < arguments.length; i++)
	{
		str = str.replace('{' + i + '}', arguments[i]);
	}
	return str;
};

function toggleCommentArea()
{
	var div = GE("commentarea");
	var show = div.style.display == "none";
	div.style.display = show ? "" : "none";
}

function hoverCommentArea(head, hover)
{
	var div = GE("commentarea");
	var show = div.style.display == "";

	if (show)
		return;

	head.className = hover ? "commentheadover" : "commenthead";
}

function checkCommentForm(artid)
{
	var nameFld = GE("name");
	var mailFld = GE("mail");
	var commentFld = GE("comment");
	var nameTxt = nameFld.value;
	var mailTxt = mailFld.value;
	var commentTxt = commentFld.value;

	GE("namepoint").style.display = isStringEmpty(nameTxt) ? "" : "none";
	GE("commentpoint").style.display = isStringEmpty(commentTxt) ? "" : "none";

	if (isStringEmpty(nameTxt))
	{
		nameFld.focus();
		return false;
	}

	if (isStringEmpty(commentTxt))
	{
		commentFld.focus();
		return false;
	}

	sendComment(artid, nameTxt, mailTxt, commentTxt);

	return false;
}

function sendComment(artid, name, mail, comment)
{
	var cb = GE("notrobot");
	GE("spannew").style.display = GE("commentarea").style.display = "none";
	GE("spansending").style.display = "";

	if (cb.checked)
	{
		$.ajax({
			type: "POST",
			url: "worker.php",
			data: {artid: artid, name: name, mail: mail, comment: comment},
			dataType: "json",
			success: function(msg)
			{
				GE("spansending").style.display = "none";
				GE("spansended").style.display = "";
				
				if (msg.Result == "ERROR" || msg.Id == "-1")
				{
					GE("spansendedmsg").innerHTML = "Комментарий не сохранен. Возможен спам.";
					GE("spansendedmsg").style.color = "#FF6F6F";
				}
			},
			error: function()
			{
				GE("spansending").style.display = "none";
				GE("spansended").style.display = "";
				GE("spansendedmsg").innerHTML = "Комментарий не сохранен. Попробуйте отправить его позже.";
				GE("spansendedmsg").style.color = "#FF6F6F";
			}
		});
	}
	else
	{
		GE("spansending").style.display = "none";
		GE("spansended").style.display = "";
		GE("spansendedmsg").innerHTML = "Комментарий не сохранен. Возможен спам.";
		GE("spansendedmsg").style.color = "#FF6F6F";
	}

	var nameFld = GE("name");
	var mailFld = GE("mail");
	var commentFld = GE("comment");
	nameFld.value = mailFld.value = commentFld.value = "";
	cb.checked = false;
}

