$(document).ready(function() {
		
		// header links to main
	$("#main_header").click(function(){
		window.location = "/index.php";
	});
	
		// ugly IE hack
	$("#main_header").height(200);
	$("#main_content").height($("#main_container").height()-225);
	var txt = "<div style='height:10px;'></div>";
	$(document.body).append(txt);
	
		// post previews on frontpage
	$(".postpreview").click(function() {
		window.location = '/index.php?act=post&id=' + $(this).attr("id").substr(1);
	});

	
		// open poems popup on frontpage
	$(".showpoem").click(function(){
		var poemid = $(this).attr("id").substr(2);
		window.open("/index.php?act=poem&id="+poemid, "poem", "height=600, width=600, scrollbars=yes");
		return false;
	});
	
	
		// poems previews on post page
	$(".showpoem").hover(
		function(){
			$(this).css({color:"black"});
			var poemid = $(this).attr("id").substr(2);
			var pos = $(this).position();
			$.post("index.php",
					{act: "poem", opt: "preview", id: poemid },
					function(data) {
						$("#poem_preview").html(data);
						$("#poem_preview").fadeIn('fast');
						$("#poem_preview").css(
								{"position": "absolute", 
									"top": pos.top,
									"left": pos.left-300} );
						$("#poem_preview").click(function() {
							window.open("/index.php?act=poem&id="+poemid, "poem", "height=600, width=600");
						});
					}
			);
		},
		function() {
			var t = setTimeout(function() {
						if ($("#over_preview").val() == 0) {
							$(".showpoem").css({color:"grey"});
							$("#poem_preview").fadeOut('fast');
						}
					}, 100);
		}
	);
	
	$("#poem_preview").mouseover(function() {
				$("#over_preview").val(1);
	});
	$("#poem_preview").hover(function() {},
			function() {
				$(".showpoem").css({color:"grey"});
				$("#over_preview").val(0);
				$(this).fadeOut('fast');
			}
	);
	
		// "write us posts" page anims
	$("._change_writepost").click(function() {
		var chosen = $(this).attr("id").substr(2);
		var visobj = false;
		$("._writepostdesc").each(function() {
			if ($(this).css("display") != "none") {
				visobj = $(this);
			}
		});
		if (visobj) {
			visobj.fadeOut("slow", function() {$("#w"+chosen).fadeIn("slow")});
		} else {
			$("#w"+chosen).fadeIn("slow");
		}
	});

		// posts interface - comments section
	$("#show_comments").click(function() { showComments(); });

	function showComments() {
		$("#comments").fadeIn('slow');
		$("#show_comments").unbind();
		$("#show_comments").html("הסתר תגובות");
		$("#show_comments").click(function() {  hideComments(); });
	}

	function hideComments() {
		$("#comments").fadeOut('slow');
		$("#show_comments").unbind();
		$("#show_comments").html("הצג תגובות");
		$("#show_comments").click(function() { showComments(); } );
	}

	$("._opencomment").click(function() {
		$("#comment_"+$(this).attr("name")).fadeIn("slow");
	});
	
		// posts interface - add new comment section
	$("#show_addcomment").click(function() { $("#addcomment").fadeIn('slow'); });
	$("#add_comment").click(function() {
		$.post("index.php",
				{ act: 'post', opt: 'comment', cmt_postid: $("#postid").val(),
				  cmt_name: $("#name").val(), cmt_email: $("#cmt_email").val(),
				  cmt_title: $("#title").val(), cmt_content: $("#comment_content").val() },
				function(data) { 
					$("#addcomment").html("התגובה נוספה בהצלחה!");
				} 
		);
	});
	
		// right side menu - subscribe and unsubscribe funcs
	$("#subscribe").click(function() {
		$.post("index.php",
			   {act: "lead", opt: "subscribe", led_email: $("#sub_email").val() },
			   function(data) {
			   		$("#register").animate({opacity:0}, 500);
			   		setTimeout(function() {$("#register").html("מזל טוב, נודיע לכם כשיתווספו חומרים חדשים!");}, 500);
			   		$("#register").animate({opacity:1}, 500);
			   }
		);
	});
	$("#unsubscribe").click(function() {
		$.post("index.php",
			   {act: "lead", opt: "unsubscribe", led_email: $("#sub_email").val() },
			   function(data) { 
			    $("#register").animate({opacity:0}, 500);
			   	setTimeout(function() {$("#register").html("נמחקתם מהמאגרים, לא נשלח לכם חומרים חדשים. דרך צלחה!");}, 500);
				$("#register").animate({opacity:1}, 500);
				}
		);
	});


});