/**************************************************************************************************
* Goldfish Comments Script (works only with jQuery)
* This javascript is created by Goldfish from Fishbeam Software: http://www.fishbeam.com
* All rights reserved. © 2009 Yves Pellot
**************************************************************************************************/

$(document).ready(function(){
	$('.addComment').click(addComment);
	$('.addComment').mousedown(function(){
		$(this).attr('src', "support/comments/addCommentActive.png");
	});
	$('.addComment').mouseup(function(){
		$(this).attr('src', "support/comments/addComment.png");
	});
	$('.editComments').click(loadEditComments);
	$('.editComments').mousedown(function(){
		$(this).attr('src', "support/comments/editCommentsActive.png");
	});
	$('.editComments').mouseup(function(){
		$(this).attr('src', "support/comments/editComments.png");
	});
	$('.addReply').click(addReply);
	$('.removeComment').click(removeComment);
	$('.commentsFrame').load(showComments);
});
var highlightId="";

//Click on addComment or addReply button
function addComment() {
	var id=$(this).attr("id");
	var commentsId=id.substring(id.lastIndexOf("_")+1);
	loadAddComment(commentsId, 0);
}
function addReply() {
	var id=$(this).attr("id");
	var commentsId=id.substring(id.indexOf("_")+1, id.lastIndexOf("_"));
	var replyId=id.substring(id.lastIndexOf("_")+1);
	parent.loadAddComment(commentsId, replyId);
}
function removeComment() {
	var id=$(this).attr("id");
	var commentsId=id.substring(id.indexOf("_")+1, id.lastIndexOf("_"));
	var replyId=id.substring(id.lastIndexOf("_")+1);
	parent.loadRemoveComment(commentsId, replyId);
}


//Loads comments field (iFrame)
function loadComments() {
	var id=$(this).attr("id");
	var commentsId=id.substring(id.lastIndexOf("_")+1);
	showSpinner(function(){
		$('#editCommentsDiv_'+commentsId).fadeOut("slow", function(){
			parent.frames['comments_'+commentsId].location.reload();
		});
	}, $('#editCommentsDiv_'+commentsId).parent());
}

//Show comments field and highlight added entry if comments field is loaded
function showComments() {
	var id=$(this).attr("id");
	var commentsId=id.substring(id.lastIndexOf("_")+1);
	if(!$(this).parent().is(":visible")) {
		$(this).parent().fadeIn("slow", function(){
			hideSpinner(function(){
				if(highlightId!="") {
					parent.frames['comments_'+commentsId].highlight('#'+highlightId);
					highlightId="";
				}
			});
		});
	}
}

//Highlight added entry
function highlight(id) {
	$(id).effect("pulsate", {times: 1}, 1000);
}

//Load form for adding a comment or reply
function loadAddComment(commentsId, replyId) {
	showSpinner(function(){
		$('#commentsField_'+commentsId).fadeOut("slow", function(){
			$.get("support/comments/commentsEdit_"+commentsId+".php", {action: 'addComment', replyId: replyId}, function(data){
				$('#editCommentsDiv_'+commentsId).html(data);
				$('#editCommentsDiv_'+commentsId).fadeIn("slow", function(){
					hideSpinner();
					$('#commentsCancel_'+commentsId).click(loadComments);
					$('#commentsForm_'+commentsId+'_'+replyId).submit(sendReply);
				});
			});
		});
	}, $('#commentsField_'+commentsId).parent());
}

//Send reply form
function sendReply() {
	var id=$(this).attr("id");
	var commentsId=id.substring(id.indexOf("_")+1, id.lastIndexOf("_"));
	var replyId=id.substring(id.lastIndexOf("_")+1);
	showSpinner(function(){
		$('#editCommentsDiv_'+commentsId).fadeOut("slow", function(){
			$.post("support/comments/commentsEdit_"+commentsId+".php", {action: 'addComment', replyId: replyId, name: $('#commentName_'+commentsId).attr('value'), email: $('#commentMail_'+commentsId).attr('value'), message: $('#commentMessage_'+commentsId).attr('value')}, function(data){
				if(data.length<50) {
					highlightId=data;
					parent.frames['comments_'+commentsId].location.reload();
				}
				else {
					$('#editCommentsDiv_'+commentsId).html(data);
					$('#editCommentsDiv_'+commentsId).fadeIn("slow", function(){
						hideSpinner();
						$('#commentsCancel_'+commentsId).click(loadComments);
						$('#commentsForm_'+commentsId+'_'+replyId).submit(sendReply);
					});
				}
			});
		});
	}, $('#commentsField_'+commentsId).parent());
	return false;
}

//Load form for password (edit comments)
function loadEditComments(commentsId) {
	var id=$(this).attr("id");
	var commentsId=id.substring(id.lastIndexOf("_")+1);
	showSpinner(function(){
		$('#commentsField_'+commentsId).fadeOut("slow", function(){
			$.get("support/comments/commentsEdit_"+commentsId+".php", {action: 'editComments'}, function(data){
				$('#editCommentsDiv_'+commentsId).html(data);
				$('#editCommentsDiv_'+commentsId).fadeIn("slow", function(){
					hideSpinner();
					$('#commentsCancel_'+commentsId).click(loadComments);
					$('#commentsForm_'+commentsId).submit(sendEditComments);
				});
			});
		});
	}, $('#commentsField_'+commentsId).parent());
}

//Send password form
function sendEditComments() {
	var id=$(this).attr("id");
	var commentsId=id.substring(id.lastIndexOf("_")+1);
	showSpinner(function(){
		$('#editCommentsDiv_'+commentsId).fadeOut("slow", function(){
			$.post("support/comments/commentsEdit_"+commentsId+".php", {action: 'editComments', password: $('#commentsPassword_'+commentsId).attr('value')}, function(data){
				if(data=='OK') {
					parent.frames['comments_'+commentsId].location.reload();
				}
				else {
					$('#editCommentsDiv_'+commentsId).html(data);
					$('#editCommentsDiv_'+commentsId).fadeIn("slow", function(){
						hideSpinner();
						$('#commentsCancel_'+commentsId).click(loadComments);
						$('#commentsForm_'+commentsId).submit(sendEditComments);
					});
				}
			});
		});
	}, $('#commentsField_'+commentsId).parent());
	return false;
}

//Remove comment
function loadRemoveComment(commentsId, commentId) {
	showSpinner(function(){
		$('#commentsField_'+commentsId).fadeOut("slow", function(){
			$.get("support/comments/commentsEdit_"+commentsId+".php", {action: 'removeComment', commentId: commentId}, function(data){
				parent.frames['comments_'+commentsId].location.reload();
			});
		});
	}, $('#commentsField_'+commentsId).parent());
}