/*
<div>Please insert control text below:</div><div><img src='random_image.php' style='border: 1px solid #cccccc;margin:0px;margin-bottom:10px;margin-top:8px;'/></div><div><input name='number' type='text' id='number' size='18' maxlength='20' class='commentTextfield'/></div>
*/

var commentDivId = "#TB_ajaxContent";

var ops = ["+","-","*"];

function dbg(obj) {
	var h = "";
	for (var key in obj)
		h+=key + " = " + obj[key] +"\n";
	alert(h);
}

function hiddenField(name,value) {
	return "<input type='hidden' name='" + name + "' value='" + value + "'/>";
}

function rand ( n ) {
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

function generateEquation() {
	var int1 = rand(10);
	var int2 = rand(10);
	var op = ops[rand(2)];
	return "" + int1 + " " + op + " " + int2 + " = ";
}

function respondToComment(pId,articleId,type,checksum) {
	var eq = generateEquation();
	$(commentDivId).text("").append("<form id='commentForm' action='comment.php' method='get'><h1>Post a new comment</h1><div class='commentText'>Name: </div><div><input type='text' name='name' size='18' class='commentTextfield' id='commentName'/></div><div class='commentText'>Comment (no HTML tags allowed): </div><div><textarea NAME='comment' class='commentTextarea' id='commentTextarea'></textarea></div><div class='commentText'>Please solve the equation below:</div><div class='commentText'>" + eq + "<input type='text' class='commentTextfield' size=3 name='eqResult'></div><div><input class='submitButton' type='submit' style='margin:0px;margin-bottom:10px;margin-top:8px;' value='Post comment' id='submitBtn'/>" + hiddenField("linkId",articleId)+hiddenField("parentPosnr",pId)+hiddenField("type",type)+hiddenField("clientChecksum",checksum) + hiddenField("mode","add") + hiddenField("eq",eq) + 
	"</form><div id='commentUserInfo' class='commentUserInfo'>&nbsp;</div><div id='commentResponse' style='display:none'></div>");
	$("#commentName").focus();
	
	 var options = { 
	  target:        '#commentResponse',   // target element(s) to be updated with server response 
	  success:       showResponse  // post-submit callback 

	  // other available options: 
	  //url:       url         // override for form's 'action' attribute 
	  //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	  //dateType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	  //clearForm: true        // clear all form fields after successful submit 
	  //resetForm: true        // reset the form after successful submit 

	  // $.ajax options can be used here too, for example: 
	  //timeout:   3000 
 	}; 
   // bind form using 'ajaxForm' 
   $('#commentForm').ajaxForm(options);
} 

function showResponse(responseText, statusText)  { 
    /*alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.');*/
    $('#commentUserInfo').empty().append(responseText);
    $('#commentUserInfo').fadeIn();
    if (responseText == "Comment saved successfully. Page will be reloaded...") {
		var url = window.location.href;
		
		if (navigator.userAgent.indexOf("Konqueror") != -1) {
			if (url.indexOf("showComments") == -1)
				url+="?a=b#showComments";
			else
				url = url.replace(/#showComments/g,"") + "&a=b#showComments";
			window.location.href = url;
		}
		else {
			if (url.indexOf("showComments") == -1) 
				url+="#showComments";
			window.location.href = url;
			window.setTimeout("window.location.reload()",700);
		}

    }
}

function showCommentsDivKonq() {
	$("#thickboxCommentDiv").css("display","block");
	commentDivId = "#thickboxCommentDiv";
}

$(document).ready(function() {
	$.each($("img[@src=http://themis.contwise.com/php/res.php?file=enlarge.gif]"),function(i,lens) {
		link = $(lens).parent();
		$(lens).remove();
		
		img = link.prev();
		img.remove();
		img.css("margin-right",img.attr("vspace") + "px");
		img.css("margin-bottom",img.attr("hspace") + "px");	
		
		title = link.attr("onclick").toString().split(/,/)[1].replace(/'/g,"");
		
		link.removeAttr("onclick");
		link.addClass("thickbox");
		link.attr("title",title);
		link.attr("rel","gallery");
		link.append(img);
		
		lensImg = document.createElement("img");
		lensImg.src = '../pics/lens.png';
		$(lensImg).addClass("lensImg");
		link.append(lensImg);
		
		img.bind("load",{"image" : img, "lensImage" : lensImg}, function(event) {
			if (event.data.image.attr("align") == "middle") {
				height = (event.data.image.height() / 2);
				$(event.data.lensImage).css("top",height);
			}
		});
	});

	if ($("#commentLink").get(0)) {
		commentLink = $("#commentLink");
		var numberComm = $("td.commentStartHeading").text().split(/ /)[0];
		commentLink.append("&nbsp;(" + numberComm +")");
		
		$.each($("td.commentHeading"), function(i, heading) {
			var text = $(heading).text();
			text = text.replace(/von/g,"by");
			text = text.replace(/ am/g,",");
			$(heading).text(text);
		});
		
		$.each($("a.commentRespondLink"), function(i,link) {
			if ($(link).text() == "Antworten") {
				$(link).text("Respond");
				$(link).css("color","#1A4680");
			}
			else {
				//$(link).parent().prepend("<h1>Comments</h1>");
				$(link).text("Post a new comment");
				$(link).css("color","#1A4680").css("font-size",14);
			}
			var linkHref = $(link).attr("href").replace(/"/g,"'");
			$(link).attr("href",linkHref);
		});
		
		var numberComm = $("td.commentStartHeading").text("");
	}
	if (navigator.userAgent.indexOf("Konqueror") == -1) {
		TB_init();
	}
	else {
		$("#commentLink").attr("href","javascript:showCommentsDivKonq()");
	}
	if (window.location.href.indexOf("showComments") != -1)
		$("#commentLink").click();
});

