var formWin = new floatWindow();

/* --------------------------------------------------------------------------------------------------------------------	*/
/* collapseExpand																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function collapseExpand (oDiv)
{
	if (oDiv.className.indexOf("_expand") == -1)
	{
		oDiv.className += "_expand";

		oDiv.nextSibling.style.display = "";
	}
	else
	{
		oDiv.className = oDiv.className.substr(0,oDiv.className.length-7)

		oDiv.nextSibling.style.display = "none";
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* highlight																											*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function highlight (oDiv) 
{
	oDiv.className =  "over_" + oDiv.className
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* turnOff																												*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function turnOff (oDiv) 
{
	oDiv.className = oDiv.className.substr(5,oDiv.className.length-1)
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* showHideComments																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function showHideComments (col,i)
{
	oComments = document.getElementById("postComments" + i);

	if (oComments.style.display == "")
	{
		oComments.style.display 	= "none";
		col.childNodes[0].innerHTML = "הצגת תגובות";
	}
	else
	{
		oComments.style.display 	= "";
		col.childNodes[0].innerHTML = "הסתרת תגובות";
	}
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* openCloseComment																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function openCloseComment (postId, i)
{
	var commentBody = document.getElementById("commentBody" + postId + "_" + i); 

   	if (commentBody.style.display == "") 
	   	commentBody.style.display = "none";
   	else
		commentBody.style.display = "";
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* changeCommentImg																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function changeCommentImg (uniqueId)
{
	var imgSrc = document.getElementById("postCommentImg" + uniqueId).src;

	if (document.getElementById("commentBody" + uniqueId).style.display == "")
		document.getElementById("postCommentImg" + uniqueId).src = imgSrc.replace(/img.png/, "img_open.png");
	else
		document.getElementById("postCommentImg" + uniqueId).src = imgSrc.replace(/img_open.png/, "img.png");
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* showHideCommentForm																									*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function showHideCommentForm (i, e)
{
	if (typeof(e) == undefined) e = event;

	diving_setClickXY (e);
		
	var html  = "<div id='popupSmallWin'>" +
					"<div id='popupSmallWin_in'>" +
						"<div id='popupWinClose'><div onclick='formWin.close()'>X</div></div>"      +
						"<div id='popupSmallWinTitle'><div>הוספת תגובה חדשה</div></div>"	+
					    "<div id='addPost'>" +
						  	"<form id='addCommentForm' name='addCommentForm'>" +
							"<table cellpadding='3' cellspacing='3' border='0'>" +
							"<tr>" + 
								"<td class='formFieldText'>שם</td>" +
							    "<td class='formField'><input type='text' id='userName' name='userName' maxLength='100' /></td>" +
							"</tr>" + 
							"<tr>" + 
								"<td class='formFieldText'>כותרת</td>" +
							    "<td class='formField'><input type='text' id='title' name='title' maxLength='200' /></td>" +
							"</tr>" + 
							"<tr>" + 
								"<td class='formFieldText'>תוכן</td>" +
							    "<td class='formField'><textarea id='content' name='content'></textarea></td>" +
							"</tr>" + 
							"<tr>" + 
								"<td></td>" +
								"<td class='actionBtns' align='center'>" +
								   "<img src='loadedFiles/sendBtn.png' onclick='addNewComment(" + i  + ")' />" +
								"</td>" +
							"</tr>" + 
							"</table>" + 
							 "</form>" +
						 "</div>" +
					 "</div>" +
				  "</div>";

	floatWindow.is_ie = false;	// in order to cancel selectblocker

	formWin.create ("header", "addNewPostComment", html, 220, clickY-100);

	floatWindow.is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );

	formWin.show ();
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* showHideForwardForm																									*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function showHideForwardForm (postId, e)
{
	if (typeof(e) == undefined) e = event;

	diving_setClickXY (e);
		
	var redirect = location.href.split("index2.php?id=");

	var html  = "<div id='popupSmallWin'>" +
					"<div id='popupSmallWin_in'>" +
						"<div id='popupWinClose'><div onclick='formWin.close()'>X</div></div>"      +
						"<div id='popupSmallWinTitle'><div>שלח לחבר</div></div>"	+
					    "<div id='forwardPost'>" +
						  	"<form id='forwardPostForm' name='forwardPostForm' action='blogActions.php' method='post'>" +
							"<input type='hidden' name='postId' id='postId' value='" + postId + "' />" +
							"<input type='hidden' name='action' id='action' value='forwardPost' />" +
							"<input type='hidden' name='redirect' id='redirect' value='" + redirect[1] + "' />" +
							"<table cellpadding='2' cellspacing='2' border='0'>" +
							"<tr>" + 
								"<td class='formFieldText'>שמך</td>" +
							    "<td class='formField'><input type='text' id='myName' name='myName' maxLength='100' /></td>" +
							"</tr>" + 
							"<tr>" + 
								"<td class='formFieldText'>אימייל שלך</td>" +
							    "<td class='formField'><input type='text' id='myEmail' name='myEmail' maxLength='200' dir='ltr' /></td>" +
							"</tr>" + 
							"<tr>" +
								"<td colspan='2'><div id='forwardPostSep'><div></div></div></td>" +
							"</tr>" +
							"<tr>" + 
								"<td class='formFieldText'>שם החבר</td>" +
							    "<td class='formField'><input type='text' id='toName' name='toName' maxLength='100' /></td>" +
							"</tr>" + 
							"<tr>" + 
								"<td class='formFieldText'>אימייל החבר</td>" +
							    "<td class='formField'><input type='text' id='toEmail' name='toEmail' maxLength='200' dir='ltr' /></td>" +
							"</tr>" + 
							"<tr height='1'><td colspan='2'></td></tr>" +
							"<tr>" + 
								"<td></td>" +
								"<td class='actionBtns' align='center'>" +
								   "<img src='loadedFiles/sendBtn.png' onclick='forwardPost()' />" +
								"</td>" +
							"</tr>" + 
							"</table>" + 
							 "</form>" +
						 "</div>" +
					 "</div>" +
				  "</div>";

	floatWindow.is_ie = false;	// in order to cancel selectblocker

	formWin.create ("header", "forwardPost", html, 220, clickY-100);

	floatWindow.is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );

	formWin.show ();
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* forwardPost																											*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function forwardPost ()
{
	var form 		  = document.getElementById("forwardPostForm");

	var formValidator = new Validator("forwardPostForm");
	formValidator.clearAllValidations ();

	formValidator.addValidation('myName',			'required',		tailJS["blog_enterYourName"]);
	formValidator.addValidation('myEmail',			'required',		tailJS["blog_enterYourEmail"]);
	formValidator.addValidation('myEmail',			'email',		tailJS["blog_enterValidEmail"]);
	formValidator.addValidation('toName',			'required',		tailJS["blog_enterFName"]);
	formValidator.addValidation('toEmail',			'required',		tailJS["blog_enterFEmail"]);
	formValidator.addValidation('toEmail',			'email',		tailJS["blog_enterValidEmail"]);
				
	if (formValidator.validate ())
		form.submit();
	else
		return false;
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* printPost																											*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function printPost (i)
{
	var postTop  = document.getElementById("postTop"  + i);
	var postBody = document.getElementById("postBody" + i);

	html = 	"<html dir='ltr'>\n" +
		   	"<head>\n" +
				"<meta http-equiv='content-type' content='text/html; charset=windows-1255'>\n" +
				"<title>הדפסת רשומת בלוג</title>\n" +
				"<link type='text/css' rel='stylesheet' href='css/layouts.css'>\n" +
				"<link type='text/css' rel='stylesheet' href='css/blogs.css'>\n" +
				"<link type='text/css' rel='stylesheet' href='common.css'>\n" +
				"<script type='text/javascript'>\n" +
				"	function doPrint ()\n" +
				"	{\n" +
				"		window.print();\n" +
				"		window.close();\n" +
				"	}\n" +
				"   setTimeout(\"doPrint()\",150);\n" + 
				"</script>\n" +
			"</head>\n" +
			"<body>\n" +
				"<div id='printPost'>\n" +
					postTop.innerHTML + "<br/><br/>\n" +
					postBody.innerHTML + "</br/>\n" +
				"</div>\n" +
			"</body>\n" +
			"</html>";
			
	win = window.open  ("print.html", "_blank");
	win.document.open  ();
	win.document.write (html);
	win.document.close ();
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* addNewComment																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function addNewComment (postId)
{
	var oForm 		  = document.getElementById("addCommentForm");

	if (oForm.userName.value == "")
	{
		alert ("יש להזין שם");
		oForm.userName.focus ();
		return false;
	}

	if (oForm.title.value == "")
	{
		alert ("יש להזין כותרת");
		oForm.title.focus ();
		return false;
	}

	var xml = "<data>" +	
					"<command>private.addBlogComment</command>"					+
					"<postId>"		+ postId		 		+ "</postId>" 		+
					"<userName>"	+ oForm.userName.value 	+ "</userName>" 	+
					"<title>"		+ oForm.title.value 	+ "</title>" 		+
					"<content>"		+ oForm.content.value	+ "</content>" 		+
			  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "reloadPage");
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* showEditPostForm																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function showEditPostForm (blogId, postId)
{
	if (postId != undefined)
	{
		var title		= "עריכת רשומת בלוג";
		var postTitle	= document.getElementById("postTitle"   + postId).innerHTML;
		var postContent	= document.getElementById("postContent" + postId).innerHTML.replace(/#NL#/g, "\n");
		var action		= "updatePost(" + blogId + "," + postId;
	}
	else
	{
		var title		= "הוספת רשומת בלוג חדשה";
		var postTitle	= "";
		var postContent	= "";
		var action		= "addNewPost(" + blogId;
	}

	htmlData	= "<div id='popupWin'>" +
					"<div id='popupWin_in'>" +
						"<div id='popupWinClose'><div onclick='formWin.close()'>X</div></div>"      +
						"<div id='popupWinTitle'><div>" + title + "</div></div>"	+
					    "<div id='addPost'>" +
						  	"<form id='addPostForm' name='addPostForm'>" +
							"<table cellpadding='3' cellspacing='3' border='0'>" +
							"<tr>" + 
								"<td class='formFieldText'>כותרת הרשומה</td>" +
							    "<td class='formField'><input type='text' id='title' name='title' maxLength='200' value='"+postTitle+"'/></td>" +
							"</tr>" + 
							"<tr>" + 
								"<td class='formFieldText'>תוכן הרשומה</td>" +
							    "<td class='formField'><textarea id='content' name='content'>" + postContent + "</textarea></td>" +
							"</tr>" + 
							"<tr height='5'><td colspan='2'></td></tr>" + 
							"<tr>" + 
								"<td></td>" +
								"<td class='actionBtns' align='center'>" +
								   "<img src='loadedFiles/publishBtn.png' onclick='" + action + ", 1)' />&nbsp;&nbsp;&nbsp;&nbsp;" +
								   "<img src='loadedFiles/draftBtn.png'   onclick='" + action + ", 0)' />" +
								"</td>" +
							"</tr>" + 
							"</table>" + 
							 "</form>" +
						 "</div>" +
					 "</div>" +
				  "</div>";

	formWin.create ("header", "editBlogPost", htmlData, 0, 100);

	tinyMCE.init({  mode : "exact",
					elements : "content",
					content_css : "/css/tinymce.css",
					theme :	"advanced",
					width: "650",
					height: "330",
					verify_html : false,
					language : "he",
					directionality: "rtl",
					file_browser_callback : "ajaxfilemanager",
					plugins : "searchreplace,directionality,paste,contextmenu,inlinepopups",
					paste_create_paragraphs : false,
					paste_create_linebreaks : false,
					paste_use_dialog : true,
					paste_auto_cleanup_on_paste : true,
					paste_convert_middot_lists : false,
					paste_unindented_list_class : "unindentedList",
					paste_convert_headers_to_strong : true,
					paste_insert_word_content_callback : "convertWord",
					theme_advanced_toolbar_location : "top",
					theme_advanced_toolbar_align : "right",
					theme_advanced_buttons1 : "bold,italic,strikethrough,underline,|,sub,sup,charmap,|,cut,copy,paste,pasteword,|," +
											  "numlist,bullist,link,image,anchor,|,justifyright,justifycenter,justifyleft,justifyfull,rtl,ltr,|," + 
											  "redo,undo,search",
				    theme_advanced_buttons2 : "",
					theme_advanced_buttons3 : ""
				});


	formWin.show ();
}

function ajaxfilemanager(field_name, url, type, win) {
	var ajaxfilemanagerurl = "javascripts/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php";
	switch (type) {
		case "image":
			break;
		case "media":
			break;
		case "flash": 
			break;
		case "file":
			break;
		default:
			return false;
	}
	tinyMCE.activeEditor.windowManager.open({
              url: ajaxfilemanagerurl,
              width: 782,
              height: 440,
              inline : "yes",
              close_previous : "no"
          },{
              window : win,
              input : field_name
    });

	return false;
	/*
	var fileBrowserWindow = new Array();
	fileBrowserWindow["file"] = ajaxfilemanagerurl;
	fileBrowserWindow["title"] = "Ajax File Manager";
	fileBrowserWindow["width"] = "782";
	fileBrowserWindow["height"] = "440";
	fileBrowserWindow["close_previous"] = "yes";
	tinyMCE.activeEditor.windowManager.open(fileBrowserWindow, {
	  window : win,
	  input : field_name,
	  resizable : "yes",
	  inline : "yes",
      close_previous : "no"
      },{
      	window : win,
        input : field_name
      });
		
	return false;
	*/
}
/* --------------------------------------------------------------------------------------------------------------------	*/
/* addNewPost																											*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function addNewPost (blogId, isReady)
{
	tinyMCE.triggerSave();

	var oForm 		  = document.getElementById("addPostForm");

	if (oForm.title.value == "")
	{
		alert ("יש להזין כותרת");
		oForm.title.focus ();
		return false;
	}

	if (oForm.content.value == "")
	{
		alert ("יש להזין תוכן");
		oForm.content.focus ();
		return false;
	}

	var theContent = "<![CDATA[" + oForm.content.value + "]]>";

	var xml = "<data>" +	
					"<command>private.addBlogPost</command>"		+
					"<blogId>"		+ blogId		 				+ "</blogId>" 		+
					"<title>"		+ oForm.title.value 			+ "</title>" 		+
					"<content>"		+ theContent					+ "</content>" 		+
					"<isReady>"		+ isReady						+ "</isReady>" 		+
			  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "reloadPage");
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* updatePost																											*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function updatePost (blogId, postId, isReady)
{
	tinyMCE.triggerSave();

	var oForm 		  = document.getElementById("addPostForm");

	if (oForm.title.value == "")
	{
		alert ("יש להזין כותרת");
		oForm.title.focus ();
		return false;
	}

	if (oForm.content.value == "")
	{
		alert ("יש להזין תוכן");
		oForm.content.focus ();
		return false;
	}

	var theContent = "<![CDATA[" + oForm.content.value + "]]>";

	var xml = "<data>" +	
					"<command>private.editBlogPost</command>"					+
					"<blogId>"		+ blogId		 				+ "</blogId>" 		+
					"<postId>"		+ postId		 				+ "</postId>" 		+
					"<title>"		+ oForm.title.value 			+ "</title>" 		+
					"<content>"		+ theContent					+ "</content>" 		+
					"<isReady>"		+ isReady						+ "</isReady>" 		+
			  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "reloadPage");
}

/* ------------------------------------------------------------------------------------------------------------ */
/* reloadPage																									*/
/* ------------------------------------------------------------------------------------------------------------ */
function reloadPage ()
{
	window.location.reload ();
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* changeCommentImg																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function changeCommentImg (uniqueId)
{
	var imgSrc = document.getElementById("postCommentImg" + uniqueId).src;

	if (document.getElementById("commentBody" + uniqueId).style.display == "")
		document.getElementById("postCommentImg" + uniqueId).src = imgSrc.replace(/img.png/, "img_open.png");
	else
		document.getElementById("postCommentImg" + uniqueId).src = imgSrc.replace(/img_open.png/, "img.png");
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* blog_searchPosts																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function blog_searchPosts ()
{
	document.getElementById("searchPostsForm").submit();
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* confirmDeletePost																									*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function confirmDeletePost (e, postId)
{
	if (typeof(e) == undefined) e = event;

	diving_setClickXY (e);

	var html = "<div id='smallWin'>" + 
					"<div id='smallWin_in'>" +
						"<div id='smallWinClose'><div onclick='formWin.close()'>X</div></div>" +
						"<div id='confirmMsg'>האם ברצונך למחוק רשומה זו?</div>" +
				  		"<div id='confirmBtns'>" +
							  "<div id='confirmYesBtn'><div onclick='doDeletePost(" + postId + ")'><img src='loadedFiles/yesBtn.png' /></div></div>" + 
							  "<div id='confirmNoBtn'><div onclick='formWin.close()'><img src='loadedFiles/noBtn.png' /></div></div>" + 
				  		"</div>"
					"</div>" +
				"</div>";

	floatWindow.is_ie = false;	// in order to cancel selectblocker

	formWin.create ("header", "confirmWin", html, 160, clickY-100);

	floatWindow.is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );

	formWin.show ();
}

function doDeletePost (postId)
{
	window.location.href = "blogActions.php?action=deletePost&postId=" + postId + "&redirect=45";
}

/* --------------------------------------------------------------------------------------------------------------------	*/
/* deleteComment																										*/
/* --------------------------------------------------------------------------------------------------------------------	*/
function deleteComment (e, pageId, commentId)
{
	if (typeof(e) == undefined) e = event;

	diving_setClickXY (e);

	var html = "<div id='smallWin'>" + 
					"<div id='smallWin_in'>" +
						"<div id='smallWinClose'><div onclick='formWin.close()'>X</div></div>" +
						"<div id='confirmMsg'>האם ברצונך למחוק תגובה זו?</div>" +
				  		"<div id='confirmBtns'>" +
							  "<div id='confirmYesBtn'><div onclick='doDeleteComment("+commentId+")'><img src='loadedFiles/yesBtn.png' /></div></div>" + 
							  "<div id='confirmNoBtn'><div onclick='formWin.close()'><img src='loadedFiles/noBtn.png' /></div></div>" + 
				  		"</div>"
					"</div>" +
				"</div>";

	floatWindow.is_ie = false;	// in order to cancel selectblocker

	formWin.create ("header", "confirmWin", html, 160, clickY-100);

	floatWindow.is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );

	formWin.show ();
}

function doDeleteComment (commentId)
{
		window.location.replace("blogActions.php?action=deleteComment&commentId=" + commentId + "&redirect=54");
}


