﻿function hotSubjects(){
	$("#btnChangeHS").click(function(){
		$("#btnChangeHS").hide();
		$.post(siteroot+"changehs.php",{linkid: $(this).attr("linkid"), subject: $("#hotsubjects").val()},function(data){
			if(data.error){
				notify("خطا",data.message,true);
				$("#btnChangeHS").show(200);
			}else{
				notify("انجام شد",data.message,true);
			}
		},"json");
	});
}
$(document).ready(function() {

	$(".showComments").click(function(){
		var title = "نظرات: "+$(this).attr("title");
		notify(title,"کمی صبر کنید...",false);
		$.post(siteroot+"showcomments.php",{linkid: $(this).attr("linkid")},function(data){
			notify(title,data,false);
			comment_opt();
			compose_opt();
		});
	});
	
	function comment_opt(){		
	
		$(".cmnt_agree, .cmnt_disagree, .cmnt_quote, .cmnt_edit, .cmnt_del").unbind('click');
		
		$(".cmnt_agree, .cmnt_disagree").click(function(){
			var name = "#cmnt_point_"+$(this).attr("cmntid")+" div";
			var pv = $(name).text();
			$("#cmnt_point_"+$(this).attr("cmntid")+" div span").html('<img border="0" src="'+siteroot+'images/tinyLoader.gif" style="margin-top:2px" width="16" height="16"/>');
			$.post(siteroot+"votecomment.php",{commentid: $(this).attr("cmntid"),action: $(this).attr("action")},function(data){
				if(data.error==false){
					$(name).html(data.message);
				}else{
					$(name+' span').html(pv);
				}
			},'json');
			$(this).unbind('click');
		});
		
		$(".cmnt_quote").click(function(){
			var hiddenName = "#html-bb-"+$(this).attr("cmntid");
			doAddTags('',"[quote]"+$(hiddenName).attr("bbcode")+"[/quote]",'compose_text');
			$.scrollTo("#compose_text",800,{offset: {top:-150}});
		});
		$(".cmnt_edit").click(function(){
			var hiddenName = "#html-bb-"+$(this).attr("cmntid");
			$('#compose_text').val($(hiddenName).attr("bbcode"));
			$("#btnCompose").attr("mode","edit"+$(this).attr("cmntid"));
			$.scrollTo("#compose_text",800,{offset: {top:-150}});
			$(".cancelEditing").fadeIn(200);
		});
		$(".cmnt_del").click(function(){
			$.post(siteroot+"deletecomment.php",{commentid: $(this).attr("cmntid")},function(data){
				if(data.error==false){
					$("#comment_"+data.id).fadeOut(200);
				}
			},'json');
		});
	}
	
	function compose_opt(){
		$(".cmnt_page li").click(function(){
			if(!$(this).hasClass("selected")){
				if($(".cmnt_refresh").attr("page")!=$(this).attr("page")){
					$(".cmnt_page li").removeClass("selected");
					$(this).addClass("selected");
					$(".cmnt_refresh").attr("page",$(this).attr("page"));
				}
				if($(this).attr("mode")=="forum"){
					$("#replys").html('<div style="padding: 10px 0">لطفاً کمی صبر کنید</div>');
					$.post(siteroot+"updatetopics.php",{page: $(this).attr("page"),topicid: $(this).attr("linkid")},function(data){
						$("#replys").html(data);
						$.scrollTo("#replys",800,{offset: {top:-50}});
					});
				}else{
					$("#new_mess").html("");
					$("#comment-panel").html('<div style="padding: 10px 0">لطفاً کمی صبر کنید</div>');
					$.post(siteroot+"updatecomments.php",{page: $(this).attr("page"),linkid: $(this).attr("linkid"), mode: $(this).attr("mode")},function(data){
						$("#comment-panel").html(data);
						comment_opt();
						$.scrollTo("#comments-top",800,{offset: {top:-50}});
					});
				}
			}
		});
		
		$(".compose_sml").click(function(){
			if($('.cmnt_smiley ul').is(":visible")){
				$(".cmnt_smiley ul").hide();
				$(".cmnt_smiley").css("width","0");
			}else{
				$(".cmnt_smiley ul").show();
				$(".cmnt_smiley").css("with","130px");
			}
		});
		
		$(".compose_bold").click(function(){
			doAddTags('[b]','[/b]','compose_text')
		});
		
		$(".compose_quote").click(function(){
			doAddTags('[quote]','[/quote]','compose_text')
		});
		
		$(".compose_link").click(function(){
			doURL('compose_text');
		});
		
		$(".cmnt_smiley li").click(function(){
			doAddTags('',$(this).attr("title"),'compose_text');
		});
		$(".cancelEditing").click(function(){
			$("#btnCompose").attr("mode","link");
			$("#compose_text").val('');
			$(this).fadeOut(200);
		});
		$("#btnCompose").click(function(){
			var mod = $(this).attr("mode");
			var lid = $(this).attr("linkid");
			var mess = $("#compose_text").val();
			if(mod!="pm" && mod!="newthread"){
				$("#compose_text").val("لطفاً صبر کنید...");
			}
			if(mod=="forum"){
				$.post(siteroot+"sendreply.php",{topicid: lid, message: mess},function(data){
					$(".cmnt_error").remove();
					if(data.error==false){
						$("#replys").append(data.message);
						$("#reply_"+data.id).fadeIn(300);
						$("#compose_text").val("");
					}else{
						$("#replys").append('<div class="cmnt_error" style="display:none">'+data.message+'</div>');
						$(".cmnt_error").fadeIn(200);
						$("#compose_text").val(mess);
					}
				},"json");
			}else if(mod!="pm"){
				$.post(siteroot+"sendcomment.php",{linkid: lid, mess: mess ,mode: mod},function(data){
					$(".cmnt_error").remove();
					if(data.error==true){
						$("#new_mess").append('<div class="cmnt_error" style="display:none">'+data.message+'</div>');
						$(".cmnt_error").fadeIn(200);
						$("#compose_text").val(mess);
					}else{
						if(data.edited){
							$("#comment_"+data.id).parent().html(data.message);
							$("#btnCompose").attr("mode","link");
							$(".cancelEditing").fadeOut(200);
						}else{
							$("#new_mess").append(data.message);
						}
						$("#comment_"+data.id).fadeIn(500);
						$.scrollTo("#comment_"+data.id, 500,{offset: {top:-50}});
						$("#compose_text").val("");
						comment_opt();
					}
				},"json");
			}
		});
	}
	
	//Voters & Hot Subjects
	
	$("#vt, #ch").click(function(){
		$("#status_bar_voters_subject").html('<img alt="loading..." src="'+siteroot+'images/panel_loading.gif" width="16" height="16" /> &nbsp;بارگذاری');
		var page;
		if($(this).attr("id")=="vt"){
			$("#vt").attr("class","PanelSelected");
			$("#ch").attr("class","dpanel_top");
			$.post(siteroot+"loadMenus.php",{action: "voters", linkid: link_id},function(data){
				$("#voters_subject").hide();
				$("#voters_subject").html(data.message);
				$("#voters_subject").fadeIn(300);
				$("#status_bar_voters_subject").text('انجام شد');
			},"json");
		}
		if($(this).attr("id")=="ch"){
			page = "changehs";
			$("#ch").attr("class","PanelSelected");
			$("#vt").attr("class","dpanel_top");
			$("#voters_subject").hide();
			$("#voters_subject").html(hsubjects);
			$("#voters_subject").fadeIn(300);
			$("#status_bar_voters_subject").text('انجام شد');
			hotSubjects();
		}
	});
	
	//Add Response
	$("#btnNewrs").click(function(){
		var url = siteroot+'addResponse/'+link_id;
		window.location.replace(url);
	});
	
	comment_opt();
	compose_opt();
})