﻿var hsp = 1;

var notify = function(title, message, error){
	var topMargin
	$("#not_title").text(title);
	$("#not_body").html(message);
	if(error){
		topMargin = $(window).scrollTop() + ($(window).height()/2-$("#notifier").height()/2);
	}else{
		if($(window).scrollTop()<100){
			topMargin = 150;
		}else{
			topMargin = $(window).scrollTop()+50;
		}
	}
	if(left_col==false && $("#notifier").css("width")!="910px"){
		$("#notifier").css("margin-left","-447px").css("width","910px");
	}
	$("#notifier").css('top', topMargin); 
	$("#notifier").fadeIn(100);
	$("#notifier").css('z-index',10000);
}

function poll(){
	$(".poll li").click(function(){
		$("#status_bar_poll_search").html('<img alt="loading..." src="'+siteroot+'images/panel_loading.gif" width="16" height="16" /> &nbsp;بارگذاری');
		$.post(siteroot+"poll_vote.php",{pollid: $(this).attr("pollid"), answerid: $(this).attr("ansid")},function(data){
			$(".poll").replaceWith(data);
			$("#status_bar_poll_search").html('انجام شد');
		},"html");
	});
	$("#loadPollAnswers").click(function(){
		if($('#poll_answers').is(":visible")){
			$('#poll_answers').hide(100);
			$("#loadPollAnswers").text("نمایش نتایج");
			$("#loadPollAnswers").css("background","url("+siteroot+"images/arrow_down.png) no-repeat center right");
		}else{
			$('#poll_answers').show(100);
			$("#loadPollAnswers").text("پنهان کن")
			$("#loadPollAnswers").css("background","url("+siteroot+"images/arrow_up.png) no-repeat center right");
		}
	});
}
$(document).ready(function() {
	if(left_col==false){
		$("#left_col").hide();
	}
	// Hide sub menus on load
	$("#bestLinks ul, #usropt ul").hide();
	$("#notifier").hide();

	// Handle clicks outside the menu
	$(document).click(function() {
		if($('#bestLinks ul').is(":visible")){
			$('#bestLinks ul').slideUp();
		}
		if($('#usropt ul').is(":visible")){
			$('#usropt ul').hide();
		}
		if($('.neg-res').is(":visible")){
			$('.neg-res').fadeOut(200);
		}
	});
	$('#usropt li, #bestLinks, .neg-res, .neg_icon').click(function(event){event.stopPropagation()});
	
	//login
	$("a").click(function(){
		if($(this).attr("href")==siteroot+"login"){
			notify("ورود",'<div style="padding:0 10px;"><form method="post" action="'+siteroot+'login"><div style="padding:5px 0 5px 0">نام کاربری</div><div style="padding:3px 0 0 0"><input type="text"name="username"class="textbox"style="width:230px"tabindex="1"/><span style="font-size:8pt; padding:0 20px;"><a href="'+siteroot+'register">اگر هنوز در سایت ثبت نام نکرده اید اینجا را کلیک کنید</a></span></div><div style="padding:10px 0 5px 0">کلمه عبور</div><div style="padding:3px 0 5px 0"><input type="password" name="password" tabindex="2" class="textbox" style="width:230px"/><span style="font-size:8pt; padding:0 20px;"><a href="'+siteroot+'register">بازیابی رمز عبور</a></span></div><div style="padding:10px 0"><input type="checkbox" id="remember" tabindex="3" name="remember" value="ON" style="vertical-align:middle"/> &nbsp;مرا به خاطر بسپار</div><div style="padding:10px 0"><input style="border:none; width:95px; height:26px"src="'+siteroot+'images/btnLogin.gif" name="submit" type="image"/></div><input type="hidden" name="lastpage" value="'+location.href+'"></form></div>',false);
			return false;
		}else{
			return true;
		}
	});
	
	// Seperator
	$("#sep").click(function(){
		if(left_col){
			$("#left_col").fadeOut(500, function(){
				$("#left_col").hide();
				if($("#notifier").is(":visible")){
					$("#notifier").animate({marginLeft:"-447px"},400).animate({width:"910px"},400);
				}else{
					$("#notifier").css("width","870px");
					$("#notifier").css("margin-left","-420px");
				}
			});
			left_col= false;
		}else{
			$("#left_col").fadeIn(1000, function(){
				$("#left_col").show();
			});
			if($("#notifier").is(":visible")){
				$("#notifier").animate({marginLeft:"-165px"},{queue:false, duration:400}).animate({width:"630px"},400);
			}else{
				$("#notifier").css("width","630px");
				$("#notifier").css("margin-left","-165px");
			}
			left_col= true;
		}
		$.post(siteroot+"changeStyle.php",{lc: left_col});
	});
	
	//Close Button
	$("#close").click(function(){
		$("#notifier").fadeOut(100);
	});
	
	//Most Descussed , Best Rated
	
	$("#md, #br").click(function(){
		$("#status_bar_todaysBest").html('<img alt="loading..." src="'+siteroot+'images/panel_loading.gif" width="16" height="16" /> &nbsp;بارگذاری');
		var page;
		if($(this).attr("id")=="md"){
			page = "MostDiscussed";
			$("#md").attr("class","PanelSelected");
			$("#br").attr("class","dpanel_top");
		}
		if($(this).attr("id")=="br"){
			page = "BestRated";
			$("#br").attr("class","PanelSelected");
			$("#md").attr("class","dpanel_top");
		}

		$.post(siteroot+"loadMenus.php",{action: page},function(data){
			$("#todaysBest").hide();
			$("#todaysBest").html(data.message);
			$("#todaysBest").fadeIn(300);
			$("#status_bar_todaysBest").text('انجام شد');
			},"json");
	});
	
	//Last Poll & Search
	
	$("#lp, #sb").click(function(){
		$("#status_bar_poll_search").html('<img alt="loading..." src="'+siteroot+'images/panel_loading.gif" width="16" height="16" /> &nbsp;بارگذاری');
		var page;
		if($(this).attr("id")=="lp"){
			page = "poll";
			$("#lp").attr("class","PanelSelected");
			$("#sb").attr("class","dpanel_top");
			
			$.post(siteroot+"loadMenus.php",{action: page},function(data){
			$("#poll_search").hide();
			$("#poll_search").html(data.message);
			$("#poll_search").fadeIn(300);
			poll();
			$("#status_bar_poll_search").text('انجام شد');
			},"json");
		}
		if($(this).attr("id")=="sb"){
			$("#sb").attr("class","PanelSelected");
			$("#lp").attr("class","dpanel_top");
			$("#poll_search").hide();
			$("#poll_search").html('<form method="post" action="'+siteroot+'search"><input name="s_keywords" style="vertical-align:middle; border: 1px solid #cecece; width: 215px; font-size:9pt; height:17px; padding:3px 5px 0 0" type="text" lang="fa"/><input name="btnSearch" type="image" src="'+siteroot+'images/search_btn.gif" style="width:22px; height:22px; vertical-align:middle"/></form>');
			$("#poll_search").fadeIn(300);
			$("#status_bar_poll_search").text('انجام شد');
		}
	});
	
	//Forum & Statistics
	
	$("#rt, #st").click(function(){
		$("#status_bar_forum_stat").html('<img alt="loading..." src="'+siteroot+'images/panel_loading.gif" width="16" height="16" /> &nbsp;بارگذاری');
		var page;
		if($(this).attr("id")=="rt"){
			page = "forum";
			$("#rt").attr("class","PanelSelected");
			$("#st").attr("class","dpanel_top");
		}
		if($(this).attr("id")=="st"){
			page = "stat";
			$("#st").attr("class","PanelSelected");
			$("#rt").attr("class","dpanel_top");
		}

		$.post(siteroot+"loadMenus.php",{action: page},function(data){
			$("#forum_stat").hide();
			$("#forum_stat").html(data.message);
			$("#forum_stat").fadeIn(300);
			$("#status_bar_forum_stat").text('انجام شد');
			},"json");
	});
	
	// Up & Down Buttons
	$(".btnShowDown, .btnShowUp").click(function(){
		if(!$(this).hasClass("disabled")){
			if($(this).attr("action")=="down"){
				if(hsp>=2){
					$("#hs_showdown").addClass("disabled");
					hsp=3;
				}else{
					$("#hs_showup").removeClass("disabled");
					hsp++;
				}
			}else if($(this).attr("action")=="up"){
				if(hsp<=2){
					$("#hs_showup").addClass("disabled");
					hsp=1;
				}else{
					hsp--;
					$("#hs_showdown").removeClass("disabled");
				}
			}
			var page = "hs"+hsp;
			$("#status_bar_hs").html('<img alt="loading..." src="'+siteroot+'images/panel_loading.gif" width="16" height="16" /> &nbsp;بارگذاری');
			$.post(siteroot+"loadMenus.php",{action: page},function(data){
				$("#hsBody").hide();
				$("#hsBody").html(data.message);
				$("#hsBody").fadeIn(300);
				poll();
				$("#status_bar_hs").text('انجام شد');
			},"json");
		}
	});
	
	$("#usropt li, #bestLinks").click(function(){
		$("> ul",this).show();
	});
	
	//refrsh
	$(".btnRefresh").click(function(){
		var id = $(this).attr("page");
		if(id=="st"){
			page = "stat";
		}
		$("#status_bar_"+id).html('<img alt="loading..." src="'+siteroot+'images/panel_loading.gif" width="16" height="16" /> &nbsp;بارگذاری');
		$.post(siteroot+"loadMenus.php",{action: page},function(data){
			$("#"+id+"Body").hide();
			$("#"+id+"Body").html(data.message);
			$("#"+id+"Body").fadeIn(300);
			$("#status_bar_"+id).text('انجام شد');
		},"json");
	});
	
	//rate the link
	$(".plus_icon, .neg_icon, .neg-res li").click(function(e){
		var id = $(this).attr("linkid");
		if($(this).attr("action")=="show_reasons"){
			var topOS = e.pageY - 20;
			$(".neg-res li").attr("linkid",id);
			$(".neg-res").css("top",topOS);
			$(".neg-res").fadeIn(200);
		}else{
			var prepoint = $("#point"+id).html();
			$("#point"+id).html('<img border="0" src="'+siteroot+'images/tinyLoader.gif" width="16" height="16"/>');
			$.post(siteroot+"changepoint.php",{id: id, action: $(this).attr("action"), reason: $(this).attr("rsn")},function(data){
					if(data.error){
						notify('خطا',data.message,true);
						$("#point"+id).html(prepoint);
					}else{
						$("#point"+id).html(data.message);
						$("#voteBar_"+id).hide();
						$("#removeVote_"+id).fadeIn(200);
					}
					$("#point"+id).fadeIn(300);
				},"json");
			if($('.neg-res').is(":visible")){
				$('.neg-res').fadeOut(200);
			}
		}
	});
	
	$(".removeVote").click(function(){
		var id = $(this).attr("linkid");
		var prepoint = $("#point"+id).html();
		$("#point"+id).html('<img border="0" src="'+siteroot+'images/tinyLoader.gif" width="16" height="16"/>');
		$.post(siteroot+"removeVote.php",{linkid: id},function(data){
			if(data.error){
				notify('خطا',data.message,true);
				$("#point"+id).html(prepoint);
			}else{
				$("#point"+id).html(data.point);
				$("#removeVote_"+id).hide();
				$("#voteBar_"+id).fadeIn(200);
			}
		},"json");
	});
	
	//add a link to your favorite list
	$(".favorite").click(function(){
		var btnFav = $(this);
		$.post(siteroot+"add2fav.php",{linkid: $(this).attr("linkid"), action: $(this).attr("action")},function(data){
				if(data.error){
					notify('خطا',data.message,true);
				}else{
					if(data.message=="add"){
						btnFav.attr("src",siteroot+"style/default/images/heart-silver.png");
					}else if(data.message=="delete"){
						btnFav.attr("src",siteroot+"style/default/images/heart-red.png");
					}
					btnFav.attr("action",data.message);
				}
			},"json");
	});
	
	//add a new hot subject
	$("#btnNewhs").click(function(){
		notify("اضافه کردن موضوع مورد بحث",'<form method="post" action="'+siteroot+'"><div style="padding:10px 0 5px 0; font-weight:bold">عنوان</div><div style="padding:3px 0 0 0"><input type="text" tabindex="1" name="hsSubject" id="hsSubject" class="textbox" style="width:98%; direction: rtl;"/></div><div style="padding:7px 0 5px 0; font-weight:bold">توضیح</div><div style="padding:3px 0 5px 0"><textarea name="hsDesc" id="hsDesc" tabindex="2" style="width: 100%; font-size:12px; height: 118px; direction:rtl; border:1px solid #c0c0c0" multiline="false" lang="fa"></textarea></div><div style="padding:7px 0 5px 0; font-weight:bold">عکس</div><div style="padding:3px 0 0 0"><input type="text" tabindex="3" name="hsPic" id="hsPic" value="http://" class="textbox" style="width:98%"/></div><div style="padding:7px 0 5px 0; color:gray; font-size:8pt">مثال: http://i22.tinypic.com/3afgmddw.jpg</div><div style="margin:5px 0; font-weight:bold">پیش نمایش</div><div style="padding:15px; margin:7px 0; background-color:#f6f6f6; border: 1px solid #bebebe; overflow:hidden"> <div id="hs_photo" style="float:right; width:65px; height:65px; overflow:hidden; border:1px solid #bebebe; background-color:white"></div> <div style="float:right; width:495px; margin-right:10px; line-height:20px"><span id="hs_title" style="font-weight:bold">عنوان</span><br/><span id="hs_desc">توضیح</span></div> </div>  <div style="padding:15px 0"><input style="border:none; width:95px; height:26px" src="'+siteroot+'images/btnSendLarge2.gif" name="submit" type="image"/></div></form>',false);
		addHSEvents();
	});
	
	//Update Hot Subject Preview
	function addHSEvents(){
		$("#hsSubject").change(function(){
			$("#hs_title").text($(this).val());
		});
		$("#hsDesc").change(function(){
			$("#hs_desc").text($(this).val());
		});
		$("#hsPic").change(function(){
			var per;
			$("#hs_photo").html('<img id="samplePhoto" border="0" alt="عکس" title="عکس" src="'+$(this).val()+'"/>');
			if($("#samplePhoto").width() > $("#samplePhoto").height())
			{
				per = $("#samplePhoto").height()/65;
				$("#samplePhoto").height(65);
				$("#samplePhoto").css("margin-right", "-"+(($("#samplePhoto").width() - 65)/2)+"px");
			}
			if($("#samplePhoto").width() <= $("#samplePhoto").height())
			{
				per = $("#samplePhoto").width()/65;
				$("#samplePhoto").width(65);
				$("#samplePhoto").css("margin-top", "-"+(($("#samplePhoto").height() - 65)/2)+"px");
			}
		});
	}
	
	
	//stop notifications
	$(".stopNot").click(function(){
		$(this).unbind("click");
		$.post(siteroot+"stopNot.php",{linkid: $(this).attr("linkid")},function(data){
			$("div[notid='L"+data.id+"']").fadeOut(400);
			$("div[notid='F"+data.id+"']").fadeOut(400);
			$("div[notid='P"+data.id+"']").fadeOut(400);
			notify("آگاه سازی",data.message,true);
		},"json");
	});
	//Checkbox private messages
	$("input:checkbox").click(function(){
		var pmid = $(this).attr("pmid");
		if($(this).is(":checked")){
			$("#pm_"+pmid).attr("class","pm_selected");
		}else{
			$("#pm_"+pmid).attr("class","pm");
		}
	});
	//Delete PM
	$("#deletePM").click(function(){
		var n = $("input:checked").length;
		if(n<1){
			notify("خطا","پیام خصوصی‌ای را برای حذف انتخاب نکرده‌اید",true);
		}else{
			var ids="";
			jQuery.each($("input:checked"), function() {
				ids += $(this).attr("pmid")+",";
			});
			$.post(siteroot+"deletepm.php",{id: ids},function(data){
				if(data.error==false){
					$("input:checked").parent().parent().fadeOut(400);
				}
			},"json");
		}
	});
	//Friends
	$(".notific").click(function(){
		if($(this).attr("type")=="friends"){
			var friend = $(this);
			if(friend.css("height")=="30px"){
				if(friend.attr("info")=="no"){
					friend.animate({height: "55px"},200);
					$.post(siteroot+"showActivity.php",{userid: friend.attr("userid")},function(data){
						if(data.error==false){
							$("#fr_loading_"+friend.attr("userid")).replaceWith(data.message);
							friend.animate({height: "95px"},200);
							friend.attr("info","yes");
						}else{
							notify("خطا",data.message,true);
							friend.animate({height: "30px"},200);
						}
					},"json");
				}else{
					friend.animate({height: "95px"},200);
				}
			}else{
				friend.animate({height: "30px"},200);
			}
		}
	});
	//Add to friends
	$(".add2friends").click(function(){
		var obj = $(this);
		obj.html('<img alt="loading..." src="'+siteroot+'images/panel_loading.gif" width="16" height="16" /> &nbsp;بارگذاری');
		var userid = obj.attr("userid");
		var action = obj.attr("action");
		$.post(siteroot+"aorfriend.php",{id: userid, action: action},function(data){
			obj.html(data.message);
			obj.unbind("click");
			obj.css("cursor","default");
		},"json");
	});
	//editProfile
	$(".editProfile").click(function(){
		var obj = $(this).attr("subdiv");
		if($('#'+obj).is(":visible")){
			$('#'+obj).slideUp(200);
		}else{
			$('#'+obj).slideDown(200);
		}
	});
	//Watch Here
	$(".watchHere").click(function(){
		$.post(siteroot+"addclick.php",{linkid: $(this).attr("linkid")});
		notify($(this).attr("vidtitle"),'<object width="100%" height="505"><param name="movie" value="http://www.youtube.com/v/'+$(this).attr("videoid")+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+$(this).attr("videoid")+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="100%" height="505"></embed></object>',true);
	});
	//Color Palette
	$(".colorplt li").click(function(){
		$.post(siteroot+"changeStyle.php",{color: $(this).attr("clr")});
		if($(this).attr("clr")=="red") $("#switchStyle").attr("href",siteroot+"red.css");
		if($(this).attr("clr")=="gray") $("#switchStyle").attr("href",siteroot+"gray.css");
		if($(this).attr("clr")=="green") $("#switchStyle").attr("href",siteroot+"green.css");
		if($(this).attr("clr")=="blue") $("#switchStyle").attr("href",siteroot+"blue.css");
	});
	poll();

});

