$(document).ready(function(){
	
	var form = $("#reply-form");
	var bt_cancel = $("button#reply-cancel");
	var reply_curmsg;
	var bt_reply;	
	
	$("button._reply").click(function(){
		
//		alert($("div#reply-form").height());
		replyCancel();

		bt_reply = this;

		var parent = $(this).parent()[0];
		reply_curmsg = $(parent).parent()[0];

		$("#reply_to_id").val( $(reply_curmsg).find("span.c_msg_id").text() );

		// $(form).hide();
		$(form).remove();
		
		$(this).css({visibility: "hidden"});
		$(bt_cancel).css({visibility: "visible"});
		$("#reply-form2").css({display: "none"});
		
		$(bt_cancel).click(replyCancel);

		$(reply_curmsg).addClass("active");
		$(reply_curmsg).append(form);
		// $(form).slideDown(100);
		$("#reply-form .comment-body").focus();
		window.scrollBy(0,-form.height());		
		
		$('#guestbook_header').keydown ( function (e) {
			if ((e.ctrlKey || e.metaKey) && e.keyCode == 13) {
				$('#guestbook_header').submit();
				}
			});		
	});
	
	$("button._reply_w_quote").click(function(){
		replyCancel();

		bt_reply = this;

		var parent = $(this).parent()[0];
		reply_curmsg = $(parent).parent()[0];

		$("#reply_to_id").val( $(reply_curmsg).find("span.c_msg_id").text() );

		// $(form).hide();
		$(form).remove();

		$(this).css({visibility: "hidden"});
		$(bt_cancel).css({visibility: "visible"});
		$("#reply-form2").css({display: "none"});
		
		$(bt_cancel).click(replyCancel);

		$(reply_curmsg).addClass("active");
		$(reply_curmsg).append(form);
		// $(form).slideDown(100);

		if ($(reply_curmsg).find("p.c_msg_text").attr("innerText"))
			var rs = $(reply_curmsg).find("p.c_msg_text").attr("innerText").split("\n");
		else
			var rs = $(reply_curmsg).find("p.c_msg_text").text().split("\n"); 

		$("#reply-form .comment-body").val(">" + rs.join("\n>") + "\n");
		$("#reply-form .comment-body").focus();

		window.scrollBy(0,-form.height());
		
		$('#guestbook_header').keydown ( function (e) {
			if ((e.ctrlKey || e.metaKey) && e.keyCode == 13) {
				$('#guestbook_header').submit();
				}
			});		
	});	
	
	function replyCancel (){
		$("#reply_to_id").val(0);
		
		$(reply_curmsg).removeClass("active");
		$(form).remove();
		$("#reply-placeholder").append(form);
		
		$(bt_cancel).css({visibility: "hidden"});
		$(bt_reply).css({visibility: "visible"});
		$("#reply-form2").css({display: "block"});
		
		window.scrollBy(0,form.height());
		
		$('#guestbook_header').keydown ( function (e) {
			if ((e.ctrlKey || e.metaKey) && e.keyCode == 13) {
				$('#guestbook_header').submit();
				}
			});		
	}
	
	
	// input fields that clear itself on focus
	
	$("input.flop").each( function(){
		$(this).val( $(this).attr("flopvalue") )
	});
	
	$("input.flop").focus(function(){
		if ( $(this).val() == $(this).attr("flopvalue") ) {
			$(this).val("");
			$(this).addClass("focus")
		}
	})

	$("input.flop").blur(function(){
		if ( $(this).val() == $(this).attr("flopvalue") || $(this).val() == "" ) {
			$(this).val( $(this).attr( "flopvalue") );
			$(this).removeClass("focus");
		}
	})

}); //close doc ready

function show_image(id, path, w, h)
{
	$('#ai_'+id).before('<span class="iload_progress"><img src="/i/images/progress.gif" width="200" height="19"></span>');
	$('#ai_'+id).before('<div><img src="' + path + '" width="' + w + '" height="' + h + '" style="display: none" onLoad="$(this).show(); $(' + "'.iload_progress'" + ').remove();"></div>');
	$('#ai_'+id).hide();
	
	return false;
}

function toggle_comments(ctype, id) 
{
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.responseJS) {
				document.getElementById('comments_subscribe').checked = req.responseJS.res == "1" ? true : false;
				$("#comments_subscribe_progress").css({display: "none"});
			}
		}
	}
	$("#comments_subscribe_progress").css({display: "inline"});
	req.open('POST', '/index.php?action=toggle_comments', true);
	req.send({ ctype: ctype, id: id });
} 

function boxes_toggle (name) 
{
	var boxes = new Array();
	boxes = document.getElementsByName(name);

	var i; var boxesCount = boxes.length;	
	for (i = 0; i < boxesCount; i++) {
		boxes[i].checked = !boxes[i].checked;	
	}
	return true;
}
