function previewArticle(author, articleId, articleLayout) {
	var previewWin = makePopup("/article/preview.jsp?author=" + author
			+ "&articleId=" + articleId + "&articleLayout=" + articleLayout, windowWidth(), windowHeight())

	previewWin.moveTo(-1, 50);
	previewWin.resizeTo(screen.availWidth + 2, screen.availHeight - 50);
	if (previewWin.focus) {
		previewWin.focus();
	}
}

var isPreview = false;

function previewComment(eventArg, elem, articleId, addDate, userName) {

	if(isPreview)
		return;
	var commentDiv = document.createElement("div");
	commentDiv.id = "commentDiv";
	commentDiv.innerHTML = "<table width=\"330px\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">"+
		"<tr bgcolor=\"#0088CC\"><td class=\"comment-preview-header\" bgcolor=\"#0088CC\" >"+
		"<span style=\"color:#fff;margin:2px 0px 0px 3px;height:25px;\">Last comment by: <b>"+userName+"</b><br> on <small>"+addDate+"</small></span>" +
		"</td><td valign=\"top\" align=\"right\" style=\"border-right: 1px solid #999999;\"><a href=\"javascript:void(0);\" onclick=\"opacityDown(document.getElementById('commentDiv'));\"" +
		" style=\"text-decoration:none;\"><img title=\"Click here to close\" src=\"/images/comment-close.png\" style=\"margin:3px 2px 0px 0px;;border:0;\"/></a></td>"+
		"</tr><tr><td colspan=\"2\" class=\"comment-preview-body\"><div id=\"comment-preview-text\">"+document.getElementById(articleId + "comment").value+"</div></td></tr></table>";
	
	var arrPos = findPos(elem);
	commentDiv.style.position = "absolute";
	if ((windowHeight() - eventArg.clientY) < 200)
		commentDiv.style.top = (arrPos[1] - 250) + "px";
	else
		commentDiv.style.top = arrPos[1] + "px";
	
	commentDiv.style.left = arrPos[0]+"px";
	commentDiv.style.width = "350px";
	commentDiv.style.height = "200px";
	commentDiv.style.margin = "13px 0 0 8px";
	commentDiv.style.opacity = "0.0";

	document.getElementsByTagName("body")[0].appendChild(commentDiv);

	opacityUp(commentDiv);
	isPreview = true;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function opacityUp(theElement) {
	var opacity = parseFloat(theElement.style.opacity);
	if (opacity >= 1)	{
		return true;
	}
	else {
		opacity += 0.1;
		theElement.style.opacity = opacity;
		setTimeout(function(){opacityUp(theElement);}, 30);
	}
	
	return true;
}


function opacityDown(theElement) {
	var opacity = parseFloat(theElement.style.opacity);
	if (opacity < 0.08)	{
		theElement.parentNode.removeChild(theElement);
	}
	else {
		opacity -= 0.1;
		theElement.style.opacity = opacity;
		setTimeout(function(){opacityDown(theElement);}, 30);
	}
	
	isPreview = false;
	return true;
}


function setPreview(layout) {
	document.getElementById("previewheadline").innerHTML = opener.document.forms.editArticleForm["headline"].value;
	var bodyArticle = new String();
	if(layout == "1") {
		bodyArticle = opener.document.forms.editArticleForm["body"].value;
	} else {
		
		bodyArticle = "<div class='article2'>" + opener.document.forms.editArticleForm["body1"].value + "</div>";
		bodyArticle += "<div class='article2'>" + opener.document.forms.editArticleForm["body2"].value + "</div>";
	}
	bodyArticle = bodyArticle.replace(/\r/g, "");
	bodyArticle = bodyArticle.replace(/\n/g, "</p>\n<p>");
	document.getElementById("previewbody").innerHTML = bodyArticle;
}

function makePopup(url, winWidth, winHeight) {
	var args = "height=" + winHeight + ",width=" + winWidth;
	args += ",resizable=yes,scrollbars=yes";
	return window.open(url, 'popup', args);
}

function windowHeight() {

	var winHeight = 0;

	if (typeof (window.innerWidth) == 'number') {
		winHeight = window.innerHeight;
	} else if (document.documentElement
			&& document.documentElement.clientHeight) {
		winHeight = document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		winHeight = document.body.clientHeight;
	}

	return winHeight;
}

function windowWidth() {

	var winWidth = 0;

	if (typeof (window.innerWidth) == 'number') {
		winWidth = window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		winWidth = document.documentElement.clientWidth;
	} else if (document.body && document.body.clientWidth) {
		winWidth = document.body.clientWidth;
	}

	return winWidth;
}